1 // (c) Bernhard Tittelbach, 2013
10 pubsub "github.com/tuxychandru/pubsub"
11 r3events "svn.spreadspace.org/realraum/go.svn/r3events"
14 //~ func StringArrayToByteArray(ss []string) [][]byte {
15 //~ bb := make([][]byte, len(ss))
16 //~ for index, s := range(ss) {
17 //~ bb[index] = []byte(s)
22 // ---------- Main Code -------------
26 sensorssub_port_ string
28 keylookup_addr_ string
29 brain_listen_addr_ string
36 flag.StringVar(&door_cmd_addr_, "doorcmdaddr", "ipc:///run/tuer/door_cmd.ipc", "zmq door event publish addr")
37 flag.StringVar(&doorsub_addr_, "doorsubaddr", "tcp://torwaechter.realraum.at:4242", "zmq door event publish addr")
38 flag.StringVar(&sensorssub_port_, "sensorsubport", "tcp://*:4243", "zmq public/listen socket addr for incoming sensor data")
39 flag.StringVar(&pub_port_, "pubport", "tcp://*:4244", "zmq port publishing consodilated events")
40 flag.StringVar(&keylookup_addr_, "keylookupaddr", "ipc:///run/tuer/door_keyname.ipc", "address to use for key/name lookups")
41 flag.StringVar(&brain_listen_addr_, "brainlisten", "tcp://*:4245", "address to listen for requests about latest stored event")
42 flag.BoolVar(&use_syslog_, "syslog", false, "log to syslog local2 facility")
43 flag.BoolVar(&enable_debuglog_, "debug", false, "enable debug logging")
48 if enable_debuglog_ { LogEnableDebuglog() }
51 Syslog_.Print("started")
52 defer Syslog_.Print("exiting")
55 zmqctx, sub_in_chans, pub_out_socket, keylookup_socket := ZmqsInit(doorsub_addr_, sensorssub_port_, pub_port_, keylookup_addr_)
56 if sub_in_chans != nil {defer sub_in_chans.Close()}
58 if pub_out_socket != nil {defer pub_out_socket.Close()}
59 if keylookup_socket != nil {defer keylookup_socket.Close()}
60 if sub_in_chans == nil || pub_out_socket == nil || keylookup_socket == nil {
61 panic("zmq sockets must not be nil !!")
65 defer ps.Shutdown() // ps.Shutdown should be called before zmq_ctx.Close(), since it will cause goroutines to shutdown and close zqm_sockets which is needed for zmq_ctx.Close() to return
66 //~ ticker := time.NewTicker(time.Duration(5) * time.Minute)
68 store_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
69 go BrainCenter(zmqctx, brain_listen_addr_, store_these_events_chan)
71 go MetaEventRoutine_Movement(ps, 10, 20, 10)
72 go MetaEventRoutine_Presence(ps)
74 // --- get update on most recent status ---
75 answ := ZmqsAskQuestionsAndClose(zmqctx, door_cmd_addr_, [][][]byte{[][]byte{[]byte("status")}})
76 for _, a := range(answ) {
77 ParseSocketInputLine(a, ps, keylookup_socket)
80 publish_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
83 case subin := <- sub_in_chans.In():
84 ParseSocketInputLine(subin, ps, keylookup_socket)
87 case event_interface := <- publish_these_events_chan:
88 data, err := r3events.MarshalEvent2ByteByte(event_interface)
89 Debug_.Printf("publishing %s",data)
94 if err := pub_out_socket.Send(data); err != nil {