1 // (c) Bernhard Tittelbach, 2013
11 pubsub "github.com/tuxychandru/pubsub"
12 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 PresenceUpdate struct {
33 func (s PresenceUpdate) Serialize() string
36 type DoorLockUpdate struct {
42 type DoorAjarUpdate struct {
48 type DoorCommandEvent struct {
55 type ButtonPressUpdate struct {
60 type TempSensorUpdate struct {
66 type IlluminationSensorUpdate struct {
72 type TimeTick struct {
76 type MovementSensorUpdate struct {
81 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
82 switch string(lines[0]) {
84 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
86 ps.Pub(DoorLockUpdate{0, false, ts}, "door")
87 case "manual": //movement
90 ps.Pub(DoorLockUpdate{0, true, ts}, "door")
91 case "timeout": //after open | after close
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]) {
105 if len(lines) < 2 { continue }
106 parseSocketInputLine_State(lines[1:], ps, ts)
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")
116 if len(lines) < 3 { continue }
117 if string(lines[2]) != "found" {
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])
125 Syslog_.Print("CardID Lookup Error",err)
126 nick := "Unresolvable KeyID"
128 // new event: toggle by user nick using card
129 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
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")
139 newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
141 // brn.Oboite("photo0", newphoto)
142 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
147 func MakeTimeTick(ps *pubsub.PubSub) {
148 ps.Pub(TimeTick{time.Now().Unix()},"time")
151 func FormatEventForSocket(event_interface interface{}) (data [][]byte, err error) {
152 msg, err := json.Marshal(data)
156 return [][]byte{msg}, nil
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)
171 //~ // brn.Oboite( "temp0", newtemp)
172 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
174 //~ } else if match_photo != nil {
175 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
177 //~ // brn.Oboite("photo0", newphoto)
178 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
180 //~ } else if line == "movement" {
181 //~ // brn.Oboite("movement", ts)
182 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")