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 event = DoorLockUpdate{}
36 err = json.Unmarshal(data[1],&event)
38 case "DoorAjarUpdate":
39 event := DoorAjarUpdate{}
40 err = json.Unmarshal(data[1], &event)
42 case "BackdoorAjarUpdate":
43 event := DoorAjarUpdate{}
44 err = json.Unmarshal(data[1], &event)
46 case "DoorCommandEvent":
47 event := DoorCommandEvent{}
48 err = json.Unmarshal(data[1], &event)
50 case "DoorProblemEvent":
51 event := DoorProblemEvent{}
52 err = json.Unmarshal(data[1], &event)
54 case "BoreDoomButtonPressEvent":
55 event := BoreDoomButtonPressEvent{}
56 err = json.Unmarshal(data[1], &event)
58 case "TempSensorUpdate":
59 event := TempSensorUpdate{}
60 err = json.Unmarshal(data[1], &event)
62 case "IlluminationSensorUpdate":
63 event := IlluminationSensorUpdate{}
64 err = json.Unmarshal(data[1], &event)
68 err = json.Unmarshal(data[1], &event)
70 case "MovementSensorUpdate":
71 event := MovementSensorUpdate{}
72 err = json.Unmarshal(data[1], &event)
74 case "PresenceUpdate":
75 event := PresenceUpdate{}
76 err = json.Unmarshal(data[1], &event)
78 case "SomethingReallyIsMoving":
79 event := SomethingReallyIsMoving{}
80 err = json.Unmarshal(data[1], &event)
84 err = errors.New("cannot unmarshal unknown type")