-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
91 lines (73 loc) · 2.41 KB
/
decoder.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cayennelpp
import (
"bytes"
pb_handler "github.com/TheThingsNetwork/api/handler"
protocol "github.com/TheThingsNetwork/go-cayenne-lib/cayennelpp"
)
// Decoder is a CayenneLPP PayloadDecoder
type Decoder struct {
result map[string]interface{}
}
// Decodes decodes the CayenneLPP payload to fields
func (d *Decoder) Decode(payload []byte, fPort uint8) (map[string]interface{}, bool, error) {
decoder := protocol.NewDecoder(bytes.NewBuffer(payload))
d.result = make(map[string]interface{})
if err := decoder.DecodeUplink(d); err != nil {
return nil, false, err
}
return d.result, true, nil
}
// Log returns the log
func (d *Decoder) Log() []*pb_handler.LogEntry {
return nil
}
func (d *Decoder) DigitalInput(channel, value uint8) {
d.result[formatName(digitalInputKey, channel)] = value
}
func (d *Decoder) DigitalOutput(channel, value uint8) {
d.result[formatName(digitalOutputKey, channel)] = value
}
func (d *Decoder) AnalogInput(channel uint8, value float32) {
d.result[formatName(analogInputKey, channel)] = value
}
func (d *Decoder) AnalogOutput(channel uint8, value float32) {
d.result[formatName(analogOutputKey, channel)] = value
}
func (d *Decoder) Luminosity(channel uint8, value uint16) {
d.result[formatName(luminosityKey, channel)] = value
}
func (d *Decoder) Presence(channel, value uint8) {
d.result[formatName(presenceKey, channel)] = value
}
func (d *Decoder) Temperature(channel uint8, celcius float32) {
d.result[formatName(temperatureKey, channel)] = celcius
}
func (d *Decoder) RelativeHumidity(channel uint8, rh float32) {
d.result[formatName(relativeHumidityKey, channel)] = rh
}
func (d *Decoder) Accelerometer(channel uint8, x, y, z float32) {
d.result[formatName(accelerometerKey, channel)] = map[string]float32{
"x": x,
"y": y,
"z": z,
}
}
func (d *Decoder) BarometricPressure(channel uint8, hpa float32) {
d.result[formatName(barometricPressureKey, channel)] = hpa
}
func (d *Decoder) Gyrometer(channel uint8, x, y, z float32) {
d.result[formatName(gyrometerKey, channel)] = map[string]float32{
"x": x,
"y": y,
"z": z,
}
}
func (d *Decoder) GPS(channel uint8, latitude, longitude, altitude float32) {
d.result[formatName(gpsKey, channel)] = map[string]float32{
"latitude": latitude,
"longitude": longitude,
"altitude": altitude,
}
}