who
[svn42.git] / go / r3-eventbroker_zmq / sockettoevent.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     "regexp"
7     "strconv"
8     "time"
9     "bytes"
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
30 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
31     switch string(lines[0]) {
32         case "closed":
33             ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
34         case "opened":
35             ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
36         case "manual", "manual_movement":   //movement
37             ps.Pub(r3events.DoorManualMovementEvent{ts}, "door")
38         case "error":
39             ps.Pub(r3events.DoorProblemEvent{100, string(bytes.Join(lines,[]byte(" "))),  ts}, "door")
40         case "reset":
41             ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
42         case "timeout_after_open":
43             ps.Pub(r3events.DoorProblemEvent{10, string(lines[0]), ts}, "door")
44             ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
45         case "timeout_after_close":
46             ps.Pub(r3events.DoorProblemEvent{20, string(lines[0]), ts}, "door")
47             // can't say for sure that door is locked if we ran into timeout while closing
48             //~ ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
49         case "opening":
50         case "closing":
51         default:
52             Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)
53     }
54 }
55
56
57 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
58     ts := time.Now().Unix()
59     if len(lines) < 1 { return }
60     Debug_.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
61     switch string(lines[0]) {
62         case "State:":
63             if len(lines) < 2 { return }
64             parseSocketInputLine_State(lines[1:], ps, ts)
65         case "Status:":
66             if len(lines) < 3 { return }
67             if len(lines[1]) < 4 { return }
68             ps.Pub(r3events.DoorLockUpdate{string(lines[1])[0:4] != "open", ts}, "door")
69             ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
70         case "Info(card):":
71             if len(lines) < 3 { return }
72             if string(lines[2]) != "found" { return }
73             match_cardid := re_cardid_.FindSubmatch(lines[1])
74             if len(match_cardid) > 1 {
75                 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
76                 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
77                 if err != nil {
78                     Syslog_.Print("CardID Lookup Error",err)
79                     nick = "Unresolvable KeyID"
80                 }
81                 // new event: toggle by user nick using card
82                 ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
83             }
84         case "Info(ajar):":
85             if len(lines) < 5 { return }
86             ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door")
87         case "open", "close", "toggle", "reset":
88             ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
89         case "BackdoorInfo(ajar):":
90             ps.Pub(r3events.BackdoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts},"door")
91         case "temp0:","temp1:", "temp2:", "temp3:":
92             sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
93             if err != nil {return }
94             newtemp, err := strconv.ParseFloat(string(lines[1]), 10)
95             if err != nil {return }
96             ps.Pub(r3events.TempSensorUpdate{int(sensorid), newtemp, ts}, "sensors")
97         case "photo0:","photo1:", "photo2:", "photo3:":
98             sensorid, err := strconv.ParseInt(string(lines[0][5]), 10, 32)
99             if err != nil {return }
100             newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
101             if err != nil {return }
102             ps.Pub(r3events.IlluminationSensorUpdate{int(sensorid), newphoto, ts}, "sensors")
103         case "rh0:":
104             //~ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
105             //~ if err != nil {return }
106             relhumid, err := strconv.ParseInt(string(lines[1]), 10, 32)
107             if err != nil {return }
108             ps.Pub(r3events.RelativeHumiditySensorUpdate{0, int(relhumid), ts}, "sensors")
109         case "dust0:","dust1:","dust2:":
110             sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
111             if err != nil {return }
112             dustlvl, err := strconv.ParseInt(string(lines[1]), 10, 32)
113             if err != nil {return }
114             ps.Pub(r3events.DustSensorUpdate{int(sensorid), dustlvl, ts}, "sensors")
115         default:
116             evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
117             if err == nil {
118                 ps.Pub(evnt, pubsubcat)
119             }
120     }
121 }
122
123 func MakeTimeTick(ps *pubsub.PubSub) {
124     ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
125 }
126
127     //~ match_presence := re_presence_.FindStringSubmatch(line)
128     //~ match_status := re_status_.FindStringSubmatch(line)
129     //~ match_command := re_command_.FindStringSubmatch(line)
130     //~ match_button := re_button_.FindStringSubmatch(line)
131     //~ match_temp := re_temp_.FindStringSubmatch(line)
132     //~ match_photo := re_photo_.FindStringSubmatch(line)
133         //~ if match_button != nil {
134         //~ // brn.Oboite("button0", ts)
135         //~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons")
136         //~ } else if match_temp != nil {
137                 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
138                 //~ if err == nil {
139             //~ // brn.Oboite( "temp0", newtemp)
140             //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
141                 //~ }
142         //~ } else if match_photo != nil {
143                 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
144                 //~ if err == nil {
145             //~ // brn.Oboite("photo0", newphoto)
146             //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
147                 //~ }
148         //~ } else if line == "movement" {
149         //~ // brn.Oboite("movement", ts)
150         //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
151         //~ }