From 51f3617510d7e89a8b432e052db16e0eb9864085 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Tue, 8 Oct 2013 13:46:39 +0000 Subject: [PATCH] tweak presence detection --- go/r3-eventbroker_zmq/main.go | 2 +- go/r3-eventbroker_zmq/metamovement.go | 9 ++++++++- go/r3-eventbroker_zmq/presence.go | 14 ++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/go/r3-eventbroker_zmq/main.go b/go/r3-eventbroker_zmq/main.go index 6745752..492e7fb 100644 --- a/go/r3-eventbroker_zmq/main.go +++ b/go/r3-eventbroker_zmq/main.go @@ -67,7 +67,7 @@ func main() { go BrainCenter(zmqctx, brain_listen_addr_, store_these_events_chan) go MetaEventRoutine_Movement(ps, 10, 20, 10) - go MetaEventRoutine_Presence(ps) + go MetaEventRoutine_Presence(ps, 21, 200) // --- get update on most recent status --- answ := ZmqsAskQuestionsAndClose(zmqctx, door_cmd_addr_, [][][]byte{[][]byte{[]byte("status")}}) diff --git a/go/r3-eventbroker_zmq/metamovement.go b/go/r3-eventbroker_zmq/metamovement.go index b274092..8efda88 100644 --- a/go/r3-eventbroker_zmq/metamovement.go +++ b/go/r3-eventbroker_zmq/metamovement.go @@ -4,13 +4,20 @@ package main import ( "time" - //~ "./brain" pubsub "github.com/tuxychandru/pubsub" "container/ring" r3events "svn.spreadspace.org/realraum/go.svn/r3events" ) +/// Movement Meta Event Injector: +/// threshold number of movements within gran_duration*granularity seconds -> SomethingReallyIsMoving{True} +/// No movement within 3 hours but movement within the last 6 hours -> SomethingReallyIsMoving{False} +/// +/// Thus SomethingReallyIsMoving{True} fires regularly, but at most every gran_duration seconds +/// While SomethingReallyIsMoving{False} fires only once to assure us that everybody might really be gone + + func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int , threshold uint32) { var last_movement int64 movement_window := ring.New(granularity+1) diff --git a/go/r3-eventbroker_zmq/presence.go b/go/r3-eventbroker_zmq/presence.go index 860a674..1a6f11c 100644 --- a/go/r3-eventbroker_zmq/presence.go +++ b/go/r3-eventbroker_zmq/presence.go @@ -9,10 +9,10 @@ import ( r3events "svn.spreadspace.org/realraum/go.svn/r3events" ) -func MetaEventRoutine_Presence(ps *pubsub.PubSub) { +func MetaEventRoutine_Presence(ps *pubsub.PubSub, movement_timeout, button_timeout int64) { //~ var last_door_cmd *r3events.DoorCommandEvent var last_presence bool - var last_movement, last_buttonpress int64 + var last_movement, last_buttonpress, last_frontlock_use int64 var front_locked, front_shut, back_shut bool = true, true, true events_chan := ps.Sub("door", "doorcmd", "buttons", "movement") @@ -28,6 +28,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { last_movement = evnt.Ts } else { last_movement = 0 + if last_presence { Syslog_.Print("Presence: Mhh, SomethingReallyIsMoving{false} received but presence still true. Quite still a bunch we have here.") } } case r3events.BoreDoomButtonPressEvent: last_buttonpress = evnt.Ts @@ -36,8 +37,13 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { //~ last_door_cmd = &evnt case r3events.DoorLockUpdate: front_locked = evnt.Locked + last_frontlock_use = evnt.Ts case r3events.DoorAjarUpdate: - front_shut = evnt.Shut + if front_shut == false && evnt.Shut && front_locked && evnt.Ts - last_frontlock_use > 2 { + Syslog_.Print("Presence: ignoring frontdoor ajar event, since obviously someone is fooling around with the microswitch while the door is still open") + } else { + front_shut = evnt.Shut + } case r3events.BackdoorAjarUpdate: back_shut = evnt.Shut } @@ -49,7 +55,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { //... skip state check .. we had a definite presence event } else if any_door_unlocked || any_door_ajar { new_presence = true - } else if last_movement != 0 || ts - last_buttonpress < 200 { + } else if ts - last_movement < movement_timeout || ts - last_buttonpress < button_timeout { new_presence = true } else { new_presence = false -- 1.7.10.4