1 // (c) Bernhard Tittelbach, 2013
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
18 func MarshalEvent2ByteByte(event_interface interface{}) (data [][]byte, err error) {
20 //~ fmt.Printf("%T%+v\n", event_interface, event_interface)
21 msg, err = json.Marshal(event_interface)
25 data = [][]byte{[]byte(NameOfStruct(event_interface)), msg} //works in all cases for etype_lastsep in range -1 to len(etype)-1
29 func UnmarshalByteByte2Event(data [][]byte) (event interface{}, category string, err error) {
31 return nil, "", errors.New("not a r3event message")
33 switch string(data[0]) {
34 case "DoorLockUpdate":
35 newevent := new(DoorLockUpdate)
36 err = json.Unmarshal(data[1],newevent)
39 case "DoorAjarUpdate":
40 newevent := new(DoorAjarUpdate)
41 err = json.Unmarshal(data[1], newevent)
44 case "BackdoorAjarUpdate":
45 newevent := new(BackdoorAjarUpdate)
46 err = json.Unmarshal(data[1], newevent)
49 case "DoorCommandEvent":
50 newevent := new(DoorCommandEvent)
51 err = json.Unmarshal(data[1], newevent)
54 case "DoorProblemEvent":
55 newevent := new(DoorProblemEvent)
56 err = json.Unmarshal(data[1], newevent)
59 case "DoorManualMovementEvent":
60 newevent := new(DoorManualMovementEvent)
61 err = json.Unmarshal(data[1], newevent)
64 case "BoreDoomButtonPressEvent":
65 newevent := new(BoreDoomButtonPressEvent)
66 err = json.Unmarshal(data[1], newevent)
69 case "TempSensorUpdate":
70 newevent := new(TempSensorUpdate)
71 err = json.Unmarshal(data[1], newevent)
74 case "IlluminationSensorUpdate":
75 newevent := new(IlluminationSensorUpdate)
76 err = json.Unmarshal(data[1], newevent)
79 case "DustSensorUpdate":
80 newevent := new(DustSensorUpdate)
81 err = json.Unmarshal(data[1], newevent)
84 case "RelativeHumiditySensorUpdate":
85 newevent := new(RelativeHumiditySensorUpdate)
86 err = json.Unmarshal(data[1], newevent)
90 newevent := new(TimeTick)
91 err = json.Unmarshal(data[1], newevent)
95 newevent := new(GasLeakAlert)
96 err = json.Unmarshal(data[1], newevent)
99 case "MovementSensorUpdate":
100 newevent := new(MovementSensorUpdate)
101 err = json.Unmarshal(data[1], newevent)
102 category = "movement"
104 case "PresenceUpdate":
105 newevent := new(PresenceUpdate)
106 err = json.Unmarshal(data[1], newevent)
107 category = "presence"
109 case "SomethingReallyIsMoving":
110 newevent := new(SomethingReallyIsMoving)
111 err = json.Unmarshal(data[1], newevent)
112 category = "movement"
114 case "TempSensorSpike":
115 newevent := new(TempSensorSpike)
116 err = json.Unmarshal(data[1], newevent)
119 case "DustSensorSpike":
120 newevent := new(DustSensorSpike)
121 err = json.Unmarshal(data[1], newevent)
125 newevent := new(NetDHCPACK)
126 err = json.Unmarshal(data[1], newevent)
129 case "NetGWStatUpdate":
130 newevent := new(NetGWStatUpdate)
131 err = json.Unmarshal(data[1], newevent)
136 err = errors.New("cannot unmarshal unknown type")