-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnightscout.go
137 lines (129 loc) · 2.85 KB
/
nightscout.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
package dexcom
import (
"fmt"
"log"
"time"
"github.com/ecc1/nightscout"
)
// NightscoutEntries converts records (in reverse-chronological order)
// into a Nightscout entries. Neighboring Sensor and EGV records are merged.
func NightscoutEntries(records Records) nightscout.Entries {
entries := make(nightscout.Entries, len(records))
for i, r := range records {
entries[i] = r.nightscoutEntry()
}
return mergeGlucoseEntries(entries)
}
func (r Record) nightscoutEntry() nightscout.Entry {
t := r.Time()
e := nightscout.Entry{
Date: nightscout.Date(t),
DateString: t.Format(nightscout.DateStringLayout),
Device: nightscout.Device(),
}
if r.Sensor != nil {
info := r.Sensor
e.Type = nightscout.SGVType
e.Unfiltered = int(info.Unfiltered)
e.Filtered = int(info.Filtered)
e.RSSI = int(info.RSSI)
return e
}
if r.EGV != nil {
info := r.EGV
e.Type = nightscout.SGVType
e.SGV = int(info.Glucose)
e.Direction = nightscoutTrend(info.Trend)
e.Noise = int(info.Noise)
return e
}
if r.Meter != nil {
info := r.Meter
e.Type = nightscout.MBGType
e.MBG = int(info.Glucose)
return e
}
if r.Calibration != nil {
info := r.Calibration
e.Type = nightscout.CalType
e.Slope = info.Slope
e.Intercept = info.Intercept
e.Scale = info.Scale
return e
}
panic(fmt.Sprintf("nightscoutEntry %+v", r))
}
func nightscoutTrend(t Trend) string {
switch t {
case UpUp:
return "DoubleUp"
case Up:
return "SingleUp"
case Up45:
return "FortyFiveUp"
case Flat:
return "Flat"
case Down45:
return "FortyFiveDown"
case Down:
return "SingleDown"
case DownDown:
return "DoubleDown"
default:
return ""
}
}
const (
// Time window within which sensor and EGV readings will be merged.
glucoseReadingWindow = 10 * time.Second
)
func mergeGlucoseEntries(entries nightscout.Entries) nightscout.Entries {
merged := make(nightscout.Entries, 0, len(entries))
i := 0
for i < len(entries) {
e := entries[i]
if e.Type == nightscout.SGVType && i+1 < len(entries) {
f := entries[i+1]
if f.Type == nightscout.SGVType {
delta := e.Time().Sub(f.Time())
if 0 <= delta && delta < glucoseReadingWindow {
e = combineEntries(e, f)
i++
}
}
}
merged = append(merged, e)
i++
}
return merged
}
func combineEntries(a, b nightscout.Entry) nightscout.Entry {
if a.Type != nightscout.SGVType || b.Type != nightscout.SGVType {
log.Panicf("combining %s and %s", a.Type, b.Type)
}
if b.Time().Before(a.Time()) {
// Use b's earlier time.
a.Date = b.Date
a.DateString = b.DateString
}
// Update a with non-zero sgv values from b.
if b.SGV != 0 {
a.SGV = b.SGV
}
if b.Direction != "" {
a.Direction = b.Direction
}
if b.Filtered != 0 {
a.Filtered = b.Filtered
}
if b.Unfiltered != 0 {
a.Unfiltered = b.Unfiltered
}
if b.RSSI != 0 {
a.RSSI = b.RSSI
}
if b.Noise != 0 {
a.Noise = b.Noise
}
return a
}