forked from ecc1/medtronic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarbratios.go
83 lines (74 loc) · 1.61 KB
/
carbratios.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
package medtronic
import (
"time"
)
const (
CarbRatios Command = 0x8A
)
type Tenths int
type CarbRatio struct {
Start TimeOfDay
CarbRatio Tenths // 10x grams/unit or 100x units/exchange
Units CarbUnitsType
}
type CarbRatioSchedule []CarbRatio
func carbRatioStep(newerPump bool) int {
if newerPump {
return 3
} else {
return 2
}
}
func decodeCarbRatioSchedule(data []byte, units CarbUnitsType, newerPump bool) CarbRatioSchedule {
sched := []CarbRatio{}
step := carbRatioStep(newerPump)
for i := 0; i < len(data); i += step {
start := halfHoursToTimeOfDay(data[i])
if start == 0 && len(sched) != 0 {
break
}
value := Tenths(0)
if newerPump {
value = Tenths(twoByteInt(data[i+1 : i+3]))
} else {
value = Tenths(10 * int(data[i+1]))
}
sched = append(sched, CarbRatio{
Start: start,
CarbRatio: value,
Units: units,
})
}
return sched
}
func (pump *Pump) CarbRatios() CarbRatioSchedule {
// Format of response depends on the pump family.
newer := pump.Family() >= 23
data := pump.Execute(CarbRatios)
if pump.Error() != nil {
return CarbRatioSchedule{}
}
if len(data) < 2 {
pump.BadResponse(CarbRatios, data)
return CarbRatioSchedule{}
}
n := int(data[0]) - 1
step := carbRatioStep(newer)
if n%step != 0 {
pump.BadResponse(CarbRatios, data)
return CarbRatioSchedule{}
}
units := CarbUnitsType(data[1])
return decodeCarbRatioSchedule(data[step:step+n], units, newer)
}
func (s CarbRatioSchedule) CarbRatioAt(t time.Time) CarbRatio {
d := sinceMidnight(t)
last := CarbRatio{}
for _, v := range s {
if v.Start > d {
break
}
last = v
}
return last
}