1 // (c) Bernhard Tittelbach, 2013
8 pubsub "github.com/tuxychandru/pubsub"
13 type doorstate struct {
18 func MetaEventRoutine_Presence(ps *pubsub.PubSub) {
19 //~ var last_door_cmd *DoorCommandEvent
20 var last_presence bool
21 var last_movement, last_buttonpress int64
22 doorstatemap := make(map[int]doorstate,1)
24 events_chan := ps.Sub("door", "doorcmd", "buttons", "movement")
25 defer ps.Unsub(events_chan, "door", "doorcmd", "buttons", "movement")
27 for event := range(events_chan) {
28 //~ log.Printf("Presence: %s - %s", event, doorstatemap)
29 new_presence := last_presence
30 ts := time.Now().Unix()
31 switch evnt := event.(type) {
32 case r3events.SomethingReallyIsMoving:
34 last_movement = evnt.Ts
38 case r3events.ButtonPressUpdate:
39 last_buttonpress = evnt.Ts
41 //~ case DoorCommandEvent:
42 //~ last_door_cmd = &evnt
43 case r3events.DoorLockUpdate:
44 doorstatemap[evnt.DoorID]=doorstate{locked:evnt.Locked, shut:doorstatemap[evnt.DoorID].shut}
45 case r3events.DoorAjarUpdate:
46 doorstatemap[evnt.DoorID]=doorstate{locked:doorstatemap[evnt.DoorID].locked, shut:evnt.Shut}
49 any_door_unlocked := false
50 any_door_ajar := false
51 for _, ds := range(doorstatemap) {
52 if ds.locked == false {any_door_unlocked = true }
53 if ds.shut == false {any_door_ajar = true }
56 if new_presence != last_presence {
57 //... skip state check .. we had a definite presence event
58 } else if any_door_unlocked || any_door_ajar {
60 } else if last_movement != 0 || ts - last_buttonpress < 200 {
65 //~ log.Printf("Presence: new: %s , last:%s", new_presence, last_presence)
66 if new_presence != last_presence {
67 last_presence = new_presence
68 ps.Pub(r3events.PresenceUpdate{new_presence, ts} , "presence")