-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrecord.go
256 lines (220 loc) · 5.88 KB
/
record.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package dexcom
import (
"bytes"
"fmt"
"time"
)
type (
// Record represents a time-stamped Dexcom receiver record.
Record struct {
Timestamp Timestamp
XML XMLInfo `json:",omitempty"`
Sensor *SensorInfo `json:",omitempty"`
EGV *EGVInfo `json:",omitempty"`
Calibration *CalibrationInfo `json:",omitempty"`
Insertion *InsertionInfo `json:",omitempty"`
Meter *MeterInfo `json:",omitempty"`
}
// Records represents a sequence of records.
Records []Record
// SensorInfo represents a sensor reading.
SensorInfo struct {
Unfiltered uint32
Filtered uint32
RSSI int8
Unknown byte
}
// EGVInfo represents an estimated glucose value.
EGVInfo struct {
Glucose uint16
DisplayOnly bool
Noise uint8
Trend Trend
}
// InsertionInfo represents a sensor change event.
InsertionInfo struct {
SystemTime time.Time
Event SensorChange
}
// MeterInfo represents a meter reading.
MeterInfo struct {
Glucose uint16
MeterTime time.Time
}
// CalibrationInfo represents a calibration event.
CalibrationInfo struct {
Slope float64
Intercept float64
Scale float64
Decay float64
Data []CalibrationRecord
}
// CalibrationRecord represents a calibration data point.
CalibrationRecord struct {
TimeEntered time.Time
Glucose int32
Raw int32
TimeApplied time.Time
}
)
// Time returns the record's display time.
func (r Record) Time() time.Time {
return r.Timestamp.DisplayTime
}
// Glucose returns the glucose field from an EGV record.
func (r Record) Glucose() uint16 {
return r.EGV.Glucose
}
// Len returns the number of records.
func (v Records) Len() int {
return len(v)
}
// Time returns the time of the record at index i.
func (v Records) Time(i int) time.Time {
return v[i].Time()
}
var recordUnmarshal = map[PageType]func(*Record, []byte){
ManufacturingData: umarshalXMLInfo,
FirmwareData: umarshalXMLInfo,
SoftwareData: umarshalXMLInfo,
SensorData: unmarshalSensorInfo,
EGVData: umarshalEGVInfo,
CalibrationData: unmarshalCalibrationInfo,
InsertionTimeData: unmarshalInsertionInfo,
MeterData: unmarshalMeterInfo,
}
func (r *Record) unmarshal(pageType PageType, v []byte) error {
f, found := recordUnmarshal[pageType]
if !found {
return fmt.Errorf("unmarshaling of %v records is unimplemented: % X", pageType, v)
}
r.Timestamp.unmarshal(v[0:8])
f(r, v)
return nil
}
func unmarshalSensorInfo(r *Record, v []byte) {
r.Sensor = &SensorInfo{
Unfiltered: unmarshalUint32(v[8:12]),
Filtered: unmarshalUint32(v[12:16]),
RSSI: int8(v[16]),
Unknown: v[17],
}
}
// SpecialGlucose represents a glucose value that indicates an exceptional condition.
type SpecialGlucose uint16
//go:generate stringer -type SpecialGlucose
// Exceptional conditions.
const (
SensorNotActive SpecialGlucose = 1
MinimalDeviation SpecialGlucose = 2
NoAntenna SpecialGlucose = 3
SensorNotCalibrated SpecialGlucose = 5
CountDeviation SpecialGlucose = 6
AbsoluteDeviation SpecialGlucose = 9
PowerDeviation SpecialGlucose = 10
BadRF SpecialGlucose = 12
specialLimit = BadRF
)
// IsSpecial checks whether a glucose value falls in the SpecialGlucose range.
func IsSpecial(glucose uint16) bool {
return glucose <= uint16(specialLimit)
}
// Trend represents a directional arrow displayed by the Dexcom CGM receiver.
type Trend byte
//go:generate stringer -type Trend
// Trend arrows.
const (
UpUp Trend = 1
Up Trend = 2
Up45 Trend = 3
Flat Trend = 4
Down45 Trend = 5
Down Trend = 6
DownDown Trend = 7
NotComputable Trend = 8
OutOfRange Trend = 9
)
var trendSymbol = map[Trend]string{
UpUp: "⇈",
Up: "↑",
Up45: "↗",
Flat: "→",
Down45: "↘",
Down: "↓",
DownDown: "⇊",
NotComputable: "⁇",
OutOfRange: "⋯",
}
// Symbol converts a Trend to a graphical representation.
func (t Trend) Symbol() string {
return trendSymbol[t]
}
// Constants used to extract EGV, noise, and trend.
const (
EGVDisplayOnly = 1 << 15
EGVValueMask = 0x3FF
EGVNoiseMask = 0x70
EGVTrendMask = 0xF
)
func umarshalEGVInfo(r *Record, v []byte) {
g := unmarshalUint16(v[8:10])
r.EGV = &EGVInfo{
Glucose: g & EGVValueMask,
DisplayOnly: g&EGVDisplayOnly != 0,
Noise: v[10] & EGVNoiseMask >> 4,
Trend: Trend(v[10] & EGVTrendMask),
}
}
func unmarshalCalibrationInfo(r *Record, v []byte) {
cal := &CalibrationInfo{
Slope: unmarshalFloat64(v[8:16]),
Intercept: unmarshalFloat64(v[16:24]),
Scale: unmarshalFloat64(v[24:32]),
Decay: unmarshalFloat64(v[35:43]),
}
n := int(v[43])
cal.Data = make([]CalibrationRecord, n)
v = v[44:]
offset := r.Timestamp.DisplayTime.Sub(r.Timestamp.SystemTime)
for i := 0; i < n; i++ {
cal.Data[i].unmarshal(v)
cal.Data[i].TimeEntered = cal.Data[i].TimeEntered.Add(offset)
cal.Data[i].TimeApplied = cal.Data[i].TimeApplied.Add(offset)
v = v[17:]
}
r.Calibration = cal
}
func (r *CalibrationRecord) unmarshal(v []byte) {
r.TimeEntered = unmarshalTime(v[0:4])
r.Glucose = unmarshalInt32(v[4:8])
r.Raw = unmarshalInt32(v[8:12])
r.TimeApplied = unmarshalTime(v[12:16])
}
// SensorChange represents a sensor change.
type SensorChange byte
//go:generate stringer -type SensorChange
// Sensor change values.
const (
Stopped SensorChange = 1
Started SensorChange = 7
)
var (
invalidTime = []byte{0xFF, 0xFF, 0xFF, 0xFF}
)
func unmarshalInsertionInfo(r *Record, v []byte) {
t := time.Time{}
u := v[8:12]
if !bytes.Equal(u, invalidTime) {
t = unmarshalTime(u)
}
r.Insertion = &InsertionInfo{
SystemTime: t,
Event: SensorChange(v[12]),
}
}
func unmarshalMeterInfo(r *Record, v []byte) {
r.Meter = &MeterInfo{
Glucose: unmarshalUint16(v[8:10]),
MeterTime: unmarshalTime(v[10:14]),
}
}