objects with roam matches
This commit is contained in:
parent
25a60a2497
commit
b3d3be7e76
@ -204,6 +204,12 @@ func (c *Controller) aofshrink() {
|
|||||||
for _, meta := range hook.Metas {
|
for _, meta := range hook.Metas {
|
||||||
values = append(values, "meta", meta.Name, meta.Value)
|
values = append(values, "meta", meta.Name, meta.Value)
|
||||||
}
|
}
|
||||||
|
if !hook.expires.IsZero() {
|
||||||
|
ex := float64(hook.expires.Sub(time.Now())) /
|
||||||
|
float64(time.Second)
|
||||||
|
values = append(values, "ex",
|
||||||
|
strconv.FormatFloat(ex, 'f', 1, 64))
|
||||||
|
}
|
||||||
for _, value := range hook.Message.Values {
|
for _, value := range hook.Message.Values {
|
||||||
values = append(values, value.String())
|
values = append(values, value.String())
|
||||||
}
|
}
|
||||||
|
@ -148,7 +148,7 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener, http
|
|||||||
c.hookex.Expired = func(item expire.Item) {
|
c.hookex.Expired = func(item expire.Item) {
|
||||||
switch v := item.(type) {
|
switch v := item.(type) {
|
||||||
case *Hook:
|
case *Hook:
|
||||||
c.possiblyExpireHook(v)
|
c.possiblyExpireHook(v.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.epc = endpoint.NewManager(c)
|
c.epc = endpoint.NewManager(c)
|
||||||
|
@ -79,11 +79,14 @@ func fenceMatch(hookName string, sw *scanWriter, fence *liveFenceSwitches, metas
|
|||||||
}
|
}
|
||||||
var roamkeys, roamids []string
|
var roamkeys, roamids []string
|
||||||
var roammeters []float64
|
var roammeters []float64
|
||||||
|
var roamobjs []geojson.Object
|
||||||
var detect = "outside"
|
var detect = "outside"
|
||||||
if fence != nil {
|
if fence != nil {
|
||||||
if fence.roam.on {
|
if fence.roam.on {
|
||||||
if details.command == "set" {
|
if details.command == "set" {
|
||||||
roamkeys, roamids, roammeters = fenceMatchRoam(sw.c, fence, details.key, details.id, details.obj)
|
roamkeys, roamids, roammeters, roamobjs =
|
||||||
|
fenceMatchRoam(sw.c, fence, details.key,
|
||||||
|
details.id, details.obj)
|
||||||
}
|
}
|
||||||
if len(roamids) == 0 || len(roamids) != len(roamkeys) {
|
if len(roamids) == 0 || len(roamids) != len(roamkeys) {
|
||||||
return nil
|
return nil
|
||||||
@ -223,46 +226,41 @@ func fenceMatch(hookName string, sw *scanWriter, fence *liveFenceSwitches, metas
|
|||||||
var nmsgs []string
|
var nmsgs []string
|
||||||
msg := msgs[0][:len(msgs[0])-1]
|
msg := msgs[0][:len(msgs[0])-1]
|
||||||
for i, id := range roamids {
|
for i, id := range roamids {
|
||||||
|
|
||||||
var nmsg []byte
|
var nmsg []byte
|
||||||
nmsg = append(nmsg, msg...)
|
nmsg = append(nmsg, msg...)
|
||||||
nmsg = append(nmsg, `,"nearby":{"key":`...)
|
nmsg = append(nmsg, `,"nearby":{"key":`...)
|
||||||
nmsg = appendJSONString(nmsg, roamkeys[i])
|
nmsg = appendJSONString(nmsg, roamkeys[i])
|
||||||
nmsg = append(nmsg, `,"id":`...)
|
nmsg = append(nmsg, `,"id":`...)
|
||||||
nmsg = appendJSONString(nmsg, id)
|
nmsg = appendJSONString(nmsg, id)
|
||||||
|
nmsg = append(nmsg, `,"object":`...)
|
||||||
|
nmsg = append(nmsg, roamobjs[i].JSON()...)
|
||||||
nmsg = append(nmsg, `,"meters":`...)
|
nmsg = append(nmsg, `,"meters":`...)
|
||||||
nmsg = append(nmsg, strconv.FormatFloat(roammeters[i], 'f', -1, 64)...)
|
nmsg = append(nmsg, strconv.FormatFloat(roammeters[i], 'f', -1, 64)...)
|
||||||
|
|
||||||
if fence.roam.scan != "" {
|
if fence.roam.scan != "" {
|
||||||
nmsg = append(nmsg, `,"scan":[`...)
|
nmsg = append(nmsg, `,"scan":[`...)
|
||||||
|
col := sw.c.getCol(roamkeys[i])
|
||||||
func() {
|
if col != nil {
|
||||||
sw.c.mu.Lock()
|
obj, _, ok := col.Get(id)
|
||||||
defer sw.c.mu.Unlock()
|
if ok {
|
||||||
col := sw.c.getCol(roamkeys[i])
|
nmsg = append(nmsg, `{"id":`+jsonString(id)+`,"self":true,"object":`+obj.JSON()+`}`...)
|
||||||
if col != nil {
|
}
|
||||||
obj, _, ok := col.Get(id)
|
pattern := id + fence.roam.scan
|
||||||
if ok {
|
iterator := func(oid string, o geojson.Object, fields []float64) bool {
|
||||||
nmsg = append(nmsg, `{"id":`+jsonString(id)+`,"self":true,"object":`+obj.JSON()+`}`...)
|
if oid == id {
|
||||||
}
|
|
||||||
pattern := id + fence.roam.scan
|
|
||||||
iterator := func(oid string, o geojson.Object, fields []float64) bool {
|
|
||||||
if oid == id {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if matched, _ := glob.Match(pattern, oid); matched {
|
|
||||||
nmsg = append(nmsg, `,{"id":`+jsonString(oid)+`,"object":`+o.JSON()+`}`...)
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
g := glob.Parse(pattern, false)
|
if matched, _ := glob.Match(pattern, oid); matched {
|
||||||
if g.Limits[0] == "" && g.Limits[1] == "" {
|
nmsg = append(nmsg, `,{"id":`+jsonString(oid)+`,"object":`+o.JSON()+`}`...)
|
||||||
col.Scan(false, iterator)
|
|
||||||
} else {
|
|
||||||
col.ScanRange(g.Limits[0], g.Limits[1], false, iterator)
|
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}()
|
g := glob.Parse(pattern, false)
|
||||||
|
if g.Limits[0] == "" && g.Limits[1] == "" {
|
||||||
|
col.Scan(false, iterator)
|
||||||
|
} else {
|
||||||
|
col.ScanRange(g.Limits[0], g.Limits[1], false, iterator)
|
||||||
|
}
|
||||||
|
}
|
||||||
nmsg = append(nmsg, ']')
|
nmsg = append(nmsg, ']')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,9 +323,10 @@ func fenceMatchObject(fence *liveFenceSwitches, obj geojson.Object) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func fenceMatchRoam(c *Controller, fence *liveFenceSwitches, tkey, tid string, obj geojson.Object) (keys, ids []string, meterss []float64) {
|
func fenceMatchRoam(
|
||||||
c.mu.RLock()
|
c *Controller, fence *liveFenceSwitches,
|
||||||
defer c.mu.RUnlock()
|
tkey, tid string, obj geojson.Object,
|
||||||
|
) (keys, ids []string, meterss []float64, objs []geojson.Object) {
|
||||||
col := c.getCol(fence.roam.key)
|
col := c.getCol(fence.roam.key)
|
||||||
if col == nil {
|
if col == nil {
|
||||||
return
|
return
|
||||||
@ -348,6 +347,7 @@ func fenceMatchRoam(c *Controller, fence *liveFenceSwitches, tkey, tid string, o
|
|||||||
keys = append(keys, fence.roam.key)
|
keys = append(keys, fence.roam.key)
|
||||||
ids = append(ids, id)
|
ids = append(ids, id)
|
||||||
meterss = append(meterss, obj.CalculatedPoint().DistanceTo(p))
|
meterss = append(meterss, obj.CalculatedPoint().DistanceTo(p))
|
||||||
|
objs = append(objs, obj)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
|
@ -285,9 +285,12 @@ func (c *Controller) cmdPDelHook(msg *server.Message, channel bool) (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) possiblyExpireHook(h *Hook) {
|
// possiblyExpireHook will evaluate a hook by it's name for expiration and
|
||||||
|
// purge it from the database if needed. This operation is called from an
|
||||||
|
// independent goroutine
|
||||||
|
func (c *Controller) possiblyExpireHook(name string) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
if h, ok := c.hooks[h.Name]; ok {
|
if h, ok := c.hooks[name]; ok {
|
||||||
if !h.expires.IsZero() && time.Now().After(h.expires) {
|
if !h.expires.IsZero() && time.Now().After(h.expires) {
|
||||||
// purge from database
|
// purge from database
|
||||||
msg := &server.Message{}
|
msg := &server.Message{}
|
||||||
|
@ -178,7 +178,13 @@ func (c *Controller) goLive(inerr error, conn net.Conn, rd *server.PipelineReade
|
|||||||
}
|
}
|
||||||
fence := lb.fence
|
fence := lb.fence
|
||||||
lb.cond.L.Unlock()
|
lb.cond.L.Unlock()
|
||||||
msgs := FenceMatch("", sw, fence, nil, details)
|
var msgs []string
|
||||||
|
func() {
|
||||||
|
// safely lock the fence because we are outside the main loop
|
||||||
|
c.mu.RLock()
|
||||||
|
defer c.mu.RUnlock()
|
||||||
|
msgs = FenceMatch("", sw, fence, nil, details)
|
||||||
|
}()
|
||||||
for _, msg := range msgs {
|
for _, msg := range msgs {
|
||||||
if err := writeLiveMessage(conn, []byte(msg), true, connType, websocket); err != nil {
|
if err := writeLiveMessage(conn, []byte(msg), true, connType, websocket); err != nil {
|
||||||
return nil // nil return is fine here
|
return nil // nil return is fine here
|
||||||
|
@ -15,7 +15,6 @@ type List struct {
|
|||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
queue queue
|
queue queue
|
||||||
bgrun bool
|
bgrun bool
|
||||||
arr []Item
|
|
||||||
Expired func(item Item)
|
Expired func(item Item)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +31,7 @@ func (list *List) Push(item Item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (list *List) bg() {
|
func (list *List) bg() {
|
||||||
unix := time.Now().UnixNano()
|
now := time.Now().UnixNano()
|
||||||
for {
|
for {
|
||||||
list.mu.Lock()
|
list.mu.Lock()
|
||||||
if list.queue.len == 0 {
|
if list.queue.len == 0 {
|
||||||
@ -40,7 +39,7 @@ func (list *List) bg() {
|
|||||||
list.mu.Unlock()
|
list.mu.Unlock()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if unix > list.queue.peek().unix {
|
if now > list.queue.peek().unix { // now.After(list.queue.peek().unix)
|
||||||
n := list.queue.pop()
|
n := list.queue.pop()
|
||||||
list.mu.Unlock()
|
list.mu.Unlock()
|
||||||
if list.Expired != nil {
|
if list.Expired != nil {
|
||||||
@ -49,7 +48,7 @@ func (list *List) bg() {
|
|||||||
} else {
|
} else {
|
||||||
list.mu.Unlock()
|
list.mu.Unlock()
|
||||||
time.Sleep(time.Second / 10)
|
time.Sleep(time.Second / 10)
|
||||||
unix = time.Now().UnixNano()
|
now = time.Now().UnixNano()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user