-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
160 lines (139 loc) · 3.23 KB
/
read.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
package airdetector
import (
"bytes"
"encoding/json"
"errors"
"log"
"net"
"strconv"
"time"
)
type packet []byte
type pType byte
const (
connect pType = 0x03
)
var tail = [6]byte{0xff, 0x23, 0x45, 0x4e, 0x44, 0x23}
type rawReading struct {
Humidity string `json:"humidity"`
Temperature string `json:"temperature"`
HCHO string `json:"hcho"`
PM25 string `json:"value"`
}
func (c rawReading) Reading() (res Reading, err error) {
res.Humidity, err = strconv.ParseFloat(c.Humidity, 64)
res.Temperature, err = strconv.ParseFloat(c.Temperature, 64)
res.HCHO, err = strconv.ParseFloat(c.HCHO, 64)
res.HCHO /= 1000.
res.PM25, err = strconv.Atoi(c.PM25)
return
}
// Reading is a device reading.
type Reading struct {
Humidity float64
Temperature float64
HCHO float64
PM25 int
}
func (c packet) MacAddr() (res [6]byte) {
copy(res[:], c[0x11:0x17])
return
}
func (c packet) Type() pType {
return pType(c[0x18])
}
func (c packet) IsReading() bool {
t := c.Type()
return t >= 0x4e && t <= 0x50
}
func (c packet) IsValid() bool {
return bytes.Equal(c[len(c)-len(tail):], tail[:])
}
func (c packet) Reading() (*Reading, error) {
if !c.IsReading() {
return nil, errors.New("attempt to get reading from a non-measurement")
}
res := new(rawReading)
err := json.Unmarshal(c[0x1c:len(c)-len(tail)], res)
if err != nil {
return nil, err
}
read, err := res.Reading()
if err != nil {
return nil, err
}
return &read, nil
}
type deviceConnection struct {
conn *net.TCPConn
deviceMAC [6]byte
}
// ReadingWithConnInfo is a reading with connection and device info included.
type ReadingWithConnInfo struct {
Reading
DeviceMAC [6]byte
RemoteAddr net.Addr
}
func (c deviceConnection) handle(output chan<- ReadingWithConnInfo) {
buf := make([]byte, 1024)
for {
c.conn.SetReadDeadline(time.Now().Add(5 * time.Minute))
l, err := c.conn.Read(buf)
if err != nil {
log.Println(err)
c.conn.Close()
return
}
data := packet(buf[:l])
if !data.IsValid() {
log.Printf("received invalid packet: %x", data)
return
}
if data.Type() == connect {
c.deviceMAC = data.MacAddr()
} else if c.deviceMAC != data.MacAddr() {
log.Printf("Received data packet of inconsistent mac address: expected %x got %x", c.deviceMAC, data.MacAddr())
}
if t := data.Type(); t != connect && !data.IsReading() {
log.Printf("Received unknown data packet of type %x, len %d", data.Type(), len(data))
}
if data.IsReading() {
reading, err := data.Reading()
if err != nil {
log.Println(err)
}
output <- ReadingWithConnInfo{
Reading: *reading,
DeviceMAC: c.deviceMAC,
RemoteAddr: c.conn.RemoteAddr(),
}
}
}
}
// Listen starts listening for connections
func Listen() (<-chan ReadingWithConnInfo, error) {
addr, err := net.ResolveTCPAddr("tcp", ":9000")
if err != nil {
return nil, err
}
n, err := net.ListenTCP("tcp", addr)
if err != nil {
return nil, err
}
output := make(chan ReadingWithConnInfo)
go func() {
for {
c, err := n.AcceptTCP()
if err != nil {
log.Println(err)
continue
}
log.Printf("Got connection from %s", c.RemoteAddr().String())
conn := deviceConnection{
conn: c,
}
go conn.handle(output)
}
}()
return output, nil
}