1 // (c) Bernhard Tittelbach, 2013
6 pubsub "github.com/tuxychandru/pubsub"
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 PresenceUpdate struct {
34 type DoorLockUpdate struct {
40 type DoorAjarUpdate struct {
46 type DoorCommandEvent struct {
53 type ButtonPressUpdate struct {
58 type TempSensorUpdate struct {
64 type IlluminationSensorUpdate struct {
70 type TimeTick struct {
74 type MovementSensorUpdate struct {
79 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts uint64) {
80 switch string(lines[0]) {
82 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
84 ps.Pub(DoorLockUpdate{0, false, ts}, "door")
85 case "manual": //movement
88 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
89 case "timeout": //after open | after close
97 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub) { //, brn *brain.Brain) {
98 var tidbit interface{}
99 ts := time.Now().Unix()
100 if len(lines) < 1 { return }
101 switch string(lines[0]) {
103 if len(lines) < 2 { continue }
104 parseSocketInputLine_State(lines[1:], ps, ts)
106 if len(lines) < 3 { continue }
107 tidbit = DoorLockUpdate{0, lines[1] == []byte("closed"), ts}
108 //~ brn.Oboite("door", tidbit)
109 ps.Pub(tidbit, "door")
110 tidbit = DoorAjarUpdate{0, lines[-1] == []byte("shut"), ts}
111 //~ brn.Oboite("door", tidbit)
112 ps.Pub(tidbit, "door")
114 if len(lines) < 3 { continue }
115 if lines[2] != []byte("found") {
118 match_cardid := re_cardid_.FindSubmatch(lines[1])
119 if len(match_cardid) > 1 {
120 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
121 nick, err := keylookup_socket.LookupCardIdNick(match_cardid[1])
123 Syslog_.Print("CardID Lookup Error",err)
124 nick := "Unresolvable KeyID"
126 // new event: toggle by user nick using card
127 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
130 if len(lines) < 5 { continue }
131 DoorAjarUpdate{0, match_status[4] == []byte("shut"), ts}
132 //~ brn.Oboite("door", tidbit)
133 ps.Pub(tidbit, "door")
134 case "open", "close", "toggle", "reset":
135 ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
139 //~ match_presence := re_presence_.FindStringSubmatch(line)
140 //~ match_status := re_status_.FindStringSubmatch(line)
141 //~ match_command := re_command_.FindStringSubmatch(line)
142 //~ match_button := re_button_.FindStringSubmatch(line)
143 //~ match_temp := re_temp_.FindStringSubmatch(line)
144 //~ match_photo := re_photo_.FindStringSubmatch(line)
145 //~ if match_button != nil {
146 //~ // brn.Oboite("button0", ts)
147 //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
148 //~ } else if match_temp != nil {
149 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
151 //~ // brn.Oboite( "temp0", newtemp)
152 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
154 //~ } else if match_photo != nil {
155 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
157 //~ // brn.Oboite("photo0", newphoto)
158 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
160 //~ } else if line == "movement" {
161 //~ // brn.Oboite("movement", ts)
162 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")