ReadRemoteControlIDsMessageBody.swift 1005 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // ReadRemoteControlIDsMessageBody.swift
  3. // MinimedKit
  4. //
  5. // Copyright © 2018 Pete Schwamb. All rights reserved.
  6. //
  7. import Foundation
  8. private let idSize = 6
  9. public class ReadRemoteControlIDsMessageBody: CarelinkLongMessageBody {
  10. public let ids: [Data]
  11. public required init?(rxData: Data) {
  12. guard rxData.count == type(of: self).length else {
  13. return nil
  14. }
  15. var ids: [Data] = []
  16. remotes: for index in stride(from: 0, to: 3, by: 1) {
  17. let start = (index * idSize + 1)
  18. let end = start + idSize
  19. var remoteID = Data(capacity: idSize)
  20. for byte in rxData[start..<end] {
  21. let isEnabled = (byte & 0b00010000) == 0b00010000
  22. guard isEnabled else {
  23. continue remotes
  24. }
  25. remoteID.append(byte & 0xf)
  26. }
  27. ids.append(remoteID)
  28. }
  29. self.ids = ids
  30. super.init(rxData: rxData)
  31. }
  32. }