TrioRemoteControl+TempTarget.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Foundation
  2. extension TrioRemoteControl {
  3. func handleTempTargetCommand(_ pushMessage: PushMessage) async {
  4. guard let targetValue = pushMessage.target,
  5. let durationValue = pushMessage.duration
  6. else {
  7. await logError("Command rejected: temp target data is incomplete or invalid.", pushMessage: pushMessage)
  8. return
  9. }
  10. let durationInMinutes = Int(durationValue)
  11. let pushMessageDate = Date(timeIntervalSince1970: pushMessage.timestamp)
  12. let tempTarget = TempTarget(
  13. name: TempTarget.custom,
  14. createdAt: pushMessageDate,
  15. targetTop: Decimal(targetValue),
  16. targetBottom: Decimal(targetValue),
  17. duration: Decimal(durationInMinutes),
  18. enteredBy: TempTarget.manual,
  19. reason: TempTarget.custom,
  20. isPreset: false,
  21. enabled: true,
  22. halfBasalTarget: settings.preferences.halfBasalExerciseTarget
  23. )
  24. // TODO: this should probably be try-catch'd ?
  25. await tempTargetsStorage.storeTempTarget(tempTarget: tempTarget)
  26. debug(
  27. .remoteControl,
  28. "Remote command processed successfully. \(pushMessage.humanReadableDescription())"
  29. )
  30. }
  31. func cancelTempTarget(_ pushMessage: PushMessage) async {
  32. debug(.remoteControl, "Cancelling temp target.")
  33. guard tempTargetsStorage.current() != nil else {
  34. await logError("Command rejected: no active temp target to cancel.")
  35. return
  36. }
  37. let cancelEntry = TempTarget.cancel(at: Date())
  38. await tempTargetsStorage.storeTempTarget(tempTarget: cancelEntry)
  39. debug(
  40. .remoteControl,
  41. "Remote command processed successfully. \(pushMessage.humanReadableDescription())"
  42. )
  43. }
  44. }