1 // (c) Bernhard Tittelbach, 2013
10 pubsub "github.com/tuxychandru/pubsub"
11 zmq "github.com/vaughan0/go-zmq"
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+)")
29 type DoorLockUpdate struct {
35 type DoorAjarUpdate struct {
41 type DoorCommandEvent struct {
48 type ButtonPressUpdate struct {
53 type TempSensorUpdate struct {
59 type IlluminationSensorUpdate struct {
65 type TimeTick struct {
69 type MovementSensorUpdate struct {
74 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
75 switch string(lines[0]) {
77 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
79 ps.Pub(DoorLockUpdate{0, false, ts}, "door")
80 case "manual": //movement
83 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
84 case "timeout": //after open | after close
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]) {
99 if len(lines) < 2 { return }
100 parseSocketInputLine_State(lines[1:], ps, ts)
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")
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])
117 Syslog_.Print("CardID Lookup Error",err)
118 nick = "Unresolvable KeyID"
120 // new event: toggle by user nick using card
121 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
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")
131 newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
133 // brn.Oboite("photo0", newphoto)
134 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
139 func MakeTimeTick(ps *pubsub.PubSub) {
140 ps.Pub(TimeTick{time.Now().Unix()},"time")
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)
155 //~ // brn.Oboite( "temp0", newtemp)
156 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
158 //~ } else if match_photo != nil {
159 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
161 //~ // brn.Oboite("photo0", newphoto)
162 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
164 //~ } else if line == "movement" {
165 //~ // brn.Oboite("movement", ts)
166 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")