forked from ecc1/medtronic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasal.go
62 lines (53 loc) · 1.18 KB
/
basal.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
package medtronic
import (
"time"
)
const (
BasalRates Command = 0x92
BasalPatternA Command = 0x93
BasalPatternB Command = 0x94
)
type BasalRate struct {
Start TimeOfDay
Rate Insulin
}
type BasalRateSchedule []BasalRate
func (pump *Pump) basalSchedule(cmd Command) BasalRateSchedule {
data := pump.Execute(cmd)
if pump.Error() != nil {
return BasalRateSchedule{}
}
sched := []BasalRate{}
for i := 1; i < len(data); i += 3 {
r := data[i]
t := data[i+2]
// Don't stop if the 00:00 rate happens to be zero.
if i > 1 && r == 0 && t == 0 {
break
}
start := halfHoursToTimeOfDay(t)
rate := byteToInsulin(r, true)
sched = append(sched, BasalRate{Start: start, Rate: rate})
}
return sched
}
func (pump *Pump) BasalRates() BasalRateSchedule {
return pump.basalSchedule(BasalRates)
}
func (pump *Pump) BasalPatternA() BasalRateSchedule {
return pump.basalSchedule(BasalPatternA)
}
func (pump *Pump) BasalPatternB() BasalRateSchedule {
return pump.basalSchedule(BasalPatternB)
}
func (s BasalRateSchedule) BasalRateAt(t time.Time) BasalRate {
d := sinceMidnight(t)
last := BasalRate{}
for _, v := range s {
if v.Start > d {
break
}
last = v
}
return last
}