diff options
author | Mike Crute <mcrute@gmail.com> | 2016-09-24 16:46:59 -0700 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2016-09-24 16:46:59 -0700 |
commit | 88a0976174eb96867af4c968fc72716f87e54094 (patch) | |
tree | 161737f5a0c5e5c96a63797e5fb999f027875b16 /inform/rx_messages.go | |
parent | 3ccc02f8f1c08fc82f581e9b3eb4315b42614c8e (diff) | |
download | go-inform-88a0976174eb96867af4c968fc72716f87e54094.tar.bz2 go-inform-88a0976174eb96867af4c968fc72716f87e54094.tar.xz go-inform-88a0976174eb96867af4c968fc72716f87e54094.zip |
Split messages again
Diffstat (limited to 'inform/rx_messages.go')
-rw-r--r-- | inform/rx_messages.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/inform/rx_messages.go b/inform/rx_messages.go new file mode 100644 index 0000000..68fd393 --- /dev/null +++ b/inform/rx_messages.go | |||
@@ -0,0 +1,93 @@ | |||
1 | package inform | ||
2 | |||
3 | // Messages we receive from devices | ||
4 | |||
5 | import ( | ||
6 | "encoding/json" | ||
7 | ) | ||
8 | |||
9 | type DeviceMessage struct { | ||
10 | IsDefault bool `json:"default"` | ||
11 | IP string `json:"ip"` | ||
12 | MacAddr string `json:"mac"` | ||
13 | ModelNumber string `json:"model"` | ||
14 | ModelName string `json:"model_display"` | ||
15 | Serial string `json:"serial"` | ||
16 | FirmwareVersion string `json:"version"` | ||
17 | Outputs []*OutputInfo | ||
18 | } | ||
19 | |||
20 | type OutputInfo struct { | ||
21 | Id string | ||
22 | Port int | ||
23 | OutputState bool | ||
24 | EnergySum float64 | ||
25 | VoltageRMS float64 | ||
26 | PowerFactor float64 | ||
27 | CurrentRMS float64 | ||
28 | Watts float64 | ||
29 | ThisMonth float64 | ||
30 | LastMonth float64 | ||
31 | Dimmer bool | ||
32 | DimmerLevel int | ||
33 | DimmerLockSetting int | ||
34 | } | ||
35 | |||
36 | func (m *DeviceMessage) UnmarshalJSON(data []byte) error { | ||
37 | type Alias DeviceMessage | ||
38 | aux := &struct { | ||
39 | Alarm []struct { | ||
40 | Entries []struct { | ||
41 | Tag string `json:"tag"` | ||
42 | Type string `json:"type"` | ||
43 | Val interface{} `json:"val"` | ||
44 | } `json:"entries"` | ||
45 | Sensor string `json:"sId"` | ||
46 | } `json:"alarm"` | ||
47 | *Alias | ||
48 | }{ | ||
49 | Alias: (*Alias)(m), | ||
50 | } | ||
51 | |||
52 | if err := json.Unmarshal(data, &aux); err != nil { | ||
53 | return err | ||
54 | } | ||
55 | |||
56 | m.Outputs = make([]*OutputInfo, len(aux.Alarm)) | ||
57 | |||
58 | for i, a := range aux.Alarm { | ||
59 | o := &OutputInfo{ | ||
60 | Id: a.Sensor, | ||
61 | Port: i + 1, | ||
62 | Dimmer: m.ModelNumber == "IWD1U", | ||
63 | } | ||
64 | m.Outputs[i] = o | ||
65 | |||
66 | for _, e := range a.Entries { | ||
67 | switch t := e.Val; e.Tag { | ||
68 | case "output": | ||
69 | o.OutputState = t.(float64) == 1 | ||
70 | case "pf": | ||
71 | o.PowerFactor = t.(float64) | ||
72 | case "energy_sum": | ||
73 | o.EnergySum = t.(float64) | ||
74 | case "v_rms": | ||
75 | o.VoltageRMS = t.(float64) | ||
76 | case "i_rms": | ||
77 | o.CurrentRMS = t.(float64) | ||
78 | case "active_pwr": | ||
79 | o.Watts = t.(float64) | ||
80 | case "thismonth": | ||
81 | o.ThisMonth = t.(float64) | ||
82 | case "lastmonth": | ||
83 | o.LastMonth = t.(float64) | ||
84 | case "dimmer_level": | ||
85 | o.DimmerLevel = int(t.(float64)) | ||
86 | case "dimmer_lock_setting": | ||
87 | o.DimmerLockSetting = int(t.(float64)) | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | return nil | ||
93 | } | ||