logo tryouts
[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 "BoreDoomButtonPressEvent":
60             newevent := new(BoreDoomButtonPressEvent)
61             err = json.Unmarshal(data[1], newevent)
62             category = "buttons"
63             event = *newevent
64         case "TempSensorUpdate":
65             newevent := new(TempSensorUpdate)
66             err = json.Unmarshal(data[1], newevent)
67             category = "sensors"
68             event = *newevent
69         case "IlluminationSensorUpdate":
70             newevent := new(IlluminationSensorUpdate)
71             err = json.Unmarshal(data[1], newevent)
72             category = "sensors"
73             event = *newevent
74         case "DustSensorUpdate":
75             newevent := new(DustSensorUpdate)
76             err = json.Unmarshal(data[1], newevent)
77             category = "sensors"
78             event = *newevent
79         case "RelativeHumiditySensorUpdate":
80             newevent := new(RelativeHumiditySensorUpdate)
81             err = json.Unmarshal(data[1], newevent)
82             category = "sensors"
83             event = *newevent
84         case "TimeTick":
85             newevent := new(TimeTick)
86             err = json.Unmarshal(data[1], newevent)
87             category = "time"
88             event = *newevent
89         case "MovementSensorUpdate":
90             newevent := new(MovementSensorUpdate)
91             err = json.Unmarshal(data[1], newevent)
92             category = "movement"
93             event = *newevent
94         case "PresenceUpdate":
95             newevent := new(PresenceUpdate)
96             err = json.Unmarshal(data[1], newevent)
97             category = "presence"
98             event = *newevent
99         case "SomethingReallyIsMoving":
100             newevent := new(SomethingReallyIsMoving)
101             err = json.Unmarshal(data[1], newevent)
102             category = "movement"
103             event = *newevent
104         case "TempSensorSpike":
105             newevent := new(TempSensorSpike)
106             err = json.Unmarshal(data[1], newevent)
107             category = "sensors"
108             event = *newevent
109         case "DustSensorSpike":
110             newevent := new(DustSensorSpike)
111             err = json.Unmarshal(data[1], newevent)
112             category = "sensors"
113             event = *newevent
114         case "NetDHCPACK":
115             newevent := new(NetDHCPACK)
116             err = json.Unmarshal(data[1], newevent)
117             category = "network"
118             event = *newevent
119         case "NetGWStatUpdate":
120             newevent := new(NetGWStatUpdate)
121             err = json.Unmarshal(data[1], newevent)
122             category = "network"
123             event = *newevent
124         default:
125             event = nil
126             err = errors.New("cannot unmarshal unknown type")
127             category = ""
128     }
129     return
130 }