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