fix presence bug
[svn42.git] / go / r3events / marshal_events.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package r3events
4
5 import (
6     "encoding/json"
7     "fmt"
8     "errors"
9     "strings"
10     )
11
12 func NameOfStruct(evi interface{}) (name string) {
13     etype := fmt.Sprintf("%T", evi)
14     etype_lastsep := strings.LastIndex(etype,".")
15     return etype[etype_lastsep+1:] //works in all cases for etype_lastsep in range -1 to len(etype)-1
16 }
17
18 func MarshalEvent2ByteByte(event_interface interface{}) (data [][]byte, err error) {
19     var msg []byte
20     //~ fmt.Printf("%T%+v\n", event_interface, event_interface)
21         msg, err = json.Marshal(event_interface)
22         if err != nil {
23                 return
24         }
25     data = [][]byte{[]byte(NameOfStruct(event_interface)), msg} //works in all cases for etype_lastsep in range -1 to len(etype)-1
26     return
27 }
28
29 func UnmarshalByteByte2Event(data [][]byte) (event interface{}, category string, err error) {
30     if len(data) != 2 {
31         return nil, "", errors.New("not a r3event message")
32     }
33     switch string(data[0]) {
34         case "DoorLockUpdate":
35             newevent := new(DoorLockUpdate)
36             err = json.Unmarshal(data[1],newevent)
37             category = "door"
38             event = *newevent
39         case "DoorAjarUpdate":
40             newevent := new(DoorAjarUpdate)
41             err = json.Unmarshal(data[1], newevent)
42             category = "door"
43             event = *newevent
44         case "BackdoorAjarUpdate":
45             newevent := new(BackdoorAjarUpdate)
46             err = json.Unmarshal(data[1], newevent)
47             category = "door"
48             event = *newevent
49         case "DoorCommandEvent":
50             newevent := new(DoorCommandEvent)
51             err = json.Unmarshal(data[1], newevent)
52             category = "door"
53             event = *newevent
54         case "DoorProblemEvent":
55             newevent := new(DoorProblemEvent)
56             err = json.Unmarshal(data[1], newevent)
57             category = "door"
58             event = *newevent
59         case "DoorManualMovementEvent":
60             newevent := new(DoorManualMovementEvent)
61             err = json.Unmarshal(data[1], newevent)
62             category = "door"
63             event = *newevent
64         case "BoreDoomButtonPressEvent":
65             newevent := new(BoreDoomButtonPressEvent)
66             err = json.Unmarshal(data[1], newevent)
67             category = "buttons"
68             event = *newevent
69         case "TempSensorUpdate":
70             newevent := new(TempSensorUpdate)
71             err = json.Unmarshal(data[1], newevent)
72             category = "sensors"
73             event = *newevent
74         case "IlluminationSensorUpdate":
75             newevent := new(IlluminationSensorUpdate)
76             err = json.Unmarshal(data[1], newevent)
77             category = "sensors"
78             event = *newevent
79         case "DustSensorUpdate":
80             newevent := new(DustSensorUpdate)
81             err = json.Unmarshal(data[1], newevent)
82             category = "sensors"
83             event = *newevent
84         case "RelativeHumiditySensorUpdate":
85             newevent := new(RelativeHumiditySensorUpdate)
86             err = json.Unmarshal(data[1], newevent)
87             category = "sensors"
88             event = *newevent
89         case "TimeTick":
90             newevent := new(TimeTick)
91             err = json.Unmarshal(data[1], newevent)
92             category = "time"
93             event = *newevent
94         case "GasLeakAlert":
95             newevent := new(GasLeakAlert)
96             err = json.Unmarshal(data[1], newevent)
97             category = "sensors"
98             event = *newevent
99         case "MovementSensorUpdate":
100             newevent := new(MovementSensorUpdate)
101             err = json.Unmarshal(data[1], newevent)
102             category = "movement"
103             event = *newevent
104         case "PresenceUpdate":
105             newevent := new(PresenceUpdate)
106             err = json.Unmarshal(data[1], newevent)
107             category = "presence"
108             event = *newevent
109         case "SomethingReallyIsMoving":
110             newevent := new(SomethingReallyIsMoving)
111             err = json.Unmarshal(data[1], newevent)
112             category = "movement"
113             event = *newevent
114         case "TempSensorSpike":
115             newevent := new(TempSensorSpike)
116             err = json.Unmarshal(data[1], newevent)
117             category = "sensors"
118             event = *newevent
119         case "DustSensorSpike":
120             newevent := new(DustSensorSpike)
121             err = json.Unmarshal(data[1], newevent)
122             category = "sensors"
123             event = *newevent
124         case "NetDHCPACK":
125             newevent := new(NetDHCPACK)
126             err = json.Unmarshal(data[1], newevent)
127             category = "network"
128             event = *newevent
129         case "NetGWStatUpdate":
130             newevent := new(NetGWStatUpdate)
131             err = json.Unmarshal(data[1], newevent)
132             category = "network"
133             event = *newevent
134         default:
135             event = nil
136             err = errors.New("cannot unmarshal unknown type")
137             category = ""
138     }
139     return
140 }