|
@@ -1,8 +1,34 @@
|
|
|
import Foundation
|
|
import Foundation
|
|
|
|
|
|
|
|
-struct PushMessage: Decodable {
|
|
|
|
|
|
|
+enum TRCCommandType: String, Codable {
|
|
|
|
|
+ case bolus
|
|
|
|
|
+ case tempTarget = "temp_target"
|
|
|
|
|
+ case cancelTempTarget = "cancel_temp_target"
|
|
|
|
|
+ case meal
|
|
|
|
|
+ case startOverride = "start_override"
|
|
|
|
|
+ case cancelOverride = "cancel_override"
|
|
|
|
|
+
|
|
|
|
|
+ var description: String {
|
|
|
|
|
+ switch self {
|
|
|
|
|
+ case .bolus:
|
|
|
|
|
+ return "Bolus"
|
|
|
|
|
+ case .tempTarget:
|
|
|
|
|
+ return "Temporary Target"
|
|
|
|
|
+ case .cancelTempTarget:
|
|
|
|
|
+ return "Cancel Temporary Target"
|
|
|
|
|
+ case .meal:
|
|
|
|
|
+ return "Meal"
|
|
|
|
|
+ case .startOverride:
|
|
|
|
|
+ return "Start Override"
|
|
|
|
|
+ case .cancelOverride:
|
|
|
|
|
+ return "Cancel Override"
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+struct PushMessage: Codable {
|
|
|
var user: String
|
|
var user: String
|
|
|
- var commandType: String
|
|
|
|
|
|
|
+ var commandType: TRCCommandType
|
|
|
var bolusAmount: Decimal?
|
|
var bolusAmount: Decimal?
|
|
|
var target: Int?
|
|
var target: Int?
|
|
|
var duration: Int?
|
|
var duration: Int?
|
|
@@ -11,8 +37,10 @@ struct PushMessage: Decodable {
|
|
|
var fat: Int?
|
|
var fat: Int?
|
|
|
var sharedSecret: String
|
|
var sharedSecret: String
|
|
|
var timestamp: TimeInterval
|
|
var timestamp: TimeInterval
|
|
|
|
|
+ var overrideName: String?
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
+ case aps
|
|
|
case user
|
|
case user
|
|
|
case commandType = "command_type"
|
|
case commandType = "command_type"
|
|
|
case bolusAmount = "bolus_amount"
|
|
case bolusAmount = "bolus_amount"
|
|
@@ -23,33 +51,100 @@ struct PushMessage: Decodable {
|
|
|
case fat
|
|
case fat
|
|
|
case sharedSecret = "shared_secret"
|
|
case sharedSecret = "shared_secret"
|
|
|
case timestamp
|
|
case timestamp
|
|
|
|
|
+ case overrideName
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ func encode(to encoder: Encoder) throws {
|
|
|
|
|
+ var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
+ try container.encode(user, forKey: .user)
|
|
|
|
|
+ try container.encode(commandType, forKey: .commandType)
|
|
|
|
|
+ try container.encodeIfPresent(bolusAmount, forKey: .bolusAmount)
|
|
|
|
|
+ try container.encodeIfPresent(target, forKey: .target)
|
|
|
|
|
+ try container.encodeIfPresent(duration, forKey: .duration)
|
|
|
|
|
+ try container.encodeIfPresent(carbs, forKey: .carbs)
|
|
|
|
|
+ try container.encodeIfPresent(protein, forKey: .protein)
|
|
|
|
|
+ try container.encodeIfPresent(fat, forKey: .fat)
|
|
|
|
|
+ try container.encode(sharedSecret, forKey: .sharedSecret)
|
|
|
|
|
+ try container.encode(timestamp, forKey: .timestamp)
|
|
|
|
|
+ try container.encodeIfPresent(overrideName, forKey: .overrideName)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ init(from decoder: Decoder) throws {
|
|
|
|
|
+ let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
+ user = try container.decode(String.self, forKey: .user)
|
|
|
|
|
+ commandType = try container.decode(TRCCommandType.self, forKey: .commandType)
|
|
|
|
|
+ bolusAmount = try container.decodeIfPresent(Decimal.self, forKey: .bolusAmount)
|
|
|
|
|
+ target = try container.decodeIfPresent(Int.self, forKey: .target)
|
|
|
|
|
+ duration = try container.decodeIfPresent(Int.self, forKey: .duration)
|
|
|
|
|
+ carbs = try container.decodeIfPresent(Int.self, forKey: .carbs)
|
|
|
|
|
+ protein = try container.decodeIfPresent(Int.self, forKey: .protein)
|
|
|
|
|
+ fat = try container.decodeIfPresent(Int.self, forKey: .fat)
|
|
|
|
|
+ sharedSecret = try container.decode(String.self, forKey: .sharedSecret)
|
|
|
|
|
+ timestamp = try container.decode(TimeInterval.self, forKey: .timestamp)
|
|
|
|
|
+ overrideName = try container.decodeIfPresent(String.self, forKey: .overrideName)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ init(
|
|
|
|
|
+ user: String,
|
|
|
|
|
+ commandType: TRCCommandType,
|
|
|
|
|
+ bolusAmount: Decimal? = nil,
|
|
|
|
|
+ target: Int? = nil,
|
|
|
|
|
+ duration: Int? = nil,
|
|
|
|
|
+ carbs: Int? = nil,
|
|
|
|
|
+ protein: Int? = nil,
|
|
|
|
|
+ fat: Int? = nil,
|
|
|
|
|
+ sharedSecret: String,
|
|
|
|
|
+ timestamp: TimeInterval,
|
|
|
|
|
+ overrideName: String? = nil
|
|
|
|
|
+ ) {
|
|
|
|
|
+ self.user = user
|
|
|
|
|
+ self.commandType = commandType
|
|
|
|
|
+ self.bolusAmount = bolusAmount
|
|
|
|
|
+ self.target = target
|
|
|
|
|
+ self.duration = duration
|
|
|
|
|
+ self.carbs = carbs
|
|
|
|
|
+ self.protein = protein
|
|
|
|
|
+ self.fat = fat
|
|
|
|
|
+ self.sharedSecret = sharedSecret
|
|
|
|
|
+ self.timestamp = timestamp
|
|
|
|
|
+ self.overrideName = overrideName
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-extension PushMessage {
|
|
|
|
|
func humanReadableDescription() -> String {
|
|
func humanReadableDescription() -> String {
|
|
|
- var description = "User: \(user). Command Type: \(commandType). "
|
|
|
|
|
|
|
+ var description = "User: \(user). Command Type: \(commandType.description). "
|
|
|
|
|
+
|
|
|
|
|
+ if let override = overrideName {
|
|
|
|
|
+ description += "Override Name: \(override). "
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
switch commandType {
|
|
switch commandType {
|
|
|
- case "bolus":
|
|
|
|
|
|
|
+ case .bolus:
|
|
|
if let amount = bolusAmount {
|
|
if let amount = bolusAmount {
|
|
|
description += "Bolus Amount: \(amount) units."
|
|
description += "Bolus Amount: \(amount) units."
|
|
|
} else {
|
|
} else {
|
|
|
description += "Bolus Amount: unknown."
|
|
description += "Bolus Amount: unknown."
|
|
|
}
|
|
}
|
|
|
- case "temp_target":
|
|
|
|
|
- let targetDescription = target != nil ? "\(target!) mg/dL" : "unknown target"
|
|
|
|
|
- let durationDescription = duration != nil ? "\(duration!) minutes" : "unknown duration"
|
|
|
|
|
- description += "Temp Target: \(targetDescription), Duration: \(durationDescription)."
|
|
|
|
|
- case "cancel_temp_target":
|
|
|
|
|
|
|
+ case .tempTarget:
|
|
|
|
|
+ let targetDesc = target != nil ? "\(target!) mg/dL" : "unknown target"
|
|
|
|
|
+ let durationDesc = duration != nil ? "\(duration!) minutes" : "unknown duration"
|
|
|
|
|
+ description += "Temp Target: \(targetDesc), Duration: \(durationDesc)."
|
|
|
|
|
+ case .cancelTempTarget:
|
|
|
description += "Cancel Temp Target command."
|
|
description += "Cancel Temp Target command."
|
|
|
- case "meal":
|
|
|
|
|
- let carbsDescription = carbs != nil ? "\(carbs!)g carbs" : "unknown carbs"
|
|
|
|
|
- let fatDescription = fat != nil ? "\(fat!)g fat" : "unknown fat"
|
|
|
|
|
- let proteinDescription = protein != nil ? "\(protein!)g protein" : "unknown protein"
|
|
|
|
|
- description += "Meal with \(carbsDescription), \(fatDescription), \(proteinDescription)."
|
|
|
|
|
- default:
|
|
|
|
|
- description += "Unsupported command type."
|
|
|
|
|
|
|
+ case .meal:
|
|
|
|
|
+ let carbsDesc = carbs != nil ? "\(carbs!)g carbs" : "unknown carbs"
|
|
|
|
|
+ let fatDesc = fat != nil ? "\(fat!)g fat" : "unknown fat"
|
|
|
|
|
+ let proteinDesc = protein != nil ? "\(protein!)g protein" : "unknown protein"
|
|
|
|
|
+ description += "Meal with \(carbsDesc), \(fatDesc), \(proteinDesc)."
|
|
|
|
|
+ case .startOverride:
|
|
|
|
|
+ if let override = overrideName {
|
|
|
|
|
+ description += "Start Override: \(override)."
|
|
|
|
|
+ } else {
|
|
|
|
|
+ description += "Start Override: unknown override name."
|
|
|
|
|
+ }
|
|
|
|
|
+ case .cancelOverride:
|
|
|
|
|
+ description += "Cancel Override command."
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return description
|
|
return description
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|