X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=go%2Fr3-eventbroker_zmq%2Fsockettoevent.go;h=9e4907ed70319abd19035c4c600cc69f36cac9b3;hb=36c81737d355f02eb05b150472c9bf44fbf46120;hp=ea9b0e5a42b1a0dcf988e22b4cbd32dc905a8c09;hpb=837698930d5eea7c100a1233ee1d92e71896bb5d;p=svn42.git diff --git a/go/r3-eventbroker_zmq/sockettoevent.go b/go/r3-eventbroker_zmq/sockettoevent.go index ea9b0e5..9e4907e 100644 --- a/go/r3-eventbroker_zmq/sockettoevent.go +++ b/go/r3-eventbroker_zmq/sockettoevent.go @@ -6,106 +6,67 @@ import ( "regexp" "strconv" "time" + "bytes" //~ "./brain" pubsub "github.com/tuxychandru/pubsub" zmq "github.com/vaughan0/go-zmq" - "log" + r3events "svn.spreadspace.org/realraum/go.svn/r3events" ) var ( - re_presence_ *regexp.Regexp = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?") - re_state_ *regexp.Regexp = regexp.MustCompile("State: (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing).*") - 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).*") - re_infocard_ *regexp.Regexp = regexp.MustCompile("Info\\(card\\): card\\(([a-fA-F0-9]+)\\) (found|not found).*") + //~ re_presence_ *regexp.Regexp = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?") + //~ re_state_ *regexp.Regexp = regexp.MustCompile("State: (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing).*") + //~ 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).*") + //~ re_infocard_ *regexp.Regexp = regexp.MustCompile("Info\\(card\\): card\\(([a-fA-F0-9]+)\\) (found|not found).*") re_cardid_ *regexp.Regexp = regexp.MustCompile("card\\(([a-fA-F0-9]+)\\)") - re_infoajar_ *regexp.Regexp = regexp.MustCompile("Info\\(ajar\\): door is now (ajar|shut)") - re_command_ *regexp.Regexp = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?") - re_button_ *regexp.Regexp = regexp.MustCompile("PanicButton|button\\d?") - re_temp_ *regexp.Regexp = regexp.MustCompile("temp0: (\\d+\\.\\d+)") - re_photo_ *regexp.Regexp = regexp.MustCompile("photo0: (\\d+)") + //~ re_infoajar_ *regexp.Regexp = regexp.MustCompile("Info\\(ajar\\): door is now (ajar|shut)") + //~ re_command_ *regexp.Regexp = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?") + //~ re_button_ *regexp.Regexp = regexp.MustCompile("PanicButton|button\\d?") + //~ re_temp_ *regexp.Regexp = regexp.MustCompile("temp0: (\\d+\\.\\d+)") + //~ re_photo_ *regexp.Regexp = regexp.MustCompile("photo0: (\\d+)") ) -type DoorLockUpdate struct { - DoorID int - Locked bool - Ts int64 -} - -type DoorAjarUpdate struct { - DoorID int - Shut bool - Ts int64 -} - -type DoorCommandEvent struct { - Command string - Using string - Who string - Ts int64 -} - -type ButtonPressUpdate struct { - Buttonindex int - Ts int64 -} - -type TempSensorUpdate struct { - Sensorindex int - Value float64 - Ts int64 -} - -type IlluminationSensorUpdate struct { - Sensorindex int - Value int64 - Ts int64 -} - -type TimeTick struct { - Ts int64 -} - -type MovementSensorUpdate struct { - Sensorindex int - Ts int64 -} - func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) { switch string(lines[0]) { case "closed": - ps.Pub(DoorLockUpdate{0, true, ts}, "door") + ps.Pub(r3events.DoorLockUpdate{true, ts}, "door") case "opened": - ps.Pub(DoorLockUpdate{0, false, ts}, "door") - case "manual": //movement + ps.Pub(r3events.DoorLockUpdate{false, ts}, "door") + case "manual", "manual_movement": //movement + ps.Pub(r3events.DoorManualMovementEvent{ts}, "door") case "error": + ps.Pub(r3events.DoorProblemEvent{100, string(bytes.Join(lines,[]byte(" "))), ts}, "door") case "reset": - ps.Pub(DoorLockUpdate{0, true, ts}, "door") - case "timeout": //after open | after close + ps.Pub(r3events.DoorLockUpdate{true, ts}, "door") + case "timeout_after_open": + ps.Pub(r3events.DoorProblemEvent{10, string(lines[0]), ts}, "door") + ps.Pub(r3events.DoorLockUpdate{false, ts}, "door") + case "timeout_after_close": + ps.Pub(r3events.DoorProblemEvent{20, string(lines[0]), ts}, "door") + // can't say for sure that door is locked if we ran into timeout while closing + //~ ps.Pub(r3events.DoorLockUpdate{true, ts}, "door") case "opening": case "closing": default: + Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines) } } func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) { - var tidbit interface{} ts := time.Now().Unix() if len(lines) < 1 { return } - log.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:]) + Debug_.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:]) switch string(lines[0]) { case "State:": if len(lines) < 2 { return } parseSocketInputLine_State(lines[1:], ps, ts) case "Status:": if len(lines) < 3 { return } - tidbit = DoorLockUpdate{0, string(lines[1]) == "closed", ts} - //~ brn.Oboite("door", tidbit) - ps.Pub(tidbit, "door") - tidbit = DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts} - //~ brn.Oboite("door", tidbit) - ps.Pub(tidbit, "door") + if len(lines[1]) < 4 { return } + ps.Pub(r3events.DoorLockUpdate{string(lines[1])[0:4] != "open", ts}, "door") + ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door") case "Info(card):": if len(lines) < 3 { return } if string(lines[2]) != "found" { return } @@ -118,26 +79,49 @@ func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *z nick = "Unresolvable KeyID" } // new event: toggle by user nick using card - ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd") + ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd") } case "Info(ajar):": if len(lines) < 5 { return } - tidbit = DoorAjarUpdate{0, string(lines[4]) == "shut", ts} - //~ brn.Oboite("door", tidbit) - ps.Pub(tidbit, "door") + ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door") case "open", "close", "toggle", "reset": - ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd") - case "photo0": + ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd") + case "BackdoorInfo(ajar):": + ps.Pub(r3events.BackdoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts},"door") + case "temp0:","temp1:", "temp2:", "temp3:": + sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32) + if err != nil {return } + newtemp, err := strconv.ParseFloat(string(lines[1]), 10) + if err != nil {return } + ps.Pub(r3events.TempSensorUpdate{int(sensorid), newtemp, ts}, "sensors") + case "photo0:","photo1:", "photo2:", "photo3:": + sensorid, err := strconv.ParseInt(string(lines[0][5]), 10, 32) + if err != nil {return } newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32) + if err != nil {return } + ps.Pub(r3events.IlluminationSensorUpdate{int(sensorid), newphoto, ts}, "sensors") + case "rh0:": + //~ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32) + //~ if err != nil {return } + relhumid, err := strconv.ParseInt(string(lines[1]), 10, 32) + if err != nil {return } + ps.Pub(r3events.RelativeHumiditySensorUpdate{0, int(relhumid), ts}, "sensors") + case "dust0:","dust1:","dust2:": + sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32) + if err != nil {return } + dustlvl, err := strconv.ParseInt(string(lines[1]), 10, 32) + if err != nil {return } + ps.Pub(r3events.DustSensorUpdate{int(sensorid), dustlvl, ts}, "sensors") + default: + evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines) if err == nil { - // brn.Oboite("photo0", newphoto) - ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors") + ps.Pub(evnt, pubsubcat) } } } func MakeTimeTick(ps *pubsub.PubSub) { - ps.Pub(TimeTick{time.Now().Unix()},"time") + ps.Pub(r3events.TimeTick{time.Now().Unix()},"time") } //~ match_presence := re_presence_.FindStringSubmatch(line) @@ -148,7 +132,7 @@ func MakeTimeTick(ps *pubsub.PubSub) { //~ match_photo := re_photo_.FindStringSubmatch(line) //~ if match_button != nil { //~ // brn.Oboite("button0", ts) - //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons") + //~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons") //~ } else if match_temp != nil { //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32) //~ if err == nil {