-
Notifications
You must be signed in to change notification settings - Fork 6
/
encoder.go
161 lines (139 loc) · 4.17 KB
/
encoder.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
// Copyright © 2021 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"
"encoding/binary"
"io"
)
// Encoder encodes values to CayenneLPP.
type Encoder interface {
Grow(n int)
Bytes() []byte
Reset()
WriteTo(w io.Writer) (int64, error)
AddPort(channel uint8, value float64)
AddDigitalInput(channel, value uint8)
AddDigitalOutput(channel, value uint8)
AddAnalogInput(channel uint8, value float64)
AddAnalogOutput(channel uint8, value float64)
AddLuminosity(channel uint8, value uint16)
AddPresence(channel, value uint8)
AddTemperature(channel uint8, celcius float64)
AddRelativeHumidity(channel uint8, rh float64)
AddAccelerometer(channel uint8, x, y, z float64)
AddBarometricPressure(channel uint8, hpa float64)
AddGyrometer(channel uint8, x, y, z float64)
AddGPS(channel uint8, latitude, longitude, meters float64)
}
type encoder struct {
buf *bytes.Buffer
}
// NewEncoder instantiates an CayenneLPP encoder.
func NewEncoder() Encoder {
return &encoder{
buf: new(bytes.Buffer),
}
}
func (e *encoder) Grow(n int) {
e.buf.Grow(n)
}
func (e *encoder) Bytes() []byte {
return e.buf.Bytes()
}
func (e *encoder) Reset() {
e.buf.Reset()
}
func (e *encoder) WriteTo(w io.Writer) (int64, error) {
return e.buf.WriteTo(w)
}
func (e *encoder) AddPort(channel uint8, value float64) {
val := int16(value * 100)
e.buf.WriteByte(channel)
binary.Write(e.buf, binary.BigEndian, val)
}
func (e *encoder) AddDigitalInput(channel, value uint8) {
e.buf.WriteByte(channel)
e.buf.WriteByte(DigitalInput)
e.buf.WriteByte(value)
}
func (e *encoder) AddDigitalOutput(channel, value uint8) {
e.buf.WriteByte(channel)
e.buf.WriteByte(DigitalOutput)
e.buf.WriteByte(value)
}
func (e *encoder) AddAnalogInput(channel uint8, value float64) {
val := int16(value * 100)
e.buf.WriteByte(channel)
e.buf.WriteByte(AnalogInput)
binary.Write(e.buf, binary.BigEndian, val)
}
func (e *encoder) AddAnalogOutput(channel uint8, value float64) {
val := int16(value * 100)
e.buf.WriteByte(channel)
e.buf.WriteByte(AnalogOutput)
binary.Write(e.buf, binary.BigEndian, val)
}
func (e *encoder) AddLuminosity(channel uint8, value uint16) {
e.buf.WriteByte(channel)
e.buf.WriteByte(Luminosity)
binary.Write(e.buf, binary.BigEndian, value)
}
func (e *encoder) AddPresence(channel, value uint8) {
e.buf.WriteByte(channel)
e.buf.WriteByte(Presence)
e.buf.WriteByte(value)
}
func (e *encoder) AddTemperature(channel uint8, celcius float64) {
val := int16(celcius * 10)
e.buf.WriteByte(channel)
e.buf.WriteByte(Temperature)
binary.Write(e.buf, binary.BigEndian, val)
}
func (e *encoder) AddRelativeHumidity(channel uint8, rh float64) {
e.buf.WriteByte(channel)
e.buf.WriteByte(RelativeHumidity)
e.buf.WriteByte(uint8(rh * 2))
}
func (e *encoder) AddAccelerometer(channel uint8, x, y, z float64) {
valX := int16(x * 1000)
valY := int16(y * 1000)
valZ := int16(z * 1000)
e.buf.WriteByte(channel)
e.buf.WriteByte(Accelerometer)
binary.Write(e.buf, binary.BigEndian, valX)
binary.Write(e.buf, binary.BigEndian, valY)
binary.Write(e.buf, binary.BigEndian, valZ)
}
func (e *encoder) AddBarometricPressure(channel uint8, hpa float64) {
val := int16(hpa * 10)
e.buf.WriteByte(channel)
e.buf.WriteByte(BarometricPressure)
binary.Write(e.buf, binary.BigEndian, val)
}
func (e *encoder) AddGyrometer(channel uint8, x, y, z float64) {
valX := int16(x * 100)
valY := int16(y * 100)
valZ := int16(z * 100)
e.buf.WriteByte(channel)
e.buf.WriteByte(Gyrometer)
binary.Write(e.buf, binary.BigEndian, valX)
binary.Write(e.buf, binary.BigEndian, valY)
binary.Write(e.buf, binary.BigEndian, valZ)
}
func (e *encoder) AddGPS(channel uint8, latitude, longitude, meters float64) {
valLat := int32(latitude * 10000)
valLon := int32(longitude * 10000)
valAlt := int32(meters * 100)
e.buf.WriteByte(channel)
e.buf.WriteByte(GPS)
e.buf.WriteByte(byte(valLat >> 16))
e.buf.WriteByte(byte(valLat >> 8))
e.buf.WriteByte(byte(valLat))
e.buf.WriteByte(byte(valLon >> 16))
e.buf.WriteByte(byte(valLon >> 8))
e.buf.WriteByte(byte(valLon))
e.buf.WriteByte(byte(valAlt >> 16))
e.buf.WriteByte(byte(valAlt >> 8))
e.buf.WriteByte(byte(valAlt))
}