-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
305 lines (278 loc) · 8.38 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
func analyzeBytes(rawData string, version int8) bool {
fmt.Println("_____________________________________________________________________________________________________________________")
//fmt.Println("rawData ", len(rawData))
if len(rawData) == 24 {
fmt.Println("Sensit v", version, "Downlink Frame")
return false
}
fmt.Println("Sensit v", version, "Uplink Frame")
parsed, err := strconv.ParseUint(rawData, 16, 32)
if err != nil {
log.Fatal(err)
}
data := fmt.Sprintf("%08b", parsed)
byte1 := data[0:8]
byte2 := data[8:16]
byte3 := data[16:24]
byte4 := data[24:32]
if version == 2 {
if len(data) == 25 { //Low battery MSB
fmt.Println("Sensit Low battery")
//TODO: Handle low battery bit shift
return false
}
//Byte 1
batteryMsb := data[0:1]
eventType, _ := strconv.ParseInt(data[1:3], 2, 8)
timeframe, _ := strconv.ParseInt(data[3:5], 2, 8)
mode, _ := strconv.ParseInt(data[5:8], 2, 8)
//Byte 2
temperatureMsb := data[8:12]
batteryLsb := data[12:16]
battData := []string{batteryMsb, batteryLsb}
battery, _ := strconv.ParseInt(strings.Join(battData, ""), 2, 8)
batVal := (float64(battery) * 0.05) + 2.7
//Byte 3
temperature := int64(0)
tempVal := float64(0)
reedSwitch := false
if mode == 0 || mode == 1 {
temperatureLsb := data[18:24]
tempData := []string{temperatureMsb, temperatureLsb}
fmt.Println("tempData", tempData)
temperature, _ := strconv.ParseInt(strings.Join(tempData, ""), 2, 16)
tempVal = (float64(temperature) - 200) / 8
if data[17] == 1 {
reedSwitch = true
}
} else {
temperature, _ = strconv.ParseInt(temperatureMsb, 2, 16)
tempVal = (float64(temperature) - 200) / 8
}
modeStr := ""
swRev := ""
humidity := float64(0.0)
light := float64(0.0)
switch mode {
case 0:
modeStr = "Button"
majorSwRev, _ := strconv.ParseInt(data[24:28], 2, 8)
minorSwRev, _ := strconv.ParseInt(data[28:32], 2, 8)
swRev = fmt.Sprintf("%d.%d", majorSwRev, minorSwRev)
case 1:
modeStr = "Temperature + Humidity"
humi, _ := strconv.ParseInt(data[24:32], 2, 16)
humidity = float64(humi) * 0.5
case 2:
modeStr = "Light"
lightVal, _ := strconv.ParseInt(data[18:24], 2, 8)
lightMulti, _ := strconv.ParseInt(data[17:18], 2, 8)
light = float64(lightVal) * 0.01
if lightMulti == 1 {
light = light * 8
}
case 3:
modeStr = "Door"
case 4:
modeStr = "Move"
case 5:
modeStr = "Reed switch"
default:
modeStr = ""
}
timeStr := ""
switch timeframe {
case 0:
timeStr = "10 mins"
case 1:
timeStr = "1 hour"
case 2:
timeStr = "6 hours"
case 3:
timeStr = "24 hours"
default:
timeStr = ""
}
typeStr := ""
switch eventType {
case 0:
typeStr = "Regular, no alert"
case 1:
typeStr = "Button call"
case 2:
typeStr = "Alert"
case 3:
typeStr = "New mode"
default:
timeStr = ""
}
// Outputing and printing values
fmt.Println("---------------------------------------------------------------------------------------------------------------------")
fmt.Println("Raw data :", byte1, byte2, byte3, byte4)
fmt.Println("Mode", mode, ":", modeStr, "\t\t", "Event type", eventType, ":", typeStr, "\t\t", "Timeframe", timeframe, ":", timeStr)
fmt.Println("Battery :", batVal, "V\t\t")
switch mode {
case 0:
fmt.Println("Temperature :", tempVal, "°C")
fmt.Println("v" + swRev)
case 1:
fmt.Println("Temperature :", tempVal, "°C")
fmt.Println(humidity, "% RH")
case 2:
fmt.Println(light, "lux")
alerts, _ := strconv.ParseInt(data[24:32], 2, 16)
fmt.Println("Number of alerts :", alerts)
case 3, 4, 5:
alerts, _ := strconv.ParseInt(data[24:32], 2, 16)
fmt.Println("Number of alerts :", alerts)
}
if reedSwitch {
fmt.Println("Reed switch on")
}
fmt.Println("_____________________________________________________________________________________________________________________")
} else { //version == 3
fmt.Println("_____________________________________________________________________________________________________________________")
modeStr := ""
fwRevVal := ""
humiVal := float32(0.0)
tempVal := float32(0.0)
lightVal := float32(0.0)
//fmt.Println("len(msg.Data):", len(rawData))
//Decoder itself
if len(rawData) <= 12 { //8 exactly, 4 bytes
fmt.Println("Sensit Uplink Message")
parsed, err := strconv.ParseUint(rawData, 16, 32)
if err != nil {
log.Fatal(err)
}
data := fmt.Sprintf("%08b", parsed)
fmt.Println("len(data):", len(data))
if len(data) == 25 { //Low battery MSB
fmt.Println("Sensit Low battery")
//TODO: Handle low battery bit shift
return false
}
//Byte 1 : 5b Battery & 3b reserved (0b110)
battery, _ := strconv.ParseInt(data[0:5], 2, 8)
batVal := (float64(battery) * 0.05) + 2.7
batVal = math.Round(batVal*100) / 100
// reserved, _ := strconv.ParseInt(data[5:8], 2, 8) //Should be 0b110
//Byte 2 : 5b Mode, 1b Alert Button, 2b data
mode, _ := strconv.ParseInt(data[8:13], 2, 8)
buttonStr := ""
if data[13:14] == "0" {
buttonStr = "Not pressed"
} else {
buttonStr = "Pressed"
}
evtVal := ""
switch mode {
case 0:
modeStr = "Standby"
fwRevMaj, _ := strconv.ParseInt(data[16:20], 2, 8)
fwRevMinJoin := []string{data[20:24], data[24:26]}
fwRevMin, _ := strconv.ParseInt(strings.Join(fwRevMinJoin, ""), 2, 16)
fwRevPatch, _ := strconv.ParseInt(data[26:32], 2, 8)
fwRevVal = fmt.Sprintf("%d.%d.%d", fwRevMaj, fwRevMin, fwRevPatch)
case 1:
modeStr = "Temperature + Humidity"
tempTab := []string{data[14:16], data[16:24]}
tempJoin := strings.Join(tempTab, "")
fmt.Println("tempJoin:", tempJoin)
temp, _ := strconv.ParseInt(tempJoin, 2, 16)
tempVal = (float32(temp) - 200) / 8
fmt.Println("MSB:", data[14:16], "\tLSB:", data[16:24])
fmt.Println("temp:", temp, "\t tempVal:", tempVal)
humi, _ := strconv.ParseInt(data[24:32], 2, 16)
humiVal = float32(humi) * 0.5
case 2:
modeStr = "Light"
lightJoin := []string{data[16:24], data[24:32]}
light, _ := strconv.ParseInt(strings.Join(lightJoin, ""), 2, 16)
lightVal = float32(light) / 96
case 3:
modeStr = "Door"
evtJoin := []string{data[16:24], data[24:32]}
eventCount, _ := strconv.ParseInt(strings.Join(evtJoin, ""), 2, 16)
switch eventCount {
case 1:
evtVal = "Calibration not done"
case 3:
evtVal = "Door closed"
case 4:
evtVal = "Door open"
}
case 4:
modeStr = "Vibration"
evtJoin := []string{data[16:24], data[24:32]}
eventCount, _ := strconv.ParseInt(strings.Join(evtJoin, ""), 2, 16)
switch eventCount {
case 0:
evtVal = "No vibration detected"
case 1:
evtVal = "Vibration detected"
}
case 5:
modeStr = "Magnet"
evtJoin := []string{data[16:24], data[24:32]}
eventCount, _ := strconv.ParseInt(strings.Join(evtJoin, ""), 2, 16)
switch eventCount {
case 0:
evtVal = "No magnet detected"
case 1:
evtVal = "Magnet detected"
}
default:
modeStr = ""
}
// Outputing and printing values
fmt.Println("---------------------------------------------------------------------------------------------------------------------")
fmt.Println("Raw data :", byte1, byte2, byte3, byte4)
fmt.Println("Mode", mode, ":", modeStr, "\t\t Button type:", buttonStr, ":\t\t Battery:", batVal, "V")
if evtVal != "" { //Modes 3,4,5
fmt.Println("Event: ", evtVal)
}
switch mode {
case 0:
fmt.Println("Software Revision :", tempVal, "°C")
fmt.Println("v" + fwRevVal)
case 1:
fmt.Println("Temperature :", tempVal, "°C")
fmt.Println(humiVal, "% RH")
case 2:
fmt.Println(lightVal, "lux")
alerts, _ := strconv.ParseInt(data[24:32], 2, 16)
fmt.Println("Number of alerts :", alerts)
case 3, 4, 5:
alerts, _ := strconv.ParseInt(data[24:32], 2, 16)
fmt.Println("Number of alerts :", alerts)
fmt.Println("Events:", evtVal)
}
fmt.Println("_____________________________________________________________________________________________________________________")
} else { //len: 24 exactly, 12 bytes
fmt.Println("Sensit Daily Downlink Message")
//TODO: Decode sensit downlink message
}
}
return true
}
func main() {
var version int8
if os.Args[1] == "v3" {
version = 3
} else {
version = 2
}
for i := 2; i <= len(os.Args[1:]); i++ {
analyzeBytes(os.Args[i], version)
}
}