//~ 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)}
--- /dev/null
+// (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
+}
"strconv"
"time"
//~ "./brain"
- "encoding/json"
pubsub "github.com/tuxychandru/pubsub"
zmq "github.com/vaughan0/go-zmq"
"log"
- "fmt"
)
var (
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)