-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.go
More file actions
66 lines (54 loc) · 1.24 KB
/
binary.go
File metadata and controls
66 lines (54 loc) · 1.24 KB
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
package main
import (
"encoding/binary"
"encoding/hex"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
)
func (p packet) analyzeBLE(c chan dbRecord) {
var nrBLE uint16 = 0
if p.content[0] == 187 && p.content[p.reqLen-1] == 221 && p.reqLen > 22 {
p.boxID = hex.EncodeToString(p.content[6:12])
nrBLE = binary.BigEndian.Uint16(p.content[12:14])
} else {
log.Println("Ignoring corrupted packet")
return
}
ble := p.content[14:]
for i := nrBLE; i > 0; i-- {
var d uint16
tnow := sqltime(time.Now().Unix())
tagID := hex.EncodeToString(ble[0:6])
nrBLEdata := binary.BigEndian.Uint16(ble[6:8])
if len(ble) > 8 {
ble = ble[8:]
} else {
return
}
for d = 0; d < nrBLEdata; d++ {
rawLen := int(ble[0])
if len(ble) < rawLen+2 {
log.Println("Packet length exceeded")
return
}
var db dbRecord
db.dateTime = tnow
db.boxID = p.boxID
db.tagID = tagID
if rawLen != 0 {
db.data = hex.EncodeToString(ble[1 : 1+rawLen])
}
db.rssi = int(ble[rawLen+2])
// we don't get the battery status in binary mode, so send fake value 100%
db.batt = 100
// send dbRecord to database through channel
c <- db
if len(ble) > rawLen+3 {
ble = ble[rawLen+3:]
} else {
return
}
}
}
}