1 // (c) Bernhard Tittelbach, 2013
8 pubsub "github.com/tuxychandru/pubsub"
9 r3events "svn.spreadspace.org/realraum/go.svn/r3events"
12 //~ func StringArrayToByteArray(ss []string) [][]byte {
13 //~ bb := make([][]byte, len(ss))
14 //~ for index, s := range(ss) {
15 //~ bb[index] = []byte(s)
20 // ---------- Main Code -------------
24 sensorssub_port_ string
26 keylookup_addr_ string
27 brain_listen_addr_ string
34 flag.StringVar(&door_cmd_addr_, "doorcmdaddr", "ipc:///run/tuer/door_cmd.ipc", "zmq door event publish addr")
35 flag.StringVar(&doorsub_addr_, "doorsubaddr", "tcp://torwaechter.realraum.at:4242", "zmq door event publish addr")
36 flag.StringVar(&sensorssub_port_, "sensorsubport", "tcp://*:4243", "zmq public/listen socket addr for incoming sensor data")
37 flag.StringVar(&pub_port_, "pubport", "tcp://*:4244", "zmq port publishing consodilated events")
38 flag.StringVar(&keylookup_addr_, "keylookupaddr", "ipc:///run/tuer/door_keyname.ipc", "address to use for key/name lookups")
39 flag.StringVar(&brain_listen_addr_, "brainlisten", "tcp://*:4245", "address to listen for requests about latest stored event")
40 flag.BoolVar(&use_syslog_, "syslog", false, "log to syslog local2 facility")
41 flag.BoolVar(&enable_debuglog_, "debug", false, "enable debug logging")
46 if enable_debuglog_ { LogEnableDebuglog() }
49 Syslog_.Print("started")
50 defer Syslog_.Print("exiting")
53 zmqctx, sub_in_chans, pub_out_socket, keylookup_socket := ZmqsInit(doorsub_addr_, sensorssub_port_, pub_port_, keylookup_addr_)
54 if sub_in_chans != nil {defer sub_in_chans.Close()}
56 if pub_out_socket != nil {defer pub_out_socket.Close()}
57 if keylookup_socket != nil {defer keylookup_socket.Close()}
58 if sub_in_chans == nil || pub_out_socket == nil || keylookup_socket == nil {
59 panic("zmq sockets must not be nil !!")
63 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
64 //~ ticker := time.NewTicker(time.Duration(5) * time.Minute)
66 store_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
67 go BrainCenter(zmqctx, brain_listen_addr_, store_these_events_chan)
69 go MetaEventRoutine_Movement(ps, 10, 20, 10)
70 go MetaEventRoutine_Presence(ps, 21, 200)
72 // --- get update on most recent status ---
73 answ := ZmqsAskQuestionsAndClose(zmqctx, door_cmd_addr_, [][][]byte{[][]byte{[]byte("status")}})
74 for _, a := range(answ) {
75 ParseSocketInputLine(a, ps, keylookup_socket)
78 publish_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
81 case subin := <- sub_in_chans.In():
82 ParseSocketInputLine(subin, ps, keylookup_socket)
85 case event_interface := <- publish_these_events_chan:
86 data, err := r3events.MarshalEvent2ByteByte(event_interface)
87 Debug_.Printf("publishing %s",data)
92 if err := pub_out_socket.Send(data); err != nil {