From 837698930d5eea7c100a1233ee1d92e71896bb5d Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Thu, 3 Oct 2013 00:54:42 +0000 Subject: [PATCH] marshal events experiment --- go/r3-eventbroker_zmq/main.go | 2 +- go/r3-eventbroker_zmq/marshal_events.go | 43 +++++++++++++++++++++++++++++++ go/r3-eventbroker_zmq/sockettoevent.go | 14 ---------- 3 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 go/r3-eventbroker_zmq/marshal_events.go diff --git a/go/r3-eventbroker_zmq/main.go b/go/r3-eventbroker_zmq/main.go index af67170..0e8f3de 100644 --- a/go/r3-eventbroker_zmq/main.go +++ b/go/r3-eventbroker_zmq/main.go @@ -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 index 0000000..6999f58 --- /dev/null +++ b/go/r3-eventbroker_zmq/marshal_events.go @@ -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 +} diff --git a/go/r3-eventbroker_zmq/sockettoevent.go b/go/r3-eventbroker_zmq/sockettoevent.go index 94d8996..ea9b0e5 100644 --- a/go/r3-eventbroker_zmq/sockettoevent.go +++ b/go/r3-eventbroker_zmq/sockettoevent.go @@ -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) -- 1.7.10.4