SetupPodCommand.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Setup PodCommand.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 2/17/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct SetupPodCommand : MessageBlock {
  10. public let blockType: MessageBlockType = .setupPod
  11. let address: UInt32
  12. let lot: UInt32
  13. let tid: UInt32
  14. let dateComponents: DateComponents
  15. let packetTimeoutLimit: UInt8
  16. public static func dateComponents(date: Date, timeZone: TimeZone) -> DateComponents {
  17. var cal = Calendar(identifier: .gregorian)
  18. cal.timeZone = timeZone
  19. return cal.dateComponents([.day, .month, .year, .hour, .minute], from: date)
  20. }
  21. public static func date(from components: DateComponents, timeZone: TimeZone) -> Date? {
  22. var cal = Calendar(identifier: .gregorian)
  23. cal.timeZone = timeZone
  24. return cal.date(from: components)
  25. }
  26. // 03 13 1f08ced2 14 04 09 0b 11 0b 08 0000a640 00097c27 83e4
  27. public var data: Data {
  28. var data = Data([
  29. blockType.rawValue,
  30. 19,
  31. ])
  32. data.appendBigEndian(self.address)
  33. let year = UInt8((dateComponents.year ?? 2000) - 2000)
  34. let month = UInt8(dateComponents.month ?? 0)
  35. let day = UInt8(dateComponents.day ?? 0)
  36. let hour = UInt8(dateComponents.hour ?? 0)
  37. let minute = UInt8(dateComponents.minute ?? 0)
  38. let data2: Data = Data([
  39. UInt8(0x14), // Unknown
  40. packetTimeoutLimit,
  41. month,
  42. day,
  43. year,
  44. hour,
  45. minute
  46. ])
  47. data.append(data2)
  48. data.appendBigEndian(self.lot)
  49. data.appendBigEndian(self.tid)
  50. return data
  51. }
  52. public init(encodedData: Data) throws {
  53. if encodedData.count < 21 {
  54. throw MessageBlockError.notEnoughData
  55. }
  56. self.address = encodedData[2...].toBigEndian(UInt32.self)
  57. packetTimeoutLimit = encodedData[7]
  58. var components = DateComponents()
  59. components.month = Int(encodedData[8])
  60. components.day = Int(encodedData[9])
  61. components.year = Int(encodedData[10]) + 2000
  62. components.hour = Int(encodedData[11])
  63. components.minute = Int(encodedData[12])
  64. self.dateComponents = components
  65. self.lot = encodedData[13...].toBigEndian(UInt32.self)
  66. self.tid = encodedData[17...].toBigEndian(UInt32.self)
  67. }
  68. public init(address: UInt32, dateComponents: DateComponents, lot: UInt32, tid: UInt32, packetTimeoutLimit: UInt8 = 4) {
  69. self.address = address
  70. self.dateComponents = dateComponents
  71. self.lot = lot
  72. self.tid = tid
  73. self.packetTimeoutLimit = packetTimeoutLimit
  74. }
  75. }
  76. extension SetupPodCommand: CustomDebugStringConvertible {
  77. public var debugDescription: String {
  78. return "SetupPodCommand(address:\(Data(bigEndian: address).hexadecimalString), dateComponents:\(dateComponents), lot:\(lot), tid:\(tid), packetTimeoutLimit:\(packetTimeoutLimit))"
  79. }
  80. }