Skip to content

Commit

Permalink
Use multiple typed fields instead of single Info field of type interf…
Browse files Browse the repository at this point in the history
…ace{}
  • Loading branch information
ecc1 committed Sep 20, 2018
1 parent 115a7bd commit 1ccc25a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
24 changes: 16 additions & 8 deletions nightscout.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,37 @@ func (r Record) nightscoutEntry() nightscout.Entry {
DateString: t.Format(nightscout.DateStringLayout),
Device: nightscout.Device(),
}
switch info := r.Info.(type) {
case SensorInfo:
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)
case EGVInfo:
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)
case MeterInfo:
return e
}
if r.Meter != nil {
info := r.Meter
e.Type = nightscout.MBGType
e.MBG = int(info.Glucose)
case CalibrationInfo:
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
default:
panic(fmt.Sprintf("nightscoutEntry %+v", r))
return e
}
return e
panic(fmt.Sprintf("nightscoutEntry %+v", r))
}

func nightscoutTrend(t Trend) string {
Expand Down
8 changes: 4 additions & 4 deletions nightscout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,29 @@ func nsDate(s string) int64 {
var (
r1 = Record{
Timestamp: ts("2017-09-17T01:13:51-04:00"),
Info: CalibrationInfo{
Calibration: &CalibrationInfo{
Slope: 939.6817717490421,
Intercept: 35926.604186515906,
Scale: 1,
},
}
r2 = Record{
Timestamp: ts("2017-09-17T01:13:49-04:00"),
Info: MeterInfo{
Meter: &MeterInfo{
Glucose: 128,
},
}
r3 = Record{
Timestamp: ts("2017-09-17T11:13:17-04:00"),
Info: EGVInfo{
EGV: &EGVInfo{
Glucose: 84,
Trend: Flat,
Noise: 1,
},
}
r4 = Record{
Timestamp: ts("2017-09-17T11:13:16-04:00"),
Info: SensorInfo{
Sensor: &SensorInfo{
Unfiltered: 119088,
Filtered: 110288,
RSSI: -62,
Expand Down
23 changes: 14 additions & 9 deletions record.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import (
type (
// Record represents a time-stamped Dexcom receiver record.
Record struct {
Timestamp Timestamp
Info interface{}
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.
Expand Down Expand Up @@ -69,7 +74,7 @@ func (r Record) Time() time.Time {

// Glucose returns the glucose field from an EGV record.
func (r Record) Glucose() uint16 {
return r.Info.(EGVInfo).Glucose
return r.EGV.Glucose
}

// Len returns the number of records.
Expand Down Expand Up @@ -104,7 +109,7 @@ func (r *Record) unmarshal(pageType PageType, v []byte) error {
}

func unmarshalSensorInfo(r *Record, v []byte) {
r.Info = SensorInfo{
r.Sensor = &SensorInfo{
Unfiltered: unmarshalUint32(v[8:12]),
Filtered: unmarshalUint32(v[12:16]),
RSSI: int8(v[16]),
Expand Down Expand Up @@ -181,7 +186,7 @@ const (

func umarshalEGVInfo(r *Record, v []byte) {
g := unmarshalUint16(v[8:10])
r.Info = EGVInfo{
r.EGV = &EGVInfo{
Glucose: g & EGVValueMask,
DisplayOnly: g&EGVDisplayOnly != 0,
Noise: v[10] & EGVNoiseMask >> 4,
Expand All @@ -190,7 +195,7 @@ func umarshalEGVInfo(r *Record, v []byte) {
}

func unmarshalCalibrationInfo(r *Record, v []byte) {
cal := CalibrationInfo{
cal := &CalibrationInfo{
Slope: unmarshalFloat64(v[8:16]),
Intercept: unmarshalFloat64(v[16:24]),
Scale: unmarshalFloat64(v[24:32]),
Expand All @@ -206,7 +211,7 @@ func unmarshalCalibrationInfo(r *Record, v []byte) {
cal.Data[i].TimeApplied = cal.Data[i].TimeApplied.Add(offset)
v = v[17:]
}
r.Info = cal
r.Calibration = cal
}

func (r *CalibrationRecord) unmarshal(v []byte) {
Expand Down Expand Up @@ -237,14 +242,14 @@ func unmarshalInsertionInfo(r *Record, v []byte) {
if !bytes.Equal(u, invalidTime) {
t = unmarshalTime(u)
}
r.Info = InsertionInfo{
r.Insertion = &InsertionInfo{
SystemTime: t,
Event: SensorChange(v[12]),
}
}

func unmarshalMeterInfo(r *Record, v []byte) {
r.Info = MeterInfo{
r.Meter = &MeterInfo{
Glucose: unmarshalUint16(v[8:10]),
MeterTime: unmarshalTime(v[10:14]),
}
Expand Down
2 changes: 1 addition & 1 deletion xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func umarshalXMLInfo(r *Record, v []byte) {
if i != -1 {
v = v[:i]
}
r.Info = umarshalXMLBytes(v)
r.XML = umarshalXMLBytes(v)
}

func umarshalXMLBytes(v []byte) XMLInfo {
Expand Down

0 comments on commit 1ccc25a

Please sign in to comment.