1 // (c) Bernhard Tittelbach, 2013
10 pubsub "github.com/tuxychandru/pubsub"
11 zmq "github.com/vaughan0/go-zmq"
12 r3events "svn.spreadspace.org/realraum/go.svn/r3events"
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", "manual_movement": //movement
37 ps.Pub(r3events.DoorProblemEvent{100, ts}, "door")
39 ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
40 case "timeout", "timeout_after_open", "timeout_after_close":
41 ps.Pub(r3events.DoorProblemEvent{10, ts}, "door")
45 Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)
50 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
51 ts := time.Now().Unix()
52 if len(lines) < 1 { return }
53 Debug_.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
54 switch string(lines[0]) {
56 if len(lines) < 2 { return }
57 parseSocketInputLine_State(lines[1:], ps, ts)
59 if len(lines) < 3 { return }
60 if len(lines[1]) < 4 { return }
61 ps.Pub(r3events.DoorLockUpdate{string(lines[1])[0:4] != "open", ts}, "door")
62 ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
64 if len(lines) < 3 { return }
65 if string(lines[2]) != "found" { return }
66 match_cardid := re_cardid_.FindSubmatch(lines[1])
67 if len(match_cardid) > 1 {
68 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
69 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
71 Syslog_.Print("CardID Lookup Error",err)
72 nick = "Unresolvable KeyID"
74 // new event: toggle by user nick using card
75 ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
78 if len(lines) < 5 { return }
79 ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door")
80 case "open", "close", "toggle", "reset":
81 ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
82 case "BackdoorInfo(ajar):":
83 ps.Pub(r3events.BackdoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts},"door")
85 //~ newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
87 //~ ps.Pub(r3events.IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
90 evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
92 ps.Pub(evnt, pubsubcat)
97 func MakeTimeTick(ps *pubsub.PubSub) {
98 ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
101 //~ match_presence := re_presence_.FindStringSubmatch(line)
102 //~ match_status := re_status_.FindStringSubmatch(line)
103 //~ match_command := re_command_.FindStringSubmatch(line)
104 //~ match_button := re_button_.FindStringSubmatch(line)
105 //~ match_temp := re_temp_.FindStringSubmatch(line)
106 //~ match_photo := re_photo_.FindStringSubmatch(line)
107 //~ if match_button != nil {
108 //~ // brn.Oboite("button0", ts)
109 //~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons")
110 //~ } else if match_temp != nil {
111 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
113 //~ // brn.Oboite( "temp0", newtemp)
114 //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
116 //~ } else if match_photo != nil {
117 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
119 //~ // brn.Oboite("photo0", newphoto)
120 //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
122 //~ } else if line == "movement" {
123 //~ // brn.Oboite("movement", ts)
124 //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")