PushMessage.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Foundation
  2. struct PushMessage: Decodable {
  3. var user: String
  4. var commandType: String
  5. var bolusAmount: Decimal?
  6. var target: Int?
  7. var duration: Int?
  8. var carbs: Int?
  9. var protein: Int?
  10. var fat: Int?
  11. var sharedSecret: String
  12. var timestamp: TimeInterval
  13. enum CodingKeys: String, CodingKey {
  14. case user
  15. case commandType = "command_type"
  16. case bolusAmount = "bolus_amount"
  17. case target
  18. case duration
  19. case carbs
  20. case protein
  21. case fat
  22. case sharedSecret = "shared_secret"
  23. case timestamp
  24. }
  25. }
  26. extension PushMessage {
  27. func humanReadableDescription() -> String {
  28. var description = "User: \(user). Command Type: \(commandType). "
  29. switch commandType {
  30. case "bolus":
  31. if let amount = bolusAmount {
  32. description += "Bolus Amount: \(amount) units."
  33. } else {
  34. description += "Bolus Amount: unknown."
  35. }
  36. case "temp_target":
  37. let targetDescription = target != nil ? "\(target!) mg/dL" : "unknown target"
  38. let durationDescription = duration != nil ? "\(duration!) minutes" : "unknown duration"
  39. description += "Temp Target: \(targetDescription), Duration: \(durationDescription)."
  40. case "cancel_temp_target":
  41. description += "Cancel Temp Target command."
  42. case "meal":
  43. let carbsDescription = carbs != nil ? "\(carbs!)g carbs" : "unknown carbs"
  44. let fatDescription = fat != nil ? "\(fat!)g fat" : "unknown fat"
  45. let proteinDescription = protein != nil ? "\(protein!)g protein" : "unknown protein"
  46. description += "Meal with \(carbsDescription), \(fatDescription), \(proteinDescription)."
  47. default:
  48. description += "Unsupported command type."
  49. }
  50. return description
  51. }
  52. }