X-Git-Url: https://git.realraum.at/?p=svn42.git;a=blobdiff_plain;f=go%2Fr3-netstatus%2Fsockettoevent.go;h=878217731b247f0de6d8436ee49074c4b20c3aa0;hp=ed3c9186aea97df760af8fd7005c17b30fff8ba9;hb=2416b163f05370fcffb2f62bea000265d10aac42;hpb=db6096af35c81fac74bacda38c734a76b7bdf9c1 diff --git a/go/r3-netstatus/sockettoevent.go b/go/r3-netstatus/sockettoevent.go index ed3c918..8782177 100644 --- a/go/r3-netstatus/sockettoevent.go +++ b/go/r3-netstatus/sockettoevent.go @@ -4,133 +4,53 @@ package main import ( pubsub "github.com/tuxychandru/pubsub" - "regexp" - "strconv" - "bufio" - "time" + //~ "bufio" + //~ "time" //~ "./brain" - "net" + //~ "net" + "encoding/json" + "log" + r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/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_infocard_ *regexp.Regexp = regexp.MustCompile("Info\(card\): card\(([a-fA-F0-9]+)\) (found|not found).*") - 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 PresenceUpdate struct { - Present bool - Ts int64 -} - -type DoorStatusUpdate struct { - Locked bool - 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 +func ParseZMQr3Event(lines [][]byte, ps *pubsub.PubSub) { //, brn *brain.Brain) { + //log.Printf("ParseZMQr3Event: len: %d lines: %s", len(lines), lines) + if len(lines) != 2 { + return + } + switch string(lines[0]) { + case "PresenceUpdate": + evnt := new(r3events.PresenceUpdate) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "presence")} + case "IlluminationSensorUpdate" : + evnt := new(r3events.IlluminationSensorUpdate) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "sensors")} + case "TempSensorUpdate" : + evnt := new(r3events.TempSensorUpdate) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "sensors")} + case "MovementSensorUpdate" : + evnt := new(r3events.MovementSensorUpdate) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "movement")} + case "ButtonPressUpdate" : + evnt := new(r3events.ButtonPressUpdate) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "buttons")} + case "DoorLockUpdate" : + log.Print("DoorLockUpdate received") + evnt := new(r3events.DoorLockUpdate) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "door")} + case "DoorAjarUpdate" : + evnt := new(r3events.DoorAjarUpdate) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "door")} + case "DoorCommandEvent" : + evnt := new(r3events.DoorCommandEvent) + err := json.Unmarshal(lines[1],evnt) + if err == nil {ps.Pub(*evnt, "door")} + } } - -type IlluminationSensorUpdate struct { - Sensorindex int - Value int64 - Ts int64 -} - -type TimeTick struct { - Ts int64 -} - -type MovementSensorUpdate struct { - Sensorindex int - Ts int64 -} - -func ParseSocketInputLine(line string, ps *pubsub.PubSub) { //, brn *brain.Brain) { - match_presence := re_presence_.FindStringSubmatch(line) - match_status := re_status_.FindStringSubmatch(line) - match_command := re_command_.FindStringSubmatch(line) - match_button := re_button_.FindStringSubmatch(line) - match_temp := re_temp_.FindStringSubmatch(line) - match_photo := re_photo_.FindStringSubmatch(line) - - //~ log.Println("ParseSocketInputLine",line) - var tidbit interface{} - ts := time.Now().Unix() - if match_presence != nil { - if match_presence[2] != "" { ps.Pub(DoorStatusUpdate{match_presence[2] == "closed", true, ts}, "door"); } - tidbit = PresenceUpdate{match_presence[1] == "yes", ts} - //~ brn.Oboite("presence", tidbit) - ps.Pub(tidbit, "presence") - } else if match_status != nil { - tidbit = DoorStatusUpdate{match_status[1] == "closed", match_status[3] == "shut", ts} - //~ brn.Oboite("door", tidbit) - ps.Pub(tidbit, "door") - } else if match_command != nil { - tidbit = DoorCommandEvent{match_command[1], match_command[2], match_command[3], ts} - //~ brn.Oboite("doorcmd", tidbit) - ps.Pub(tidbit, "door") - } else if match_button != nil { - //~ brn.Oboite("button0", ts) - ps.Pub(ButtonPressUpdate{0, ts}, "buttons") - } else if match_temp != nil { - newtemp, err := strconv.ParseFloat((match_temp[1]), 32) - if err == nil { - //~ brn.Oboite( "temp0", newtemp) - ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors") - } - } else if match_photo != nil { - newphoto, err := strconv.ParseInt(match_photo[1], 10, 32) - if err == nil { - //~ brn.Oboite("photo0", newphoto) - ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors") - } - } else if line == "movement" { - //~ brn.Oboite("movement", ts) - ps.Pub(MovementSensorUpdate{0, ts}, "movements") - } -} - -func ReadFromUSocket(path string, c chan string) { -ReOpenSocket: - for { - presence_socket, err := net.Dial("unix", path) - if err != nil { - //Waiting on Socket - time.Sleep(5 * time.Second) - continue ReOpenSocket - } - presence_reader := bufio.NewReader(presence_socket) - for { - line, err := presence_reader.ReadString('\n') - if err != nil { - //Socket closed - presence_socket.Close() - continue ReOpenSocket - } - c <- line - } - } -} \ No newline at end of file