tile38/controller/fence.go

158 lines
4.1 KiB
Go
Raw Normal View History

2016-03-19 17:16:19 +03:00
package controller
import (
"strings"
2016-03-30 19:32:38 +03:00
"github.com/tidwall/tile38/controller/server"
2016-03-19 17:16:19 +03:00
"github.com/tidwall/tile38/geojson"
)
2016-04-02 17:20:30 +03:00
var tmfmt = "2006-01-02T15:04:05.999999999Z07:00"
func FenceMatch(hookName string, sw *scanWriter, fence *liveFenceSwitches, details *commandDetailsT) []string {
2016-04-03 00:13:20 +03:00
jshookName := jsonString(hookName)
jstime := jsonString(details.timestamp.Format(tmfmt))
2016-03-19 17:16:19 +03:00
glob := fence.glob
if details.command == "drop" {
2016-04-03 00:13:20 +03:00
return []string{`{"cmd":"drop","hook":` + jshookName + `,"time":` + jstime + `}`}
2016-03-19 17:16:19 +03:00
}
match := true
if glob != "" && glob != "*" {
match, _ = globMatch(glob, details.id)
}
if !match {
return nil
}
2016-04-03 00:13:20 +03:00
sw.mu.Lock()
nofields := sw.nofields
sw.mu.Unlock()
if details.obj == nil || (details.command == "fset" && nofields) {
2016-03-19 17:16:19 +03:00
return nil
}
match = false
detect := "outside"
if fence != nil {
match1 := fenceMatchObject(fence, details.oldObj)
match2 := fenceMatchObject(fence, details.obj)
if match1 && match2 {
match = true
detect = "inside"
} else if match1 && !match2 {
match = true
detect = "exit"
} else if !match1 && match2 {
match = true
detect = "enter"
2016-04-02 17:20:30 +03:00
if details.command == "fset" {
detect = "inside"
}
2016-03-19 17:16:19 +03:00
} else {
2016-04-02 17:20:30 +03:00
if details.command != "fset" {
// Maybe the old object and new object create a line that crosses the fence.
// Must detect for that possibility.
if details.oldObj != nil {
ls := geojson.LineString{
Coordinates: []geojson.Position{
details.oldObj.CalculatedPoint(),
details.obj.CalculatedPoint(),
},
}
temp := false
if fence.cmd == "within" {
// because we are testing if the line croses the area we need to use
// "intersects" instead of "within".
fence.cmd = "intersects"
temp = true
}
if fenceMatchObject(fence, ls) {
match = true
detect = "cross"
}
if temp {
fence.cmd = "within"
}
2016-03-19 17:16:19 +03:00
}
}
}
}
if details.command == "del" {
2016-04-03 00:13:20 +03:00
return []string{`{"command":"del","hook":` + jshookName + `,"id":` + jsonString(details.id) + `,"time":` + jstime + `}`}
2016-03-19 17:16:19 +03:00
}
2016-04-02 17:20:30 +03:00
if details.fmap == nil {
2016-03-19 17:16:19 +03:00
return nil
}
2016-04-03 00:13:20 +03:00
sw.mu.Lock()
2016-04-02 17:20:30 +03:00
sw.fmap = details.fmap
2016-03-19 17:16:19 +03:00
sw.fullFields = true
2016-03-30 19:32:38 +03:00
sw.msg.OutputType = server.JSON
2016-04-03 00:13:20 +03:00
sw.writeObject(details.id, details.obj, details.fields, true)
2016-03-19 17:16:19 +03:00
if sw.wr.Len() == 0 {
2016-04-03 00:13:20 +03:00
sw.mu.Unlock()
2016-03-19 17:16:19 +03:00
return nil
}
res := sw.wr.String()
2016-04-03 00:13:20 +03:00
resb := make([]byte, len(res))
copy(resb, res)
res = string(resb)
2016-03-19 17:16:19 +03:00
sw.wr.Reset()
if sw.output == outputIDs {
res = `{"id":` + res + `}`
}
2016-04-03 00:13:20 +03:00
sw.mu.Unlock()
2016-04-02 17:20:30 +03:00
2016-04-03 00:13:20 +03:00
if strings.HasPrefix(res, ",") {
res = res[1:]
}
jskey := jsonString(details.key)
2016-04-01 22:46:39 +03:00
ores := res
msgs := make([]string, 0, 2)
if fence.detect == nil || fence.detect[detect] {
if strings.HasPrefix(ores, "{") {
2016-04-03 00:13:20 +03:00
res = `{"command":"` + details.command + `","detect":"` + detect + `","hook":` + jshookName + `,"key":` + jskey + `,"time":` + jstime + `,` + ores[1:]
2016-04-01 22:46:39 +03:00
}
msgs = append(msgs, res)
2016-03-19 17:16:19 +03:00
}
switch detect {
case "enter":
2016-04-01 22:46:39 +03:00
if fence.detect == nil || fence.detect["inside"] {
2016-04-03 00:13:20 +03:00
msgs = append(msgs, `{"command":"`+details.command+`","detect":"inside","hook":`+jshookName+`,"key":`+jskey+`,"time":`+jstime+`,`+ores[1:])
2016-04-01 22:46:39 +03:00
}
2016-03-19 17:16:19 +03:00
case "exit", "cross":
2016-04-01 22:46:39 +03:00
if fence.detect == nil || fence.detect["outside"] {
2016-04-03 00:13:20 +03:00
msgs = append(msgs, `{"command":"`+details.command+`","detect":"outside","hook":`+jshookName+`,"key":`+jskey+`,"time":`+jstime+`,`+ores[1:])
2016-04-01 22:46:39 +03:00
}
2016-03-19 17:16:19 +03:00
}
return msgs
}
func fenceMatchObject(fence *liveFenceSwitches, obj geojson.Object) bool {
if obj == nil {
return false
}
if fence.cmd == "nearby" {
return obj.Nearby(geojson.Position{X: fence.lon, Y: fence.lat, Z: 0}, fence.meters)
} else if fence.cmd == "within" {
if fence.o != nil {
return obj.Within(fence.o)
} else {
return obj.WithinBBox(geojson.BBox{
Min: geojson.Position{X: fence.minLon, Y: fence.minLat, Z: 0},
Max: geojson.Position{X: fence.maxLon, Y: fence.maxLat, Z: 0},
})
}
} else if fence.cmd == "intersects" {
if fence.o != nil {
return obj.Intersects(fence.o)
} else {
return obj.IntersectsBBox(geojson.BBox{
Min: geojson.Position{X: fence.minLon, Y: fence.minLat, Z: 0},
Max: geojson.Position{X: fence.maxLon, Y: fence.maxLat, Z: 0},
})
}
}
return false
}