NightscoutConfigStateModel.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import Combine
  2. import CoreData
  3. import G7SensorKit
  4. import LoopKit
  5. import SwiftDate
  6. import SwiftUI
  7. extension NightscoutConfig {
  8. final class StateModel: BaseStateModel<Provider> {
  9. @Injected() private var keychain: Keychain!
  10. @Injected() private var nightscoutManager: NightscoutManager!
  11. @Injected() private var glucoseStorage: GlucoseStorage!
  12. @Injected() private var healthKitManager: HealthKitManager!
  13. @Injected() private var cgmManager: FetchGlucoseManager!
  14. @Injected() private var storage: FileStorage!
  15. @Injected() var apsManager: APSManager!
  16. let coredataContext = CoreDataStack.shared.newTaskContext()
  17. @Published var url = ""
  18. @Published var secret = ""
  19. @Published var message = ""
  20. @Published var connecting = false
  21. @Published var backfilling = false
  22. @Published var isUploadEnabled = false // Allow uploads
  23. @Published var isDownloadEnabled = false // Allow downloads
  24. @Published var uploadGlucose = true // Upload Glucose
  25. @Published var changeUploadGlucose = true // if plugin, need to be change in CGM configuration
  26. @Published var useLocalSource = false
  27. @Published var localPort: Decimal = 0
  28. @Published var units: GlucoseUnits = .mgdL
  29. @Published var dia: Decimal = 6
  30. @Published var maxBasal: Decimal = 2
  31. @Published var maxBolus: Decimal = 10
  32. @Published var allowAnnouncements: Bool = false
  33. @Published var isConnectedToNS: Bool = false
  34. override func subscribe() {
  35. url = keychain.getValue(String.self, forKey: Config.urlKey) ?? ""
  36. secret = keychain.getValue(String.self, forKey: Config.secretKey) ?? ""
  37. units = settingsManager.settings.units
  38. dia = settingsManager.pumpSettings.insulinActionCurve
  39. maxBasal = settingsManager.pumpSettings.maxBasal
  40. maxBolus = settingsManager.pumpSettings.maxBolus
  41. changeUploadGlucose = (cgmManager.cgmGlucoseSourceType != CGMType.plugin)
  42. subscribeSetting(\.allowAnnouncements, on: $allowAnnouncements) { allowAnnouncements = $0 }
  43. subscribeSetting(\.isUploadEnabled, on: $isUploadEnabled) { isUploadEnabled = $0 }
  44. subscribeSetting(\.isDownloadEnabled, on: $isDownloadEnabled) { isDownloadEnabled = $0 }
  45. subscribeSetting(\.useLocalGlucoseSource, on: $useLocalSource) { useLocalSource = $0 }
  46. subscribeSetting(\.localGlucosePort, on: $localPort.map(Int.init)) { localPort = Decimal($0) }
  47. subscribeSetting(\.uploadGlucose, on: $uploadGlucose, initial: { uploadGlucose = $0 })
  48. isConnectedToNS = nightscoutAPI != nil
  49. }
  50. func connect() {
  51. if let CheckURL = url.last, CheckURL == "/" {
  52. let fixedURL = url.dropLast()
  53. url = String(fixedURL)
  54. }
  55. guard let url = URL(string: url) else {
  56. message = "Invalid URL"
  57. return
  58. }
  59. connecting = true
  60. message = ""
  61. provider.checkConnection(url: url, secret: secret.isEmpty ? nil : secret)
  62. .receive(on: DispatchQueue.main)
  63. .sink { completion in
  64. switch completion {
  65. case .finished: break
  66. case let .failure(error):
  67. self.message = "Error: \(error.localizedDescription)"
  68. }
  69. self.connecting = false
  70. } receiveValue: {
  71. self.message = "Connected!"
  72. self.keychain.setValue(self.url, forKey: Config.urlKey)
  73. self.keychain.setValue(self.secret, forKey: Config.secretKey)
  74. self.connecting = true
  75. self.isConnectedToNS = self.nightscoutAPI != nil
  76. }
  77. .store(in: &lifetime)
  78. }
  79. private var nightscoutAPI: NightscoutAPI? {
  80. guard let urlString = keychain.getValue(String.self, forKey: NightscoutConfig.Config.urlKey),
  81. let url = URL(string: urlString),
  82. let secret = keychain.getValue(String.self, forKey: NightscoutConfig.Config.secretKey)
  83. else {
  84. return nil
  85. }
  86. return NightscoutAPI(url: url, secret: secret)
  87. }
  88. private func getMedianTarget(
  89. lowTargetValue: Decimal,
  90. lowTargetTime: String,
  91. highTarget: [NightscoutTimevalue],
  92. units: GlucoseUnits
  93. ) -> Decimal {
  94. if let idx = highTarget.firstIndex(where: { $0.time == lowTargetTime }) {
  95. let median = (lowTargetValue + highTarget[idx].value) / 2
  96. switch units {
  97. case .mgdL:
  98. return Decimal(round(Double(median)))
  99. case .mmolL:
  100. return Decimal(round(Double(median) * 10) / 10)
  101. }
  102. }
  103. return lowTargetValue
  104. }
  105. func importSettings() {
  106. guard let nightscout = nightscoutAPI else {
  107. saveError("Can't access nightscoutAPI")
  108. return
  109. }
  110. let group = DispatchGroup()
  111. group.enter()
  112. var error = ""
  113. let path = "/api/v1/profile.json"
  114. let timeout: TimeInterval = 60
  115. var components = URLComponents()
  116. components.scheme = nightscout.url.scheme
  117. components.host = nightscout.url.host
  118. components.port = nightscout.url.port
  119. components.path = path
  120. components.queryItems = [
  121. URLQueryItem(name: "count", value: "1")
  122. ]
  123. var url = URLRequest(url: components.url!)
  124. url.allowsConstrainedNetworkAccess = false
  125. url.timeoutInterval = timeout
  126. if let secret = nightscout.secret {
  127. url.addValue(secret.sha1(), forHTTPHeaderField: "api-secret")
  128. }
  129. let task = URLSession.shared.dataTask(with: url) { data, response, error_ in
  130. if let error_ = error_ {
  131. print("Error occured: " + error_.localizedDescription)
  132. // handle error
  133. self.saveError("Error occured: " + error_.localizedDescription)
  134. error = error_.localizedDescription
  135. return
  136. }
  137. guard let httpResponse = response as? HTTPURLResponse,
  138. (200 ... 299).contains(httpResponse.statusCode)
  139. else {
  140. print("Error occured! " + error_.debugDescription)
  141. // handle error
  142. self.saveError(error_.debugDescription)
  143. return
  144. }
  145. let jsonDecoder = JSONCoding.decoder
  146. if let mimeType = httpResponse.mimeType, mimeType == "application/json",
  147. let data = data
  148. {
  149. do {
  150. let fetchedProfileStore = try jsonDecoder.decode([FetchedNightscoutProfileStore].self, from: data)
  151. let loop = fetchedProfileStore.first?.enteredBy.contains("Loop")
  152. guard let fetchedProfile: FetchedNightscoutProfile =
  153. (fetchedProfileStore.first?.store["default"] != nil) ?
  154. fetchedProfileStore.first?.store["default"] :
  155. fetchedProfileStore.first?.store["Default"]
  156. else {
  157. error = "\nCan't find the default Nightscout Profile."
  158. group.leave()
  159. return
  160. }
  161. guard fetchedProfile.units.contains(self.units.rawValue.prefix(4)) else {
  162. debug(
  163. .nightscout,
  164. "Mismatching glucose units in Nightscout and Pump Settings. Import settings aborted."
  165. )
  166. error = "\nMismatching glucose units in Nightscout and Pump Settings. Import settings aborted."
  167. group.leave()
  168. return
  169. }
  170. var areCRsOK = true
  171. let carbratios = fetchedProfile.carbratio
  172. .map { carbratio -> CarbRatioEntry in
  173. if carbratio.value <= 0 {
  174. error =
  175. "\nInvalid Carb Ratio settings in Nightscout.\n\nImport aborted. Please check your Nightscout Profile Carb Ratios Settings!"
  176. areCRsOK = false
  177. }
  178. return CarbRatioEntry(
  179. start: carbratio.time,
  180. offset: self.offset(carbratio.time) / 60,
  181. ratio: carbratio.value
  182. ) }
  183. let carbratiosProfile = CarbRatios(units: CarbUnit.grams, schedule: carbratios)
  184. guard areCRsOK else {
  185. group.leave()
  186. return
  187. }
  188. var areBasalsOK = true
  189. let pumpName = self.apsManager.pumpName.value
  190. let basals = fetchedProfile.basal
  191. .map { basal -> BasalProfileEntry in
  192. if pumpName != "Omnipod DASH", basal.value <= 0
  193. {
  194. error =
  195. "\nInvalid Nightcsout Basal Settings. Some or all of your basal settings are 0 U/h.\n\nImport aborted. Please check your Nightscout Profile Basal Settings before trying to import again. Import has been aborted.)"
  196. areBasalsOK = false
  197. }
  198. return BasalProfileEntry(
  199. start: basal.time,
  200. minutes: self.offset(basal.time) / 60,
  201. rate: basal.value
  202. ) }
  203. // DASH pumps can have 0U/h basal rates but don't import if total basals (24 hours) amount to 0 U.
  204. if pumpName == "Omnipod DASH", basals.map({ each in each.rate }).reduce(0, +) <= 0
  205. {
  206. error =
  207. "\nYour total Basal insulin amount to 0 U or lower in Nightscout Profile settings.\n\n Please check your Nightscout Profile Basal Settings before trying to import again. Import has been aborted.)"
  208. areBasalsOK = false
  209. }
  210. guard areBasalsOK else {
  211. group.leave()
  212. return
  213. }
  214. let sensitivities = fetchedProfile.sens.map { sensitivity -> InsulinSensitivityEntry in
  215. InsulinSensitivityEntry(
  216. sensitivity: sensitivity.value,
  217. offset: self.offset(sensitivity.time) / 60,
  218. start: sensitivity.time
  219. )
  220. }
  221. if sensitivities.filter({ $0.sensitivity <= 0 }).isNotEmpty {
  222. error =
  223. "\nInvalid Nightscout Sensitivities Settings. \n\nImport aborted. Please check your Nightscout Profile Sensitivities Settings!"
  224. group.leave()
  225. return
  226. }
  227. let sensitivitiesProfile = InsulinSensitivities(
  228. units: self.units,
  229. userPrefferedUnits: self.units,
  230. sensitivities: sensitivities
  231. )
  232. let targets = fetchedProfile.target_low
  233. .map { target -> BGTargetEntry in
  234. let median = loop! ? self.getMedianTarget(
  235. lowTargetValue: target.value,
  236. lowTargetTime: target.time,
  237. highTarget: fetchedProfile.target_high,
  238. units: self.units
  239. ) : target.value
  240. return BGTargetEntry(
  241. low: median,
  242. high: median,
  243. start: target.time,
  244. offset: self.offset(target.time) / 60
  245. ) }
  246. let targetsProfile = BGTargets(
  247. units: self.units,
  248. userPrefferedUnits: self.units,
  249. targets: targets
  250. )
  251. // IS THERE A PUMP?
  252. guard let pump = self.apsManager.pumpManager else {
  253. self.storage.save(carbratiosProfile, as: OpenAPS.Settings.carbRatios)
  254. self.storage.save(basals, as: OpenAPS.Settings.basalProfile)
  255. self.storage.save(sensitivitiesProfile, as: OpenAPS.Settings.insulinSensitivities)
  256. self.storage.save(targetsProfile, as: OpenAPS.Settings.bgTargets)
  257. debug(
  258. .service,
  259. "Settings were imported but the Basals couldn't be saved to pump (No pump). Check your basal settings and tap ´Save on Pump´ to sync the new basal settings"
  260. )
  261. error =
  262. "\nSettings were imported but the Basals couldn't be saved to pump (No pump). Check your basal settings and tap ´Save on Pump´ to sync the new basal settings"
  263. group.leave()
  264. return
  265. }
  266. let syncValues = basals.map {
  267. RepeatingScheduleValue(startTime: TimeInterval($0.minutes * 60), value: Double($0.rate))
  268. }
  269. // SSAVE TO STORAGE. SAVE TO PUMP (LoopKit)
  270. pump.syncBasalRateSchedule(items: syncValues) { result in
  271. switch result {
  272. case .success:
  273. self.storage.save(basals, as: OpenAPS.Settings.basalProfile)
  274. self.storage.save(carbratiosProfile, as: OpenAPS.Settings.carbRatios)
  275. self.storage.save(sensitivitiesProfile, as: OpenAPS.Settings.insulinSensitivities)
  276. self.storage.save(targetsProfile, as: OpenAPS.Settings.bgTargets)
  277. debug(.service, "Settings have been imported and the Basals saved to pump!")
  278. // DIA. Save if changed.
  279. let dia = fetchedProfile.dia
  280. print("dia: " + dia.description)
  281. print("pump dia: " + self.dia.description)
  282. if dia != self.dia, dia >= 0 {
  283. let file = PumpSettings(
  284. insulinActionCurve: dia,
  285. maxBolus: self.maxBolus,
  286. maxBasal: self.maxBasal
  287. )
  288. self.storage.save(file, as: OpenAPS.Settings.settings)
  289. debug(.nightscout, "DIA setting updated to " + dia.description + " after a NS import.")
  290. }
  291. group.leave()
  292. case .failure:
  293. error =
  294. "\nSettings were imported but the Basals couldn't be saved to pump (communication error). Check your basal settings and tap ´Save on Pump´ to sync the new basal settings"
  295. debug(.service, "Basals couldn't be save to pump")
  296. group.leave()
  297. }
  298. }
  299. } catch let parsingError {
  300. print(parsingError)
  301. error = parsingError.localizedDescription
  302. group.leave()
  303. }
  304. }
  305. }
  306. task.resume()
  307. group.wait(wallTimeout: .now() + 5)
  308. group.notify(queue: .global(qos: .background)) {
  309. self.saveError(error)
  310. }
  311. }
  312. func offset(_ string: String) -> Int {
  313. let hours = Int(string.prefix(2)) ?? 0
  314. let minutes = Int(string.suffix(2)) ?? 0
  315. return ((hours * 60) + minutes) * 60
  316. }
  317. func saveError(_ string: String) {
  318. coredataContext.performAndWait {
  319. let saveToCoreData = ImportError(context: self.coredataContext)
  320. saveToCoreData.date = Date()
  321. saveToCoreData.error = string
  322. if coredataContext.hasChanges {
  323. try? coredataContext.save()
  324. }
  325. }
  326. }
  327. func backfillGlucose() async {
  328. backfilling = true
  329. let glucose = await nightscoutManager.fetchGlucose(since: Date().addingTimeInterval(-1.days.timeInterval))
  330. if glucose.isNotEmpty {
  331. await MainActor.run {
  332. self.backfilling = false
  333. self.healthKitManager.saveIfNeeded(bloodGlucose: glucose)
  334. self.glucoseStorage.storeGlucose(glucose)
  335. }
  336. } else {
  337. await MainActor.run {
  338. self.backfilling = false
  339. }
  340. }
  341. }
  342. func delete() {
  343. keychain.removeObject(forKey: Config.urlKey)
  344. keychain.removeObject(forKey: Config.secretKey)
  345. url = ""
  346. secret = ""
  347. isConnectedToNS = false
  348. }
  349. }
  350. }