"log/syslog"
"log"
pubsub "github.com/tuxychandru/pubsub"
+ "./r3events"
)
//~ func StringArrayToByteArray(ss []string) [][]byte {
//~ 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)}
+++ /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
-}
//~ "./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
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:
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
"time"
//~ "./brain"
pubsub "github.com/tuxychandru/pubsub"
+ "./r3events"
)
-type PresenceUpdate struct {
- Present bool
- Ts int64
-}
-
type doorstate struct {
locked bool
shut bool
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}
}
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
--- /dev/null
+// (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
--- /dev/null
+// (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
+}
--- /dev/null
+// (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
pubsub "github.com/tuxychandru/pubsub"
zmq "github.com/vaughan0/go-zmq"
"log"
+ "./r3events"
)
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":
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):":
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)