diff options
Diffstat (limited to 'inform/tx_messages.go')
-rw-r--r-- | inform/tx_messages.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/inform/tx_messages.go b/inform/tx_messages.go new file mode 100644 index 0000000..0021ef3 --- /dev/null +++ b/inform/tx_messages.go | |||
@@ -0,0 +1,69 @@ | |||
1 | package inform | ||
2 | |||
3 | // Messages we send to devices | ||
4 | |||
5 | import ( | ||
6 | "strconv" | ||
7 | "time" | ||
8 | ) | ||
9 | |||
10 | type CommandMessage struct { | ||
11 | Id string `json:"_id,omitempty"` | ||
12 | Type string `json:"_type"` | ||
13 | Command string `json:"cmd"` | ||
14 | DateTime string `json:"datetime"` | ||
15 | DeviceId string `json:"device_id,omitempty"` | ||
16 | MacAddress string `json:"mac,omitempty"` | ||
17 | Model string `json:"model,omitempty"` | ||
18 | OffVoltage int `json:"off_volt,omitempty"` | ||
19 | Port int `json:"port"` | ||
20 | SensorId string `json:"sId,omitempty"` | ||
21 | ServerTime string `json:"server_time_in_utc"` | ||
22 | Time int64 `json:"time"` | ||
23 | Timer int `json:"timer"` | ||
24 | Value int `json:"val"` | ||
25 | Voltage int `json:"volt,omitempty"` | ||
26 | } | ||
27 | |||
28 | func NewOutputCommand(port, val, timer int) *CommandMessage { | ||
29 | return &CommandMessage{ | ||
30 | Type: "cmd", | ||
31 | Command: "mfi-output", | ||
32 | DateTime: time.Now().Format(time.RFC3339), | ||
33 | Port: port, | ||
34 | ServerTime: unixMicroPSTString(), | ||
35 | Time: unixMicroPST(), | ||
36 | Timer: timer, | ||
37 | Value: val, | ||
38 | } | ||
39 | } | ||
40 | |||
41 | type NoopMessage struct { | ||
42 | Type string `json:"_type"` | ||
43 | Interval int `json:"interval"` | ||
44 | ServerTimeUTC string `json:"server_time_in_utc"` | ||
45 | } | ||
46 | |||
47 | func unixMicroPST() int64 { | ||
48 | l, _ := time.LoadLocation("America/Los_Angeles") | ||
49 | tnano := time.Now().In(l).UnixNano() | ||
50 | return tnano / int64(time.Millisecond) | ||
51 | } | ||
52 | |||
53 | func unixMicroPSTString() string { | ||
54 | return strconv.FormatInt(unixMicroPST(), 10) | ||
55 | } | ||
56 | |||
57 | func unixMicroUTCString() string { | ||
58 | tnano := time.Now().UTC().UnixNano() | ||
59 | t := tnano / int64(time.Millisecond) | ||
60 | return strconv.FormatInt(t, 10) | ||
61 | } | ||
62 | |||
63 | func NewNoop(interval int) *NoopMessage { | ||
64 | return &NoopMessage{ | ||
65 | Type: "noop", | ||
66 | Interval: interval, | ||
67 | ServerTimeUTC: unixMicroUTCString(), | ||
68 | } | ||
69 | } | ||