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")
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 {
57 defer sub_in_chans.Close()
60 if pub_out_socket != nil {
61 defer pub_out_socket.Close()
63 if keylookup_socket != nil {
64 defer keylookup_socket.Close()
66 if sub_in_chans == nil || pub_out_socket == nil || keylookup_socket == nil {
67 panic("zmq sockets must not be nil !!")
71 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
72 //~ ticker := time.NewTicker(time.Duration(5) * time.Minute)
74 store_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
75 go BrainCenter(zmqctx, brain_listen_addr_, store_these_events_chan)
77 go MetaEventRoutine_Movement(ps, 10, 20, 10)
78 go MetaEventRoutine_Presence(ps, 21, 200)
80 // --- get update on most recent status ---
81 answ := ZmqsAskQuestionsAndClose(zmqctx, door_cmd_addr_, [][][]byte{[][]byte{[]byte("status")}})
82 for _, a := range answ {
83 ParseSocketInputLine(a, ps, keylookup_socket)
86 publish_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
89 case subin := <-sub_in_chans.In():
90 ParseSocketInputLine(subin, ps, keylookup_socket)
93 case event_interface := <-publish_these_events_chan:
94 data, err := r3events.MarshalEvent2ByteByte(event_interface)
95 Debug_.Printf("publishing %s", data)
100 if err := pub_out_socket.Send(data); err != nil {