Problem string
[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     //~ "./brain"
10     pubsub "github.com/tuxychandru/pubsub"
11     zmq "github.com/vaughan0/go-zmq"
12     r3events "svn.spreadspace.org/realraum/go.svn/r3events"
13     )
14
15 var (
16         //~ re_presence_    *regexp.Regexp     = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?")
17         //~ re_state_      *regexp.Regexp     = regexp.MustCompile("State: (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing).*")
18         //~ 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).*")
19         //~ re_infocard_      *regexp.Regexp     = regexp.MustCompile("Info\\(card\\): card\\(([a-fA-F0-9]+)\\) (found|not found).*")
20         re_cardid_      *regexp.Regexp     = regexp.MustCompile("card\\(([a-fA-F0-9]+)\\)")
21         //~ re_infoajar_      *regexp.Regexp     = regexp.MustCompile("Info\\(ajar\\): door is now (ajar|shut)")
22         //~ re_command_     *regexp.Regexp     = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?")
23         //~ re_button_      *regexp.Regexp     = regexp.MustCompile("PanicButton|button\\d?")
24         //~ re_temp_        *regexp.Regexp     = regexp.MustCompile("temp0: (\\d+\\.\\d+)")
25         //~ re_photo_       *regexp.Regexp     = regexp.MustCompile("photo0: (\\d+)")
26 )
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, ts}, "door")
39         case "reset":
40             ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
41         case "timeout_after_open":
42             ps.Pub(r3events.DoorProblemEvent{10, ts}, "door")
43             ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
44         case "timeout_after_close":
45             ps.Pub(r3events.DoorProblemEvent{20, 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
56 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
57     ts := time.Now().Unix()
58     if len(lines) < 1 { return }
59     Debug_.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
60     switch string(lines[0]) {
61         case "State:":
62             if len(lines) < 2 { return }
63             parseSocketInputLine_State(lines[1:], ps, ts)
64         case "Status:":
65             if len(lines) < 3 { return }
66             if len(lines[1]) < 4 { return }
67             ps.Pub(r3events.DoorLockUpdate{string(lines[1])[0:4] != "open", ts}, "door")
68             ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
69         case "Info(card):":
70             if len(lines) < 3 { return }
71             if string(lines[2]) != "found" { return }
72             match_cardid := re_cardid_.FindSubmatch(lines[1])
73             if len(match_cardid) > 1 {
74                 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
75                 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
76                 if err != nil {
77                     Syslog_.Print("CardID Lookup Error",err)
78                     nick = "Unresolvable KeyID"
79                 }
80                 // new event: toggle by user nick using card
81                 ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
82             }
83         case "Info(ajar):":
84             if len(lines) < 5 { return }
85             ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door")
86         case "open", "close", "toggle", "reset":
87             ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
88         case "BackdoorInfo(ajar):":
89             ps.Pub(r3events.BackdoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts},"door")
90         case "temp0:","temp1:", "temp2:", "temp3:":
91             sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
92             if err != nil {return }
93             newtemp, err := strconv.ParseFloat(string(lines[1]), 10)
94             if err != nil {return }
95             ps.Pub(r3events.TempSensorUpdate{int(sensorid), newtemp, ts}, "sensors")
96         case "photo0:","photo1:", "photo2:", "photo3:":
97             sensorid, err := strconv.ParseInt(string(lines[0][5]), 10, 32)
98             if err != nil {return }
99             newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
100             if err != nil {return }
101             ps.Pub(r3events.IlluminationSensorUpdate{int(sensorid), newphoto, ts}, "sensors")
102         case "rh0:":
103             //~ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
104             //~ if err != nil {return }
105             relhumid, err := strconv.ParseInt(string(lines[1]), 10, 32)
106             if err != nil {return }
107             ps.Pub(r3events.RelativeHumiditySensorUpdate{0, int(relhumid), ts}, "sensors")
108         case "dust0:","dust1:","dust2:":
109             sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
110             if err != nil {return }
111             dustlvl, err := strconv.ParseInt(string(lines[1]), 10, 32)
112             if err != nil {return }
113             ps.Pub(r3events.DustSensorUpdate{int(sensorid), dustlvl, ts}, "sensors")
114         default:
115             evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
116             if err == nil {
117                 ps.Pub(evnt, pubsubcat)
118             }
119     }
120 }
121
122 func MakeTimeTick(ps *pubsub.PubSub) {
123     ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
124 }
125
126     //~ match_presence := re_presence_.FindStringSubmatch(line)
127     //~ match_status := re_status_.FindStringSubmatch(line)
128     //~ match_command := re_command_.FindStringSubmatch(line)
129     //~ match_button := re_button_.FindStringSubmatch(line)
130     //~ match_temp := re_temp_.FindStringSubmatch(line)
131     //~ match_photo := re_photo_.FindStringSubmatch(line)
132         //~ if match_button != nil {
133         //~ // brn.Oboite("button0", ts)
134         //~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons")
135         //~ } else if match_temp != nil {
136                 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
137                 //~ if err == nil {
138             //~ // brn.Oboite( "temp0", newtemp)
139             //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
140                 //~ }
141         //~ } else if match_photo != nil {
142                 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
143                 //~ if err == nil {
144             //~ // brn.Oboite("photo0", newphoto)
145             //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
146                 //~ }
147         //~ } else if line == "movement" {
148         //~ // brn.Oboite("movement", ts)
149         //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
150         //~ }