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 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
30 switch string(lines[0]) {
32 ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
34 ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
35 case "manual": //movement
38 ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
39 case "timeout": //after open | after close
43 Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)
48 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
49 ts := time.Now().Unix()
50 if len(lines) < 1 { return }
51 Debug_.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
52 switch string(lines[0]) {
54 if len(lines) < 2 { return }
55 parseSocketInputLine_State(lines[1:], ps, ts)
57 if len(lines) < 3 { return }
58 ps.Pub(r3events.DoorLockUpdate{string(lines[1]) == "closed,", ts}, "door")
59 ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
61 if len(lines) < 3 { return }
62 if string(lines[2]) != "found" { return }
63 match_cardid := re_cardid_.FindSubmatch(lines[1])
64 if len(match_cardid) > 1 {
65 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
66 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
68 Syslog_.Print("CardID Lookup Error",err)
69 nick = "Unresolvable KeyID"
71 // new event: toggle by user nick using card
72 ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
75 if len(lines) < 5 { return }
76 ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door")
77 case "open", "close", "toggle", "reset":
78 ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
80 newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
82 // brn.Oboite("photo0", newphoto)
83 ps.Pub(r3events.IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
85 case "IlluminationSensorUpdate","TempSensorUpdate":
87 evnt, err := r3events.UnmarshalByteByte2Event(lines)
88 if err == nil {ps.Pub(evnt, "sensors")}
89 case "MovementSensorUpdate" :
91 evnt, err := r3events.UnmarshalByteByte2Event(lines)
92 if err == nil {ps.Pub(evnt, "movement")}
93 case "BoreDoomButtonPressEvent" :
95 evnt, err := r3events.UnmarshalByteByte2Event(lines)
96 if err == nil {ps.Pub(evnt, "buttons")}
100 func MakeTimeTick(ps *pubsub.PubSub) {
101 ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
104 //~ match_presence := re_presence_.FindStringSubmatch(line)
105 //~ match_status := re_status_.FindStringSubmatch(line)
106 //~ match_command := re_command_.FindStringSubmatch(line)
107 //~ match_button := re_button_.FindStringSubmatch(line)
108 //~ match_temp := re_temp_.FindStringSubmatch(line)
109 //~ match_photo := re_photo_.FindStringSubmatch(line)
110 //~ if match_button != nil {
111 //~ // brn.Oboite("button0", ts)
112 //~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons")
113 //~ } else if match_temp != nil {
114 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
116 //~ // brn.Oboite( "temp0", newtemp)
117 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
119 //~ } else if match_photo != nil {
120 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
122 //~ // brn.Oboite("photo0", newphoto)
123 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
125 //~ } else if line == "movement" {
126 //~ // brn.Oboite("movement", ts)
127 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")