-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcheck.go
1724 lines (1643 loc) · 47.6 KB
/
dcheck.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
// Support for device performance verification
package main
import (
"fmt"
"math"
"strings"
"time"
ttdata "github.com/Safecast/safecast-go"
)
// GOALS:
// - makes sure it got at least some data from each kind of sensor
// - does some simple range check on each data value
// MeasurementStat contains information about a single measurement
type MeasurementStat struct {
DeviceUID string
IsSolarcastNano bool
Valid bool
Test bool
Firmware string
Uploaded time.Time
LoraModule string
FonaModule string
Transport string
LoraTransport bool
FonaTransport bool
TestMeasurement bool
MotionBegan string
ErrorsOpc uint32
ErrorsPms uint32
ErrorsPms2 uint32
ErrorsBme0 uint32
ErrorsBme1 uint32
ErrorsLora uint32
ErrorsFona uint32
ErrorsCommsPowerFails uint32
ErrorsOvercurrentEvents uint32
ErrorsCommsAntFails uint32
ErrorsGeiger uint32
ErrorsMax01 uint32
ErrorsUgps uint32
ErrorsLis uint32
ErrorsSpi uint32
ErrorsMtu uint32
ErrorsTwi uint32
ErrorsTwiInfo string
ErrorsConnectLora uint32
ErrorsConnectFona uint32
ErrorsConnectWireless uint32
ErrorsConnectGateway uint32
ErrorsConnectData uint32
ErrorsConnectService uint32
ErrorsCommsFailures uint32
ErrorsDeviceRestarts uint32
UptimeMinutes uint32
hasBat bool
BatWarning bool
hasEnv bool
EnvWarning bool
hasEnc bool
EncWarning bool
hasLndU7318 bool
hasLndC7318 bool
hasLndEC7128 bool
hasLndU712 bool
hasLndW78017 bool
GeigerWarning bool
hasPms bool
PmsWarning bool
hasPms2 bool
Pms2Warning bool
hasOpc bool
OpcWarning bool
BatV float64
BatI float64
BatS float64
EnvT float64
EnvH float64
EnvP float64
EncT float64
EncH float64
EncP float64
LndU float64
LndC float64
LndEC float64
LndU2 float64
LndW float64
Opc010 float64
Opc025 float64
Opc100 float64
Pms010 float64
Pms025 float64
Pms100 float64
Pms2010 float64
Pms2025 float64
Pms2100 float64
}
// MeasurementDataset contains stats about all measurements
type MeasurementDataset struct {
DeviceUID []string
IsSolarcastNano []bool
Firmware string
MultiFirmware bool
OldestUpload time.Time
NewestUpload time.Time
MinUploadGapSecs uint32
MaxUploadGapSecs uint32
GapsGt1week uint32
GapsGt1day uint32
GapsGt12hr uint32
GapsGt6hr uint32
GapsGt2hr uint32
GapsGt1hr uint32
GapsGt55m uint32
GapsGt50m uint32
GapsGt45m uint32
GapsGt40m uint32
GapsGt35m uint32
GapsGt30m uint32
GapsGt25m uint32
GapsGt20m uint32
GapsGt15m uint32
GapsGt10m uint32
GapsGt5m uint32
GapsGt0m uint32
Measurements uint32
TestMeasurements uint32
AnyTransport bool
Transports string
LoraTransports uint32
FonaTransports uint32
LoraModule string
FonaModule string
AnyErrors bool
ConnectErrors uint32
AnyPointcastErrors bool
PrevMotionBegan string
UniqueMotionBegans uint32
PrevErrorsOpc uint32
ThisErrorsOpc uint32
PrevErrorsPms uint32
ThisErrorsPms uint32
PrevErrorsPms2 uint32
ThisErrorsPms2 uint32
PrevErrorsBme0 uint32
ThisErrorsBme0 uint32
PrevErrorsBme1 uint32
ThisErrorsBme1 uint32
PrevErrorsLora uint32
ThisErrorsLora uint32
PrevErrorsFona uint32
ThisErrorsFona uint32
PrevErrorsCommsPowerFails uint32
ThisErrorsCommsPowerFails uint32
PrevErrorsOvercurrentEvents uint32
ThisErrorsOvercurrentEvents uint32
PrevErrorsCommsAntFails uint32
ThisErrorsCommsAntFails uint32
PrevErrorsGeiger uint32
ThisErrorsGeiger uint32
PrevErrorsMax01 uint32
ThisErrorsMax01 uint32
PrevErrorsUgps uint32
ThisErrorsUgps uint32
PrevErrorsLis uint32
ThisErrorsLis uint32
PrevErrorsSpi uint32
ThisErrorsSpi uint32
PrevErrorsMtu uint32
ThisErrorsMtu uint32
PrevErrorsTwi uint32
ThisErrorsTwi uint32
ErrorsTwiInfo string
ThisErrorsConnectLora uint32
PrevErrorsConnectLora uint32
ThisErrorsConnectFona uint32
PrevErrorsConnectFona uint32
ThisErrorsConnectWireless uint32
PrevErrorsConnectWireless uint32
ThisErrorsConnectGateway uint32
PrevErrorsConnectGateway uint32
ThisErrorsConnectData uint32
PrevErrorsConnectData uint32
ThisErrorsConnectService uint32
PrevErrorsConnectService uint32
MinErrorsCommsFailures uint32
ThisErrorsCommsFailures uint32
PrevErrorsCommsFailures uint32
MinErrorsDeviceRestarts uint32
ThisErrorsDeviceRestarts uint32
PrevErrorsDeviceRestarts uint32
PrevUptimeMinutes uint32
MaxUptimeMinutes uint32
Boots uint32
BatCount uint32
BatWarningCount uint32
BatWarningFirst time.Time
EnvCount uint32
EnvWarningCount uint32
EnvWarningFirst time.Time
EncCount uint32
EncWarningCount uint32
EncWarningFirst time.Time
LndU7318Count uint32
LndC7318Count uint32
LndEC7128Count uint32
LndU712Count uint32
LndW78017Count uint32
GeigerWarningCount int32
GeigerWarningFirst time.Time
PmsCount uint32
PmsWarningCount uint32
PmsWarningFirst time.Time
Pms2Count uint32
Pms2WarningCount uint32
Pms2WarningFirst time.Time
OpcCount uint32
OpcWarningCount uint32
OpcWarningFirst time.Time
LoOpc010 float64
HiOpc010 float64
LoOpc025 float64
HiOpc025 float64
LoOpc100 float64
HiOpc100 float64
LoPms010 float64
HiPms010 float64
LoPms025 float64
HiPms025 float64
LoPms100 float64
HiPms100 float64
LoPms2010 float64
HiPms2010 float64
LoPms2025 float64
HiPms2025 float64
LoPms2100 float64
HiPms2100 float64
LoLndU float64
HiLndU float64
LoLndC float64
HiLndC float64
LoLndEC float64
HiLndEC float64
LoLndU2 float64
HiLndU2 float64
LoLndW float64
HiLndW float64
LoBatV float64
HiBatV float64
LoBatI float64
HiBatI float64
LoBatS float64
HiBatS float64
LoEnvT float64
HiEnvT float64
LoEnvH float64
HiEnvH float64
LoEnvP float64
HiEnvP float64
LoEncT float64
HiEncT float64
LoEncH float64
HiEncH float64
LoEncP float64
HiEncP float64
}
// NewMeasurementDataset begins a new measurement by creating the object to record data
func NewMeasurementDataset() MeasurementDataset {
ds := MeasurementDataset{}
ds.LoraModule = "unidentified lora module"
ds.FonaModule = "unidentified fona module"
ds.Boots++
return ds
}
// CheckMeasurement verifies an individual measurement
func CheckMeasurement(sd ttdata.SafecastData) MeasurementStat {
stat := MeasurementStat{}
// Ignore old-format data that didn't have service_uploaded
if sd.Service == nil || sd.Service.UploadedAt == nil {
return stat
}
stat.Valid = true
// Device ID
stat.DeviceUID = sd.DeviceUID
stat.IsSolarcastNano = SafecastDeviceIsSolarcastNano(sd.DeviceID)
// Process service-related stats
if sd.Service != nil {
stat.Uploaded, _ = time.Parse("2006-01-02T15:04:05Z", *sd.Service.UploadedAt)
if sd.Service.Transport != nil {
str := strings.Split(*sd.Service.Transport, ":")
scheme := ""
if len(str) >= 1 {
scheme = str[0]
}
stat.Transport = scheme
switch scheme {
case "lora":
fallthrough
case "ttn-http":
fallthrough
case "ttn-mqqt":
stat.LoraTransport = true
case "device-udp":
fallthrough
case "device-tcp":
stat.FonaTransport = true
}
}
}
if sd.Dev != nil {
if sd.Dev.Test != nil {
stat.Test = *sd.Dev.Test
}
if sd.Dev.AppVersion != nil {
firmware := *sd.Dev.AppVersion
pieces := strings.Split(firmware, ".")
if len(pieces) >= 3 {
stat.Firmware = pieces[2]
}
}
if sd.Dev.ModuleLora != nil {
stat.LoraModule = *sd.Dev.ModuleLora
}
if sd.Dev.ModuleFona != nil {
stat.FonaModule = *sd.Dev.ModuleFona
}
if sd.Dev.ErrorsOpc != nil {
stat.ErrorsOpc = *sd.Dev.ErrorsOpc
}
if sd.Dev.ErrorsPms != nil {
stat.ErrorsPms = *sd.Dev.ErrorsPms
}
if sd.Dev.ErrorsPms2 != nil {
stat.ErrorsPms2 = *sd.Dev.ErrorsPms2
}
if sd.Dev.ErrorsBme0 != nil {
stat.ErrorsBme0 = *sd.Dev.ErrorsBme0
}
if sd.Dev.ErrorsBme1 != nil {
stat.ErrorsBme1 = *sd.Dev.ErrorsBme1
}
if sd.Dev.ErrorsLora != nil {
stat.ErrorsLora = *sd.Dev.ErrorsLora
}
if sd.Dev.ErrorsFona != nil {
stat.ErrorsFona = *sd.Dev.ErrorsFona
}
if sd.Dev.CommsPowerFails != nil {
stat.ErrorsCommsPowerFails = *sd.Dev.CommsPowerFails
}
if sd.Dev.OvercurrentEvents != nil {
stat.ErrorsOvercurrentEvents = *sd.Dev.OvercurrentEvents
}
if sd.Dev.CommsAntFails != nil {
stat.ErrorsCommsAntFails = *sd.Dev.CommsAntFails
}
if sd.Dev.ErrorsGeiger != nil {
stat.ErrorsGeiger = *sd.Dev.ErrorsGeiger
}
if sd.Dev.ErrorsMax01 != nil {
stat.ErrorsMax01 = *sd.Dev.ErrorsMax01
}
if sd.Dev.ErrorsUgps != nil {
stat.ErrorsUgps = *sd.Dev.ErrorsUgps
}
if sd.Dev.ErrorsLis != nil {
stat.ErrorsLis = *sd.Dev.ErrorsLis
}
if sd.Dev.ErrorsSpi != nil {
stat.ErrorsSpi = *sd.Dev.ErrorsSpi
}
if sd.Dev.ErrorsMtu != nil {
stat.ErrorsMtu = *sd.Dev.ErrorsMtu
}
if sd.Dev.ErrorsTwi != nil {
stat.ErrorsTwi = *sd.Dev.ErrorsTwi
}
if sd.Dev.ErrorsTwiInfo != nil {
stat.ErrorsTwiInfo = *sd.Dev.ErrorsTwiInfo
}
if sd.Dev.ErrorsConnectLora != nil {
stat.ErrorsConnectLora = *sd.Dev.ErrorsConnectLora
}
if sd.Dev.ErrorsConnectFona != nil {
stat.ErrorsConnectFona = *sd.Dev.ErrorsConnectFona
}
if sd.Dev.ErrorsConnectWireless != nil {
stat.ErrorsConnectWireless = *sd.Dev.ErrorsConnectWireless
}
if sd.Dev.ErrorsConnectGateway != nil {
stat.ErrorsConnectGateway = *sd.Dev.ErrorsConnectGateway
}
if sd.Dev.ErrorsConnectData != nil {
stat.ErrorsConnectData = *sd.Dev.ErrorsConnectData
}
if sd.Dev.ErrorsConnectService != nil {
stat.ErrorsConnectService = *sd.Dev.ErrorsConnectService
}
if sd.Dev.CommsFails != nil {
stat.ErrorsCommsFailures = *sd.Dev.CommsFails
}
if sd.Dev.DeviceRestarts != nil {
stat.ErrorsDeviceRestarts = *sd.Dev.DeviceRestarts
}
if sd.Dev.UptimeMinutes != nil {
stat.UptimeMinutes = *sd.Dev.UptimeMinutes
}
}
if sd.Bat != nil {
stat.hasBat = true
if sd.Bat.Voltage != nil {
val := *sd.Bat.Voltage
if val < 3.0 || val > 12.0 {
stat.BatWarning = true
}
stat.BatV = float64(val)
}
if sd.Bat.Current != nil {
val := *sd.Bat.Current
if val < -2000.0 || val > 50 {
stat.BatWarning = true
}
stat.BatI = float64(val)
}
// As of 2017-03-23 we no longer verify charge, for two reasons:
// 1) most devices require SOC training, and thus fall out of range
// 2) we don't actually use SOC for device performance throttling,
// instead using a calculation derived from the voltage. As such,
// SOC is largely for informational purposes.
if sd.Bat.Charge != nil {
val := *sd.Bat.Charge
if false {
if val < 25.0 || val > 200 {
stat.BatWarning = true
}
}
stat.BatS = float64(val)
}
}
if sd.Env != nil {
stat.hasEnv = true
if sd.Env.Temp != nil {
val := *sd.Env.Temp
if val < -25.0 || val > 80.0 {
stat.EnvWarning = true
}
stat.EnvT = float64(val)
}
if sd.Env.Humid != nil {
val := *sd.Env.Humid
if val < 0 || val > 110 {
stat.EnvWarning = true
}
stat.EnvH = float64(val)
}
if sd.Env.Press != nil {
val := *sd.Env.Press
stat.EnvP = float64(val)
}
}
if sd.Dev != nil {
if sd.Dev.Temp != nil {
stat.hasEnc = true
val := *sd.Dev.Temp
if val < -25.0 || val > 80.0 {
stat.EncWarning = true
}
stat.EncT = float64(val)
}
if sd.Dev.Humid != nil {
stat.hasEnc = true
val := *sd.Dev.Humid
if val < 0 || val > 110 {
stat.EncWarning = true
}
stat.EncH = float64(val)
}
if sd.Dev.Press != nil {
val := *sd.Dev.Press
stat.EncP = float64(val)
}
}
if sd.Loc != nil {
if sd.Loc.MotionBegan != nil {
stat.MotionBegan = *sd.Loc.MotionBegan
}
}
if sd.Lnd != nil {
if sd.Lnd.U7318 != nil {
stat.hasLndU7318 = true
val := *sd.Lnd.U7318
if val <= 0 || val > 500 {
stat.GeigerWarning = true
}
stat.LndU = float64(val)
}
if sd.Lnd.C7318 != nil {
stat.hasLndC7318 = true
val := *sd.Lnd.C7318
if val <= 0 || val > 500 {
stat.GeigerWarning = true
}
stat.LndC = float64(val)
}
if sd.Lnd.EC7128 != nil {
stat.hasLndEC7128 = true
val := *sd.Lnd.EC7128
if val <= 0 || val > 500 {
stat.GeigerWarning = true
}
stat.LndEC = float64(val)
}
if sd.Lnd.U712 != nil {
stat.hasLndU712 = true
val := *sd.Lnd.U712
if val <= 0 || val > 500 {
stat.GeigerWarning = true
}
stat.LndU2 = float64(val)
}
if sd.Lnd.W78017 != nil {
stat.hasLndW78017 = true
val := *sd.Lnd.W78017
if val <= 0 || val > 500 {
stat.GeigerWarning = true
}
stat.LndW = float64(val)
}
}
if sd.Pms != nil {
stat.hasPms = true
if sd.Pms.Pm01_0 != nil {
val := *sd.Pms.Pm01_0
if val < 0 || val > 600 {
stat.PmsWarning = true
}
stat.Pms010 = float64(val)
}
if sd.Pms.Std01_0 != nil && *sd.Pms.Std01_0 > 0 {
stat.PmsWarning = true
}
if sd.Pms.Pm02_5 != nil {
val := *sd.Pms.Pm02_5
if val < 0 || val > 600 {
stat.PmsWarning = true
}
stat.Pms025 = float64(val)
}
if sd.Pms.Std02_5 != nil && *sd.Pms.Std02_5 > 0 {
stat.PmsWarning = true
}
if sd.Pms.Pm10_0 != nil {
val := *sd.Pms.Pm10_0
if val < 0 || val > 600 {
stat.PmsWarning = true
}
stat.Pms100 = float64(val)
}
if sd.Pms.Std10_0 != nil && *sd.Pms.Std10_0 > 0 {
stat.PmsWarning = true
}
}
if sd.Pms2 != nil {
stat.hasPms2 = true
if sd.Pms2.Pm01_0 != nil {
val := *sd.Pms2.Pm01_0
if val < 0 || val > 600 {
stat.Pms2Warning = true
}
stat.Pms2010 = float64(val)
}
if sd.Pms2.Std01_0 != nil && *sd.Pms2.Std01_0 > 0 {
stat.Pms2Warning = true
}
if sd.Pms2.Pm02_5 != nil {
val := *sd.Pms2.Pm02_5
if val < 0 || val > 600 {
stat.Pms2Warning = true
}
stat.Pms2025 = float64(val)
}
if sd.Pms2.Std02_5 != nil && *sd.Pms2.Std02_5 > 0 {
stat.Pms2Warning = true
}
if sd.Pms2.Pm10_0 != nil {
val := *sd.Pms2.Pm10_0
if val < 0 || val > 600 {
stat.Pms2Warning = true
}
stat.Pms2100 = float64(val)
}
if sd.Pms2.Std10_0 != nil && *sd.Pms2.Std10_0 > 0 {
stat.Pms2Warning = true
}
}
if sd.Opc != nil {
stat.hasOpc = true
if sd.Opc.Pm01_0 != nil {
val := *sd.Opc.Pm01_0
if val < 0 || val > 600 {
stat.OpcWarning = true
}
stat.Opc010 = float64(val)
}
if sd.Opc.Std01_0 != nil && *sd.Opc.Std01_0 > 0 {
stat.OpcWarning = true
}
if sd.Opc.Pm02_5 != nil {
val := *sd.Opc.Pm02_5
if val < 0 || val > 600 {
stat.OpcWarning = true
}
stat.Opc025 = float64(val)
}
if sd.Opc.Std02_5 != nil && *sd.Opc.Std02_5 > 0 {
stat.OpcWarning = true
}
if sd.Opc.Pm10_0 != nil {
val := *sd.Opc.Pm10_0
if val < 0 || val > 600 {
stat.OpcWarning = true
}
stat.Opc100 = float64(val)
}
if sd.Opc.Std10_0 != nil && *sd.Opc.Std10_0 > 0 {
stat.OpcWarning = true
}
}
// Done
return stat
}
// AggregateMeasurementIntoDataset checks an individual measurement
func AggregateMeasurementIntoDataset(ds *MeasurementDataset, stat MeasurementStat) {
// Only record valid stats
if !stat.Valid {
return
}
ds.Measurements++
if stat.Test {
ds.TestMeasurements++
}
// Device ID
if stat.DeviceUID != "" {
foundDevice := false
for _, d := range ds.DeviceUID {
if d == stat.DeviceUID {
foundDevice = true
break
}
}
if !foundDevice {
ds.IsSolarcastNano = append(ds.IsSolarcastNano, stat.IsSolarcastNano)
ds.DeviceUID = append(ds.DeviceUID, stat.DeviceUID)
}
}
// Firmware
if stat.Firmware != "" {
foundFirmware := false
for _, c := range strings.Split(ds.Firmware, ",") {
if c == stat.Firmware {
foundFirmware = true
break
}
}
if !foundFirmware {
if ds.Firmware == "" {
ds.Firmware = stat.Firmware
} else {
ds.Firmware = ds.Firmware + "," + stat.Firmware
ds.MultiFirmware = true
}
}
}
// Init oldest and newest
if ds.Measurements == 1 {
ds.OldestUpload = stat.Uploaded
ds.NewestUpload = stat.Uploaded
}
// Timing. Note that it is possible to have uploads that are out-of-order because of
// multi-instance server concurrency. For those measurements we will still check the
// sensor readings, but we won't factor the measurement into our "gap" calculations.
if stat.Uploaded.Sub(ds.NewestUpload) >= 0 {
SecondsGap := uint32(stat.Uploaded.Sub(ds.NewestUpload) / time.Second)
MinutesGap := SecondsGap / 60
if SecondsGap != 0 && MinutesGap == 0 {
SecondsGap = 1
} else {
SecondsGap = MinutesGap * 60
}
if SecondsGap > 0 {
if ds.MinUploadGapSecs == 0 || SecondsGap < ds.MinUploadGapSecs {
ds.MinUploadGapSecs = SecondsGap
}
if SecondsGap > ds.MaxUploadGapSecs {
ds.MaxUploadGapSecs = SecondsGap
}
}
if SecondsGap > 0 {
ds.GapsGt0m++
}
if SecondsGap > 60*5 {
ds.GapsGt5m++
}
if SecondsGap > 60*10 {
ds.GapsGt10m++
}
if SecondsGap > 60*15 {
ds.GapsGt15m++
}
if SecondsGap > 60*20 {
ds.GapsGt20m++
}
if SecondsGap > 60*25 {
ds.GapsGt25m++
}
if SecondsGap > 60*30 {
ds.GapsGt30m++
}
if SecondsGap > 60*35 {
ds.GapsGt35m++
}
if SecondsGap > 60*40 {
ds.GapsGt40m++
}
if SecondsGap > 60*45 {
ds.GapsGt45m++
}
if SecondsGap > 60*50 {
ds.GapsGt50m++
}
if SecondsGap > 60*55 {
ds.GapsGt55m++
}
if SecondsGap > 60*60*1 {
ds.GapsGt1hr++
}
if SecondsGap > 60*60*2 {
ds.GapsGt2hr++
}
if SecondsGap > 60*60*6 {
ds.GapsGt6hr++
}
if SecondsGap > 60*60*12 {
ds.GapsGt12hr++
}
if SecondsGap > 60*60*24*1 {
ds.GapsGt1day++
}
if SecondsGap > 60*60*24*7 {
ds.GapsGt1week++
}
ds.NewestUpload = stat.Uploaded
}
// Motion
if stat.MotionBegan != "" {
if stat.MotionBegan != ds.PrevMotionBegan {
ds.PrevMotionBegan = stat.MotionBegan
ds.UniqueMotionBegans++
}
}
// Transport
if stat.LoraTransport {
ds.LoraTransports++
ds.AnyTransport = true
}
if stat.FonaTransport {
ds.FonaTransports++
ds.AnyTransport = true
}
foundTransport := false
for _, c := range strings.Split(ds.Transports, ",") {
if c == stat.Transport {
foundTransport = true
break
}
}
if !foundTransport {
if ds.Transports == "" {
ds.Transports = stat.Transport
} else {
ds.Transports = ds.Transports + "," + stat.Transport
}
}
if stat.LoraModule != "" {
ds.LoraModule = stat.LoraModule
}
if stat.FonaModule != "" {
ds.FonaModule = stat.FonaModule
}
// Errors this session
if stat.ErrorsOpc > ds.ThisErrorsOpc {
ds.ThisErrorsOpc = stat.ErrorsOpc
ds.AnyErrors = true
}
if stat.ErrorsPms > ds.ThisErrorsPms {
ds.ThisErrorsPms = stat.ErrorsPms
ds.AnyErrors = true
}
if stat.ErrorsPms2 > ds.ThisErrorsPms2 {
ds.ThisErrorsPms2 = stat.ErrorsPms2
ds.AnyErrors = true
}
if stat.ErrorsBme0 > ds.ThisErrorsBme0 {
ds.ThisErrorsBme0 = stat.ErrorsBme0
ds.AnyErrors = true
}
if stat.ErrorsBme1 > ds.ThisErrorsBme1 {
ds.ThisErrorsBme1 = stat.ErrorsBme1
ds.AnyErrors = true
}
if stat.ErrorsLora > ds.ThisErrorsLora {
ds.ThisErrorsLora = stat.ErrorsLora
ds.AnyErrors = true
}
if stat.ErrorsFona > ds.ThisErrorsFona {
ds.ThisErrorsFona = stat.ErrorsFona
ds.AnyErrors = true
}
if stat.ErrorsCommsPowerFails > ds.ThisErrorsCommsPowerFails {
ds.ThisErrorsCommsPowerFails = stat.ErrorsCommsPowerFails
ds.AnyErrors = true
}
if stat.ErrorsOvercurrentEvents > ds.ThisErrorsOvercurrentEvents {
ds.ThisErrorsOvercurrentEvents = stat.ErrorsOvercurrentEvents
ds.AnyErrors = true
}
if stat.ErrorsCommsAntFails > ds.ThisErrorsCommsAntFails {
ds.ThisErrorsCommsAntFails = stat.ErrorsCommsAntFails
ds.AnyErrors = true
}
if stat.ErrorsGeiger > ds.ThisErrorsGeiger {
ds.ThisErrorsGeiger = stat.ErrorsGeiger
ds.AnyErrors = true
}
if stat.ErrorsMax01 > ds.ThisErrorsMax01 {
ds.ThisErrorsMax01 = stat.ErrorsMax01
ds.AnyErrors = true
}
if stat.ErrorsUgps > ds.ThisErrorsUgps {
ds.ThisErrorsUgps = stat.ErrorsUgps
ds.AnyErrors = true
}
if stat.ErrorsLis > ds.ThisErrorsLis {
ds.ThisErrorsLis = stat.ErrorsLis
ds.AnyErrors = true
}
if stat.ErrorsSpi > ds.ThisErrorsSpi {
ds.ThisErrorsSpi = stat.ErrorsSpi
ds.AnyErrors = true
}
if stat.ErrorsMtu > ds.ThisErrorsMtu {
ds.ThisErrorsMtu = stat.ErrorsMtu
ds.AnyErrors = true
}
if stat.ErrorsTwi > ds.ThisErrorsTwi {
ds.ThisErrorsTwi = stat.ErrorsTwi
ds.AnyErrors = true
}
for _, staterr := range strings.Split(stat.ErrorsTwiInfo, " ") {
foundError := false
if staterr != "" {
for _, c := range strings.Split(ds.ErrorsTwiInfo, ",") {
if c == staterr {
foundError = true
break
}
}
if !foundError {
ds.AnyErrors = true
if ds.ErrorsTwiInfo == "" {
ds.ErrorsTwiInfo = staterr
} else {
ds.ErrorsTwiInfo = ds.ErrorsTwiInfo + "," + staterr
}
}
}
}
// Connect errors
if stat.ErrorsConnectLora > ds.ThisErrorsConnectLora {
ds.ThisErrorsConnectLora = stat.ErrorsConnectLora
ds.ConnectErrors++
}
if stat.ErrorsConnectFona > ds.ThisErrorsConnectFona {
ds.ThisErrorsConnectFona = stat.ErrorsConnectFona
ds.ConnectErrors++
}
if stat.ErrorsConnectWireless > ds.ThisErrorsConnectWireless {
ds.ThisErrorsConnectWireless = stat.ErrorsConnectWireless
ds.ConnectErrors++
}
if stat.ErrorsConnectGateway > ds.ThisErrorsConnectGateway {
ds.ThisErrorsConnectGateway = stat.ErrorsConnectGateway
ds.ConnectErrors++
}
if stat.ErrorsConnectData > ds.ThisErrorsConnectData {
ds.ThisErrorsConnectData = stat.ErrorsConnectData
ds.ConnectErrors++
}
if stat.ErrorsConnectService > ds.ThisErrorsConnectService {
ds.ThisErrorsConnectService = stat.ErrorsConnectService
ds.ConnectErrors++
}
if stat.ErrorsCommsFailures != 0 {
if ds.MinErrorsCommsFailures == 0 || (ds.MinErrorsCommsFailures != 0 && stat.ErrorsCommsFailures < ds.MinErrorsCommsFailures) {
ds.MinErrorsCommsFailures = stat.ErrorsCommsFailures
}
}
if stat.ErrorsCommsFailures > ds.ThisErrorsCommsFailures {
ds.ThisErrorsCommsFailures = stat.ErrorsCommsFailures
ds.AnyPointcastErrors = true
}
if stat.ErrorsDeviceRestarts != 0 {
if ds.MinErrorsDeviceRestarts == 0 || (ds.MinErrorsDeviceRestarts != 0 && stat.ErrorsDeviceRestarts < ds.MinErrorsDeviceRestarts) {
ds.MinErrorsDeviceRestarts = stat.ErrorsDeviceRestarts
}
}
if stat.ErrorsDeviceRestarts > ds.ThisErrorsDeviceRestarts {
ds.ThisErrorsDeviceRestarts = stat.ErrorsDeviceRestarts
ds.AnyPointcastErrors = true
}
// Uptime
if stat.UptimeMinutes != 0 {
if stat.UptimeMinutes > ds.MaxUptimeMinutes {
ds.MaxUptimeMinutes = stat.UptimeMinutes
}
if stat.UptimeMinutes < ds.PrevUptimeMinutes {
ds.Boots++
// Aggregate and reset error totals
aggregateErrors(ds)
}
ds.PrevUptimeMinutes = stat.UptimeMinutes
}
// Sensors
if stat.hasBat {
ds.BatCount++
if ds.BatCount == 1 {
ds.LoBatV = stat.BatV
ds.HiBatV = stat.BatV
ds.LoBatI = stat.BatI
ds.HiBatI = stat.BatI
ds.LoBatS = stat.BatS
ds.HiBatS = stat.BatS
} else {
ds.LoBatV = math.Min(ds.LoBatV, stat.BatV)
ds.HiBatV = math.Max(ds.HiBatV, stat.BatV)
ds.LoBatI = math.Min(ds.LoBatI, stat.BatI)
ds.HiBatI = math.Max(ds.HiBatI, stat.BatI)
ds.LoBatS = math.Min(ds.LoBatS, stat.BatS)