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 "BoreDoomButtonPressEvent":
60 newevent := new(BoreDoomButtonPressEvent)
61 err = json.Unmarshal(data[1], newevent)
64 case "TempSensorUpdate":
65 newevent := new(TempSensorUpdate)
66 err = json.Unmarshal(data[1], newevent)
69 case "IlluminationSensorUpdate":
70 newevent := new(IlluminationSensorUpdate)
71 err = json.Unmarshal(data[1], newevent)
74 case "DustSensorUpdate":
75 newevent := new(DustSensorUpdate)
76 err = json.Unmarshal(data[1], newevent)
79 case "RelativeHumiditySensorUpdate":
80 newevent := new(RelativeHumiditySensorUpdate)
81 err = json.Unmarshal(data[1], newevent)
85 newevent := new(TimeTick)
86 err = json.Unmarshal(data[1], newevent)
89 case "MovementSensorUpdate":
90 newevent := new(MovementSensorUpdate)
91 err = json.Unmarshal(data[1], newevent)
94 case "PresenceUpdate":
95 newevent := new(PresenceUpdate)
96 err = json.Unmarshal(data[1], newevent)
99 case "SomethingReallyIsMoving":
100 newevent := new(SomethingReallyIsMoving)
101 err = json.Unmarshal(data[1], newevent)
102 category = "movement"
104 case "TempSensorSpike":
105 newevent := new(TempSensorSpike)
106 err = json.Unmarshal(data[1], newevent)
109 case "DustSensorSpike":
110 newevent := new(DustSensorSpike)
111 err = json.Unmarshal(data[1], newevent)
115 newevent := new(NetDHCPACK)
116 err = json.Unmarshal(data[1], newevent)
119 case "NetGWStatUpdate":
120 newevent := new(NetGWStatUpdate)
121 err = json.Unmarshal(data[1], newevent)
126 err = errors.New("cannot unmarshal unknown type")