marshal events experiment
authorBernhard Tittelbach <xro@realraum.at>
Thu, 3 Oct 2013 00:54:42 +0000 (00:54 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Thu, 3 Oct 2013 00:54:42 +0000 (00:54 +0000)
go/r3-eventbroker_zmq/main.go
go/r3-eventbroker_zmq/marshal_events.go [new file with mode: 0644]
go/r3-eventbroker_zmq/sockettoevent.go

index af67170..0e8f3de 100644 (file)
@@ -78,7 +78,7 @@ func main() {
             //~ case <- ticker.C:
                 //~ MakeTimeTick(ps)
             case event_interface := <- publish_these_events_chan:
-                data, err := FormatEventForSocket(event_interface)
+                data, err := MarshalEvent(event_interface)
                 log.Printf("publishing %s",data)
                 if err != nil {
                     if Syslog_ != nil {Syslog_.Print(err)}
diff --git a/go/r3-eventbroker_zmq/marshal_events.go b/go/r3-eventbroker_zmq/marshal_events.go
new file mode 100644 (file)
index 0000000..6999f58
--- /dev/null
@@ -0,0 +1,43 @@
+// (c) Bernhard Tittelbach, 2013
+
+package main
+
+import (
+    "encoding/json"
+    "fmt"
+    "errors"
+    )
+
+
+func MarshalEvent(event_interface interface{}) (data [][]byte, err error) {
+    var msg []byte
+    fmt.Printf("%T%+v\n", event_interface, event_interface)
+       msg, err = json.Marshal(event_interface)
+       if err != nil {
+               return
+       }
+    etype := []byte(fmt.Sprintf("%T", event_interface)[5:])
+    data = [][]byte{etype, msg}
+    return
+}
+
+func UnmarshalEvent(data [][]byte) (event interface{}, err error) {
+    switch string(data[0]) {
+        case "DoorLockUpdate":
+            typedevent := new(DoorLockUpdate)
+            err = json.Unmarshal(data[1], typedevent)
+            event = typedevent
+        case "DoorAjarUpdate":
+            typedevent := new(DoorAjarUpdate)
+            err = json.Unmarshal(data[1], typedevent)
+            event = typedevent
+        case "DoorCommandEvent":
+            typedevent := new(DoorCommandEvent)
+            err = json.Unmarshal(data[1], typedevent)
+            event = typedevent
+        default:
+            event = nil
+            err = errors.New("unknown type")
+    }
+    return
+}
index 94d8996..ea9b0e5 100644 (file)
@@ -7,11 +7,9 @@ import (
     "strconv"
     "time"
     //~ "./brain"
-    "encoding/json"
     pubsub "github.com/tuxychandru/pubsub"
     zmq "github.com/vaughan0/go-zmq"
     "log"
-    "fmt"
     )
 
 var (
@@ -142,18 +140,6 @@ func MakeTimeTick(ps *pubsub.PubSub) {
     ps.Pub(TimeTick{time.Now().Unix()},"time")
 }
 
-func FormatEventForSocket(event_interface interface{}) (data [][]byte, err error) {
-    var msg []byte
-    fmt.Printf("%T%+v\n", event_interface, event_interface)
-       msg, err = json.Marshal(event_interface)
-       if err != nil {
-               return
-       }
-    etype := []byte(fmt.Sprintf("%T", event_interface)[5:])
-    data = [][]byte{etype, msg}
-    return
-}
-
     //~ match_presence := re_presence_.FindStringSubmatch(line)
     //~ match_status := re_status_.FindStringSubmatch(line)
     //~ match_command := re_command_.FindStringSubmatch(line)