SAge.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // LoopFollow
  2. // SAge.swift
  3. import Foundation
  4. extension MainViewController {
  5. // NS Sage Web Call
  6. func webLoadNSSage() {
  7. let lastDateString = dateTimeUtils.getDateTimeString(addingDays: -60)
  8. let currentTimeString = dateTimeUtils.getDateTimeString()
  9. let parameters: [String: String] = [
  10. "find[eventType]": NightscoutUtils.EventType.sage.rawValue,
  11. "find[created_at][$gte]": lastDateString,
  12. "find[created_at][$lte]": currentTimeString,
  13. "count": "1",
  14. ]
  15. NightscoutUtils.executeRequest(eventType: .sage, parameters: parameters) { (result: Result<[sageData], Error>) in
  16. switch result {
  17. case let .success(data):
  18. DispatchQueue.main.async {
  19. self.updateSage(data: data)
  20. }
  21. case let .failure(error):
  22. LogManager.shared.log(category: .nightscout, message: "webLoadNSSage, failed to fetch data: \(error.localizedDescription)")
  23. }
  24. }
  25. }
  26. // NS Sage Response Processor
  27. func updateSage(data: [sageData]) {
  28. infoManager.clearInfoData(type: .sage)
  29. if data.count == 0 {
  30. return
  31. }
  32. currentSage = data[0]
  33. var lastSageString = data[0].created_at
  34. let formatter = ISO8601DateFormatter()
  35. formatter.formatOptions = [.withFullDate,
  36. .withTime,
  37. .withDashSeparatorInDate,
  38. .withColonSeparatorInTime]
  39. Storage.shared.sageInsertTime.value = formatter.date(from: lastSageString)?.timeIntervalSince1970 as! TimeInterval
  40. // -- Auto-snooze CGM start ────────────────────────────────────────────────
  41. let now = Date()
  42. // 1. Do we *want* the automatic global snooze?
  43. if Storage.shared.alarmConfiguration.value.autoSnoozeCGMStart {
  44. // 2. When did the sensor start?
  45. let insertTime = Storage.shared.sageInsertTime.value
  46. // 3. If the start is less than 2 h ago, snooze *all* alarms for the
  47. // remainder of that 2-hour window.
  48. if now.timeIntervalSince1970 - insertTime < 7200 {
  49. var cfg = Storage.shared.alarmConfiguration.value
  50. cfg.snoozeUntil = Date(timeIntervalSince1970: insertTime + 7200)
  51. Storage.shared.alarmConfiguration.value = cfg
  52. }
  53. }
  54. if let sageTime = formatter.date(from: (lastSageString as! String))?.timeIntervalSince1970 {
  55. let now = dateTimeUtils.getNowTimeIntervalUTC()
  56. let secondsAgo = now - sageTime
  57. let formatter = DateComponentsFormatter()
  58. formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
  59. formatter.allowedUnits = [.day, .hour] // Units to display in the formatted string
  60. formatter.zeroFormattingBehavior = [.pad] // Pad with zeroes where appropriate for the locale
  61. if let formattedDuration = formatter.string(from: secondsAgo) {
  62. infoManager.updateInfoData(type: .sage, value: formattedDuration, numericValue: secondsAgo / 86400)
  63. }
  64. }
  65. }
  66. }