aboutsummaryrefslogtreecommitdiff
path: root/inform/rx_messages.go
blob: 68fd3936392945ea1e26399e9a48f42d4740a57e (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
package inform

// Messages we receive from devices

import (
	"encoding/json"
)

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
}

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
}

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
}