-
Notifications
You must be signed in to change notification settings - Fork 29
/
model.go
65 lines (60 loc) · 1.35 KB
/
model.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
package medtronic
import (
"log"
"strconv"
)
// Family represents a pump family.
// Use int8 so the compiler will warn about
// accidental uses of 523 instead of 23, etc.
type Family int8
// Model requests the model number from the pump and returns it,
// caching the pump family as a side effect.
// Use Family to avoid contacting the pump more than once.
func (pump *Pump) Model() string {
data := pump.Execute(model)
if pump.Error() != nil {
return ""
}
if len(data) < 2 {
pump.BadResponse(model, data)
return ""
}
n := int(data[1])
if len(data) < 2+n {
pump.BadResponse(model, data)
return ""
}
m := string(data[2 : 2+n])
pump.cacheFamily(m)
return m
}
func (pump *Pump) cacheFamily(model string) {
if pump.family != 0 {
return
}
log.Printf("model %s pump", model)
family := -1
n, err := strconv.Atoi(model)
if err != nil {
log.Printf("%v", err)
} else if 500 < n && n < 600 {
family = n - 500
} else if 700 < n && n < 800 {
family = n - 700
} else {
log.Printf("unsupported pump model %d", n)
}
pump.family = Family(family)
}
// Family returns 22 for 522/722 pumps, 23 for 523/723 pumps, etc.,
// and returns -1 for an unrecognized model. It calls Model once and
// caches the result.
func (pump *Pump) Family() Family {
if pump.family == 0 {
pump.Model()
if pump.Error() != nil {
return -1
}
}
return pump.family
}