-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.go
301 lines (238 loc) · 8.36 KB
/
switch.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package shelly
import (
"context"
"github.com/mongoose-os/mos/common/mgrpc"
"github.com/mongoose-os/mos/common/mgrpc/frame"
)
type SwitchGetConfigRequest struct {
// ID of the switch component instance.
ID int `json:"id"`
}
func (r *SwitchGetConfigRequest) Method() string {
return "Switch.GetConfig"
}
func (r *SwitchGetConfigRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*SwitchConfig,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
func (r *SwitchGetConfigRequest) NewTypedResponse() *SwitchConfig {
return &SwitchConfig{}
}
func (r *SwitchGetConfigRequest) NewResponse() any {
return r.NewTypedResponse()
}
type SwitchSetConfigRequest struct {
// ID of the switch component instance.
ID int `json:"id"`
// Config that the method takes.
Config SwitchConfig `json:"config"`
}
func (r *SwitchSetConfigRequest) Method() string {
return "Switch.SetConfig"
}
func (r *SwitchSetConfigRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*SetConfigResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
func (r *SwitchSetConfigRequest) NewTypedResponse() *SetConfigResponse {
return &SetConfigResponse{}
}
func (r *SwitchSetConfigRequest) NewResponse() any {
return r.NewTypedResponse()
}
type SwitchGetStatusRequest struct {
// ID of the switch component instance.
ID int `json:"id"`
}
func (r *SwitchGetStatusRequest) Method() string {
return "Switch.GetStatus"
}
func (r *SwitchGetStatusRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*SwitchStatus,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
func (r *SwitchGetStatusRequest) NewTypedResponse() *SwitchStatus {
return &SwitchStatus{}
}
func (r *SwitchGetStatusRequest) NewResponse() any {
return r.NewTypedResponse()
}
type SwitchSetRequest struct {
// ID of the switch component instance.
ID int `json:"id"`
// On is true for switch on, false otherwise. Required
On bool `json:"on"`
// ToggleAfter is the number of seconds afterwhich the switch will flip-back.s
ToggleAfter *float64 `json:"toggle_after,omitempty"`
}
func (r *SwitchSetRequest) Method() string {
return "Switch.Set"
}
func (r *SwitchSetRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*SwitchActionResponse,
*frame.Response,
error,
) {
resp := &SwitchActionResponse{}
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
func (r *SwitchSetRequest) NewTypedResponse() *SwitchActionResponse {
return &SwitchActionResponse{}
}
func (r *SwitchSetRequest) NewResponse() any {
return r.NewTypedResponse()
}
type SwitchToggleRequest struct {
// ID of the switch component instance.
ID int `json:"id"`
}
func (r *SwitchToggleRequest) Method() string {
return "Switch.Toggle"
}
func (r *SwitchToggleRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*SwitchActionResponse,
*frame.Response,
error,
) {
resp := &SwitchActionResponse{}
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
func (r *SwitchToggleRequest) NewTypedResponse() *SwitchActionResponse {
return &SwitchActionResponse{}
}
func (r *SwitchToggleRequest) NewResponse() any {
return r.NewTypedResponse()
}
type SwitchConfig struct {
// ID of the switch component instance.
ID int `json:"id"`
// Name of the switch instance.
Name *string `json:"name"`
// InMode is the mode of the associated input. Range of values: momentary,
// follow, flip, detached, cycle (if applicable), activate (if applicable)
InMode *string `json:"in_mode,omitempty"`
// InitialState is the output state to set on power_on. Range of values: off,
// on, restore_last, match_input
InitialState *string `json:"initial_state,omitempty"`
// AutoOn is true if the "Automatic ON" function is enabled, false otherwise.
AutoOn *bool `json:"auto_on,omitempty"`
// AutoOnDelay is the number of seconds to pass until the component is
// switched back.
AutoOnDelay *float64 `json:"auto_on_delay,omitempty"`
// AutoOff is true if the "Automatic OFF" function is enabled, false otherwise.
AutoOff *bool `json:"auto_off,omitempty"`
// AutoOffDelay is the number of seconds to pass until the component is switched back off.
AutoOffDelay *float64 `json:"auto_off_delay,omitempty"`
// AutorecoverVoltageErrors is true if switch output state should be restored
// after over/undervoltage error is cleared, false otherwise (shown if applicable).
AutorecoverVoltageErrors *bool `json:"autorecover_voltage_errors,omitempty"`
// InputID is the ID of the Input component which controls the Switch.
// Applicable only to Pro1 and Pro1PM devices. Valid values: 0, 1
InputID *int `json:"input_id,omitempty"`
// PowerLimit (in Watts) over which overpower condition occurs (shown if applicable).
PowerLimit *float64 `json:"power_limit,omitempty"`
// VoltageLimit (in Volts) over which overvoltage condition occurs (shown if applicable).
VoltageLimit *float64 `json:"voltage_limit,omitempty"`
// UndervoltageLimit (in Volts) over which overvoltage condition occurs (shown if applicable)
UndervoltageLimit *float64 `json:"undervoltage_limit,omitempty"`
// CurrentLimit (in Amperes) over which overcurrent condition occurs (shown if applicable)
CurrentLimit *float64 `json:"current_limit,omitempty"`
}
type SwitchStatus struct {
// ID of the switch component instance.
ID int `json:"id"`
// Source of the last command, for example: init, WS_in, http, ...
Source *string `json:"source,omitempty"`
// Output is true if the output channel is currently on, false otherwise.
Output *bool `json:"output,omitempty"`
// TimerStartedAt is the unix timestamp, start time of the timer (in UTC)
// (shown if the timer is triggered)
TimerStartedAt *float64 `json:"timer_started_at,omitempty"`
// TimerDuration is the number of seconds for the timer (shown if the timer
// is triggered).
TimerDuration *float64 `json:"timer_duration,omitempty"`
// APower is the last measured instantaneous active power (in Watts)
// delivered to the attached load (shown if applicable).
APower *float64 `json:"apower,omitempty"`
// Voltage last measured in Volts (shown if applicable).
Voltage *float64 `json:"voltage,omitempty"`
// Current last measured in Amperes (shown if applicable).
Current *float64 `json:"current,omitempty"`
// PF is the last measured power factor (shown if applicable).
PF *float64 `json:"pf,omitempty"`
// Freq is the last measured network frequency in Hz (shown if applicable).
Freq *float64 `json:"freq,omitempty"`
// AEnergy contains information about the active energy counter (shown if
// applicable)
AEnergy *EnergyCounters `json:"aenergy,omitempty"`
// RetAEnergy contains information about the returned active energy counter
// (shown if applicable)
RetAEnergy *EnergyCounters `json:"ret_aenergy,omitempty"`
// Temperature describes the internal temperature of the relay.
Temperature *Temperature `json:"temperature,omitempty"`
// Errors lists error conditions occurred. May contain overtemp, overpower,
// overvoltage, undervoltage, (shown if at least one error is present).
Errors []string `json:"errors,omitempty"`
}
// EnergyCounters describes active energy counters.
type EnergyCounters struct {
// Total energy consumed in Watt-hours.
Total float64 `json:"total"`
// ByMinute is the energy consumption by minute (in Milliwatt-hours) for
// the last three minutes (the lower the index of the element in the array,
// the closer to the current moment the minute)
ByMinute []float64 `json:"by_minute"`
// MinuteTS is the Unix timestamp of the first second of the last minute (in UTC)
MinuteTS float64 `json:"minute_ts,omitempty"`
}
// Temperature describes a temperature measurement.
type Temperature struct {
// C is the temperature in Celsius (null if temperature is out of the
// measurement range)
C *float64 `json:"tC,omitempty"`
// F is the temperature in Fahrenheit (null if temperature is out of the
// measurement range)
F *float64 `json:"tF,omitempty"`
}
type SwitchActionResponse struct {
// WasOn is true if the switch was on before the method was executed,
// false otherwise.
WasOn bool `json:"was_on"`
}