1 // (c) Bernhard Tittelbach, 2013
10 pubsub "github.com/tuxychandru/pubsub"
11 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 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
31 switch string(lines[0]) {
33 ps.Pub(r3events.DoorLockUpdate{0, true, ts}, "door")
35 ps.Pub(r3events.DoorLockUpdate{0, false, ts}, "door")
36 case "manual": //movement
39 ps.Pub(r3events.DoorLockUpdate{0, true, ts}, "door")
40 case "timeout": //after open | after close
48 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
49 var tidbit interface{}
50 ts := time.Now().Unix()
51 if len(lines) < 1 { return }
52 log.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
53 switch string(lines[0]) {
55 if len(lines) < 2 { return }
56 parseSocketInputLine_State(lines[1:], ps, ts)
58 if len(lines) < 3 { return }
59 tidbit = r3events.DoorLockUpdate{0, string(lines[1]) == "closed", ts}
60 //~ brn.Oboite("door", tidbit)
61 ps.Pub(tidbit, "door")
62 tidbit = r3events.DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
63 //~ brn.Oboite("door", tidbit)
64 ps.Pub(tidbit, "door")
66 if len(lines) < 3 { return }
67 if string(lines[2]) != "found" { return }
68 match_cardid := re_cardid_.FindSubmatch(lines[1])
69 if len(match_cardid) > 1 {
70 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
71 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
73 Syslog_.Print("CardID Lookup Error",err)
74 nick = "Unresolvable KeyID"
76 // new event: toggle by user nick using card
77 ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
80 if len(lines) < 5 { return }
81 tidbit = r3events.DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
82 //~ brn.Oboite("door", tidbit)
83 ps.Pub(tidbit, "door")
84 case "open", "close", "toggle", "reset":
85 ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
87 newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
89 // brn.Oboite("photo0", newphoto)
90 ps.Pub(r3events.IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
92 case "IlluminationSensorUpdate","TempSensorUpdate":
94 evnt, err := r3events.UnmarshalByteByte2Event(lines)
95 if err == nil {ps.Pub(evnt, "sensors")}
96 case "MovementSensorUpdate" :
98 evnt, err := r3events.UnmarshalByteByte2Event(lines)
99 if err == nil {ps.Pub(evnt, "movement")}
100 case "ButtonPressUpdate" :
102 evnt, err := r3events.UnmarshalByteByte2Event(lines)
103 if err == nil {ps.Pub(evnt, "buttons")}
107 func MakeTimeTick(ps *pubsub.PubSub) {
108 ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
111 //~ match_presence := re_presence_.FindStringSubmatch(line)
112 //~ match_status := re_status_.FindStringSubmatch(line)
113 //~ match_command := re_command_.FindStringSubmatch(line)
114 //~ match_button := re_button_.FindStringSubmatch(line)
115 //~ match_temp := re_temp_.FindStringSubmatch(line)
116 //~ match_photo := re_photo_.FindStringSubmatch(line)
117 //~ if match_button != nil {
118 //~ // brn.Oboite("button0", ts)
119 //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
120 //~ } else if match_temp != nil {
121 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
123 //~ // brn.Oboite( "temp0", newtemp)
124 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
126 //~ } else if match_photo != nil {
127 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
129 //~ // brn.Oboite("photo0", newphoto)
130 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
132 //~ } else if line == "movement" {
133 //~ // brn.Oboite("movement", ts)
134 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")