r3events package
authorBernhard Tittelbach <xro@realraum.at>
Thu, 3 Oct 2013 07:37:35 +0000 (07:37 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Thu, 3 Oct 2013 07:37:35 +0000 (07:37 +0000)
go/r3-eventbroker_zmq/main.go
go/r3-eventbroker_zmq/marshal_events.go [deleted file]
go/r3-eventbroker_zmq/metamovement.go
go/r3-eventbroker_zmq/presence.go
go/r3-eventbroker_zmq/r3events/basicevents.go [new file with mode: 0644]
go/r3-eventbroker_zmq/r3events/marshal_events.go [new file with mode: 0644]
go/r3-eventbroker_zmq/r3events/metaevents.go [new file with mode: 0644]
go/r3-eventbroker_zmq/sockettoevent.go

index 0e8f3de..aaaf8bd 100644 (file)
@@ -10,6 +10,7 @@ import (
     "log/syslog"
     "log"
     pubsub "github.com/tuxychandru/pubsub"
+    "./r3events"
 )
 
 //~ func StringArrayToByteArray(ss []string) [][]byte {
@@ -78,7 +79,7 @@ func main() {
             //~ case <- ticker.C:
                 //~ MakeTimeTick(ps)
             case event_interface := <- publish_these_events_chan:
-                data, err := MarshalEvent(event_interface)
+                data, err := r3events.MarshalEvent2ByteByte(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
deleted file mode 100644 (file)
index 6999f58..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// (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 71aa209..d908758 100644 (file)
@@ -7,13 +7,9 @@ import (
     //~ "./brain"
     pubsub "github.com/tuxychandru/pubsub"
     "container/ring"
+    "./r3events"
     )
 
-type SomethingReallyIsMoving struct {
-    Movement bool
-    Ts int64
-}
-
 
 func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int , threshold uint32) {
     var last_movement int64
@@ -24,7 +20,7 @@ func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int
     for { select {
         case event := <- events_chan:
             switch event.(type) {
-                case MovementSensorUpdate:
+                case r3events.MovementSensorUpdate:
                     movement_window.Value =  (uint32) (movement_window.Value.(uint32)  + 1)
             }
         case <- myticker.C:
@@ -34,13 +30,13 @@ func MetaEventRoutine_Movement(ps *pubsub.PubSub, granularity, gran_duration int
             movement_window.Do(func(v interface{}){if v != nil {movsum += v.(uint32)}})
             ts :=  time.Now().Unix()
             if movsum > threshold {
-                ps.Pub( SomethingReallyIsMoving{true,ts}, "movement")
+                ps.Pub( r3events.SomethingReallyIsMoving{true,ts}, "movement")
                 last_movement = ts
             }
 
             if last_movement > 0 && ts - last_movement < 3600*6 && ts - last_movement > 3600*3 {
                 last_movement = 0
-                ps.Pub( SomethingReallyIsMoving{false, ts}, "movement")
+                ps.Pub( r3events.SomethingReallyIsMoving{false, ts}, "movement")
             }
     } }
 }
\ No newline at end of file
index 154c6d2..aac99f5 100644 (file)
@@ -6,13 +6,9 @@ import (
     "time"
     //~ "./brain"
     pubsub "github.com/tuxychandru/pubsub"
+    "./r3events"
     )
 
-type PresenceUpdate struct {
-    Present bool
-    Ts int64
-}
-
 type doorstate struct {
     locked bool
     shut bool
@@ -30,20 +26,20 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) {
         new_presence := last_presence
         ts := time.Now().Unix()
         switch evnt := event.(type) {
-            case SomethingReallyIsMoving:
+            case r3events.SomethingReallyIsMoving:
                 if evnt.Movement {
                     last_movement = evnt.Ts
                 } else {
                     last_movement = 0
                 }
-            case ButtonPressUpdate:
+            case r3events.ButtonPressUpdate:
                 last_buttonpress = evnt.Ts
                 new_presence = true
             //~ case DoorCommandEvent:
                 //~ last_door_cmd = &evnt
-            case DoorLockUpdate:
+            case r3events.DoorLockUpdate:
                 doorstatemap[evnt.DoorID]=doorstate{locked:evnt.Locked, shut:doorstatemap[evnt.DoorID].shut}
-            case DoorAjarUpdate:
+            case r3events.DoorAjarUpdate:
                 doorstatemap[evnt.DoorID]=doorstate{locked:doorstatemap[evnt.DoorID].locked, shut:evnt.Shut}
         }
 
@@ -66,7 +62,7 @@ func MetaEventRoutine_Presence(ps *pubsub.PubSub) {
 
         if new_presence != last_presence {
             last_presence = new_presence
-            ps.Pub(PresenceUpdate{new_presence, ts} , "presence")
+            ps.Pub(r3events.PresenceUpdate{new_presence, ts} , "presence")
         }
     }
 }
\ No newline at end of file
diff --git a/go/r3-eventbroker_zmq/r3events/basicevents.go b/go/r3-eventbroker_zmq/r3events/basicevents.go
new file mode 100644 (file)
index 0000000..e5fa2dc
--- /dev/null
@@ -0,0 +1,49 @@
+// (c) Bernhard Tittelbach, 2013
+
+package r3events
+
+
+type DoorLockUpdate struct {
+    DoorID int
+    Locked bool
+    Ts int64
+}
+
+type DoorAjarUpdate struct {
+    DoorID int
+    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
+}
+
+type IlluminationSensorUpdate struct {
+    Sensorindex int
+    Value int64
+    Ts int64
+}
+
+type TimeTick struct {
+    Ts int64
+}
+
+type MovementSensorUpdate struct {
+    Sensorindex int
+    Ts int64
+}
\ No newline at end of file
diff --git a/go/r3-eventbroker_zmq/r3events/marshal_events.go b/go/r3-eventbroker_zmq/r3events/marshal_events.go
new file mode 100644 (file)
index 0000000..d00d462
--- /dev/null
@@ -0,0 +1,66 @@
+// (c) Bernhard Tittelbach, 2013
+
+package r3events
+
+import (
+    "encoding/json"
+    "fmt"
+    "errors"
+    "strings"
+    )
+
+
+func MarshalEvent2ByteByte(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 := fmt.Sprintf("%T", event_interface)
+    etype_lastsep := strings.LastIndex(etype,".")
+    data = [][]byte{[]byte(etype[etype_lastsep+1:]), msg} //works in all cases for etype_lastsep in range -1 to len(etype)-1
+    return
+}
+
+func UnmarshalByteByte2Event(data [][]byte) (event interface{}, err error) {
+    if len(data) != 2 {
+        return nil, errors.New("not a r3event message")
+    }
+    switch string(data[0]) {
+        case "DoorLockUpdate":
+            event = new(DoorLockUpdate)
+            err = json.Unmarshal(data[1],event)
+        case "DoorAjarUpdate":
+            event := new(DoorAjarUpdate)
+            err = json.Unmarshal(data[1], event)
+        case "DoorCommandEvent":
+            event := new(DoorCommandEvent)
+            err = json.Unmarshal(data[1], event)
+        case "ButtonPressUpdate":
+            event := new(ButtonPressUpdate)
+            err = json.Unmarshal(data[1], event)
+        case "TempSensorUpdate":
+            event := new(TempSensorUpdate)
+            err = json.Unmarshal(data[1], event)
+        case "IlluminationSensorUpdate":
+            event := new(IlluminationSensorUpdate)
+            err = json.Unmarshal(data[1], event)
+        case "TimeTick":
+            event := new(TimeTick)
+            err = json.Unmarshal(data[1], event)
+        case "MovementSensorUpdate":
+            event := new(MovementSensorUpdate)
+            err = json.Unmarshal(data[1], event)
+        case "PresenceUpdate":
+            event := new(PresenceUpdate)
+            err = json.Unmarshal(data[1], event)
+        case "SomethingReallyIsMoving":
+            event := new(SomethingReallyIsMoving)
+            err = json.Unmarshal(data[1], event)
+        default:
+            event = nil
+            err = errors.New("cannot unmarshal unknown type")
+    }
+    return
+}
diff --git a/go/r3-eventbroker_zmq/r3events/metaevents.go b/go/r3-eventbroker_zmq/r3events/metaevents.go
new file mode 100644 (file)
index 0000000..c22a79d
--- /dev/null
@@ -0,0 +1,13 @@
+// (c) Bernhard Tittelbach, 2013
+
+package r3events
+
+type PresenceUpdate struct {
+    Present bool
+    Ts int64
+}
+
+type SomethingReallyIsMoving struct {
+    Movement bool
+    Ts int64
+}
\ No newline at end of file
index ea9b0e5..8e202da 100644 (file)
@@ -10,6 +10,7 @@ import (
     pubsub "github.com/tuxychandru/pubsub"
     zmq "github.com/vaughan0/go-zmq"
     "log"
+    "./r3events"
     )
 
 var (
@@ -26,61 +27,16 @@ var (
 )
 
 
-type DoorLockUpdate struct {
-    DoorID int
-    Locked bool
-    Ts int64
-}
-
-type DoorAjarUpdate struct {
-    DoorID int
-    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
-}
-
-type IlluminationSensorUpdate struct {
-    Sensorindex int
-    Value int64
-    Ts int64
-}
-
-type TimeTick struct {
-    Ts int64
-}
-
-type MovementSensorUpdate struct {
-    Sensorindex int
-    Ts int64
-}
-
 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
     switch string(lines[0]) {
         case "closed":
-            ps.Pub(DoorLockUpdate{0, true, ts}, "door")
+            ps.Pub(r3events.DoorLockUpdate{0, true, ts}, "door")
         case "opened":
-            ps.Pub(DoorLockUpdate{0, false, ts}, "door")
+            ps.Pub(r3events.DoorLockUpdate{0, false, ts}, "door")
         case "manual":   //movement
         case "error":
         case "reset":
-            ps.Pub(DoorLockUpdate{0, true, ts}, "door")
+            ps.Pub(r3events.DoorLockUpdate{0, true, ts}, "door")
         case "timeout":   //after open | after close
         case "opening":
         case "closing":
@@ -100,10 +56,10 @@ func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *z
             parseSocketInputLine_State(lines[1:], ps, ts)
         case "Status:":
             if len(lines) < 3 { return }
-            tidbit = DoorLockUpdate{0, string(lines[1]) == "closed", ts}
+            tidbit = r3events.DoorLockUpdate{0, string(lines[1]) == "closed", ts}
             //~ brn.Oboite("door", tidbit)
             ps.Pub(tidbit, "door")
-            tidbit = DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
+            tidbit = r3events.DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
             //~ brn.Oboite("door", tidbit)
             ps.Pub(tidbit, "door")
         case "Info(card):":
@@ -118,26 +74,26 @@ func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *z
                     nick = "Unresolvable KeyID"
                 }
                 // new event: toggle by user nick using card
-                ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
+                ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
             }
         case "Info(ajar):":
             if len(lines) < 5 { return }
-            tidbit = DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
+            tidbit = r3events.DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
             //~ brn.Oboite("door", tidbit)
             ps.Pub(tidbit, "door")
         case "open", "close", "toggle", "reset":
-            ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
+            ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
         case "photo0":
             newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
             if err == nil {
                 // brn.Oboite("photo0", newphoto)
-                ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
+                ps.Pub(r3events.IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
             }
     }
 }
 
 func MakeTimeTick(ps *pubsub.PubSub) {
-    ps.Pub(TimeTick{time.Now().Unix()},"time")
+    ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
 }
 
     //~ match_presence := re_presence_.FindStringSubmatch(line)