reformat and fix array index bug
[svn42.git] / go / r3-eventbroker_zmq / main.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6         "flag"
7         //~ "time"
8         pubsub "github.com/tuxychandru/pubsub"
9         r3events "svn.spreadspace.org/realraum/go.svn/r3events"
10 )
11
12 //~ func StringArrayToByteArray(ss []string) [][]byte {
13 //~ bb := make([][]byte, len(ss))
14 //~ for index, s := range(ss) {
15 //~ bb[index] = []byte(s)
16 //~ }
17 //~ return bb
18 //~ }
19
20 // ---------- Main Code -------------
21
22 var (
23         doorsub_addr_      string
24         sensorssub_port_   string
25         pub_port_          string
26         keylookup_addr_    string
27         brain_listen_addr_ string
28         door_cmd_addr_     string
29         use_syslog_        bool
30         enable_debuglog_   bool
31 )
32
33 func init() {
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")
42         flag.Parse()
43 }
44
45 func main() {
46         if enable_debuglog_ {
47                 LogEnableDebuglog()
48         }
49         if use_syslog_ {
50                 LogEnableSyslog()
51                 Syslog_.Print("started")
52                 defer Syslog_.Print("exiting")
53         }
54
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()
58         }
59         defer zmqctx.Close()
60         if pub_out_socket != nil {
61                 defer pub_out_socket.Close()
62         }
63         if keylookup_socket != nil {
64                 defer keylookup_socket.Close()
65         }
66         if sub_in_chans == nil || pub_out_socket == nil || keylookup_socket == nil {
67                 panic("zmq sockets must not be nil !!")
68         }
69
70         ps := pubsub.New(10)
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)
73
74         store_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
75         go BrainCenter(zmqctx, brain_listen_addr_, store_these_events_chan)
76
77         go MetaEventRoutine_Movement(ps, 10, 20, 10)
78         go MetaEventRoutine_Presence(ps, 21, 200)
79
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)
84         }
85
86         publish_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
87         for {
88                 select {
89                 case subin := <-sub_in_chans.In():
90                         ParseSocketInputLine(subin, ps, keylookup_socket)
91                 //~ case <- ticker.C:
92                 //~ MakeTimeTick(ps)
93                 case event_interface := <-publish_these_events_chan:
94                         data, err := r3events.MarshalEvent2ByteByte(event_interface)
95                         Debug_.Printf("publishing %s", data)
96                         if err != nil {
97                                 Syslog_.Print(err)
98                                 continue
99                         }
100                         if err := pub_out_socket.Send(data); err != nil {
101                                 panic(err)
102                         }
103                 }
104         }
105
106 }