1 // (c) Bernhard Tittelbach, 2013
8 pubsub "github.com/tuxychandru/pubsub"
9 r3events "svn.spreadspace.org/realraum/go.svn/r3events"
12 func MetaEventRoutine_Presence(ps *pubsub.PubSub, movement_timeout, button_timeout int64) {
13 //~ var last_door_cmd *r3events.DoorCommandEvent
14 var last_presence bool
15 var last_event_indicating_presence, last_frontlock_use int64
16 var front_locked, front_shut, back_shut bool = true, true, true
18 events_chan := ps.Sub("door", "doorcmd", "buttons", "movement")
19 defer ps.Unsub(events_chan, "door", "doorcmd", "buttons", "movement")
21 for event := range(events_chan) {
22 Debug_.Printf("Presence prior: %t : %T %+v", last_presence, event, event)
23 new_presence := last_presence
24 ts := time.Now().Unix()
25 switch evnt := event.(type) {
26 case r3events.SomethingReallyIsMoving:
28 //ignore movements that happened just after locking door
29 if (evnt.Ts - last_event_indicating_presence) > movement_timeout {
32 last_event_indicating_presence = evnt.Ts
34 if last_presence { Syslog_.Printf("Presence: Mhh, SomethingReallyIsMoving{%+v} received but presence still true. Quite still a bunch we have here.", evnt) }
35 if front_locked && front_shut && back_shut && evnt.Confidence >= 20 && last_event_indicating_presence > 1800 {
39 case r3events.BoreDoomButtonPressEvent:
41 last_event_indicating_presence = evnt.Ts
42 case r3events.DoorCommandEvent:
43 //~ last_door_cmd = &evnt
44 case r3events.DoorLockUpdate:
45 front_locked = evnt.Locked
46 last_frontlock_use = evnt.Ts
47 last_event_indicating_presence = evnt.Ts
48 case r3events.DoorAjarUpdate:
49 if front_shut == false && evnt.Shut && front_locked && evnt.Ts - last_frontlock_use > 2 {
50 Syslog_.Print("Presence: ignoring frontdoor ajar event, since obviously someone is fooling around with the microswitch while the door is still open")
52 front_shut = evnt.Shut
54 last_event_indicating_presence = evnt.Ts
55 case r3events.BackdoorAjarUpdate:
57 last_event_indicating_presence = evnt.Ts
60 any_door_unlocked := (front_locked == false)
61 any_door_ajar := ! (front_shut && back_shut)
63 if new_presence != last_presence {
64 //... skip state check .. we had a definite presence event
65 } else if any_door_unlocked || any_door_ajar {
70 //~ Debug_.Printf("Presence: new: %s , last:%s", new_presence, last_presence)
71 if new_presence != last_presence {
72 last_presence = new_presence
73 ps.Pub(r3events.PresenceUpdate{new_presence, ts} , "presence")