-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsettings.go
86 lines (81 loc) · 2.25 KB
/
settings.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
package medtronic
import (
"time"
)
// SettingsInfo represents the pump's settings.
type SettingsInfo struct {
AutoOff time.Duration
InsulinAction time.Duration
InsulinConcentration int // 50 or 100
MaxBolus Insulin
MaxBasal Insulin
RFEnabled bool
TempBasalType TempBasalType
SelectedPattern int
}
func decodeSettings(data []byte, family Family) (SettingsInfo, error) {
var info SettingsInfo
if family <= 12 {
if len(data) < 19 || data[0] != 18 {
return info, BadResponseError{Command: settings, Data: data}
}
info.MaxBolus = byteToInsulin(data[6], 22)
info.MaxBasal = twoByteInsulin(data[7:9], 23)
if data[18] == 0 {
// Fast-acting insulin type.
info.InsulinAction = 6 * time.Hour
} else {
// Regular insulin type.
info.InsulinAction = 8 * time.Hour
}
} else if family <= 22 {
if len(data) < 22 || data[0] != 21 {
return info, BadResponseError{Command: settings, Data: data}
}
info.MaxBolus = byteToInsulin(data[6], 22)
info.MaxBasal = twoByteInsulin(data[7:9], 23)
info.InsulinAction = time.Duration(data[18]) * time.Hour
} else {
if len(data) < 26 || data[0] != 25 {
return info, BadResponseError{Command: settings, Data: data}
}
info.MaxBolus = byteToInsulin(data[7], 22)
info.MaxBasal = twoByteInsulin(data[8:10], 23)
info.InsulinAction = time.Duration(data[18]) * time.Hour
}
info.AutoOff = time.Duration(data[1]) * time.Hour
info.SelectedPattern = int(data[12])
info.RFEnabled = data[13] == 1
info.TempBasalType = TempBasalType(data[14])
var err error
info.InsulinConcentration, err = insulinConcentration(data)
return info, err
}
// Settings returns the pump's settings.
func (pump *Pump) Settings() SettingsInfo {
// Command opcode and format of response depend on the pump family.
family := pump.Family()
var cmd Command
if family <= 12 {
cmd = settings512
} else {
cmd = settings
}
data := pump.Execute(cmd)
if pump.Error() != nil {
return SettingsInfo{}
}
i, err := decodeSettings(data, family)
pump.SetError(err)
return i
}
func insulinConcentration(data []byte) (int, error) {
switch data[10] {
case 0:
return 100, nil
case 1:
return 50, nil
default:
return 0, BadResponseError{Command: settings, Data: data}
}
}