tweak presence detection
[svn42.git] / go / r3-eventbroker_zmq / metamovement.go
index 71aa209..8efda88 100644 (file)
@@ -4,27 +4,31 @@ package main
 
 import (
     "time"
-    //~ "./brain"
     pubsub "github.com/tuxychandru/pubsub"
     "container/ring"
+    r3events "svn.spreadspace.org/realraum/go.svn/r3events"
     )
 
-type SomethingReallyIsMoving struct {
-    Movement bool
-    Ts int64
-}
+
+/// Movement Meta Event Injector:
+///     threshold number of movements within gran_duration*granularity seconds -> SomethingReallyIsMoving{True}
+///     No movement within 3 hours but movement within the last 6 hours -> SomethingReallyIsMoving{False}
+///
+/// Thus SomethingReallyIsMoving{True} fires regularly, but at most every gran_duration seconds
+/// While SomethingReallyIsMoving{False} fires only once to assure us that everybody might really be gone
 
 
 func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int , threshold uint32) {
     var last_movement int64
     movement_window := ring.New(granularity+1)
     events_chan := ps.Sub("movement")
+    defer ps.Unsub(events_chan, "movement")
     myticker := time.NewTicker(time.Duration(gran_duration) * time.Second)
 
     for { select {
         case event := <- events_chan:
             switch event.(type) {
-                case MovementSensorUpdate:
+                case r3events.MovementSensorUpdate:
                     movement_window.Value =  (uint32) (movement_window.Value.(uint32)  + 1)
             }
         case <- myticker.C:
@@ -34,13 +38,13 @@ func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int
             movement_window.Do(func(v interface{}){if v != nil {movsum += v.(uint32)}})
             ts :=  time.Now().Unix()
             if movsum > threshold {
-                ps.Pub( SomethingReallyIsMoving{true,ts}, "movement")
+                ps.Pub( r3events.SomethingReallyIsMoving{true,ts}, "movement")
                 last_movement = ts
             }
 
             if last_movement > 0 && ts - last_movement < 3600*6 && ts - last_movement > 3600*3 {
                 last_movement = 0
-                ps.Pub( SomethingReallyIsMoving{false, ts}, "movement")
+                ps.Pub( r3events.SomethingReallyIsMoving{false, ts}, "movement")
             }
     } }
 }
\ No newline at end of file