SensorTimestampGlucoseEvent.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // SensorTimestampGlucoseEvent.swift
  3. // RileyLink
  4. //
  5. // Created by Timothy Mecklem on 10/16/16.
  6. // Copyright © 2016 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public enum SensorTimestampType: String {
  10. case lastRf
  11. case pageEnd
  12. case gap
  13. case unknown
  14. init(code: UInt8) {
  15. switch code {
  16. case 0x00:
  17. self = .lastRf
  18. case 0x01:
  19. self = .pageEnd
  20. case 0x02:
  21. self = .gap
  22. default:
  23. self = .unknown
  24. }
  25. }
  26. }
  27. public struct SensorTimestampGlucoseEvent: GlucoseEvent {
  28. public let length: Int
  29. public let rawData: Data
  30. public let timestamp: DateComponents
  31. public let timestampType: SensorTimestampType
  32. public init?(availableData: Data, relativeTimestamp: DateComponents) {
  33. length = 5
  34. guard length <= availableData.count else {
  35. return nil
  36. }
  37. rawData = availableData.subdata(in: 0..<length)
  38. timestamp = DateComponents(glucoseEventBytes: availableData.subdata(in: 1..<5))
  39. timestampType = SensorTimestampType(code: availableData[3] >> 5 & 0b00000011)
  40. }
  41. public func isForwardOffsetReference() -> Bool {
  42. return timestampType == .lastRf || timestampType == .pageEnd
  43. }
  44. public var dictionaryRepresentation: [String: Any] {
  45. return [
  46. "name": "SensorTimestamp",
  47. "timestampType": timestampType
  48. ]
  49. }
  50. }