mirror of https://github.com/tidwall/tile38.git
detect inside on enter, fixes #150
This commit is contained in:
parent
0e692a8196
commit
374d199d07
|
@ -82,7 +82,6 @@ func fenceMatch(hookName string, sw *scanWriter, fence *liveFenceSwitches, metas
|
|||
if details.command == "del" {
|
||||
return [][]byte{[]byte(`{"command":"del"` + hookJSONString(hookName, metas) + `,"id":` + jsonString(details.id) + `,"time":` + jsonTimeFormat(details.timestamp) + `}`)}
|
||||
}
|
||||
|
||||
var roamkeys, roamids []string
|
||||
var roammeters []float64
|
||||
var detect string = "outside"
|
||||
|
@ -141,10 +140,20 @@ func fenceMatch(hookName string, sw *scanWriter, fence *liveFenceSwitches, metas
|
|||
if details.fmap == nil {
|
||||
return nil
|
||||
}
|
||||
for {
|
||||
if fence.detect != nil && !fence.detect[detect] {
|
||||
if detect == "enter" {
|
||||
detect = "inside"
|
||||
continue
|
||||
}
|
||||
if detect == "exit" {
|
||||
detect = "outside"
|
||||
continue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
sw.mu.Lock()
|
||||
sw.fmap = details.fmap
|
||||
sw.fullFields = true
|
||||
|
|
|
@ -16,7 +16,8 @@ import (
|
|||
)
|
||||
|
||||
func subTestFence(t *testing.T, mc *mockServer) {
|
||||
runStep(t, mc, "basic", json_FENCE_basic_test)
|
||||
runStep(t, mc, "basic", fence_basic_test)
|
||||
runStep(t, mc, "detect inside,outside", fence_detect_inside_test)
|
||||
}
|
||||
|
||||
type fenceReader struct {
|
||||
|
@ -68,7 +69,7 @@ func (fr *fenceReader) receiveExpect(valex ...string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func json_FENCE_basic_test(mc *mockServer) error {
|
||||
func fence_basic_test(mc *mockServer) error {
|
||||
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", mc.port))
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -152,3 +153,66 @@ func json_FENCE_basic_test(mc *mockServer) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
func fence_detect_inside_test(mc *mockServer) error {
|
||||
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", mc.port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
_, err = fmt.Fprintf(conn, "WITHIN users FENCE DETECT inside,outside POINTS BOUNDS 33.618824 -84.457973 33.654359 -84.399859\r\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buf := make([]byte, 4096)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res := string(buf[:n])
|
||||
if res != "+OK\r\n" {
|
||||
return fmt.Errorf("expected OK, got '%v'", res)
|
||||
}
|
||||
rd := &fenceReader{conn, bufio.NewReader(conn)}
|
||||
|
||||
// send a point
|
||||
c, err := redis.Dial("tcp", fmt.Sprintf(":%d", mc.port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
res, err = redis.String(c.Do("SET", "users", "200", "POINT", "33.642301", "-84.43118"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res != "OK" {
|
||||
return fmt.Errorf("expected OK, got '%v'", res)
|
||||
}
|
||||
|
||||
if err := rd.receiveExpect("command", "set",
|
||||
"detect", "inside",
|
||||
"key", "users",
|
||||
"id", "200",
|
||||
"point", `{"lat":33.642301,"lon":-84.43118}`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err = redis.String(c.Do("SET", "users", "200", "POINT", "34.642301", "-84.43118"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res != "OK" {
|
||||
return fmt.Errorf("expected OK, got '%v'", res)
|
||||
}
|
||||
|
||||
// receive the message
|
||||
if err := rd.receiveExpect("command", "set",
|
||||
"detect", "outside",
|
||||
"key", "users",
|
||||
"id", "200",
|
||||
"point", `{"lat":34.642301,"lon":-84.43118}`); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue