-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
261 lines (214 loc) · 6.92 KB
/
input.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
package shelly
import (
"context"
"encoding/json"
"fmt"
"github.com/mongoose-os/mos/common/mgrpc"
"github.com/mongoose-os/mos/common/mgrpc/frame"
)
type InputGetStatusRequest struct {
// ID of the switch component instance.
ID int `json:"id"`
}
func (r *InputGetStatusRequest) Method() string {
return "Input.GetStatus"
}
func (r *InputGetStatusRequest) NewTypedResponse() *InputStatus {
return &InputStatus{}
}
func (r *InputGetStatusRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *InputGetStatusRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*InputStatus,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type InputStatus struct {
// ID of the input component instance.
ID int `json:"id"`
// State of the input (null if the input instance is stateless, i.e. for type button)
// (only for type switch, button).
State *bool `json:"state,omitempty"`
// Percent is the analog value in percent (null if the valid value could not be obtained)
// (only for type "analog").
Percent *float64 `json:"percent,omitempty"`
// XPercent is percent transformed with config.xpercent.expr. Present only when both
// config.xpercent.expr and config.xpercent.unit are set to non-empty values. null if
// config.xpercent.expr can not be evaluated.
// (only for type "analog").
XPercent *float64 `json:"xpercent,omitempty"`
// Errors is shown only if at least one error is present. May contain out_of_range, read.
Errors []string `json:"errors,omitempty"`
}
type InputGetConfigRequest struct {
// ID of the input component instance.
ID int `json:"id"`
}
func (r *InputGetConfigRequest) Method() string {
return "Input.GetConfig"
}
func (r *InputGetConfigRequest) NewTypedResponse() *InputConfig {
return &InputConfig{}
}
func (r *InputGetConfigRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *InputGetConfigRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*InputConfig,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type InputConfig struct {
// ID of the switch component instance.
ID int `json:"id"`
// Name of the switch instance.
Name *string `json:"name"`
// Type of associated input. Range of values switch, button, analog (only if applicable).
Type *string `json:"type,omitempty"`
// Enable flag. When disabled, the input instance doesn't emit any events and reports
// status properties as null. Applies for all input types.
Enable *bool `json:"enable,omitempty"`
// Invert is true if the logical state of the associated input is inverted, false otherwise.
// For the change to be applied, the physical switch has to be toggled once after invert
// is set. For type analog inverts percent range - 100% becomes 0% and 0% becomes 100%
Invert *bool `json:"invert,omitempty"`
// FactoryReset is true if input-triggered factory reset option is enabled, false otherwise
// (shown if applicable). (only for type switch, button).
FactorReset *bool `json:"factory_reset,omitempty"`
// ReportThr is the analog input report threshold in percent. The accepted range is
// device-specific, default [1.0..50.0]% unless specified otherwise.
ReportThr *float64 `json:"report_thr,omitempty"`
// RangeMap remaps 0%-100% range to values in array. The first value in the array is the
// min setting, and the second value is the max setting. Array elements are of type number.
// Float values are supported. The accepted range for values is from 0% to 100%. Default
// values are [0, 100]. max must be greater than min. Equality is supported.
RangeMap []float64 `json:"range_map,omitempty"`
// XPercent is value transformation config for status.percent.
XPercent *InputXPercent `json:"xpercent,omitempty"`
}
// InputXPercent is value transformation config for status.percent.
type InputXPercent struct {
// Expr is a JS expression containing x, where x is the raw value to be transformed
// (status.percent), for example "x+1". Accepted range: null or [0..100] chars. Both
// null and "" mean value transformation is disabled.
Expr *string `json:"expr,omitempty"`
// Unit of the transformed value (status.xpercent), for example, "m/s".
// Accepted range: null or [0..20] chars. Both null and "" mean value transformation
// is disabled.
Unit *string `json:"unit,omitempty"`
}
type InputSetConfigRequest struct {
// ID of the input component instance.
ID int `json:"id"`
// Configuration that the method takes.
Config InputConfig `json:"config"`
}
func (r *InputSetConfigRequest) Method() string {
return "Input.SetConfig"
}
func (r *InputSetConfigRequest) NewTypedResponse() *SetConfigResponse {
return &SetConfigResponse{}
}
func (r *InputSetConfigRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *InputSetConfigRequest) 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
}
type InputCheckExpressionRequest struct {
// Expr is the JS expression to evaluate.
Expr string `json:"expr,omitempty"`
// Inputs on which to apply expr. Elements are allowed to be null
Inputs []*float64 `json:"inputs,omitempty"`
}
func (r *InputCheckExpressionRequest) Method() string {
return "Input.CheckExpression"
}
func (r *InputCheckExpressionRequest) NewTypedResponse() *InputCheckExpressionResponse {
return &InputCheckExpressionResponse{}
}
func (r *InputCheckExpressionRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *InputCheckExpressionRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*InputCheckExpressionResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type InputCheckExpressionResponse struct {
Results []InputCheckExpressionResult
}
type InputCheckExpressionResult struct {
Input *float64
Output *float64
Error *string
}
func (r *InputCheckExpressionResult) UnmarshalJSON(b []byte) error {
var got []interface{}
if err := json.Unmarshal(b, &got); err != nil {
return err
}
if len(got) >= 1 {
if n, ok := got[0].(json.Number); ok {
f64, err := n.Float64()
if err != nil {
return fmt.Errorf("parsing input result: %w", err)
}
r.Input = Float64Ptr(f64)
} else if n, ok := got[0].(float64); ok {
r.Input = Float64Ptr(n)
}
}
if len(got) >= 2 {
if n, ok := got[1].(json.Number); ok {
f64, err := n.Float64()
if err != nil {
return fmt.Errorf("parsing output result: %w", err)
}
r.Output = Float64Ptr(f64)
} else if n, ok := got[1].(float64); ok {
r.Output = Float64Ptr(n)
}
}
if len(got) >= 3 {
if s, ok := got[2].(string); ok {
r.Error = StrPtr(s)
}
}
return nil
}