1 // (c) Bernhard Tittelbach, 2013
8 pubsub "github.com/tuxychandru/pubsub"
12 type SomethingReallyIsMoving struct {
18 func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int , threshold uint32) {
19 var last_movement int64
20 movement_window := ring.New(granularity+1)
21 events_chan := ps.Sub("movement")
22 myticker := time.NewTicker(time.Duration(gran_duration) * time.Second)
25 case event := <- events_chan:
27 case MovementSensorUpdate:
28 movement_window.Value = (uint32) (movement_window.Value.(uint32) + 1)
31 movement_window.Prev().Value = (uint32) (0)
32 movement_window = movement_window.Next()
34 movement_window.Do(func(v interface{}){if v != nil {movsum += v.(uint32)}})
35 ts := time.Now().Unix()
36 if movsum > threshold {
37 ps.Pub( SomethingReallyIsMoving{true,ts}, "movement")
41 if last_movement > 0 && ts - last_movement < 3600*6 && ts - last_movement > 3600*3 {
43 ps.Pub( SomethingReallyIsMoving{false, ts}, "movement")