1 // (c) Bernhard Tittelbach, 2013
11 pubsub "github.com/tuxychandru/pubsub"
12 zmq "github.com/vaughan0/go-zmq"
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+)")
30 type DoorLockUpdate struct {
36 type DoorAjarUpdate struct {
42 type DoorCommandEvent struct {
49 type ButtonPressUpdate struct {
54 type TempSensorUpdate struct {
60 type IlluminationSensorUpdate struct {
66 type TimeTick struct {
70 type MovementSensorUpdate struct {
75 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
76 switch string(lines[0]) {
78 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
80 ps.Pub(DoorLockUpdate{0, false, ts}, "door")
81 case "manual": //movement
84 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
85 case "timeout": //after open | after close
93 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
94 var tidbit interface{}
95 ts := time.Now().Unix()
96 if len(lines) < 1 { return }
97 log.Print("ParseSocketInputLine",string(lines[0]))
98 switch string(lines[0]) {
100 if len(lines) < 2 { return }
101 parseSocketInputLine_State(lines[1:], ps, ts)
103 if len(lines) < 3 { return }
104 tidbit = DoorLockUpdate{0, string(lines[1]) == "closed", ts}
105 //~ brn.Oboite("door", tidbit)
106 ps.Pub(tidbit, "door")
107 tidbit = DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
108 //~ brn.Oboite("door", tidbit)
109 ps.Pub(tidbit, "door")
111 if len(lines) < 3 { return }
112 if string(lines[2]) != "found" { return }
113 match_cardid := re_cardid_.FindSubmatch(lines[1])
114 if len(match_cardid) > 1 {
115 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
116 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
118 Syslog_.Print("CardID Lookup Error",err)
119 nick = "Unresolvable KeyID"
121 // new event: toggle by user nick using card
122 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
125 if len(lines) < 5 { return }
126 tidbit = DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
127 //~ brn.Oboite("door", tidbit)
128 ps.Pub(tidbit, "door")
129 case "open", "close", "toggle", "reset":
130 ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
132 newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
134 // brn.Oboite("photo0", newphoto)
135 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
140 func MakeTimeTick(ps *pubsub.PubSub) {
141 ps.Pub(TimeTick{time.Now().Unix()},"time")
144 func FormatEventForSocket(event_interface interface{}) (data [][]byte, err error) {
145 msg, err := json.Marshal(data)
149 return [][]byte{msg}, nil
152 //~ match_presence := re_presence_.FindStringSubmatch(line)
153 //~ match_status := re_status_.FindStringSubmatch(line)
154 //~ match_command := re_command_.FindStringSubmatch(line)
155 //~ match_button := re_button_.FindStringSubmatch(line)
156 //~ match_temp := re_temp_.FindStringSubmatch(line)
157 //~ match_photo := re_photo_.FindStringSubmatch(line)
158 //~ if match_button != nil {
159 //~ // brn.Oboite("button0", ts)
160 //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
161 //~ } else if match_temp != nil {
162 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
164 //~ // brn.Oboite( "temp0", newtemp)
165 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
167 //~ } else if match_photo != nil {
168 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
170 //~ // brn.Oboite("photo0", newphoto)
171 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
173 //~ } else if line == "movement" {
174 //~ // brn.Oboite("movement", ts)
175 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")