From 8730091c3867902511b81c17a5c074220066b03c Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Sat, 5 Oct 2013 16:15:37 +0000 Subject: [PATCH] init with current status --- go/r3-eventbroker_zmq/main.go | 8 ++++++++ go/r3-eventbroker_zmq/presence.go | 8 ++++---- go/r3-eventbroker_zmq/zeromq.go | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/go/r3-eventbroker_zmq/main.go b/go/r3-eventbroker_zmq/main.go index 0495edd..c80ea3a 100644 --- a/go/r3-eventbroker_zmq/main.go +++ b/go/r3-eventbroker_zmq/main.go @@ -27,6 +27,7 @@ var ( pub_port_ string keylookup_addr_ string brain_listen_addr_ string + door_cmd_addr_ string use_syslog_ bool enable_debuglog_ bool ) @@ -37,6 +38,7 @@ func usage() { } func init() { + flag.StringVar(&door_cmd_addr_, "doorcmdaddr", "ipc:///run/tuer/door_cmd.ipc", "zmq door event publish addr") flag.StringVar(&doorsub_addr_, "doorsubaddr", "tcp://torwaechter.realraum.at:4242", "zmq door event publish addr") flag.StringVar(&sensorssub_port_, "sensorsubport", "tcp://*:4243", "zmq public/listen socket addr for incoming sensor data") flag.StringVar(&pub_port_, "pubport", "tcp://*:4244", "zmq port publishing consodilated events") @@ -75,6 +77,12 @@ func main() { go MetaEventRoutine_Movement(ps, 10, 20, 10) go MetaEventRoutine_Presence(ps) + // --- get update on most recent status --- + answ := ZmqsAskQuestionsAndClose(zmqctx, door_cmd_addr_, [][][]byte{[][]byte{[]byte("status")}}) + for _, a := range(answ) { + ParseSocketInputLine(a, ps, keylookup_socket) + } + publish_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement") for { select { diff --git a/go/r3-eventbroker_zmq/presence.go b/go/r3-eventbroker_zmq/presence.go index d65788d..860a674 100644 --- a/go/r3-eventbroker_zmq/presence.go +++ b/go/r3-eventbroker_zmq/presence.go @@ -10,7 +10,7 @@ import ( ) func MetaEventRoutine_Presence(ps *pubsub.PubSub) { - //~ var last_door_cmd *DoorCommandEvent + //~ var last_door_cmd *r3events.DoorCommandEvent var last_presence bool var last_movement, last_buttonpress int64 var front_locked, front_shut, back_shut bool = true, true, true @@ -19,7 +19,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { defer ps.Unsub(events_chan, "door", "doorcmd", "buttons", "movement") for event := range(events_chan) { - //~ Debug_.Printf("Presence: %s - %s", event, doorstatemap) + Debug_.Printf("Presence prior: %t : %T %+v", last_presence, event, event) new_presence := last_presence ts := time.Now().Unix() switch evnt := event.(type) { @@ -32,7 +32,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { case r3events.BoreDoomButtonPressEvent: last_buttonpress = evnt.Ts new_presence = true - //~ case DoorCommandEvent: + case r3events.DoorCommandEvent: //~ last_door_cmd = &evnt case r3events.DoorLockUpdate: front_locked = evnt.Locked @@ -42,7 +42,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) { back_shut = evnt.Shut } - any_door_unlocked := ! front_locked + any_door_unlocked := (front_locked == false) any_door_ajar := ! (front_shut && back_shut) if new_presence != last_presence { diff --git a/go/r3-eventbroker_zmq/zeromq.go b/go/r3-eventbroker_zmq/zeromq.go index 81952c9..a3d6b02 100644 --- a/go/r3-eventbroker_zmq/zeromq.go +++ b/go/r3-eventbroker_zmq/zeromq.go @@ -109,6 +109,26 @@ func ZmqsRequestAnswer(sock *zmq.Socket, request [][]byte) (answer [][]byte) { return parts } +func ZmqsAskQuestionsAndClose(ctx *zmq.Context, addr string, questions [][][]byte) [][][]byte { + if len(addr) == 0 || ctx == nil { return nil } + + req_sock, err := ctx.Socket(zmq.Req) + if err != nil { + return nil + } + defer req_sock.Close() + + if err = req_sock.Connect(addr); err != nil { + return nil + } + + rv := make([][][]byte, len(questions)) + for index, q := range(questions) { + rv[index] = ZmqsRequestAnswer(req_sock, q) + } + return rv +} + func LookupCardIdNick(s *zmq.Socket, hexbytes []byte) (string, error) { answ := ZmqsRequestAnswer(s, [][]byte{hexbytes}) if len(answ) == 0 { -- 1.7.10.4