DoorProblemEvent and unmarshall and global pubsub categories
authorBernhard Tittelbach <xro@realraum.at>
Sat, 5 Oct 2013 00:35:19 +0000 (00:35 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Sat, 5 Oct 2013 00:35:19 +0000 (00:35 +0000)
go/r3-eventbroker_zmq/r3events/basicevents.go
go/r3-eventbroker_zmq/r3events/marshal_events.go
go/r3-eventbroker_zmq/sockettoevent.go
go/r3-netstatus/sockettoevent.go

index e1dacc6..73776cb 100644 (file)
@@ -25,6 +25,11 @@ type DoorCommandEvent struct {
     Ts int64
 }
 
     Ts int64
 }
 
+type DoorProblemEvent struct {
+    Severity int
+    Ts int64
+}
+
 type BoreDoomButtonPressEvent struct {
     Ts int64
 }
 type BoreDoomButtonPressEvent struct {
     Ts int64
 }
index 66799c7..830ff6b 100644 (file)
@@ -26,47 +26,63 @@ func MarshalEvent2ByteByte(event_interface interface{}) (data [][]byte, err erro
     return
 }
 
     return
 }
 
-func UnmarshalByteByte2Event(data [][]byte) (event interface{}, err error) {
+func UnmarshalByteByte2Event(data [][]byte) (event interface{}, category string, err error) {
     if len(data) != 2 {
     if len(data) != 2 {
-        return nil, errors.New("not a r3event message")
+        return nil, "", errors.New("not a r3event message")
     }
     switch string(data[0]) {
         case "DoorLockUpdate":
     }
     switch string(data[0]) {
         case "DoorLockUpdate":
-            event = new(DoorLockUpdate)
-            err = json.Unmarshal(data[1],event)
+            event = DoorLockUpdate{}
+            err = json.Unmarshal(data[1],&event)
+            category = "door"
         case "DoorAjarUpdate":
         case "DoorAjarUpdate":
-            event := new(DoorAjarUpdate)
-            err = json.Unmarshal(data[1], event)
+            event := DoorAjarUpdate{}
+            err = json.Unmarshal(data[1], &event)
+            category = "door"
         case "BackdoorAjarUpdate":
         case "BackdoorAjarUpdate":
-            event := new(DoorAjarUpdate)
-            err = json.Unmarshal(data[1], event)
+            event := DoorAjarUpdate{}
+            err = json.Unmarshal(data[1], &event)
+            category = "door"
         case "DoorCommandEvent":
         case "DoorCommandEvent":
-            event := new(DoorCommandEvent)
-            err = json.Unmarshal(data[1], event)
+            event := DoorCommandEvent{}
+            err = json.Unmarshal(data[1], &event)
+            category = "door"
+        case "DoorProblemEvent":
+            event := DoorProblemEvent{}
+            err = json.Unmarshal(data[1], &event)
+            category = "door"
         case "BoreDoomButtonPressEvent":
         case "BoreDoomButtonPressEvent":
-            event := new(BoreDoomButtonPressEvent)
-            err = json.Unmarshal(data[1], event)
+            event := BoreDoomButtonPressEvent{}
+            err = json.Unmarshal(data[1], &event)
+            category = "buttons"
         case "TempSensorUpdate":
         case "TempSensorUpdate":
-            event := new(TempSensorUpdate)
-            err = json.Unmarshal(data[1], event)
+            event := TempSensorUpdate{}
+            err = json.Unmarshal(data[1], &event)
+            category = "sensors"
         case "IlluminationSensorUpdate":
         case "IlluminationSensorUpdate":
-            event := new(IlluminationSensorUpdate)
-            err = json.Unmarshal(data[1], event)
+            event := IlluminationSensorUpdate{}
+            err = json.Unmarshal(data[1], &event)
+            category = "sensors"
         case "TimeTick":
         case "TimeTick":
-            event := new(TimeTick)
-            err = json.Unmarshal(data[1], event)
+            event := TimeTick{}
+            err = json.Unmarshal(data[1], &event)
+            category = "time"
         case "MovementSensorUpdate":
         case "MovementSensorUpdate":
-            event := new(MovementSensorUpdate)
-            err = json.Unmarshal(data[1], event)
+            event := MovementSensorUpdate{}
+            err = json.Unmarshal(data[1], &event)
+            category = "movement"
         case "PresenceUpdate":
         case "PresenceUpdate":
-            event := new(PresenceUpdate)
-            err = json.Unmarshal(data[1], event)
+            event := PresenceUpdate{}
+            err = json.Unmarshal(data[1], &event)
+            category = "presence"
         case "SomethingReallyIsMoving":
         case "SomethingReallyIsMoving":
-            event := new(SomethingReallyIsMoving)
-            err = json.Unmarshal(data[1], event)
+            event := SomethingReallyIsMoving{}
+            err = json.Unmarshal(data[1], &event)
+            category = "movement"
         default:
             event = nil
             err = errors.New("cannot unmarshal unknown type")
         default:
             event = nil
             err = errors.New("cannot unmarshal unknown type")
+            category = ""
     }
     return
 }
     }
     return
 }
index 2ebcc76..5287683 100644 (file)
@@ -34,9 +34,11 @@ func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
             ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
         case "manual":   //movement
         case "error":
             ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
         case "manual":   //movement
         case "error":
+            ps.Pub(r3events.DoorProblemEvent{100, ts}, "door")
         case "reset":
             ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
         case "timeout":   //after open | after close
         case "reset":
             ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
         case "timeout":   //after open | after close
+            ps.Pub(r3events.DoorProblemEvent{10, ts}, "door")
         case "opening":
         case "closing":
         default:
         case "opening":
         case "closing":
         default:
@@ -81,18 +83,11 @@ func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *z
             //~ if err == nil {
                 //~ ps.Pub(r3events.IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
             //~ }
             //~ if err == nil {
                 //~ ps.Pub(r3events.IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
             //~ }
-        case "IlluminationSensorUpdate","TempSensorUpdate":
-            //try decode r3event
-            evnt, err := r3events.UnmarshalByteByte2Event(lines)
-            if err == nil {ps.Pub(evnt, "sensors")}
-        case "MovementSensorUpdate" :
-            //try decode r3event
-            evnt, err := r3events.UnmarshalByteByte2Event(lines)
-            if err == nil {ps.Pub(evnt, "movement")}
-        case "BoreDoomButtonPressEvent" :
-            //try decode r3event
-            evnt, err := r3events.UnmarshalByteByte2Event(lines)
-            if err == nil {ps.Pub(evnt, "buttons")}
+        default:
+            evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
+            if err == nil {
+                ps.Pub(evnt, pubsubcat)
+            }
     }
 }
 
     }
 }
 
index 463838c..7165ac6 100644 (file)
@@ -4,51 +4,11 @@ package main
 
 import (
     pubsub "github.com/tuxychandru/pubsub"
 
 import (
     pubsub "github.com/tuxychandru/pubsub"
-    //~ "bufio"
-    //~ "time"
-    //~ "./brain"
-    //~ "net"
-    "encoding/json"
     r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events"
     )
 
     r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events"
     )
 
-func ParseZMQr3Event(lines [][]byte, ps *pubsub.PubSub) { //, brn *brain.Brain) {
-    //Debug_.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 "BoreDoomButtonPressEvent" :
-            evnt := new(r3events.BoreDoomButtonPressEvent)
-            err := json.Unmarshal(lines[1],evnt)
-            if err == nil {ps.Pub(*evnt, "buttons")}
-        case "DoorLockUpdate" :
-            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")}
-    }
+func ParseZMQr3Event(lines [][]byte, ps *pubsub.PubSub) {
+    evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
+    if err != nil { return }
+    ps.Pub(evnt, pubsubcat)
 }
 }