-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat.go
516 lines (452 loc) · 12.3 KB
/
reformat.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Safecast v1-to-current data structure reformatting
package main
import (
"fmt"
"strconv"
"strings"
"time"
ttdata "github.com/Safecast/safecast-go"
olc "github.com/google/open-location-code/go"
)
// SafecastDeviceIsSolarcastNano determines if this is a Solarcast Nano
func SafecastDeviceIsSolarcastNano(deviceid uint32) bool {
sn, _ := sheetDeviceIDToSN(deviceid)
if sn == "" {
return false
}
u64, err := strconv.ParseUint(sn, 10, 32)
if err == nil {
if u64 >= 33000 && u64 < 34000 {
return true
}
}
return false
}
// SafecastDeviceUIDType returns just the scheme
func SafecastDeviceUIDType(deviceUID string) (scheme string) {
return strings.Split(deviceUID, ":")[0]
}
// SafecastDeviceType returns the type of a Safecast device AS NUMBERED in our
// V2 address space
func SafecastDeviceType(deviceid uint32) (programmatic string, display string) {
// Pointcast
if deviceid >= 10000 && deviceid <= 29999 {
return "pointcast", "Pointcast"
}
// Exception for pointcast device 100
if deviceid == 100 {
return "pointcast", "Pointcast"
}
// Air
if deviceid >= 50000 && deviceid <= 59999 {
return "safecast-air", "Safecast Air"
}
// GeigieCast
if deviceid >= 60000 && deviceid <= 64999 {
return "geigiecast", "bGeigiecast"
}
// GeigieCastZen (sub-divided GeigeiCast namespace on 12-Jun-2024)
if deviceid >= 65000 && deviceid <= 69999 {
return "geigiecast-zen", "bGeigiecast-zen"
}
// nGeigie
if deviceid > 0 && deviceid <= 999 {
return "ngeigie", "nGeigie"
}
// Unknown device type - must be a solarcast
return "", "Solarcast"
}
// SafecastV1DeviceType returns the type of a device AS NATIVELY NUMBERED
// by pointcast, safecast-air, or ngeigie devices
func SafecastV1DeviceType(deviceid uint32) (devicetype string, devicename string, v2DeviceID uint32) {
// For standard V1 pointcast numbering space
if deviceid >= 100000 && deviceid <= 299999 {
return "pointcast", "Pointcast", deviceid / 10
}
// Exception for pointcast device 100x
if deviceid >= 1000 && deviceid <= 1999 {
return "pointcast", "Pointcast", deviceid / 10
}
// Air
if deviceid >= 50000 && deviceid <= 59999 {
return "safecast-air", "Safecast Air", deviceid
}
// GeigieCast
if deviceid >= 60000 && deviceid <= 64999 {
return "geigiecast", "bGeigiecast", deviceid
}
// GeigieCast Zen (sub-divided GeigeiCast namespace on 12-Jun-2024)
if deviceid >= 65000 && deviceid <= 69999 {
return "geigiecast-zen", "bGeigiecast-zen", deviceid
}
// nGeigie
if deviceid > 0 && deviceid <= 999 {
return "ngeigie", "nGeigie", deviceid
}
return "", "Solarcast", deviceid
}
// SafecastReformatFromV1 reformats a special V1 payload to Current
func SafecastReformatFromV1(v1 *SafecastDataV1, isTestMeasurement bool) (deviceid uint32, devtype string, data ttdata.SafecastData) {
var sd ttdata.SafecastData
var v1DeviceID uint32
// Required field
if v1.DeviceID == nil {
fmt.Printf("*** Reformat: Missing Device ID\n")
return 0, "", sd
}
// Fetch the V1 device ID and place it into a var so we can manipulate it
v1DeviceID = *v1.DeviceID
// Catch attempts to use DeviceID == 0 by placing it into somewhere we can watch.
// We're putting this here 2017-04-12 because we're observing that nGeigie Device #40
// is occasionally sending:
// {"longitude":"140.9917","latitude":"37.5635","device_id":"0","value":"0",
// "unit":"cpm","height":"5","devicetype_id":"Pointcast V1"}
if v1DeviceID == 0 {
return 0, "", sd
}
// Special-case a single nGeigie that had been partially converted to Pointcast firmware,
// a bug fix we put in on 2017-04-13 at Rob's guidance.
if v1DeviceID == 48 {
v1DeviceID = 40
}
// Detect what range it is within, and process the conversion differently,
// rejecting non-reformattable devices
devicetype, devicename, v2DeviceID := SafecastV1DeviceType(v1DeviceID)
if devicetype == "" {
fmt.Printf("*** Reformat: unsuccessful attempt to reformat Device ID %d\n", v1DeviceID)
return 0, "", sd
}
// THIS is where we determine sensor types based on device ID
tubeType := "unknown"
if v2DeviceID == 100 || v2DeviceID == 63 || v2DeviceID == 54 {
tubeType = "U712"
} else if v2DeviceID == 78 || v2DeviceID == 20105 {
tubeType = "W78017"
} else if devicetype == "ngeigie" {
tubeType = "U7318"
} else if devicetype == "geigiecast" || devicetype == "geigiecast-zen" {
tubeType = "U7318"
} else if devicetype == "pointcast" && v1DeviceID%10 == 1 {
tubeType = "U7318"
} else if devicetype == "pointcast" && v1DeviceID%10 == 2 {
tubeType = "EC7128"
}
// Device ID
sd.DeviceID = v2DeviceID
urn := fmt.Sprintf("%s:%d", devicetype, v2DeviceID)
sd.DeviceUID = urn
// Generate the device class
sd.DeviceClass = devicetype
// Device Serial Number
sn, _ := sheetDeviceIDToSN(v2DeviceID)
if sn != "" {
u64, err2 := strconv.ParseUint(sn, 10, 32)
if err2 == nil {
sn = fmt.Sprintf("#%d", u64)
}
// snstr := fmt.Sprintf("%s %s", devicename, sn) // 2020-07-31 rob added device type to SN
_ = devicename
sd.DeviceSN = sn
}
// Captured
if v1.CapturedAt != nil {
// Correct for badly formatted safecast-air data of the form 2017-9-7T2:3:4Z
t, err := time.Parse("2006-1-2T15:4:5Z", *v1.CapturedAt)
if err != nil {
sd.CapturedAt = v1.CapturedAt
} else {
s := t.UTC().Format("2006-01-02T15:04:05Z")
sd.CapturedAt = &s
}
}
// Loc
if v1.Latitude != nil && v1.Longitude != nil {
var loc ttdata.Loc
loc.Lat = v1.Latitude
loc.Lon = v1.Longitude
if v1.Height != nil {
alt := float64(*v1.Height)
loc.Alt = &alt
}
// 11 digits is 3m accuracy
Olc := olc.Encode(float64(*loc.Lat), float64(*loc.Lon), 11)
loc.Olc = &Olc
sd.Loc = &loc
}
// Reverse-engineer Unit/Value to yield the good stuff
if v1.Unit != nil && v1.Value != nil {
switch strings.ToLower(*v1.Unit) {
case "pm1":
var opc ttdata.Opc
pm := *v1.Value
opc.Pm01_0 = &pm
sd.Opc = &opc
case "pm2.5":
var opc ttdata.Opc
pm := *v1.Value
opc.Pm02_5 = &pm
sd.Opc = &opc
case "pm10":
var opc ttdata.Opc
pm := *v1.Value
opc.Pm10_0 = &pm
sd.Opc = &opc
case "humd%":
var env ttdata.Env
humid := *v1.Value
env.Humid = &humid
sd.Env = &env
case "celcius":
fallthrough
case "tempc":
var env ttdata.Env
temp := *v1.Value
env.Temp = &temp
sd.Env = &env
case "cpm":
var lnd ttdata.Lnd
cpm := *v1.Value
// Special case for missing tube on this sensor
if v1DeviceID == 1001 && cpm == 0 {
return 0, "", sd
}
switch tubeType {
case "U7318":
lnd.U7318 = &cpm
sd.Lnd = &lnd
case "U712":
lnd.U712 = &cpm
sd.Lnd = &lnd
case "EC7128":
lnd.EC7128 = &cpm
sd.Lnd = &lnd
case "W78017":
lnd.W78017 = &cpm
sd.Lnd = &lnd
default:
fmt.Printf("*** Reformat: Received CPM for unrecognized device %d\n", sd.DeviceID)
}
case "status":
// The value is the temp
var env ttdata.Env
TempC := *v1.Value
env.Temp = &TempC
sd.Env = &env
// Parse subfields
var bat ttdata.Bat
var dobat = false
var dev ttdata.Dev
var dodev = false
unrecognized := ""
status := ""
if v1.DeviceTypeID != nil {
status = *v1.DeviceTypeID
}
fields := strings.Split(status, ",")
for v := range fields {
field := strings.Split(fields[v], ":")
switch field[0] {
case "Battery Voltage":
f64, _ := strconv.ParseFloat(field[1], 64)
if f64 != 0 {
bat.Voltage = &f64
dobat = true
}
case "Fails":
u64, _ := strconv.ParseUint(field[1], 10, 32)
u32 := uint32(u64)
if u32 != 0 {
dev.CommsFails = &u32
dodev = true
}
case "Restarts":
u64, _ := strconv.ParseUint(field[1], 10, 32)
u32 := uint32(u64)
if u32 != 0 {
dev.DeviceRestarts = &u32
dodev = true
}
case "FreeRam":
u64, _ := strconv.ParseUint(field[1], 10, 32)
u32 := uint32(u64)
if u32 != 0 {
dev.FreeMem = &u32
dodev = true
}
case "NTP count":
u64, _ := strconv.ParseUint(field[1], 10, 32)
u32 := uint32(u64)
if u32 != 0 {
dev.NTPCount = &u32
dodev = true
}
case "Last failure":
var LastFailure = field[1]
dev.LastFailure = &LastFailure
dodev = true
case "DeviceID":
case "Temperature":
default:
if unrecognized == "" {
unrecognized = "{"
} else {
unrecognized = unrecognized + ","
}
unrecognized = unrecognized + "\"" + field[0] + "\":\"" + field[1] + "\""
}
}
// If we found unrecognized fields, emit them
if unrecognized != "" {
unrecognized = unrecognized + "}"
dev.Status = &unrecognized
dodev = true
}
// Include in the uploads
if dobat {
sd.Bat = &bat
}
if dodev {
sd.Dev = &dev
}
default:
fmt.Printf("*** Reformat Warning ***\n*** %s id=%d Unit %s = Value %f UNRECOGNIZED\n", devicetype, v1DeviceID, *v1.Unit, *v1.Value)
}
}
// Test
if isTestMeasurement {
if sd.Dev == nil {
var dev ttdata.Dev
sd.Dev = &dev
}
sd.Dev.Test = &isTestMeasurement
}
return uint32(sd.DeviceID), devicetype, sd
}
// SafecastReformatToV1 reformats a special V1 payload to Current
func SafecastReformatToV1(sd ttdata.SafecastData) (v1Data1 *SafecastDataV1ToEmit, v1Data2 *SafecastDataV1ToEmit, v1Data9 *SafecastDataV1ToEmit, err error) {
// Start under the assumption that we will generate all three
sd1 := &SafecastDataV1ToEmit{}
sd2 := &SafecastDataV1ToEmit{}
sd9 := &SafecastDataV1ToEmit{}
// Process the fields common to all outputs
if sd.CapturedAt != nil {
sd1.CapturedAt = sd.CapturedAt
}
if sd.Loc != nil {
if sd.Loc.Lat != nil {
value := fmt.Sprintf("%f", *sd.Loc.Lat)
sd1.Latitude = &value
}
if sd.Loc.Lon != nil {
value := fmt.Sprintf("%f", *sd.Loc.Lon)
sd1.Longitude = &value
}
if sd.Loc.Alt != nil {
value := fmt.Sprintf("%d", int(*sd.Loc.Alt))
sd1.Height = &value
}
}
*sd2 = *sd1
*sd9 = *sd1
// Generate the device IDs. If this isn't a solarcast device, don't generate anything.
if sd.DeviceID == 0 {
return
}
deviceType, _ := SafecastDeviceType(sd.DeviceID)
if deviceType != "" {
return
}
// Solarcast. Return S/N as V1 device ID
id, _ := sheetDeviceIDToSN(sd.DeviceID)
if id == "" {
return
}
// Gross workaround for Rob's 2020-07-31 change to format of device SN's for old devices
if strings.Contains(id, "#") {
tmp := strings.Split(id, "#")
id = tmp[len(tmp)-1]
}
u64, err2 := strconv.ParseUint(id, 10, 32)
if err2 != nil || u64 == 0 {
return
}
id1 := fmt.Sprintf("%d", u64*10+1)
sd1.DeviceID = &id1
id2 := fmt.Sprintf("%d", u64*10+2)
sd2.DeviceID = &id2
id9 := fmt.Sprintf("%d", u64*10+9)
sd9.DeviceID = &id9
// Generate the first geiger tube value
if sd.Lnd != nil {
if sd.U7318 != nil {
value := fmt.Sprintf("%d", int(*sd.Lnd.U7318))
sd1.Value = &value
unit := "cpm"
sd1.Unit = &unit
deviceType := "lnd_7318u"
sd1.DeviceTypeID = &deviceType
} else if sd.U712 != nil {
value := fmt.Sprintf("%d", int(*sd.Lnd.U712))
sd1.Value = &value
unit := "cpm"
sd1.Unit = &unit
deviceType := "lnd_712u"
sd1.DeviceTypeID = &deviceType
} else if sd.W78017 != nil {
value := fmt.Sprintf("%d", int(*sd.Lnd.W78017))
sd1.Value = &value
unit := "cpm"
sd1.Unit = &unit
deviceType := "lnd_78017w"
sd1.DeviceTypeID = &deviceType
} else {
sd1 = nil
}
} else {
sd1 = nil
}
// Generate the second geiger tube value
if sd.Lnd != nil {
if sd.C7318 != nil {
value := fmt.Sprintf("%d", int(*sd.Lnd.C7318))
sd2.Value = &value
unit := "cpm"
sd2.Unit = &unit
deviceType := "lnd_7318c"
sd2.DeviceTypeID = &deviceType
} else if sd.EC7128 != nil {
value := fmt.Sprintf("%d", int(*sd.Lnd.EC7128))
sd2.Value = &value
unit := "cpm"
sd2.Unit = &unit
deviceType := "lnd_7128ec"
sd2.DeviceTypeID = &deviceType
} else {
sd2 = nil
}
} else {
sd2 = nil
}
// Generate the temp value
if sd.Env != nil && sd.Env.Temp != nil {
value := fmt.Sprintf("%.1f", *sd.Env.Temp)
sd9.Value = &value
unit := "celcius"
sd9.Unit = &unit
info := fmt.Sprintf("DeviceID:%s,Temperature:%.1f", id, *sd.Env.Temp)
if sd.Bat != nil && sd.Bat.Voltage != nil {
info += fmt.Sprintf(",Battery Voltage:%1f", *sd.Bat.Voltage)
}
sd9.DeviceTypeID = &info
} else {
sd9 = nil
}
// Done
v1Data1 = sd1
v1Data2 = sd2
v1Data9 = sd9
return
}