-
Notifications
You must be signed in to change notification settings - Fork 29
/
historyrecord.go
853 lines (728 loc) · 24 KB
/
historyrecord.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
package medtronic
import (
"fmt"
"time"
)
// HistoryRecordType represents a history record type.
type HistoryRecordType byte
//go:generate stringer -type HistoryRecordType
// Events stored in the pump's history pages.
const (
Bolus HistoryRecordType = 0x01
Prime HistoryRecordType = 0x03
Alarm HistoryRecordType = 0x06
DailyTotal HistoryRecordType = 0x07
BasalProfileBefore HistoryRecordType = 0x08
BasalProfileAfter HistoryRecordType = 0x09
BGCapture HistoryRecordType = 0x0A
SensorAlarm HistoryRecordType = 0x0B
ClearAlarm HistoryRecordType = 0x0C
ChangeBasalPattern HistoryRecordType = 0x14
TempBasalDuration HistoryRecordType = 0x16
ChangeTime HistoryRecordType = 0x17
NewTime HistoryRecordType = 0x18
LowBattery HistoryRecordType = 0x19
BatteryChange HistoryRecordType = 0x1A
SetAutoOff HistoryRecordType = 0x1B
PrepareInsulinChange HistoryRecordType = 0x1C
SuspendPump HistoryRecordType = 0x1E
ResumePump HistoryRecordType = 0x1F
SelfTest HistoryRecordType = 0x20
Rewind HistoryRecordType = 0x21
ClearSettings HistoryRecordType = 0x22
EnableChildBlock HistoryRecordType = 0x23
MaxBolus HistoryRecordType = 0x24
EnableRemote HistoryRecordType = 0x26
MaxBasal HistoryRecordType = 0x2C
EnableBolusWizard HistoryRecordType = 0x2D
Unknown2E HistoryRecordType = 0x2E
BolusWizard512 HistoryRecordType = 0x2F
UnabsorbedInsulin512 HistoryRecordType = 0x30
ChangeBGReminder HistoryRecordType = 0x31
SetAlarmClockTime HistoryRecordType = 0x32
TempBasalRate HistoryRecordType = 0x33
LowReservoir HistoryRecordType = 0x34
AlarmClock HistoryRecordType = 0x35
ChangeMeterID HistoryRecordType = 0x36
BGReceived512 HistoryRecordType = 0x39
ConfirmInsulinChange HistoryRecordType = 0x3A
SensorStatus HistoryRecordType = 0x3B
EnableMeter HistoryRecordType = 0x3C
BGReceived HistoryRecordType = 0x3F
MealMarker HistoryRecordType = 0x40
ExerciseMarker HistoryRecordType = 0x41
InsulinMarker HistoryRecordType = 0x42
OtherMarker HistoryRecordType = 0x43
EnableSensorAutoCal HistoryRecordType = 0x44
ChangeBolusWizardSetup HistoryRecordType = 0x4F
SensorSetup HistoryRecordType = 0x50
Sensor51 HistoryRecordType = 0x51
Sensor52 HistoryRecordType = 0x52
ChangeSensorAlarm HistoryRecordType = 0x53
Sensor54 HistoryRecordType = 0x54
Sensor55 HistoryRecordType = 0x55
ChangeSensorAlert HistoryRecordType = 0x56
ChangeBolusStep HistoryRecordType = 0x57
BolusWizardSetup HistoryRecordType = 0x5A
BolusWizard HistoryRecordType = 0x5B
UnabsorbedInsulin HistoryRecordType = 0x5C
SaveSettings HistoryRecordType = 0x5D
EnableVariableBolus HistoryRecordType = 0x5E
ChangeEasyBolus HistoryRecordType = 0x5F
EnableBGReminder HistoryRecordType = 0x60
EnableAlarmClock HistoryRecordType = 0x61
ChangeTempBasalType HistoryRecordType = 0x62
ChangeAlarmType HistoryRecordType = 0x63
ChangeTimeFormat HistoryRecordType = 0x64
ChangeReservoirWarning HistoryRecordType = 0x65
EnableBolusReminder HistoryRecordType = 0x66
SetBolusReminderTime HistoryRecordType = 0x67
DeleteBolusReminderTime HistoryRecordType = 0x68
BolusReminder HistoryRecordType = 0x69
DeleteAlarmClockTime HistoryRecordType = 0x6A
DailyTotal515 HistoryRecordType = 0x6C
DailyTotal522 HistoryRecordType = 0x6D
DailyTotal523 HistoryRecordType = 0x6E
ChangeCarbUnits HistoryRecordType = 0x6F
BasalProfileStart HistoryRecordType = 0x7B
ConnectOtherDevices HistoryRecordType = 0x7C
ChangeOtherDevice HistoryRecordType = 0x7D
ChangeMarriage HistoryRecordType = 0x81
DeleteOtherDevice HistoryRecordType = 0x82
EnableCaptureEvent HistoryRecordType = 0x83
)
var decode = map[HistoryRecordType]decoder{
Bolus: decodeBolus,
Prime: decodePrime,
Alarm: decodeAlarm,
DailyTotal: decodeDailyTotal,
BasalProfileBefore: decodeBasalProfileBefore,
BasalProfileAfter: decodeBasalProfileAfter,
BGCapture: decodeBGCapture,
SensorAlarm: decodeSensorAlarm,
ClearAlarm: decodeClearAlarm,
ChangeBasalPattern: decodeChangeBasalPattern,
TempBasalDuration: decodeTempBasalDuration,
ChangeTime: decodeChangeTime,
NewTime: decodeNewTime,
LowBattery: decodeLowBattery,
BatteryChange: decodeBatteryChange,
SetAutoOff: decodeSetAutoOff,
PrepareInsulinChange: decodePrepareInsulinChange,
SuspendPump: decodeSuspendPump,
ResumePump: decodeResumePump,
SelfTest: decodeSelfTest,
Rewind: decodeRewind,
ClearSettings: decodeClearSettings,
EnableChildBlock: decodeEnableChildBlock,
MaxBolus: decodeMaxBolus,
EnableRemote: decodeEnableRemote,
MaxBasal: decodeMaxBasal,
EnableBolusWizard: decodeEnableBolusWizard,
Unknown2E: decodeUnknown2E,
BolusWizard512: decodeBolusWizard512,
UnabsorbedInsulin512: decodeUnabsorbedInsulin512,
ChangeBGReminder: decodeChangeBGReminder,
SetAlarmClockTime: decodeSetAlarmClockTime,
TempBasalRate: decodeTempBasalRate,
LowReservoir: decodeLowReservoir,
AlarmClock: decodeAlarmClock,
ChangeMeterID: decodeChangeMeterID,
BGReceived512: decodeBGReceived512,
ConfirmInsulinChange: decodeConfirmInsulinChange,
SensorStatus: decodeSensorStatus,
EnableMeter: decodeEnableMeter,
BGReceived: decodeBGReceived,
MealMarker: decodeMealMarker,
ExerciseMarker: decodeExerciseMarker,
InsulinMarker: decodeInsulinMarker,
OtherMarker: decodeOtherMarker,
EnableSensorAutoCal: decodeEnableSensorAutoCal,
ChangeBolusWizardSetup: decodeChangeBolusWizardSetup,
SensorSetup: decodeSensorSetup,
Sensor51: decodeSensor51,
Sensor52: decodeSensor52,
ChangeSensorAlarm: decodeChangeSensorAlarm,
Sensor54: decodeSensor54,
Sensor55: decodeSensor55,
ChangeSensorAlert: decodeChangeSensorAlert,
ChangeBolusStep: decodeChangeBolusStep,
BolusWizardSetup: decodeBolusWizardSetup,
BolusWizard: decodeBolusWizard,
UnabsorbedInsulin: decodeUnabsorbedInsulin,
SaveSettings: decodeSaveSettings,
EnableVariableBolus: decodeEnableVariableBolus,
ChangeEasyBolus: decodeChangeEasyBolus,
EnableBGReminder: decodeEnableBGReminder,
EnableAlarmClock: decodeEnableAlarmClock,
ChangeTempBasalType: decodeChangeTempBasalType,
ChangeAlarmType: decodeChangeAlarmType,
ChangeTimeFormat: decodeChangeTimeFormat,
ChangeReservoirWarning: decodeChangeReservoirWarning,
EnableBolusReminder: decodeEnableBolusReminder,
SetBolusReminderTime: decodeSetBolusReminderTime,
DeleteBolusReminderTime: decodeDeleteBolusReminderTime,
BolusReminder: decodeBolusReminder,
DeleteAlarmClockTime: decodeDeleteAlarmClockTime,
DailyTotal515: decodeDailyTotal515,
DailyTotal522: decodeDailyTotal522,
DailyTotal523: decodeDailyTotal523,
ChangeCarbUnits: decodeChangeCarbUnits,
BasalProfileStart: decodeBasalProfileStart,
ConnectOtherDevices: decodeConnectOtherDevices,
ChangeOtherDevice: decodeChangeOtherDevice,
ChangeMarriage: decodeChangeMarriage,
DeleteOtherDevice: decodeDeleteOtherDevice,
EnableCaptureEvent: decodeEnableCaptureEvent,
}
//AlarmCode represents a pump alarm code.
type AlarmCode byte
//go:generate stringer -type AlarmCode
const (
BatteryOutLimitExceeded AlarmCode = 0x03
NoDelivery AlarmCode = 0x04
BatteryDepleted AlarmCode = 0x05
AutoOff AlarmCode = 0x06
DeviceReset AlarmCode = 0x10
ReprogramError AlarmCode = 0x3D
EmptyReservoir AlarmCode = 0x3E
)
type (
decoder func([]byte, Family) HistoryRecord
HistoryRecord struct {
Data []byte
Time time.Time
Info interface{} `json:",omitempty"`
}
History []HistoryRecord
GlucoseRecord struct {
Units GlucoseUnitsType
Glucose Glucose
MeterID string `json:",omitempty"`
}
CarbRecord struct {
Units CarbUnitsType
Carbs Carbs
}
TempBasalRecord struct {
Type TempBasalType
Value interface{}
}
BasalProfileStartRecord struct {
ProfileIndex int
BasalRate BasalRate
}
PrimeRecord struct {
Fixed Insulin
Manual Insulin
}
BolusRecord struct {
Programmed Insulin
Amount Insulin
Unabsorbed Insulin
Duration Duration // non-zero for square wave bolus
}
BolusWizardRecord struct {
GlucoseInput Glucose
CarbInput Carbs
GlucoseUnits GlucoseUnitsType
CarbUnits CarbUnitsType
TargetLow Glucose
TargetHigh Glucose
Sensitivity Glucose // glucose reduction per insulin unit
CarbRatio Ratio // 10x grams/unit or 1000x units/exchange
Correction Insulin
Food Insulin
Unabsorbed Insulin
Bolus Insulin
}
BolusWizardConfig struct {
Ratios CarbRatioSchedule
Sensitivities InsulinSensitivitySchedule
Targets GlucoseTargetSchedule
InsulinAction Duration
}
BolusWizardSetupRecord struct {
Before BolusWizardConfig
After BolusWizardConfig
}
UnabsorbedBolus struct {
Bolus Insulin
Age Duration
}
UnabsorbedBolusHistory []UnabsorbedBolus
UnknownRecordTypeError struct {
Data []byte
}
)
// Type returns the history record type.
func (r HistoryRecord) Type() HistoryRecordType {
return HistoryRecordType(r.Data[0])
}
func decodeBase(data []byte, family Family) HistoryRecord {
return HistoryRecord{
Time: decodeTime(data[2:7]),
Data: data[:7],
}
}
func decodeEnable(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = data[1] != 0
return r
}
func decodeDailyTotalDate(data []byte, family Family) HistoryRecord {
return HistoryRecord{
Time: decodeDate(data[1:3]),
Data: data[:3],
}
}
func extendDecoder(orig decoder) func(int) decoder {
return func(length int) decoder {
return func(data []byte, family Family) HistoryRecord {
r := orig(data, family)
r.Data = data[:length]
return r
}
}
}
var decodeBaseN = extendDecoder(decodeBase)
var decodeEnableN = extendDecoder(decodeEnable)
var decodeDailyTotalN = extendDecoder(decodeDailyTotalDate)
func decodeValue(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = int(data[1])
return r
}
func decodeInsulin(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = byteToInsulin(data[1], 23)
return r
}
func decodeBolus(data []byte, family Family) HistoryRecord {
if family <= 22 {
return HistoryRecord{
Info: BolusRecord{
Programmed: byteToInsulin(data[1], family),
Amount: byteToInsulin(data[2], family),
Duration: halfHoursToDuration(data[3]),
},
Time: decodeTime(data[4:9]),
Data: data[:9],
}
}
return HistoryRecord{
Info: BolusRecord{
Programmed: twoByteInsulin(data[1:3], family),
Amount: twoByteInsulin(data[3:5], family),
Unabsorbed: twoByteInsulin(data[5:7], family),
Duration: halfHoursToDuration(data[7]),
},
Time: decodeTime(data[8:13]),
Data: data[:13],
}
}
func decodePrime(data []byte, family Family) HistoryRecord {
return HistoryRecord{
Info: PrimeRecord{
Fixed: byteToInsulin(data[2], 22),
Manual: byteToInsulin(data[4], 22),
},
Time: decodeTime(data[5:10]),
Data: data[:10],
}
}
func decodeAlarm(data []byte, family Family) HistoryRecord {
r := HistoryRecord{
Time: decodeTime(data[4:9]),
Data: data[:9],
}
r.Info = AlarmCode(data[1])
return r
}
func decodeDailyTotal(data []byte, family Family) HistoryRecord {
t := decodeDate(data[5:7])
total := twoByteInsulin(data[3:5], 23)
if family <= 22 {
return HistoryRecord{
Time: t,
Info: total,
Data: data[:7],
}
}
return HistoryRecord{
Time: t,
Info: total,
Data: data[:10],
}
}
// Note that this is a different format than the response to BasalRates.
func decodeBasalRate(data []byte) BasalRate {
return BasalRate{
Start: halfHoursToTimeOfDay(data[0]),
Rate: twoByteInsulinLE(data[1:3]),
}
}
func decodeBasalProfile(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
body := data[7:]
var sched BasalRateSchedule
for i := 0; i < 144; i += 3 {
if body[i] == 0x3F {
break
}
b := decodeBasalRate(body[i : i+3])
// Don't stop if the 00:00 rate happens to be zero.
if i > 0 && b.Start == 0 && b.Rate == 0 {
break
}
sched = append(sched, b)
}
r.Info = sched
r.Data = data[:152]
return r
}
var decodeBasalProfileBefore = decodeBasalProfile
var decodeBasalProfileAfter = decodeBasalProfile
func decodeBGCapture(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
units := GlucoseUnitsType((data[4] >> 5) & 0x3)
r.Info = GlucoseRecord{
Units: units,
Glucose: intToGlucose(int(data[4]>>7)<<9|int(data[6]>>7)<<8|int(data[1]), units),
}
return r
}
func decodeSensorAlarm(data []byte, family Family) HistoryRecord {
r := HistoryRecord{
Time: decodeTime(data[3:8]),
Data: data[:8],
}
r.Info = int(data[1])
return r
}
var decodeClearAlarm = decodeValue
var decodeChangeBasalPattern = decodeValue
func decodeTempBasalDuration(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = halfHoursToDuration(data[1])
return r
}
var decodeChangeTime = decodeBase
var decodeNewTime = decodeBase
var decodeLowBattery = decodeBase
var decodeBatteryChange = decodeBase
func decodeSetAutoOff(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = hoursToDuration(data[1])
return r
}
var decodePrepareInsulinChange = decodeBase
var decodeSuspendPump = decodeBase
var decodeResumePump = decodeBase
var decodeSelfTest = decodeBase
var decodeRewind = decodeBase
var decodeClearSettings = decodeBase
var decodeEnableChildBlock = decodeEnable
var decodeMaxBolus = decodeInsulin
var decodeEnableRemote = decodeEnableN(21)
var decodeMaxBasal = decodeInsulin
var decodeEnableBolusWizard = decodeEnable
var decodeUnknown2E = decodeBaseN(107)
func decodeBolusWizard512(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
bg := int(data[1])
body := data[7:]
bgU := GlucoseUnitsType((body[1] >> 6) & 0x3)
carbU := CarbUnitsType((body[1] >> 4) & 0x3)
info := BolusWizardRecord{
GlucoseInput: intToGlucose(int(body[1]&0x3)<<8|bg, bgU),
CarbInput: Carbs(int(body[1]&0xC)<<6 | int(body[0])),
GlucoseUnits: bgU,
CarbUnits: carbU,
TargetLow: byteToGlucose(body[4], bgU),
Sensitivity: byteToGlucose(body[3], bgU),
CarbRatio: intToRatio(int(body[2]), carbU, family),
Correction: intToInsulin(int(body[7])+int(body[5]&0xF), 12),
Food: byteToInsulin(body[6], 12),
Unabsorbed: byteToInsulin(body[9], 12),
Bolus: byteToInsulin(body[11], 12),
}
info.TargetHigh = info.TargetLow
r.Info = info
r.Data = data[:19]
return r
}
var decodeUnabsorbedInsulin512 = decodeUnabsorbedInsulin
var decodeChangeBGReminder = decodeBase
var decodeSetAlarmClockTime = decodeBase
func decodeTempBasalRate(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
tb := TempBasalRecord{
Type: TempBasalType(data[7] >> 3),
}
if tb.Type == Absolute {
tb.Value = intToInsulin(int(data[7]&0x7)<<8|int(data[1]), 23)
} else {
tb.Value = int(data[1])
}
r.Info = tb
r.Data = data[:8]
return r
}
func decodeLowReservoir(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = byteToInsulin(data[1], 22)
return r
}
var decodeAlarmClock = decodeBase
var decodeChangeMeterID = decodeBaseN(21)
var decodeBGReceived512 = decodeBGReceived
var decodeConfirmInsulinChange = decodeEnable
var decodeSensorStatus = decodeEnable
var decodeEnableMeter = decodeEnableN(21)
func decodeBGReceived(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Data = data[:10]
units := MgPerDeciLiter // are units encoded somehow?
r.Info = GlucoseRecord{
Units: units,
Glucose: intToGlucose(int(data[1])<<3|int(data[4]>>5), units),
MeterID: fmt.Sprintf("%06X", data[7:10]),
}
return r
}
func decodeMealMarker(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Data = data[:9]
r.Info = CarbRecord{
Carbs: Carbs(int(data[1])<<8 | int(data[7])),
Units: CarbUnitsType(data[8] & 0x3),
}
return r
}
var decodeExerciseMarker = decodeBaseN(8)
func decodeInsulinMarker(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Data = data[:8]
r.Info = intToInsulin(int(data[4]&0x60)<<3|int(data[1]), 22)
return r
}
var decodeOtherMarker = decodeBase
var decodeEnableSensorAutoCal = decodeBase
var decodeChangeBolusWizardSetup = decodeBaseN(39)
func decodeSensorSetup(data []byte, family Family) HistoryRecord {
var d decoder
if family >= 51 {
d = decodeBaseN(41)
} else {
d = decodeBaseN(37)
}
return d(data, family)
}
var decodeSensor51 = decodeBase
var decodeSensor52 = decodeBase
var decodeChangeSensorAlarm = decodeBaseN(8)
var decodeSensor54 = decodeBaseN(64)
var decodeSensor55 = decodeBaseN(55)
var decodeChangeSensorAlert = decodeBaseN(12)
var decodeChangeBolusStep = decodeBase
func decodeBolusWizardConfig(data []byte, family Family) BolusWizardConfig {
const numEntries = 8
r := BolusWizardConfig{}
carbUnits := CarbUnitsType(data[0] & 0x3)
if carbUnits != Exchanges {
// Pumps can return 0 when first turned on.
carbUnits = Grams
}
bgUnits := GlucoseUnitsType((data[0] >> 2) & 0x3)
if bgUnits != MMolPerLiter {
// Pumps can return 0 when first turned on.
bgUnits = MgPerDeciLiter
}
step := carbRatioStep(family)
r.Ratios = decodeCarbRatioSchedule(data[2:2+numEntries*step], carbUnits, family)
data = data[2+numEntries*step:]
r.Sensitivities = decodeInsulinSensitivitySchedule(data[:numEntries*2], bgUnits)
if family <= 22 {
data = data[numEntries*2:]
} else {
data = data[numEntries*2+2:]
}
r.Targets = decodeGlucoseTargetSchedule(data[:numEntries*3], bgUnits, family)
return r
}
func decodeBolusWizardSetup(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
if family <= 22 {
r.Data = data[:124]
} else {
r.Data = data[:144]
}
n := len(r.Data) - 1
body := data[7:n]
half := (n - 7) / 2
setup := BolusWizardSetupRecord{
Before: decodeBolusWizardConfig(body[:half], family),
After: decodeBolusWizardConfig(body[half:], family),
}
setup.Before.InsulinAction = hoursToDuration(data[n] & 0xF)
setup.After.InsulinAction = hoursToDuration(data[n] >> 4)
r.Info = setup
return r
}
func decodeBolusWizard(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
bg := int(data[1])
body := data[7:]
bgU := GlucoseUnitsType((body[1] >> 6) & 0x3)
carbU := CarbUnitsType((body[1] >> 4) & 0x3)
if family <= 22 {
r.Info = BolusWizardRecord{
GlucoseInput: intToGlucose(int(body[1]&0x3)<<8|bg, bgU),
CarbInput: Carbs(int(body[1]&0xC)<<6 | int(body[0])),
GlucoseUnits: bgU,
CarbUnits: carbU,
TargetLow: byteToGlucose(body[4], bgU),
TargetHigh: byteToGlucose(body[12], bgU),
Sensitivity: byteToGlucose(body[3], bgU),
CarbRatio: intToRatio(int(body[2]), carbU, family),
Correction: intToInsulin(int(body[7])+int(body[5]&0xF), family),
Food: byteToInsulin(body[6], family),
Unabsorbed: byteToInsulin(body[9], family),
Bolus: byteToInsulin(body[11], family),
}
r.Data = data[:20]
} else {
r.Info = BolusWizardRecord{
GlucoseInput: intToGlucose(int(body[1]&0x3)<<8|bg, bgU),
CarbInput: Carbs(int(body[1]&0xC)<<6 | int(body[0])),
GlucoseUnits: bgU,
CarbUnits: carbU,
TargetLow: byteToGlucose(body[5], bgU),
TargetHigh: byteToGlucose(body[14], bgU),
Sensitivity: byteToGlucose(body[4], bgU),
CarbRatio: intToRatio(int(body[2]&0xF)<<8|int(body[3]), carbU, family),
Correction: intToInsulin(int(body[9]&0x38)<<5|int(body[6]), family),
Food: twoByteInsulin(body[7:9], family),
Unabsorbed: twoByteInsulin(body[10:12], family),
Bolus: twoByteInsulin(body[12:14], family),
}
r.Data = data[:22]
}
return r
}
func decodeUnabsorbedInsulin(data []byte, family Family) HistoryRecord {
n := int(data[1]) - 2
body := data[2:]
var unabsorbed UnabsorbedBolusHistory
for i := 0; i < n; i += 3 {
amount := byteToInsulin(body[i], 23)
curve := body[i+2]
age := minutesToDuration(body[i+1] + (curve&0x30)<<4)
unabsorbed = append(unabsorbed, UnabsorbedBolus{
Bolus: amount,
Age: age,
})
}
return HistoryRecord{
Data: data[:n+2],
Info: unabsorbed,
}
}
var decodeSaveSettings = decodeBase
var decodeEnableVariableBolus = decodeEnable
var decodeChangeEasyBolus = decodeBase
var decodeEnableBGReminder = decodeEnable
var decodeEnableAlarmClock = decodeEnable
func decodeChangeTempBasalType(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = TempBasalType(data[1])
return r
}
var decodeChangeAlarmType = decodeValue
var decodeChangeTimeFormat = decodeValue
func decodeChangeReservoirWarning(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
v := data[1]
if v&0x1 == 0 {
r.Info = Insulin(1000 * int(v>>2))
} else {
r.Info = halfHoursToDuration(v >> 2)
}
return r
}
var decodeEnableBolusReminder = decodeEnable
var decodeSetBolusReminderTime = decodeEnableN(9)
var decodeDeleteBolusReminderTime = decodeEnableN(9)
var decodeBolusReminder = decodeBaseN(9)
var decodeDeleteAlarmClockTime = decodeBase
var decodeDailyTotal515 = decodeDailyTotalN(38)
var decodeDailyTotal522 = decodeDailyTotalN(44)
var decodeDailyTotal523 = decodeDailyTotalN(52)
var decodeChangeCarbUnits = decodeValue
func decodeBasalProfileStart(data []byte, family Family) HistoryRecord {
r := decodeBase(data, family)
r.Info = BasalProfileStartRecord{
ProfileIndex: int(data[1]),
BasalRate: decodeBasalRate(data[7:10]),
}
r.Data = data[:10]
return r
}
var decodeConnectOtherDevices = decodeEnable
var decodeChangeOtherDevice = decodeBaseN(37)
var decodeChangeMarriage = decodeBaseN(12)
var decodeDeleteOtherDevice = decodeBaseN(12)
var decodeEnableCaptureEvent = decodeEnable
// DecodeHistoryRecord decodes a history record based on its type.
func DecodeHistoryRecord(data []byte, family Family) (HistoryRecord, error) {
if len(data) == 0 {
return HistoryRecord{}, fmt.Errorf("empty history record")
}
decoder := decode[HistoryRecordType(data[0])]
if decoder == nil {
return HistoryRecord{}, unknownRecord(data)
}
return decoder(data, family), nil
}
func (e UnknownRecordTypeError) Error() string {
return fmt.Sprintf("unknown record type here: % X", e.Data)
}
func unknownRecord(data []byte) error {
return UnknownRecordTypeError{
Data: data,
}
}
// DecodeHistory decodes the records in a page of data and
// returns them in reverse chronological order (most recent first),
// to match the order of the history pages themselves.
func DecodeHistory(data []byte, family Family) (History, error) {
var results History
var r HistoryRecord
var err error
for !allZero(data) {
r, err = DecodeHistoryRecord(data, family)
if err != nil {
break
}
results = append(results, r)
data = data[len(r.Data):]
}
ReverseHistory(results)
return results, err
}
// Partially filled history pages are padded to the end with zero bytes.
func allZero(data []byte) bool {
for _, b := range data {
if b != 0 {
return false
}
}
return true
}
// ReverseHistory reverses a slice of history records.
func ReverseHistory(a History) {
for i, j := 0, len(a)-1; i < len(a)/2; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
}