88
99import Foundation
1010import LoopKit
11- import HealthKit
12-
13- public enum RemoteCommandError : LocalizedError {
14- case expired
15- case invalidOTP
16- case missingMaxBolus
17- case exceedsMaxBolus
18- case exceedsMaxCarbs
19- case invalidCarbs
20-
21- public var errorDescription : String ? {
22- get {
23- switch self {
24- case . expired:
25- return NSLocalizedString ( " Expired " , comment: " Remote command error description: expired. " )
26- case . invalidOTP:
27- return NSLocalizedString ( " Invalid OTP " , comment: " Remote command error description: invalid OTP. " )
28- case . missingMaxBolus:
29- return NSLocalizedString ( " Missing maximum allowed bolus in settings " , comment: " Remote command error description: missing maximum bolus in settings. " )
30- case . exceedsMaxBolus:
31- return NSLocalizedString ( " Exceeds maximum allowed bolus in settings " , comment: " Remote command error description: bolus exceeds maximum bolus in settings. " )
32- case . exceedsMaxCarbs:
33- return NSLocalizedString ( " Exceeds maximum allowed carbs " , comment: " Remote command error description: carbs exceed maximum amount. " )
34- case . invalidCarbs:
35- return NSLocalizedString ( " Invalid carb amount " , comment: " Remote command error description: invalid carb amount. " )
36- }
37- }
38- }
39- }
40-
41-
42- enum RemoteCommand {
43- case temporaryScheduleOverride( TemporaryScheduleOverride )
44- case cancelTemporaryOverride
45- case bolusEntry( Double )
46- case carbsEntry( NewCarbEntry )
47- }
48-
4911
5012// Push Notifications
51- extension RemoteCommand {
52- static func createRemoteCommand ( notification: [ String : Any ] , allowedPresets : [ TemporaryScheduleOverridePreset ] , defaultAbsorptionTime : TimeInterval , nowDate : Date = Date ( ) ) -> Result < RemoteCommand , RemoteCommandParseError > {
13+ extension RemoteAction {
14+ static func createRemoteAction ( notification: [ String : Any ] ) -> Result < RemoteAction , RemoteCommandParseError > {
5315 if let overrideName = notification [ " override-name " ] as? String ,
54- let preset = allowedPresets. first ( where: { $0. name == overrideName } ) ,
5516 let remoteAddress = notification [ " remote-address " ] as? String
5617 {
57- var override = preset . createOverride ( enactTrigger : . remote ( remoteAddress ) )
18+ var overrideTime : TimeInterval ? = nil
5819 if let overrideDurationMinutes = notification [ " override-duration-minutes " ] as? Double {
59- override . duration = . finite ( TimeInterval ( minutes: overrideDurationMinutes) )
20+ overrideTime = TimeInterval ( minutes: overrideDurationMinutes)
6021 }
61- return . success( . temporaryScheduleOverride( override) )
62- } else if let _ = notification [ " cancel-temporary-override " ] as? String {
63- return . success( . cancelTemporaryOverride)
22+ return . success( . temporaryScheduleOverride( RemoteOverrideAction ( name: overrideName, durationTime: overrideTime, remoteAddress: remoteAddress) ) )
23+ } else if let _ = notification [ " cancel-temporary-override " ] as? String ,
24+ let remoteAddress = notification [ " remote-address " ] as? String
25+ {
26+ return . success( . cancelTemporaryOverride( RemoteOverrideCancelAction ( remoteAddress: remoteAddress) ) )
6427 } else if let bolusValue = notification [ " bolus-entry " ] as? Double {
65- return . success( . bolusEntry( bolusValue) )
28+ return . success( . bolusEntry( RemoteBolusAction ( amountInUnits : bolusValue) ) )
6629 } else if let carbsValue = notification [ " carbs-entry " ] as? Double {
67-
68- let minAbsorptionTime = TimeInterval ( hours: 0.5 )
69- let maxAbsorptionTime = LoopConstants . maxCarbAbsorptionTime
7030
71- var absorptionTime = defaultAbsorptionTime
31+ var absorptionTime : TimeInterval ? = nil
7232 if let absorptionOverrideInHours = notification [ " absorption-time " ] as? Double {
7333 absorptionTime = TimeInterval ( hours: absorptionOverrideInHours)
7434 }
7535
76- if absorptionTime < minAbsorptionTime || absorptionTime > maxAbsorptionTime {
77- return . failure( RemoteCommandParseError . invalidAbsorptionSeconds ( absorptionTime) )
78- }
79-
80- let quantity = HKQuantity ( unit: . gram( ) , doubleValue: carbsValue)
36+ var foodType = notification [ " food-type " ] as? String ?? nil
8137
82- var startDate = nowDate
38+ var startDate : Date ? = nil
8339 if let notificationStartTimeString = notification [ " start-time " ] as? String {
8440 let formatter = ISO8601DateFormatter ( )
8541 formatter. formatOptions = [ . withInternetDateTime, . withFractionalSeconds]
@@ -90,16 +46,14 @@ extension RemoteCommand {
9046 }
9147 }
9248
93- let newEntry = NewCarbEntry ( quantity: quantity, startDate: startDate, foodType: " " , absorptionTime: absorptionTime)
94- return . success( . carbsEntry( newEntry) )
49+ return . success( . carbsEntry( RemoteCarbAction ( amountInGrams: carbsValue, absorptionTime: absorptionTime, foodType: foodType, startDate: startDate) ) )
9550 } else {
9651 return . failure( RemoteCommandParseError . unhandledNotication ( " \( notification) " ) )
9752 }
9853 }
9954
10055 enum RemoteCommandParseError : LocalizedError {
10156 case invalidStartTime( String )
102- case invalidAbsorptionSeconds( Double )
10357 case unhandledNotication( String )
10458 }
10559}
0 commit comments