MySentryAlertMessageBody.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // MySentryAlertMessageBody.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 9/6/15.
  6. // Copyright © 2015 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. /**
  10. Describes an alert message sent immediately from the pump to any paired MySentry devices
  11. See: [MinimedRF Class](https://github.com/ps2/minimed_rf/blob/master/lib/minimed_rf/messages/alert.rb)
  12. ```
  13. a2 594040 01 7c 65 0727070f0906 0175 4c
  14. ```
  15. */
  16. public struct MySentryAlertMessageBody: MessageBody, DictionaryRepresentable {
  17. public static let length = 10
  18. public let alertType: MySentryAlertType?
  19. public let alertDate: Date
  20. private let rxData: Data
  21. public init?(rxData: Data) {
  22. guard rxData.count == type(of: self).length, let
  23. alertDate = DateComponents(mySentryBytes: rxData.subdata(in: 2..<8)).date
  24. else {
  25. return nil
  26. }
  27. self.rxData = rxData
  28. alertType = MySentryAlertType(rawValue: rxData[1])
  29. self.alertDate = alertDate
  30. }
  31. public var txData: Data {
  32. return rxData
  33. }
  34. public var dictionaryRepresentation: [String: Any] {
  35. let dateFormatter = DateFormatter()
  36. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
  37. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  38. return [
  39. "alertDate": dateFormatter.string(from: alertDate),
  40. "alertType": (alertType != nil ? String(describing: alertType!) : rxData.subdata(in: 1..<2).hexadecimalString),
  41. "byte89": rxData.subdata(in: 8..<10).hexadecimalString
  42. ]
  43. }
  44. }