-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
62 lines (52 loc) · 1.36 KB
/
Copy pathjson.go
File metadata and controls
62 lines (52 loc) · 1.36 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
package main
import (
"encoding/json"
"log"
"time"
)
func (p packet) analyzeJSON(c chan dbRecord) {
// p.content is json data
var tags []map[string]interface{}
err := json.Unmarshal([]byte(p.content[:p.reqLen]), &tags)
if err != nil {
log.Println("json error:", err.Error())
log.Println(string(p.content[:p.reqLen]))
return
}
tnow := sqltime(time.Now().Unix())
for key, tag := range tags {
// first tag is the gateway, extract boxID
if key == 0 {
if str, ok := tag["mac"].(string); ok {
p.boxID = str
}
continue
}
var db dbRecord
db.dateTime = tnow
db.boxID = p.boxID
if str, ok := tag["mac"].(string); ok {
db.tagID = str
} else {
log.Println("mac failed:", tag["mac"])
continue
}
if num, ok := tag["rssi"].(float64); ok {
db.rssi = int(num)
} else {
db.rssi = -127
}
if num, ok := tag["battery"].(float64); ok {
db.batt = int(num)
} else {
db.batt = 0
}
if str, ok := tag["rawData"].(string); ok {
db.data = str
} else {
db.data = ""
}
// send dbRecord to database through channel
c <- db
}
}