SensorSyncGlucoseEvent.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // SensorSyncGlucoseEvent.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 struct SensorSyncGlucoseEvent: GlucoseEvent {
  10. public let length: Int
  11. public let rawData: Data
  12. public let timestamp: DateComponents
  13. private let syncType: String
  14. public init?(availableData: Data, relativeTimestamp: DateComponents) {
  15. length = 5
  16. guard length <= availableData.count else {
  17. return nil
  18. }
  19. func d(_ idx: Int) -> Int {
  20. return Int(availableData[idx])
  21. }
  22. rawData = availableData.subdata(in: 0..<length)
  23. timestamp = DateComponents(glucoseEventBytes: availableData.subdata(in: 1..<5))
  24. switch (d(3) >> 5 & 0b00000011) {
  25. case 0x01:
  26. syncType = "new"
  27. case 0x02:
  28. syncType = "old"
  29. default:
  30. syncType = "find"
  31. }
  32. }
  33. public var dictionaryRepresentation: [String: Any] {
  34. return [
  35. "name": "SensorSync",
  36. "syncType": syncType
  37. ]
  38. }
  39. }