-
Notifications
You must be signed in to change notification settings - Fork 25
/
data.go
370 lines (324 loc) · 10.2 KB
/
data.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
l "gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/pkg/errors"
"github.com/robertkrimen/otto"
"github.com/scartill/giox"
xmat "github.com/scartill/giox/material"
log "github.com/sirupsen/logrus"
)
type encodedType struct {
Name string `toml:"name"`
Value float64 `toml:"value"`
MaxValue float64 `toml:"max_value"`
MinValue float64 `toml:"min_value"`
IsFloat bool `toml:"is_float"`
NumBytes int `toml:"num_bytes"`
}
//rawPayload holds optional raw bytes payload (hex encoded).
type rawPayload struct {
Payload string `toml:"payload"`
UseRaw bool `toml:"use_raw"`
Script string `toml:"script"`
UseEncoder bool `toml:"use_encoder"`
MaxExecTime int `toml:"max_exec_time"`
Obj string `toml:"js_object"`
FPort int `toml:"fport"`
}
var openScript bool
var defaultScript = `
// Encode encodes the given object into an array of bytes.
// - fPort contains the LoRaWAN fPort number
// - obj is an object, e.g. {"temperature": 22.5}
// The function must return an array of bytes, e.g. [225, 230, 255, 0]
function Encode(fPort, obj) {
return [];
}
`
type encodedTypeWidgets struct {
Name widget.Editor
NumBytes widget.Editor
IsFloat widget.Bool
DeleteButton widget.Clickable
Value widget.Editor
MaxValue widget.Editor
MinValue widget.Editor
}
// MaxEncodedTypes defines max number of simulated data types to send
const MaxEncodedTypes = 10
var (
rawBytesEditor widget.Editor
sendRawCheckbox widget.Bool
useEncoderCheckBox widget.Bool
openEncoderButton widget.Clickable
fPortEditor widget.Editor
intervalEditor widget.Editor
repeatCheckbox widget.Bool
sendDataButton widget.Clickable
stopDataButton widget.Clickable
addEncodedType widget.Clickable
encodedWidgets []encodedTypeWidgets
funcEditor widget.Editor
objEditor widget.Editor
clearScriptEditor widget.Clickable
closeScriptEditor widget.Clickable
)
func createDataForm() {
encodedWidgets = make([]encodedTypeWidgets, MaxEncodedTypes)
openScript = false
}
func dataResetGuiValues() {
rawBytesEditor.SetText(config.RawPayload.Payload)
sendRawCheckbox.Value = config.RawPayload.UseRaw
useEncoderCheckBox.Value = config.RawPayload.UseEncoder
fPortEditor.SetText(strconv.Itoa(config.RawPayload.FPort))
intervalEditor.SetText(fmt.Sprintf("%d", interval))
repeatCheckbox.Value = repeat
for i := 0; i < len(config.EncodedType); i++ {
encodedWidgets[i].Name.SetText(config.EncodedType[i].Name)
encodedWidgets[i].NumBytes.SetText(strconv.Itoa(config.EncodedType[i].NumBytes))
encodedWidgets[i].IsFloat.Value = config.EncodedType[i].IsFloat
encodedWidgets[i].Value.SetText(
fmt.Sprintf("%f", config.EncodedType[i].Value))
encodedWidgets[i].MaxValue.SetText(
fmt.Sprintf("%f", config.EncodedType[i].MaxValue))
encodedWidgets[i].MinValue.SetText(
fmt.Sprintf("%f", config.EncodedType[i].MinValue))
}
funcEditor.SetText(config.RawPayload.Script)
objEditor.SetText(config.RawPayload.Obj)
}
func dataForm(th *material.Theme) l.FlexChild {
config.RawPayload.Payload = rawBytesEditor.Text()
config.RawPayload.UseRaw = sendRawCheckbox.Value
config.RawPayload.UseEncoder = useEncoderCheckBox.Value
extractInt(&fPortEditor, &config.RawPayload.FPort, 0)
extractInt32(&intervalEditor, &interval, 1)
repeat = repeatCheckbox.Value
for i := 0; i < len(config.EncodedType); i++ {
config.EncodedType[i].Name = encodedWidgets[i].Name.Text()
extractInt(&encodedWidgets[i].NumBytes, &config.EncodedType[i].NumBytes, 0)
config.EncodedType[i].IsFloat = encodedWidgets[i].IsFloat.Value
extractFloat(&encodedWidgets[i].Value, &config.EncodedType[i].Value, 0)
extractFloat(&encodedWidgets[i].MaxValue, &config.EncodedType[i].MaxValue, 0)
extractFloat(&encodedWidgets[i].MinValue, &config.EncodedType[i].MinValue, 0)
}
config.RawPayload.Script = funcEditor.Text()
config.RawPayload.Obj = objEditor.Text()
for openEncoderButton.Clicked() {
openScript = true
}
if !running {
for sendDataButton.Clicked() {
go run()
}
}
if repeat && running {
for stopDataButton.Clicked() {
running = false
}
}
for addEncodedType.Clicked() {
et := &encodedType{
Name: "New type",
Value: 0,
MaxValue: 0,
MinValue: 0,
NumBytes: 0,
}
config.EncodedType = append(config.EncodedType, et)
log.Println("added new type")
}
for i := 0; i < len(config.EncodedType); i++ {
for encodedWidgets[i].DeleteButton.Clicked() {
if len(config.EncodedType) == 1 {
config.EncodedType = make([]*encodedType, 0)
} else {
copy(config.EncodedType[i:], config.EncodedType[i+1:])
config.EncodedType[len(config.EncodedType)-1] = &encodedType{}
config.EncodedType = config.EncodedType[:len(config.EncodedType)-1]
}
}
}
for clearScriptEditor.Clicked() {
config.RawPayload.Script = defaultScript
funcEditor.SetText(config.RawPayload.Script)
}
for closeScriptEditor.Clicked() {
openScript = false
}
widgets := make([]l.FlexChild, 0)
if !openScript {
widgets = append(widgets,
xmat.RigidSection(th, "Raw Data"),
xmat.RigidEditor(th, "Raw bytes in hex", "DEADBEEF", &rawBytesEditor),
xmat.RigidCheckBox(th, "Send raw", &sendRawCheckbox),
xmat.RigidCheckBox(th, "Use encoder", &useEncoderCheckBox),
xmat.RigidButton(th, "Open encoder", &openEncoderButton),
xmat.RigidEditor(th, "fPort", "<fport>", &fPortEditor),
l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Horizontal}.Layout(gtx,
xmat.RigidEditor(th, "Interval", "<inteval>", &intervalEditor),
xmat.RigidCheckBox(th, "Send every X seconds", &repeatCheckbox),
)
}),
)
if !running {
widgets = append(widgets, xmat.RigidButton(th, "Send data", &sendDataButton))
}
if repeat && running {
widgets = append(widgets, xmat.RigidButton(th, "Stop", &stopDataButton))
}
widgets = append(widgets,
xmat.RigidSection(th, "Encoded data"),
xmat.RigidButton(th, "Add encoded type", &addEncodedType),
)
for i := 0; i < len(config.EncodedType); i++ {
etw := &encodedWidgets[i]
widgets = append(widgets,
xmat.RigidSeparator(th, &giox.Separator{}),
l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Horizontal}.Layout(gtx,
xmat.RigidEditor(th, "Name", "<name>", &etw.Name),
xmat.RigidEditor(th, "Bytes", "<bytes>", &etw.NumBytes),
xmat.RigidCheckBox(th, "Float", &etw.IsFloat),
xmat.RigidButton(th, "Delete", &etw.DeleteButton),
)
}),
l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Horizontal}.Layout(gtx,
xmat.RigidEditor(th, "Value", "0", &etw.Value),
xmat.RigidEditor(th, "Max", "0", &etw.MaxValue),
xmat.RigidEditor(th, "Min", "0", &etw.MinValue),
)
}),
)
}
} else {
widgets = append(widgets,
xmat.RigidSection(th, "JS Encoder"),
xmat.RigidLabel(th, `If "Use encoder" is checked, you may write a function that accepts a JS object`),
xmat.RigidLabel(th, `and returns a byte array that'll be used as the raw bytes when sending data.`),
xmat.RigidLabel(th, `The function must be named Encode and accept a port and JS object.`),
xmat.RigidEditor(th, "Encoder Function", "JS", &funcEditor),
xmat.RigidLabel(th, `JS Object:`),
xmat.RigidEditor(th, "Encoder object", "JS", &objEditor),
xmat.RigidButton(th, "Clear", &clearScriptEditor),
xmat.RigidButton(th, "Close", &closeScriptEditor),
)
}
inset := l.Inset{Left: unit.Dp(30)}
return l.Rigid(func(gtx l.Context) l.Dimensions {
return inset.Layout(gtx, func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx, widgets...)
})
})
}
// EncodeToBytes encodes the payload to a slice of bytes.
// Taken from github.com/brocaar/lora-app-server.
func EncodeToBytes() (b []byte, err error) {
defer func() {
if caught := recover(); caught != nil {
err = fmt.Errorf("%s", caught)
}
}()
script := config.RawPayload.Script + "\n\nEncode(fPort, obj);\n"
vm := otto.New()
vm.Interrupt = make(chan func(), 1)
vm.SetStackDepthLimit(32)
var jsonData interface{}
err = json.Unmarshal([]byte(config.RawPayload.Obj), &jsonData)
if err != nil {
log.Errorf("couldn't unmarshal object: %s", err)
return nil, err
}
log.Debugf("JS object: %v", jsonData)
vm.Set("obj", jsonData)
vm.Set("fPort", config.RawPayload.FPort)
go func() {
time.Sleep(time.Duration(config.RawPayload.MaxExecTime) * time.Millisecond)
vm.Interrupt <- func() {
panic(errors.New("execution timeout"))
}
}()
var val otto.Value
val, err = vm.Run(script)
if err != nil {
return nil, errors.Wrap(err, "js vm error")
}
if !val.IsObject() {
return nil, errors.New("function must return an array")
}
var out interface{}
out, err = val.Export()
if err != nil {
return nil, errors.Wrap(err, "export error")
}
return interfaceToByteSlice(out)
}
// Taken from github.com/brocaar/lora-app-server.
func interfaceToByteSlice(obj interface{}) ([]byte, error) {
if obj == nil {
return nil, errors.New("value must not be nil")
}
if reflect.TypeOf(obj).Kind() != reflect.Slice {
return nil, errors.New("value must be an array")
}
s := reflect.ValueOf(obj)
l := s.Len()
var out []byte
for i := 0; i < l; i++ {
var b int64
el := s.Index(i).Interface()
switch v := el.(type) {
case int:
b = int64(v)
case uint:
b = int64(v)
case uint8:
b = int64(v)
case int8:
b = int64(v)
case uint16:
b = int64(v)
case int16:
b = int64(v)
case uint32:
b = int64(v)
case int32:
b = int64(v)
case uint64:
b = int64(v)
if uint64(b) != v {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %d", v)
}
case int64:
b = int64(v)
case float32:
b = int64(v)
if float32(b) != v {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %f", v)
}
case float64:
b = int64(v)
if float64(b) != v {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %f", v)
}
default:
return nil, fmt.Errorf("array value must be an array of ints or floats, got: %T", el)
}
if b < 0 || b > 255 {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %d", b)
}
out = append(out, byte(b))
}
return out, nil
}