SensorCalFactorGlucoseEvent.swift 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // SensorCalFactorGlucoseEvent.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 SensorCalFactorGlucoseEvent: GlucoseEvent {
  10. public let length: Int
  11. public let rawData: Data
  12. public let timestamp: DateComponents
  13. public let factor: Float
  14. public init?(availableData: Data, relativeTimestamp: DateComponents) {
  15. length = 7
  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. factor = Float(UInt16(d(5) << 8 | d(6))) / Float(1000.0)
  25. }
  26. public var dictionaryRepresentation: [String: Any] {
  27. return [
  28. "name": "SensorCalFactor",
  29. "factor": factor
  30. ]
  31. }
  32. }