SensorStatusGlucoseEvent.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // SensorStatusGlucoseEvent.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 SensorStatusGlucoseEvent: GlucoseEvent {
  10. public let length: Int
  11. public let rawData: Data
  12. public let timestamp: DateComponents
  13. private let statusType: 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 0x00:
  26. statusType = "off"
  27. case 0x01:
  28. statusType = "on"
  29. case 0x02:
  30. statusType = "lost"
  31. default:
  32. statusType = "unknown"
  33. }
  34. }
  35. public var dictionaryRepresentation: [String: Any] {
  36. return [
  37. "name": "SensorStatus",
  38. "statusType": statusType
  39. ]
  40. }
  41. }