Announcement.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import Foundation
  2. struct Announcement: JSON, Equatable, Hashable {
  3. let createdAt: Date
  4. let enteredBy: String
  5. let notes: String
  6. static let remote = "remote"
  7. var action: AnnouncementAction? {
  8. let components = notes.replacingOccurrences(of: " ", with: "").split(separator: ":")
  9. guard components.count == 2 else {
  10. return nil
  11. }
  12. let command = String(components[0]).lowercased()
  13. let arguments = String(components[1]).lowercased()
  14. switch command {
  15. case "bolus":
  16. guard let amount = Decimal(from: arguments) else { return nil }
  17. return .bolus(amount)
  18. case "pump":
  19. guard let action = PumpAction(rawValue: arguments) else { return nil }
  20. return .pump(action)
  21. case "looping":
  22. guard let looping = Bool(from: arguments) else { return nil }
  23. return .looping(looping)
  24. case "tempbasal":
  25. let basalComponents = arguments.split(separator: ",")
  26. guard basalComponents.count == 2 else { return nil }
  27. let rateArg = String(basalComponents[0])
  28. let durationArg = String(basalComponents[1])
  29. guard let rate = Decimal(from: rateArg), let duration = Decimal(from: durationArg) else { return nil }
  30. return .tempbasal(rate: rate, duration: duration)
  31. default: return nil
  32. }
  33. }
  34. }
  35. extension Announcement {
  36. private enum CodingKeys: String, CodingKey {
  37. case createdAt = "created_at"
  38. case enteredBy
  39. case notes
  40. }
  41. }
  42. enum AnnouncementAction {
  43. case bolus(Decimal)
  44. case pump(PumpAction)
  45. case looping(Bool)
  46. case tempbasal(rate: Decimal, duration: Decimal)
  47. }
  48. enum PumpAction: String {
  49. case suspend
  50. case resume
  51. }