-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.go
200 lines (167 loc) · 4.22 KB
/
sensor.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
package sds011
import (
"bytes"
"encoding/binary"
"errors"
"time"
)
type sensor struct {
conn Connection
id uint16
onMeasurement []chan Measurement
firmware time.Time
}
func NewSensor(conn Connection) *sensor {
return &sensor{
conn: conn,
id: 0xffff,
}
}
func (sensor *sensor) OnQuery(ch chan Measurement) {
for i := range sensor.onMeasurement {
if sensor.onMeasurement[i] == ch {
return
}
}
sensor.onMeasurement = append(sensor.onMeasurement, ch)
}
func (sensor *sensor) Listen() {
go func(handlers []chan Measurement) {
for true {
measure, err := sensor.queryResponse()
if err != nil {
for i := range handlers {
handlers[i] <- *measure
}
}
}
}(sensor.onMeasurement)
}
func (sensor *sensor) emit(measure Measurement) {
for i := range sensor.onMeasurement {
go func(handler chan Measurement) {
handler <- measure
}(sensor.onMeasurement[i])
}
}
func (sensor *sensor) write(cmd Command, data []byte) error {
var commandData = make([]byte, 12)
copy(commandData, data)
checksum := (sum(data) + byte(cmd) - 0x02) % 0xff
var ret bytes.Buffer
ret.Write([]byte{0xaa, 0xb4}) // Sender head
ret.WriteByte(byte(cmd))
ret.Write(commandData)
ret.Write(parseId(sensor.id)) // Device ID
ret.WriteByte(checksum) // Checksum
ret.WriteByte(0xab) // Tail
_, err := sensor.conn.Write(ret.Bytes())
return err
}
func (sensor *sensor) read() []byte {
responseHead := make([]byte, 1)
_, err := sensor.conn.Read(responseHead)
for err == nil && responseHead[0] != 0xaa {
_, err = sensor.conn.Read(responseHead)
}
response := make([]byte, 10)
count, err := sensor.conn.Read(response)
return response[:count]
}
func (sensor *sensor) Sleep(sleep bool) error {
var mode byte = 0x00
if !sleep {
mode = 0x01
}
err := sensor.write(SleepCommand, []byte{0x01, mode})
if err != nil {
return err
}
response := sensor.read()
if len(response) > 1 && response[1] == byte(SleepCommand) {
return nil
} else {
return errors.New("sleep bad response")
}
}
func (sensor *sensor) SetMode(mode Mode) error {
err := sensor.write(ModeCommand, []byte{0x01, byte(mode)})
if err != nil {
return err
}
response := sensor.read()
if len(response) > 0 && response[1] == byte(ModeCommand) {
return nil
} else {
return errors.New("set mode bad response")
}
}
func (sensor *sensor) queryResponse() (*Measurement, error) {
response := sensor.read()
if len(response) > 5 && response[0] == byte(0xc0) {
measure := Measurement{
PM2_5: float32(binary.LittleEndian.Uint16(response[1:3])) / 10.0,
PM10: float32(binary.LittleEndian.Uint16(response[3:5])) / 10.0,
}
sensor.emit(measure)
return &measure, nil
} else {
return nil, errors.New("query bad response")
}
}
func (sensor *sensor) Query() (*Measurement, error) {
err := sensor.write(QueryDataCommand, nil)
if err != nil {
return nil, err
}
return sensor.queryResponse()
}
func (sensor *sensor) GetFirmwareVersion() (time.Time, error) {
if !sensor.firmware.IsZero() {
return sensor.firmware, nil
}
err := sensor.write(FirmwareCommand, nil)
if err != nil {
return time.Time{}, err
}
response := sensor.read()
if len(response) > 4 && response[1] == byte(FirmwareCommand) {
sensor.firmware = time.Date(2000+int(response[2]), time.Month(response[3]), int(response[4]), 0, 0, 0, 0, time.UTC)
return sensor.firmware, nil
} else {
return time.Time{}, errors.New("get firmware bad response")
}
}
func (sensor *sensor) SetWorkingPeriod(minutes uint) error {
if minutes < 0 {
minutes = 0
} else if minutes > 30 {
minutes = 30
}
err := sensor.write(WorkingPeriodCommand, []byte{0x1, byte(minutes)})
if err != nil {
return err
}
response := sensor.read()
if len(response) > 1 && response[1] == byte(WorkingPeriodCommand) {
return nil
} else {
return errors.New("set working period bad response")
}
}
func (sensor *sensor) SetId(id uint16) error {
err := sensor.write(DeviceIdCommand, append(make([]byte, 10), parseId(id)...))
if err != nil {
return err
}
response := sensor.read()
if len(response) > 1 && response[1] == byte(DeviceIdCommand) {
sensor.id = id
return nil
} else {
return errors.New("set id bad response")
}
}
func (sensor *sensor) GetId() uint16 {
return sensor.id
}