temp sensor location names
[svn42.git] / go / r3-eventbroker_zmq / presence.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     "time"
7     //~ "./brain"
8     pubsub "github.com/tuxychandru/pubsub"
9     r3events "svn.spreadspace.org/realraum/go.svn/r3events"
10     )
11
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
17
18     events_chan := ps.Sub("door", "doorcmd", "buttons", "movement")
19     defer ps.Unsub(events_chan, "door", "doorcmd", "buttons", "movement")
20
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:
27                 if evnt.Movement {
28                     //ignore movements that happened just after locking door
29                     if (evnt.Ts - last_event_indicating_presence) > movement_timeout {
30                         new_presence = true
31                     }
32                     last_event_indicating_presence = evnt.Ts
33                 } else {
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 {
36                         new_presence = false
37                     }
38                 }
39             case r3events.BoreDoomButtonPressEvent:
40                 new_presence = true
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")
51                 } else {
52                     front_shut = evnt.Shut
53                 }
54                 last_event_indicating_presence = evnt.Ts
55             case r3events.BackdoorAjarUpdate:
56                 back_shut = evnt.Shut
57                 last_event_indicating_presence = evnt.Ts
58         }
59
60         any_door_unlocked := (front_locked == false)
61         any_door_ajar := ! (front_shut && back_shut)
62
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 {
66             new_presence = true
67         } else {
68             new_presence = false
69         }
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")
74         }
75     }
76 }