9e3c3f035ac26cece68a00940da7978f51bf7d8c
[svn42.git] / go / r3-eventbroker_zmq / sockettoevent.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6         "bytes"
7         "regexp"
8         "strconv"
9         "time"
10         //~ "./brain"
11         pubsub "github.com/tuxychandru/pubsub"
12         zmq "github.com/vaughan0/go-zmq"
13         r3events "svn.spreadspace.org/realraum/go.svn/r3events"
14 )
15
16 var (
17         //~ re_presence_    *regexp.Regexp     = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?")
18         //~ re_state_      *regexp.Regexp     = regexp.MustCompile("State: (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing).*")
19         //~ re_status_      *regexp.Regexp     = regexp.MustCompile("Status: (closed|opened), (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing), (ajar|shut).*")
20         //~ re_infocard_      *regexp.Regexp     = regexp.MustCompile("Info\\(card\\): card\\(([a-fA-F0-9]+)\\) (found|not found).*")
21         re_cardid_ *regexp.Regexp = regexp.MustCompile("card\\(([a-fA-F0-9]+)\\)")
22         //~ re_infoajar_      *regexp.Regexp     = regexp.MustCompile("Info\\(ajar\\): door is now (ajar|shut)")
23         //~ re_command_     *regexp.Regexp     = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?")
24         //~ re_button_      *regexp.Regexp     = regexp.MustCompile("PanicButton|button\\d?")
25         //~ re_temp_        *regexp.Regexp     = regexp.MustCompile("temp0: (\\d+\\.\\d+)")
26         //~ re_photo_       *regexp.Regexp     = regexp.MustCompile("photo0: (\\d+)")
27 )
28
29 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
30         switch string(lines[0]) {
31         case "closed":
32                 ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
33         case "opened":
34                 ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
35         case "manual", "manual_movement": //movement
36                 ps.Pub(r3events.DoorManualMovementEvent{ts}, "door")
37         case "error":
38                 ps.Pub(r3events.DoorProblemEvent{100, string(bytes.Join(lines, []byte(" "))), ts}, "door")
39         case "reset":
40                 ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
41         case "timeout_after_open":
42                 ps.Pub(r3events.DoorProblemEvent{10, string(lines[0]), ts}, "door")
43                 ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
44         case "timeout_after_close":
45                 ps.Pub(r3events.DoorProblemEvent{20, string(lines[0]), ts}, "door")
46                 // can't say for sure that door is locked if we ran into timeout while closing
47                 //~ ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
48         case "opening":
49         case "closing":
50         default:
51                 Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)
52         }
53 }
54
55 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
56         ts := time.Now().Unix()
57         if len(lines) < 1 {
58                 return
59         }
60         Debug_.Printf("ParseSocketInputLine: %s %s", string(lines[0]), lines[1:])
61         switch string(lines[0]) {
62         case "State:":
63                 if len(lines) < 2 {
64                         return
65                 }
66                 parseSocketInputLine_State(lines[1:], ps, ts)
67         case "Status:":
68                 if len(lines) < 3 {
69                         return
70                 }
71                 if len(lines[1]) < 4 {
72                         return
73                 }
74                 ps.Pub(r3events.DoorLockUpdate{string(lines[1])[0:4] != "open", ts}, "door")
75                 ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
76         case "Info(card):":
77                 if len(lines) < 3 {
78                         return
79                 }
80                 if string(lines[2]) != "found" {
81                         return
82                 }
83                 match_cardid := re_cardid_.FindSubmatch(lines[1])
84                 if len(match_cardid) > 1 {
85                         // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
86                         nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
87                         if err != nil {
88                                 Syslog_.Print("CardID Lookup Error", err)
89                                 nick = "Unresolvable KeyID"
90                         }
91                         // new event: toggle by user nick using card
92                         ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts}, "doorcmd")
93                 }
94         case "Info(ajar):":
95                 if len(lines) < 5 {
96                         return
97                 }
98                 ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door")
99         case "open", "close", "toggle", "reset":
100                 switch len(lines) {
101                 case 2:
102                         ps.Pub(r3events.DoorCommandEvent{Command: string(lines[0]), Using: string(lines[1]), Ts: ts}, "doorcmd")
103                 case 3:
104                         ps.Pub(r3events.DoorCommandEvent{Command: string(lines[0]), Using: string(lines[1]), Who: string(lines[2]), Ts: ts}, "doorcmd")
105                 default:
106                         return
107                 }
108         case "BackdoorInfo(ajar):":
109                 ps.Pub(r3events.BackdoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
110         case "GasLeakAlert":
111                 ps.Pub(r3events.GasLeakAlert{ts}, "sensors")
112         case "temp0:", "temp1:", "temp2:", "temp3:":
113                 sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
114                 if err != nil {
115                         return
116                 }
117                 newtemp, err := strconv.ParseFloat(string(lines[1]), 10)
118                 if err != nil {
119                         return
120                 }
121                 ps.Pub(r3events.TempSensorUpdate{int(sensorid), newtemp, ts}, "sensors")
122         case "photo0:", "photo1:", "photo2:", "photo3:":
123                 sensorid, err := strconv.ParseInt(string(lines[0][5]), 10, 32)
124                 if err != nil {
125                         return
126                 }
127                 newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
128                 if err != nil {
129                         return
130                 }
131                 ps.Pub(r3events.IlluminationSensorUpdate{int(sensorid), newphoto, ts}, "sensors")
132         case "rh0:":
133                 //~ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
134                 //~ if err != nil {return }
135                 relhumid, err := strconv.ParseInt(string(lines[1]), 10, 32)
136                 if err != nil {
137                         return
138                 }
139                 ps.Pub(r3events.RelativeHumiditySensorUpdate{0, int(relhumid), ts}, "sensors")
140         case "dust0:", "dust1:", "dust2:":
141                 sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
142                 if err != nil {
143                         return
144                 }
145                 dustlvl, err := strconv.ParseInt(string(lines[1]), 10, 32)
146                 if err != nil {
147                         return
148                 }
149                 ps.Pub(r3events.DustSensorUpdate{int(sensorid), dustlvl, ts}, "sensors")
150         default:
151                 evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
152                 if err == nil {
153                         ps.Pub(evnt, pubsubcat)
154                 }
155         }
156 }
157
158 func MakeTimeTick(ps *pubsub.PubSub) {
159         ps.Pub(r3events.TimeTick{time.Now().Unix()}, "time")
160 }
161
162 //~ match_presence := re_presence_.FindStringSubmatch(line)
163 //~ match_status := re_status_.FindStringSubmatch(line)
164 //~ match_command := re_command_.FindStringSubmatch(line)
165 //~ match_button := re_button_.FindStringSubmatch(line)
166 //~ match_temp := re_temp_.FindStringSubmatch(line)
167 //~ match_photo := re_photo_.FindStringSubmatch(line)
168 //~ if match_button != nil {
169 //~ // brn.Oboite("button0", ts)
170 //~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons")
171 //~ } else if match_temp != nil {
172 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
173 //~ if err == nil {
174 //~ // brn.Oboite( "temp0", newtemp)
175 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
176 //~ }
177 //~ } else if match_photo != nil {
178 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
179 //~ if err == nil {
180 //~ // brn.Oboite("photo0", newphoto)
181 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
182 //~ }
183 //~ } else if line == "movement" {
184 //~ // brn.Oboite("movement", ts)
185 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
186 //~ }