PodInfoActivationTime.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // PodInfoActivationTime.swift
  3. // OmniKit
  4. //
  5. // Created by Eelke Jager on 25/09/2018.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. // Type 5 PodInfo returns the pod activation time, time pod alive, and the possible fault code
  10. public struct PodInfoActivationTime : PodInfo {
  11. // OFF 1 2 3 4 5 6 7 8 9 10111213 1415161718
  12. // DATA 0 1 2 3 4 5 6 7 8 9 1011 1213141516
  13. // 02 11 05 PP QQQQ 00000000 00000000 MMDDYYHHMM
  14. public let podInfoType: PodInfoResponseSubType = .activationTime
  15. public let faultEventCode: FaultEventCode
  16. public let timeActivation: TimeInterval
  17. public let dateTime: DateComponents
  18. public let data: Data
  19. public init(encodedData: Data) throws {
  20. guard encodedData.count >= 16 else {
  21. throw MessageBlockError.notEnoughData
  22. }
  23. self.faultEventCode = FaultEventCode(rawValue: encodedData[1])
  24. self.timeActivation = TimeInterval(minutes: Double((Int(encodedData[2] & 0b1) << 8) + Int(encodedData[3])))
  25. self.dateTime = DateComponents(encodedDateTime: encodedData.subdata(in: 12..<17))
  26. self.data = Data(encodedData)
  27. }
  28. }
  29. extension DateComponents {
  30. init(encodedDateTime: Data) {
  31. self.init()
  32. year = Int(encodedDateTime[2]) + 2000
  33. month = Int(encodedDateTime[0])
  34. day = Int(encodedDateTime[1])
  35. hour = Int(encodedDateTime[3])
  36. minute = Int(encodedDateTime[4])
  37. calendar = Calendar(identifier: .gregorian)
  38. }
  39. }