/
shelly.go
178 lines (166 loc) · 4.22 KB
/
shelly.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package smarthome
import (
"fmt"
)
type ShellyInfo struct {
Settings ShellySettings
Status ShellyStatus
}
type ShellySettings struct {
// Common HTTP API
Device struct {
Type string
MAC string
Hostname string
NumOutputs int `json:"num_outputs"`
NumMeters int `json:"num_meters"`
NumRollers int `json:"num_rollers"`
}
WiFiAP struct {
Enabled bool
SSID string
Key string
} `json:"wifi_ap"`
WiFiStatus struct {
Enabled bool
SSID string
IPv4Method string `json:"ipv4_method"`
IP string
GW string
Mask string
DNS string
} `json:"wifi_sta"`
WiFiStatus1 struct {
Enabled bool
SSID string
IPv4Method string `json:"ipv4_method"`
IP string
GW string
Mask string
DNS string
} `json:"wifi_sta1"`
APRoaming struct {
Enabled bool
Threshold int
} `json:"ap_roaming"`
MQTT struct {
Enabled bool
Server string
User string
ID string
ReconnectTimeoutMax float64 `json:"reconnect_timeout_max"`
ReconnectTimeoutMin float64 `json:"reconnect_timeout_min"`
CleanSession bool `json:"clean_session"`
KeepAlive int `json:"keep_alive"`
MaxQoS int `json:"max_qos"`
Retain bool
UpdatePeriod int `json:"update_period"`
}
CoIoT struct {
Enabled bool
UpdatePeriod int `json:"update_period"`
Peer string
}
SNTP struct {
Server string
Enabled bool
}
Login struct {
Enabled bool
Unprotected bool
Username string
}
PinCode string `json:"pin_code"`
Name string
FW string
Discoverable bool
BuildInfo struct {
BuildID string `json:"build_id"`
BuildTimestamp string `json:"build_timestamp"`
BuildVersion string `json:"build_version"`
} `json:"build_info"`
Cloud struct {
Enabled bool
Connected bool
}
Timezone string
Lat float64
Lng float64
TZAutodetect bool
TZUTCOffset int `json:"tz_utc_offset"`
TZDST bool `json:"tz_dst"`
TZDSTAuto bool `json:"tz_dst_auto"`
Time string
Unixtime int
DebugEnable bool `json:"debug_enable"`
AllowCrossOrigin bool `json:"allow_cross_origin"`
WiFiRecoveryRebootEnabled bool `json:"wifirecovery_reboot_enabled"`
// Shelly 1/2.5/...
Mode string
// ...many more missing...
}
type ShellyStatus struct {
// Common HTTP API
WiFiStatus struct {
Connected bool
SSID string
IP string
RSSI int
} `json:"wifi_sta"`
// Extra HTTP API for some Shellies
Temperature float64
Voltage float64
Relays []struct {
IsOn bool
HasTimer bool `json:"has_timer"`
TimerDuration int `json:"timer_duration"`
IsValid bool `json:"is_valid"`
Source string
}
Rollers []struct {
State string
Source string
Power float64
IsValid bool `json:"is_valid"`
LastDirection string `json:"last_direction"`
CurrentPos int `json:"current_pos"`
Positioning bool
}
Meters []struct {
Power float64
IsValid bool `json:"is_valid"`
Timestamp int
Counters []float64
Total int
}
Inputs []struct {
Input int
Event string
EventCnt int `json:"event_cnt"`
}
Emeters []struct {
Power float64
Reactive float64
Voltage float64
IsValid bool `json:"is_valid"`
Total float64
TotalReturned float64 `json:"total_returned"`
}
}
func ShellyGetInfo(host string) (*ShellyInfo, error) {
var err error
var shelly ShellyInfo
err = HTTPtoJSON(fmt.Sprintf("http://%s/settings", host), &shelly.Settings)
if err != nil {
return nil, err
}
// fmt.Printf("smarthome.ShellyInfo(): rawSettings=%v\n", string(body))
// fmt.Printf("smarthome.ShellyInfo(): settings=%+v\n", shelly.Settings)
err = HTTPtoJSON(fmt.Sprintf("http://%s/status", host), &shelly.Status)
if err != nil {
return nil, err
}
// fmt.Printf("smarthome.ShellyInfo(): rawStatus=%v\n", string(body))
// fmt.Printf("smarthome.ShellyInfo(): status=%+v\n", shelly.Status)
return &shelly, nil
}