ea9b0e5a42b1a0dcf988e22b4cbd32dc905a8c09
[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     "log"
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 type DoorLockUpdate struct {
30     DoorID int
31     Locked bool
32     Ts int64
33 }
34
35 type DoorAjarUpdate struct {
36     DoorID int
37     Shut bool
38     Ts int64
39 }
40
41 type DoorCommandEvent struct {
42     Command string
43     Using string
44     Who string
45     Ts int64
46 }
47
48 type ButtonPressUpdate struct {
49     Buttonindex int
50     Ts int64
51 }
52
53 type TempSensorUpdate struct {
54     Sensorindex int
55     Value float64
56     Ts int64
57 }
58
59 type IlluminationSensorUpdate struct {
60     Sensorindex int
61     Value int64
62     Ts int64
63 }
64
65 type TimeTick struct {
66     Ts int64
67 }
68
69 type MovementSensorUpdate struct {
70     Sensorindex int
71     Ts int64
72 }
73
74 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
75     switch string(lines[0]) {
76         case "closed":
77             ps.Pub(DoorLockUpdate{0, true, ts}, "door")
78         case "opened":
79             ps.Pub(DoorLockUpdate{0, false, ts}, "door")
80         case "manual":   //movement
81         case "error":
82         case "reset":
83             ps.Pub(DoorLockUpdate{0, true, ts}, "door")
84         case "timeout":   //after open | after close
85         case "opening":
86         case "closing":
87         default:
88     }
89 }
90
91
92 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
93     var tidbit interface{}
94     ts := time.Now().Unix()
95     if len(lines) < 1 { return }
96     log.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
97     switch string(lines[0]) {
98         case "State:":
99             if len(lines) < 2 { return }
100             parseSocketInputLine_State(lines[1:], ps, ts)
101         case "Status:":
102             if len(lines) < 3 { return }
103             tidbit = DoorLockUpdate{0, string(lines[1]) == "closed", ts}
104             //~ brn.Oboite("door", tidbit)
105             ps.Pub(tidbit, "door")
106             tidbit = DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
107             //~ brn.Oboite("door", tidbit)
108             ps.Pub(tidbit, "door")
109         case "Info(card):":
110             if len(lines) < 3 { return }
111             if string(lines[2]) != "found" { return }
112             match_cardid := re_cardid_.FindSubmatch(lines[1])
113             if len(match_cardid) > 1 {
114                 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
115                 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
116                 if err != nil {
117                     Syslog_.Print("CardID Lookup Error",err)
118                     nick = "Unresolvable KeyID"
119                 }
120                 // new event: toggle by user nick using card
121                 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
122             }
123         case "Info(ajar):":
124             if len(lines) < 5 { return }
125             tidbit = DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
126             //~ brn.Oboite("door", tidbit)
127             ps.Pub(tidbit, "door")
128         case "open", "close", "toggle", "reset":
129             ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
130         case "photo0":
131             newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
132             if err == nil {
133                 // brn.Oboite("photo0", newphoto)
134                 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
135             }
136     }
137 }
138
139 func MakeTimeTick(ps *pubsub.PubSub) {
140     ps.Pub(TimeTick{time.Now().Unix()},"time")
141 }
142
143     //~ match_presence := re_presence_.FindStringSubmatch(line)
144     //~ match_status := re_status_.FindStringSubmatch(line)
145     //~ match_command := re_command_.FindStringSubmatch(line)
146     //~ match_button := re_button_.FindStringSubmatch(line)
147     //~ match_temp := re_temp_.FindStringSubmatch(line)
148     //~ match_photo := re_photo_.FindStringSubmatch(line)
149         //~ if match_button != nil {
150         //~ // brn.Oboite("button0", ts)
151         //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
152         //~ } else if match_temp != nil {
153                 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
154                 //~ if err == nil {
155             //~ // brn.Oboite( "temp0", newtemp)
156             //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
157                 //~ }
158         //~ } else if match_photo != nil {
159                 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
160                 //~ if err == nil {
161             //~ // brn.Oboite("photo0", newphoto)
162             //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
163                 //~ }
164         //~ } else if line == "movement" {
165         //~ // brn.Oboite("movement", ts)
166         //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
167         //~ }