aboutsummaryrefslogtreecommitdiff
path: root/inform/messages.go
blob: 3dd8f6d6f7d8d35fc79be6098014fb85823f4aae (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package inform

import (
	"encoding/json"
	"strconv"
	"time"
)

type AdminMetadata struct {
	Id       string `json:"_id"`
	Language string `json:"lang"`
	Username string `json:"name"`
	Password string `json:"x_password"`
}

type CommandMessage struct {
	Metadata   *AdminMetadata `json:"_admin,omitempty"`
	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(),
	}
}

type DeviceMessage struct {
	IsDefault       bool   `json:"default"`
	IP              string `json:"ip"`
	MacAddr         string `json:"mac"`
	ModelNumber     string `json:"model"`
	ModelName       string `json:"model_display"`
	Serial          string `json:"serial"`
	FirmwareVersion string `json:"version"`
	Outputs         []*OutputInfo
}

func (m *DeviceMessage) UnmarshalJSON(data []byte) error {
	type Alias DeviceMessage
	aux := &struct {
		Alarm []struct {
			Entries []struct {
				Tag  string      `json:"tag"`
				Type string      `json:"type"`
				Val  interface{} `json:"val"`
			} `json:"entries"`
			Sensor string `json:"sId"`
		} `json:"alarm"`
		*Alias
	}{
		Alias: (*Alias)(m),
	}

	if err := json.Unmarshal(data, &aux); err != nil {
		return err
	}

	m.Outputs = make([]*OutputInfo, len(aux.Alarm))

	for i, a := range aux.Alarm {
		o := &OutputInfo{
			Id:     a.Sensor,
			Port:   i + 1,
			Dimmer: m.ModelNumber == "IWD1U",
		}
		m.Outputs[i] = o

		for _, e := range a.Entries {
			switch t := e.Val; e.Tag {
			case "output":
				o.OutputState = t.(float64) == 1
			case "pf":
				o.PowerFactor = t.(float64)
			case "energy_sum":
				o.EnergySum = t.(float64)
			case "v_rms":
				o.VoltageRMS = t.(float64)
			case "i_rms":
				o.CurrentRMS = t.(float64)
			case "active_pwr":
				o.Watts = t.(float64)
			case "thismonth":
				o.ThisMonth = t.(float64)
			case "lastmonth":
				o.LastMonth = t.(float64)
			case "dimmer_level":
				o.DimmerLevel = int(t.(float64))
			case "dimmer_lock_setting":
				o.DimmerLockSetting = int(t.(float64))
			}
		}
	}

	return nil
}

type OutputInfo struct {
	Id                string
	Port              int
	OutputState       bool
	EnergySum         float64
	VoltageRMS        float64
	PowerFactor       float64
	CurrentRMS        float64
	Watts             float64
	ThisMonth         float64
	LastMonth         float64
	Dimmer            bool
	DimmerLevel       int
	DimmerLockSetting int
}