diff options
author | Mike Crute <mcrute@gmail.com> | 2016-09-24 16:41:32 -0700 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2016-09-24 16:41:32 -0700 |
commit | 3ccc02f8f1c08fc82f581e9b3eb4315b42614c8e (patch) | |
tree | 548856f40bc0cce12bc964500a193b665530ccf2 /inform/messages.go | |
parent | 80aac9a2da274b4f539e5b6cafbfd06b1a75c9d9 (diff) | |
download | go-inform-3ccc02f8f1c08fc82f581e9b3eb4315b42614c8e.tar.bz2 go-inform-3ccc02f8f1c08fc82f581e9b3eb4315b42614c8e.tar.xz go-inform-3ccc02f8f1c08fc82f581e9b3eb4315b42614c8e.zip |
Cleanup message unpacking
Diffstat (limited to 'inform/messages.go')
-rw-r--r-- | inform/messages.go | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/inform/messages.go b/inform/messages.go new file mode 100644 index 0000000..3dd8f6d --- /dev/null +++ b/inform/messages.go | |||
@@ -0,0 +1,162 @@ | |||
1 | package inform | ||
2 | |||
3 | import ( | ||
4 | "encoding/json" | ||
5 | "strconv" | ||
6 | "time" | ||
7 | ) | ||
8 | |||
9 | type AdminMetadata struct { | ||
10 | Id string `json:"_id"` | ||
11 | Language string `json:"lang"` | ||
12 | Username string `json:"name"` | ||
13 | Password string `json:"x_password"` | ||
14 | } | ||
15 | |||
16 | type CommandMessage struct { | ||
17 | Metadata *AdminMetadata `json:"_admin,omitempty"` | ||
18 | Id string `json:"_id,omitempty"` | ||
19 | Type string `json:"_type"` | ||
20 | Command string `json:"cmd"` | ||
21 | DateTime string `json:"datetime"` | ||
22 | DeviceId string `json:"device_id,omitempty"` | ||
23 | MacAddress string `json:"mac,omitempty"` | ||
24 | Model string `json:"model,omitempty"` | ||
25 | OffVoltage int `json:"off_volt,omitempty"` | ||
26 | Port int `json:"port"` | ||
27 | SensorId string `json:"sId,omitempty"` | ||
28 | ServerTime string `json:"server_time_in_utc"` | ||
29 | Time int64 `json:"time"` | ||
30 | Timer int `json:"timer"` | ||
31 | Value int `json:"val"` | ||
32 | Voltage int `json:"volt,omitempty"` | ||
33 | } | ||
34 | |||
35 | func NewOutputCommand(port, val, timer int) *CommandMessage { | ||
36 | return &CommandMessage{ | ||
37 | Type: "cmd", | ||
38 | Command: "mfi-output", | ||
39 | DateTime: time.Now().Format(time.RFC3339), | ||
40 | Port: port, | ||
41 | ServerTime: unixMicroPSTString(), | ||
42 | Time: unixMicroPST(), | ||
43 | Timer: timer, | ||
44 | Value: val, | ||
45 | } | ||
46 | } | ||
47 | |||
48 | type NoopMessage struct { | ||
49 | Type string `json:"_type"` | ||
50 | Interval int `json:"interval"` | ||
51 | ServerTimeUTC string `json:"server_time_in_utc"` | ||
52 | } | ||
53 | |||
54 | func unixMicroPST() int64 { | ||
55 | l, _ := time.LoadLocation("America/Los_Angeles") | ||
56 | tnano := time.Now().In(l).UnixNano() | ||
57 | return tnano / int64(time.Millisecond) | ||
58 | } | ||
59 | |||
60 | func unixMicroPSTString() string { | ||
61 | return strconv.FormatInt(unixMicroPST(), 10) | ||
62 | } | ||
63 | |||
64 | func unixMicroUTCString() string { | ||
65 | tnano := time.Now().UTC().UnixNano() | ||
66 | t := tnano / int64(time.Millisecond) | ||
67 | return strconv.FormatInt(t, 10) | ||
68 | } | ||
69 | |||
70 | func NewNoop(interval int) *NoopMessage { | ||
71 | return &NoopMessage{ | ||
72 | Type: "noop", | ||
73 | Interval: interval, | ||
74 | ServerTimeUTC: unixMicroUTCString(), | ||
75 | } | ||
76 | } | ||
77 | |||
78 | type DeviceMessage struct { | ||
79 | IsDefault bool `json:"default"` | ||
80 | IP string `json:"ip"` | ||
81 | MacAddr string `json:"mac"` | ||
82 | ModelNumber string `json:"model"` | ||
83 | ModelName string `json:"model_display"` | ||
84 | Serial string `json:"serial"` | ||
85 | FirmwareVersion string `json:"version"` | ||
86 | Outputs []*OutputInfo | ||
87 | } | ||
88 | |||
89 | func (m *DeviceMessage) UnmarshalJSON(data []byte) error { | ||
90 | type Alias DeviceMessage | ||
91 | aux := &struct { | ||
92 | Alarm []struct { | ||
93 | Entries []struct { | ||
94 | Tag string `json:"tag"` | ||
95 | Type string `json:"type"` | ||
96 | Val interface{} `json:"val"` | ||
97 | } `json:"entries"` | ||
98 | Sensor string `json:"sId"` | ||
99 | } `json:"alarm"` | ||
100 | *Alias | ||
101 | }{ | ||
102 | Alias: (*Alias)(m), | ||
103 | } | ||
104 | |||
105 | if err := json.Unmarshal(data, &aux); err != nil { | ||
106 | return err | ||
107 | } | ||
108 | |||
109 | m.Outputs = make([]*OutputInfo, len(aux.Alarm)) | ||
110 | |||
111 | for i, a := range aux.Alarm { | ||
112 | o := &OutputInfo{ | ||
113 | Id: a.Sensor, | ||
114 | Port: i + 1, | ||
115 | Dimmer: m.ModelNumber == "IWD1U", | ||
116 | } | ||
117 | m.Outputs[i] = o | ||
118 | |||
119 | for _, e := range a.Entries { | ||
120 | switch t := e.Val; e.Tag { | ||
121 | case "output": | ||
122 | o.OutputState = t.(float64) == 1 | ||
123 | case "pf": | ||
124 | o.PowerFactor = t.(float64) | ||
125 | case "energy_sum": | ||
126 | o.EnergySum = t.(float64) | ||
127 | case "v_rms": | ||
128 | o.VoltageRMS = t.(float64) | ||
129 | case "i_rms": | ||
130 | o.CurrentRMS = t.(float64) | ||
131 | case "active_pwr": | ||
132 | o.Watts = t.(float64) | ||
133 | case "thismonth": | ||
134 | o.ThisMonth = t.(float64) | ||
135 | case "lastmonth": | ||
136 | o.LastMonth = t.(float64) | ||
137 | case "dimmer_level": | ||
138 | o.DimmerLevel = int(t.(float64)) | ||
139 | case "dimmer_lock_setting": | ||
140 | o.DimmerLockSetting = int(t.(float64)) | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | return nil | ||
146 | } | ||
147 | |||
148 | type OutputInfo struct { | ||
149 | Id string | ||
150 | Port int | ||
151 | OutputState bool | ||
152 | EnergySum float64 | ||
153 | VoltageRMS float64 | ||
154 | PowerFactor float64 | ||
155 | CurrentRMS float64 | ||
156 | Watts float64 | ||
157 | ThisMonth float64 | ||
158 | LastMonth float64 | ||
159 | Dimmer bool | ||
160 | DimmerLevel int | ||
161 | DimmerLockSetting int | ||
162 | } | ||