aboutsummaryrefslogtreecommitdiff
path: root/inform/tx_messages.go
blob: 0021ef30cd6c5e849485836f6d5a2b10eb9e13d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package inform

// Messages we send to devices

import (
	"strconv"
	"time"
)

type CommandMessage struct {
	Id         string `json:"_id,omitempty"`
	Type       string `json:"_type"`
	Command    string `json:"cmd"`
	DateTime   string `json:"datetime"`
	DeviceId   string `json:"device_id,omitempty"`
	MacAddress string `json:"mac,omitempty"`
	Model      string `json:"model,omitempty"`
	OffVoltage int    `json:"off_volt,omitempty"`
	Port       int    `json:"port"`
	SensorId   string `json:"sId,omitempty"`
	ServerTime string `json:"server_time_in_utc"`
	Time       int64  `json:"time"`
	Timer      int    `json:"timer"`
	Value      int    `json:"val"`
	Voltage    int    `json:"volt,omitempty"`
}

func NewOutputCommand(port, val, timer int) *CommandMessage {
	return &CommandMessage{
		Type:       "cmd",
		Command:    "mfi-output",
		DateTime:   time.Now().Format(time.RFC3339),
		Port:       port,
		ServerTime: unixMicroPSTString(),
		Time:       unixMicroPST(),
		Timer:      timer,
		Value:      val,
	}
}

type NoopMessage struct {
	Type          string `json:"_type"`
	Interval      int    `json:"interval"`
	ServerTimeUTC string `json:"server_time_in_utc"`
}

func unixMicroPST() int64 {
	l, _ := time.LoadLocation("America/Los_Angeles")
	tnano := time.Now().In(l).UnixNano()
	return tnano / int64(time.Millisecond)
}

func unixMicroPSTString() string {
	return strconv.FormatInt(unixMicroPST(), 10)
}

func unixMicroUTCString() string {
	tnano := time.Now().UTC().UnixNano()
	t := tnano / int64(time.Millisecond)
	return strconv.FormatInt(t, 10)
}

func NewNoop(interval int) *NoopMessage {
	return &NoopMessage{
		Type:          "noop",
		Interval:      interval,
		ServerTimeUTC: unixMicroUTCString(),
	}
}