1 // (c) Bernhard Tittelbach, 2013
7 pubsub "github.com/tuxychandru/pubsub"
9 r3events "svn.spreadspace.org/realraum/go.svn/r3events"
13 /// Movement Meta Event Injector:
14 /// threshold number of movements within gran_duration*granularity seconds -> SomethingReallyIsMoving{True}
15 /// No movement within 3 hours but movement within the last 6 hours -> SomethingReallyIsMoving{False}
17 /// Thus SomethingReallyIsMoving{True} fires regularly, but at most every gran_duration seconds
18 /// While SomethingReallyIsMoving{False} fires only once to assure us that everybody might really be gone
21 func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int , threshold uint32) {
22 var last_movement,last_movement1,last_movement2,last_movement3 int64
24 movement_window := ring.New(granularity+1)
25 events_chan := ps.Sub("movement")
26 defer ps.Unsub(events_chan, "movement")
27 myticker := time.NewTicker(time.Duration(gran_duration) * time.Second)
30 case event := <- events_chan:
32 case r3events.MovementSensorUpdate:
33 if movement_window.Value == nil {
34 movement_window.Value = uint32(1)
36 movement_window.Value = uint32(movement_window.Value.(uint32) + 1)
39 case gots := <- myticker.C:
41 movement_window.Prev().Value = (uint32) (0)
42 movement_window = movement_window.Next()
44 movement_window.Do(func(v interface{}){if v != nil {movsum += v.(uint32)}})
45 if movsum > threshold {
46 confidence = uint8(movsum)
47 ps.Pub( r3events.SomethingReallyIsMoving{true, confidence ,ts}, "movement")
54 if last_movement > 0 && ts - last_movement < 3600*6 {
55 if ts - last_movement > 3600*3 {
57 ps.Pub( r3events.SomethingReallyIsMoving{false,99,ts}, "movement")
58 } else if ts - last_movement > 3600 && last_movement3 > 0 {
60 ps.Pub( r3events.SomethingReallyIsMoving{false,50,ts}, "movement")
61 } else if ts - last_movement > 1800 && last_movement2 > 0 {
63 ps.Pub( r3events.SomethingReallyIsMoving{false,20,ts}, "movement")
64 } else if ts - last_movement > 120 && last_movement1 > 0 {
66 ps.Pub( r3events.SomethingReallyIsMoving{false,5,ts}, "movement")