Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X22 sensor timestamp refinement #296

Merged
merged 13 commits into from
Dec 12, 2016
Merged

Conversation

tmecklem
Copy link
Contributor

Refines the cgm page timestamping and adds low and high sgv to nightscout

  • Remove copy/paste write sensor timestamp repeatCount of 255
  • Group sensor data, sensor high, and sensor low events under SensorDataGlucoseEvent for easier sgv filtering/processing to Nightscout and loop.
  • Do not use "gap" sensor timestamps for forward offsets since they represent the start of a gap in time after the timestamp.
  • Do not attempt to write a sensor timestamp when decoding a completed history page.

case gap
case unknown

public static func eventType(code: UInt8) -> SensorTimestampType {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be more semantic as an initializer:
public init(code: UInt8)
(also it might not need to be public)

default:
timestampType = "unknown"
}
timestampType = SensorTimestampType.eventType(code: UInt8(d(3) >> 5) & 0b00000011)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for just noticing this now, but "d" is useless here as you're undoing the unnecessary Int cast. Coupled with the initializer change:

timestampType = SensorTimestampType(code: availableData[3] >> 5 & 0b11)

is much cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered if there wasn't a better way. Thanks for the feedback on this.

} else if let event = event as? ReferenceTimestampedGlucoseEvent {
let offsetDate = calendar.date(byAdding: Calendar.Component.minute, value: 5 * relativeEventCount, to: event.timestamp.date!)!
} else if let sensorTimestampEvent = event as? SensorTimestampGlucoseEvent,
relativeEventCount == 0 || (event as! SensorTimestampGlucoseEvent).isForwardOffsetReference() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace that last part with simply sensorTimestampEvent.isForwardOffsetReference() as your unwrapping statement is in-scope at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I might be able to do that but Xcode was throwing a fit about it. Must have been a stale compiler error because it worked this time around. 👍

} else if let sensorTimestampEvent = event as? SensorTimestampGlucoseEvent,
relativeEventCount == 0 || (event as! SensorTimestampGlucoseEvent).isForwardOffsetReference() {

let offsetDate = calendar.date(byAdding: Calendar.Component.minute, value: 5 * relativeEventCount, to: sensorTimestampEvent.timestamp.date!)!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

byAdding: .minute, since the type is inferred.

Also, I'm not sure this line to be a calendar operation if you're just adding a double to a double (the next one absolutely does)
sensorTimestampEvent.timestamp.date?.addingTimeInterval(TimeInterval(minutes: 5 * relativeEventCount))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to go about changing this. Could you help me out with an example of what you think could improve it?

Copy link
Collaborator

@loudnate loudnate Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I did?

let offsetDate = sensorTimestampEvent.timestamp.date!.addingTimeInterval(TimeInterval(minutes: 5 * relativeEventCount))

force-unwrapping should be avoided when possible, so I assume you're certain that timestamp.date is non-nil?

} else if let sensorTimestampEvent = event as? SensorTimestampGlucoseEvent,
relativeEventCount == 0 || (event as! SensorTimestampGlucoseEvent).isForwardOffsetReference() {

let offsetDate = calendar.date(byAdding: Calendar.Component.minute, value: 5 * relativeEventCount, to: sensorTimestampEvent.timestamp.date!)!
var relativeTimestamp = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: offsetDate)
relativeTimestamp.calendar = calendar
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you need this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which line are you referring to? 72?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

75/76. I assumed that creating DateComponents from a Calendar would result in components that included a reference to the calendar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good question. I'll try some things out.

@tmecklem
Copy link
Contributor Author

tmecklem commented Dec 12, 2016

@loudnate, I appreciate you taking some of your time to review this code and make it better. Both because it's not a language I'm all that familiar with yet, and because you know this codebase so well. 👍

@ps2 ps2 merged commit 92e801a into ps2:dev Dec 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants