FaultConfigCommand.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // FaultConfigCommand.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 12/18/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct FaultConfigCommand : NonceResyncableMessageBlock {
  10. // OFF 1 2 3 4 5 6 7
  11. // 08 06 NNNNNNNN JJ KK
  12. public let blockType: MessageBlockType = .faultConfig
  13. public let length: UInt8 = 6
  14. public var nonce: UInt32
  15. public let tab5Sub16: UInt8
  16. public let tab5Sub17: UInt8
  17. public init(nonce: UInt32, tab5Sub16: UInt8, tab5Sub17: UInt8) {
  18. self.nonce = nonce
  19. self.tab5Sub16 = tab5Sub16
  20. self.tab5Sub17 = tab5Sub17
  21. }
  22. public init(encodedData: Data) throws {
  23. if encodedData.count < 8 {
  24. throw MessageBlockError.notEnoughData
  25. }
  26. nonce = encodedData[2...].toBigEndian(UInt32.self)
  27. self.tab5Sub16 = encodedData[6]
  28. self.tab5Sub17 = encodedData[7]
  29. }
  30. public var data: Data {
  31. var data = Data([
  32. blockType.rawValue,
  33. length])
  34. data.appendBigEndian(nonce)
  35. data.append(tab5Sub16)
  36. data.append(tab5Sub17)
  37. return data
  38. }
  39. }
  40. extension FaultConfigCommand: CustomDebugStringConvertible {
  41. public var debugDescription: String {
  42. return "FaultConfigCommand(nonce:\(Data(bigEndian: nonce).hexadecimalString), tab5Sub16:\(tab5Sub16), tab5Sub17:\(tab5Sub17))"
  43. }
  44. }