APSManager.swift 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. import Combine
  2. import CoreData
  3. import Foundation
  4. import LoopKit
  5. import LoopKitUI
  6. import OmniBLE
  7. import OmniKit
  8. import RileyLinkKit
  9. import SwiftDate
  10. import Swinject
  11. protocol APSManager {
  12. func heartbeat(date: Date)
  13. func autotune() -> AnyPublisher<Autotune?, Never>
  14. func enactBolus(amount: Double, isSMB: Bool)
  15. var pumpManager: PumpManagerUI? { get set }
  16. var bluetoothManager: BluetoothStateManager? { get }
  17. var pumpDisplayState: CurrentValueSubject<PumpDisplayState?, Never> { get }
  18. var pumpName: CurrentValueSubject<String, Never> { get }
  19. var isLooping: CurrentValueSubject<Bool, Never> { get }
  20. var lastLoopDate: Date { get }
  21. var lastLoopDateSubject: PassthroughSubject<Date, Never> { get }
  22. var bolusProgress: CurrentValueSubject<Decimal?, Never> { get }
  23. var pumpExpiresAtDate: CurrentValueSubject<Date?, Never> { get }
  24. var isManualTempBasal: Bool { get }
  25. func enactTempBasal(rate: Double, duration: TimeInterval)
  26. func makeProfiles() -> AnyPublisher<Bool, Never>
  27. func determineBasal() -> AnyPublisher<Bool, Never>
  28. func determineBasalSync()
  29. func roundBolus(amount: Decimal) -> Decimal
  30. var lastError: CurrentValueSubject<Error?, Never> { get }
  31. func cancelBolus()
  32. func enactAnnouncement(_ announcement: Announcement)
  33. }
  34. enum APSError: LocalizedError {
  35. case pumpError(Error)
  36. case invalidPumpState(message: String)
  37. case glucoseError(message: String)
  38. case apsError(message: String)
  39. case deviceSyncError(message: String)
  40. case manualBasalTemp(message: String)
  41. var errorDescription: String? {
  42. switch self {
  43. case let .pumpError(error):
  44. return "Pump error: \(error.localizedDescription)"
  45. case let .invalidPumpState(message):
  46. return "Error: Invalid Pump State: \(message)"
  47. case let .glucoseError(message):
  48. return "Error: Invalid glucose: \(message)"
  49. case let .apsError(message):
  50. return "APS error: \(message)"
  51. case let .deviceSyncError(message):
  52. return "Sync error: \(message)"
  53. case let .manualBasalTemp(message):
  54. return "Manual Basal Temp : \(message)"
  55. }
  56. }
  57. }
  58. final class BaseAPSManager: APSManager, Injectable {
  59. private let processQueue = DispatchQueue(label: "BaseAPSManager.processQueue")
  60. @Injected() private var storage: FileStorage!
  61. @Injected() private var pumpHistoryStorage: PumpHistoryStorage!
  62. @Injected() private var alertHistoryStorage: AlertHistoryStorage!
  63. @Injected() private var glucoseStorage: GlucoseStorage!
  64. @Injected() private var tempTargetsStorage: TempTargetsStorage!
  65. @Injected() private var carbsStorage: CarbsStorage!
  66. @Injected() private var announcementsStorage: AnnouncementsStorage!
  67. @Injected() private var deviceDataManager: DeviceDataManager!
  68. @Injected() private var nightscout: NightscoutManager!
  69. @Injected() private var settingsManager: SettingsManager!
  70. @Injected() private var broadcaster: Broadcaster!
  71. @Persisted(key: "lastAutotuneDate") private var lastAutotuneDate = Date()
  72. @Persisted(key: "lastStartLoopDate") private var lastStartLoopDate: Date = .distantPast
  73. @Persisted(key: "lastLoopDate") var lastLoopDate: Date = .distantPast {
  74. didSet {
  75. lastLoopDateSubject.send(lastLoopDate)
  76. }
  77. }
  78. let coredataContext = CoreDataStack.shared.persistentContainer.newBackgroundContext()
  79. private var openAPS: OpenAPS!
  80. private var lifetime = Lifetime()
  81. private var backGroundTaskID: UIBackgroundTaskIdentifier?
  82. var pumpManager: PumpManagerUI? {
  83. get { deviceDataManager.pumpManager }
  84. set { deviceDataManager.pumpManager = newValue }
  85. }
  86. var bluetoothManager: BluetoothStateManager? { deviceDataManager.bluetoothManager }
  87. @Persisted(key: "isManualTempBasal") var isManualTempBasal: Bool = false
  88. let isLooping = CurrentValueSubject<Bool, Never>(false)
  89. let lastLoopDateSubject = PassthroughSubject<Date, Never>()
  90. let lastError = CurrentValueSubject<Error?, Never>(nil)
  91. let bolusProgress = CurrentValueSubject<Decimal?, Never>(nil)
  92. var pumpDisplayState: CurrentValueSubject<PumpDisplayState?, Never> {
  93. deviceDataManager.pumpDisplayState
  94. }
  95. var pumpName: CurrentValueSubject<String, Never> {
  96. deviceDataManager.pumpName
  97. }
  98. var pumpExpiresAtDate: CurrentValueSubject<Date?, Never> {
  99. deviceDataManager.pumpExpiresAtDate
  100. }
  101. var settings: FreeAPSSettings {
  102. get { settingsManager.settings }
  103. set { settingsManager.settings = newValue }
  104. }
  105. init(resolver: Resolver) {
  106. injectServices(resolver)
  107. openAPS = OpenAPS(storage: storage)
  108. subscribe()
  109. lastLoopDateSubject.send(lastLoopDate)
  110. isLooping
  111. .weakAssign(to: \.deviceDataManager.loopInProgress, on: self)
  112. .store(in: &lifetime)
  113. }
  114. private func subscribe() {
  115. deviceDataManager.recommendsLoop
  116. .receive(on: processQueue)
  117. .sink { [weak self] in
  118. self?.loop()
  119. }
  120. .store(in: &lifetime)
  121. pumpManager?.addStatusObserver(self, queue: processQueue)
  122. deviceDataManager.errorSubject
  123. .receive(on: processQueue)
  124. .map { APSError.pumpError($0) }
  125. .sink {
  126. self.processError($0)
  127. }
  128. .store(in: &lifetime)
  129. deviceDataManager.bolusTrigger
  130. .receive(on: processQueue)
  131. .sink { bolusing in
  132. if bolusing {
  133. self.createBolusReporter()
  134. } else {
  135. self.clearBolusReporter()
  136. }
  137. }
  138. .store(in: &lifetime)
  139. // manage a manual Temp Basal from OmniPod - Force loop() after stop a temp basal or finished
  140. deviceDataManager.manualTempBasal
  141. .receive(on: processQueue)
  142. .sink { manualBasal in
  143. if manualBasal {
  144. self.isManualTempBasal = true
  145. } else {
  146. if self.isManualTempBasal {
  147. self.isManualTempBasal = false
  148. self.loop()
  149. }
  150. }
  151. }
  152. .store(in: &lifetime)
  153. }
  154. func heartbeat(date: Date) {
  155. deviceDataManager.heartbeat(date: date)
  156. }
  157. // Loop entry point
  158. private func loop() {
  159. // check the last start of looping is more the loopInterval but the previous loop was completed
  160. if lastLoopDate > lastStartLoopDate {
  161. guard lastStartLoopDate.addingTimeInterval(Config.loopInterval) < Date() else {
  162. debug(.apsManager, "too close to do a loop : \(lastStartLoopDate)")
  163. return
  164. }
  165. }
  166. guard !isLooping.value else {
  167. warning(.apsManager, "Loop already in progress. Skip recommendation.")
  168. return
  169. }
  170. // start background time extension
  171. backGroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "Loop starting") {
  172. guard let backgroundTask = self.backGroundTaskID else { return }
  173. UIApplication.shared.endBackgroundTask(backgroundTask)
  174. self.backGroundTaskID = .invalid
  175. }
  176. debug(.apsManager, "Starting loop with a delay of \(UIApplication.shared.backgroundTimeRemaining.rounded())")
  177. lastStartLoopDate = Date()
  178. var loopStatRecord = LoopStats(
  179. start: lastStartLoopDate,
  180. loopStatus: "Starting"
  181. )
  182. isLooping.send(true)
  183. determineBasal()
  184. .replaceEmpty(with: false)
  185. .flatMap { [weak self] success -> AnyPublisher<Void, Error> in
  186. guard let self = self, success else {
  187. return Fail(error: APSError.apsError(message: "Determine basal failed")).eraseToAnyPublisher()
  188. }
  189. // Open loop completed
  190. guard self.settings.closedLoop else {
  191. self.nightscout.uploadStatus()
  192. return Just(()).setFailureType(to: Error.self).eraseToAnyPublisher()
  193. }
  194. self.nightscout.uploadStatus()
  195. // Closed loop - enact suggested
  196. return self.enactSuggested()
  197. }
  198. .sink { [weak self] completion in
  199. guard let self = self else { return }
  200. loopStatRecord.end = Date()
  201. loopStatRecord.duration = self.roundDouble(
  202. (loopStatRecord.end! - loopStatRecord.start).timeInterval / 60,
  203. 2
  204. )
  205. if case let .failure(error) = completion {
  206. loopStatRecord.loopStatus = error.localizedDescription
  207. self.loopCompleted(error: error, loopStatRecord: loopStatRecord)
  208. } else {
  209. loopStatRecord.loopStatus = "Success"
  210. self.loopCompleted(loopStatRecord: loopStatRecord)
  211. }
  212. } receiveValue: {}
  213. .store(in: &lifetime)
  214. }
  215. // Loop exit point
  216. private func loopCompleted(error: Error? = nil, loopStatRecord: LoopStats) {
  217. isLooping.send(false)
  218. if let error = error {
  219. warning(.apsManager, "Loop failed with error: \(error.localizedDescription)")
  220. if let backgroundTask = backGroundTaskID {
  221. UIApplication.shared.endBackgroundTask(backgroundTask)
  222. backGroundTaskID = .invalid
  223. }
  224. processError(error)
  225. } else {
  226. debug(.apsManager, "Loop succeeded")
  227. lastLoopDate = Date()
  228. lastError.send(nil)
  229. }
  230. loopStats(loopStatRecord: loopStatRecord)
  231. if settings.closedLoop {
  232. reportEnacted(received: error == nil)
  233. }
  234. // end of the BG tasks
  235. if let backgroundTask = backGroundTaskID {
  236. UIApplication.shared.endBackgroundTask(backgroundTask)
  237. backGroundTaskID = .invalid
  238. }
  239. }
  240. private func verifyStatus() -> Error? {
  241. guard let pump = pumpManager else {
  242. return APSError.invalidPumpState(message: "Pump not set")
  243. }
  244. let status = pump.status.pumpStatus
  245. guard !status.bolusing else {
  246. return APSError.invalidPumpState(message: "Pump is bolusing")
  247. }
  248. guard !status.suspended else {
  249. return APSError.invalidPumpState(message: "Pump suspended")
  250. }
  251. let reservoir = storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self) ?? 100
  252. guard reservoir >= 0 else {
  253. return APSError.invalidPumpState(message: "Reservoir is empty")
  254. }
  255. return nil
  256. }
  257. private func autosens() -> AnyPublisher<Bool, Never> {
  258. guard let autosens = storage.retrieve(OpenAPS.Settings.autosense, as: Autosens.self),
  259. (autosens.timestamp ?? .distantPast).addingTimeInterval(30.minutes.timeInterval) > Date()
  260. else {
  261. return openAPS.autosense()
  262. .map { $0 != nil }
  263. .eraseToAnyPublisher()
  264. }
  265. return Just(false).eraseToAnyPublisher()
  266. }
  267. func determineBasal() -> AnyPublisher<Bool, Never> {
  268. debug(.apsManager, "Start determine basal")
  269. guard let glucose = storage.retrieve(OpenAPS.Monitor.glucose, as: [BloodGlucose].self), glucose.isNotEmpty else {
  270. debug(.apsManager, "Not enough glucose data")
  271. processError(APSError.glucoseError(message: "Not enough glucose data"))
  272. return Just(false).eraseToAnyPublisher()
  273. }
  274. let lastGlucoseDate = glucoseStorage.lastGlucoseDate()
  275. guard lastGlucoseDate >= Date().addingTimeInterval(-12.minutes.timeInterval) else {
  276. debug(.apsManager, "Glucose data is stale")
  277. processError(APSError.glucoseError(message: "Glucose data is stale"))
  278. return Just(false).eraseToAnyPublisher()
  279. }
  280. guard glucoseStorage.isGlucoseNotFlat() else {
  281. debug(.apsManager, "Glucose data is too flat")
  282. processError(APSError.glucoseError(message: "Glucose data is too flat"))
  283. return Just(false).eraseToAnyPublisher()
  284. }
  285. let now = Date()
  286. let temp = currentTemp(date: now)
  287. let mainPublisher = makeProfiles()
  288. .flatMap { _ in self.autosens() }
  289. .flatMap { _ in self.dailyAutotune() }
  290. .flatMap { _ in self.openAPS.determineBasal(currentTemp: temp, clock: now) }
  291. .map { suggestion -> Bool in
  292. if let suggestion = suggestion {
  293. DispatchQueue.main.async {
  294. self.broadcaster.notify(SuggestionObserver.self, on: .main) {
  295. $0.suggestionDidUpdate(suggestion)
  296. }
  297. }
  298. }
  299. return suggestion != nil
  300. }
  301. .eraseToAnyPublisher()
  302. if temp.duration == 0,
  303. settings.closedLoop,
  304. settingsManager.preferences.unsuspendIfNoTemp,
  305. let pump = pumpManager,
  306. pump.status.pumpStatus.suspended
  307. {
  308. return pump.resumeDelivery()
  309. .flatMap { _ in mainPublisher }
  310. .replaceError(with: false)
  311. .eraseToAnyPublisher()
  312. }
  313. return mainPublisher
  314. }
  315. func determineBasalSync() {
  316. determineBasal().cancellable().store(in: &lifetime)
  317. }
  318. func makeProfiles() -> AnyPublisher<Bool, Never> {
  319. openAPS.makeProfiles(useAutotune: settings.useAutotune)
  320. .map { tunedProfile in
  321. if let basalProfile = tunedProfile?.basalProfile {
  322. self.processQueue.async {
  323. self.broadcaster.notify(BasalProfileObserver.self, on: self.processQueue) {
  324. $0.basalProfileDidChange(basalProfile)
  325. }
  326. }
  327. }
  328. return tunedProfile != nil
  329. }
  330. .eraseToAnyPublisher()
  331. }
  332. func roundBolus(amount: Decimal) -> Decimal {
  333. guard let pump = pumpManager else { return amount }
  334. let rounded = Decimal(pump.roundToSupportedBolusVolume(units: Double(amount)))
  335. let maxBolus = Decimal(pump.roundToSupportedBolusVolume(units: Double(settingsManager.pumpSettings.maxBolus)))
  336. return min(rounded, maxBolus)
  337. }
  338. private var bolusReporter: DoseProgressReporter?
  339. func enactBolus(amount: Double, isSMB: Bool) {
  340. if let error = verifyStatus() {
  341. processError(error)
  342. processQueue.async {
  343. self.broadcaster.notify(BolusFailureObserver.self, on: self.processQueue) {
  344. $0.bolusDidFail()
  345. }
  346. }
  347. return
  348. }
  349. guard let pump = pumpManager else { return }
  350. let roundedAmout = pump.roundToSupportedBolusVolume(units: amount)
  351. debug(.apsManager, "Enact bolus \(roundedAmout), manual \(!isSMB)")
  352. pump.enactBolus(units: roundedAmout, automatic: isSMB).sink { completion in
  353. if case let .failure(error) = completion {
  354. warning(.apsManager, "Bolus failed with error: \(error.localizedDescription)")
  355. self.processError(APSError.pumpError(error))
  356. if !isSMB {
  357. self.processQueue.async {
  358. self.broadcaster.notify(BolusFailureObserver.self, on: self.processQueue) {
  359. $0.bolusDidFail()
  360. }
  361. }
  362. }
  363. } else {
  364. debug(.apsManager, "Bolus succeeded")
  365. if !isSMB {
  366. self.determineBasal().sink { _ in }.store(in: &self.lifetime)
  367. }
  368. self.bolusProgress.send(0)
  369. }
  370. } receiveValue: { _ in }
  371. .store(in: &lifetime)
  372. }
  373. func cancelBolus() {
  374. guard let pump = pumpManager, pump.status.pumpStatus.bolusing else { return }
  375. debug(.apsManager, "Cancel bolus")
  376. pump.cancelBolus().sink { completion in
  377. if case let .failure(error) = completion {
  378. debug(.apsManager, "Bolus cancellation failed with error: \(error.localizedDescription)")
  379. self.processError(APSError.pumpError(error))
  380. } else {
  381. debug(.apsManager, "Bolus cancelled")
  382. }
  383. self.bolusReporter?.removeObserver(self)
  384. self.bolusReporter = nil
  385. self.bolusProgress.send(nil)
  386. } receiveValue: { _ in }
  387. .store(in: &lifetime)
  388. }
  389. func enactTempBasal(rate: Double, duration: TimeInterval) {
  390. if let error = verifyStatus() {
  391. processError(error)
  392. return
  393. }
  394. guard let pump = pumpManager else { return }
  395. // unable to do temp basal during manual temp basal 😁
  396. if isManualTempBasal {
  397. processError(APSError.manualBasalTemp(message: "Loop not possible during the manual basal temp"))
  398. return
  399. }
  400. debug(.apsManager, "Enact temp basal \(rate) - \(duration)")
  401. let roundedAmout = pump.roundToSupportedBasalRate(unitsPerHour: rate)
  402. pump.enactTempBasal(unitsPerHour: roundedAmout, for: duration) { error in
  403. if let error = error {
  404. debug(.apsManager, "Temp Basal failed with error: \(error.localizedDescription)")
  405. self.processError(APSError.pumpError(error))
  406. } else {
  407. debug(.apsManager, "Temp Basal succeeded")
  408. let temp = TempBasal(duration: Int(duration / 60), rate: Decimal(rate), temp: .absolute, timestamp: Date())
  409. self.storage.save(temp, as: OpenAPS.Monitor.tempBasal)
  410. if rate == 0, duration == 0 {
  411. self.pumpHistoryStorage.saveCancelTempEvents()
  412. }
  413. }
  414. }
  415. }
  416. func dailyAutotune() -> AnyPublisher<Bool, Never> {
  417. guard settings.useAutotune else {
  418. return Just(false).eraseToAnyPublisher()
  419. }
  420. let now = Date()
  421. guard lastAutotuneDate.isBeforeDate(now, granularity: .day) else {
  422. return Just(false).eraseToAnyPublisher()
  423. }
  424. lastAutotuneDate = now
  425. return autotune().map { $0 != nil }.eraseToAnyPublisher()
  426. }
  427. func autotune() -> AnyPublisher<Autotune?, Never> {
  428. openAPS.autotune().eraseToAnyPublisher()
  429. }
  430. func enactAnnouncement(_ announcement: Announcement) {
  431. guard let action = announcement.action else {
  432. warning(.apsManager, "Invalid Announcement action")
  433. return
  434. }
  435. guard let pump = pumpManager else {
  436. warning(.apsManager, "Pump is not set")
  437. return
  438. }
  439. debug(.apsManager, "Start enact announcement: \(action)")
  440. switch action {
  441. case let .bolus(amount):
  442. if let error = verifyStatus() {
  443. processError(error)
  444. return
  445. }
  446. let roundedAmount = pump.roundToSupportedBolusVolume(units: Double(amount))
  447. pump.enactBolus(units: roundedAmount, activationType: .manualRecommendationAccepted) { error in
  448. if let error = error {
  449. // warning(.apsManager, "Announcement Bolus failed with error: \(error.localizedDescription)")
  450. switch error {
  451. case .uncertainDelivery:
  452. // Do not generate notification on uncertain delivery error
  453. break
  454. default:
  455. // Do not generate notifications for automatic boluses that fail.
  456. warning(.apsManager, "Announcement Bolus failed with error: \(error.localizedDescription)")
  457. }
  458. } else {
  459. debug(.apsManager, "Announcement Bolus succeeded")
  460. self.announcementsStorage.storeAnnouncements([announcement], enacted: true)
  461. self.bolusProgress.send(0)
  462. }
  463. }
  464. case let .pump(pumpAction):
  465. switch pumpAction {
  466. case .suspend:
  467. if let error = verifyStatus() {
  468. processError(error)
  469. return
  470. }
  471. pump.suspendDelivery { error in
  472. if let error = error {
  473. debug(.apsManager, "Pump not suspended by Announcement: \(error.localizedDescription)")
  474. } else {
  475. debug(.apsManager, "Pump suspended by Announcement")
  476. self.announcementsStorage.storeAnnouncements([announcement], enacted: true)
  477. self.nightscout.uploadStatus()
  478. }
  479. }
  480. case .resume:
  481. guard pump.status.pumpStatus.suspended else {
  482. return
  483. }
  484. pump.resumeDelivery { error in
  485. if let error = error {
  486. warning(.apsManager, "Pump not resumed by Announcement: \(error.localizedDescription)")
  487. } else {
  488. debug(.apsManager, "Pump resumed by Announcement")
  489. self.announcementsStorage.storeAnnouncements([announcement], enacted: true)
  490. self.nightscout.uploadStatus()
  491. }
  492. }
  493. }
  494. case let .looping(closedLoop):
  495. settings.closedLoop = closedLoop
  496. debug(.apsManager, "Closed loop \(closedLoop) by Announcement")
  497. announcementsStorage.storeAnnouncements([announcement], enacted: true)
  498. case let .tempbasal(rate, duration):
  499. if let error = verifyStatus() {
  500. processError(error)
  501. return
  502. }
  503. // unable to do temp basal during manual temp basal 😁
  504. if isManualTempBasal {
  505. processError(APSError.manualBasalTemp(message: "Loop not possible during the manual basal temp"))
  506. return
  507. }
  508. guard !settings.closedLoop else {
  509. return
  510. }
  511. let roundedRate = pump.roundToSupportedBasalRate(unitsPerHour: Double(rate))
  512. pump.enactTempBasal(unitsPerHour: roundedRate, for: TimeInterval(duration) * 60) { error in
  513. if let error = error {
  514. warning(.apsManager, "Announcement TempBasal failed with error: \(error.localizedDescription)")
  515. } else {
  516. debug(.apsManager, "Announcement TempBasal succeeded")
  517. self.announcementsStorage.storeAnnouncements([announcement], enacted: true)
  518. }
  519. }
  520. }
  521. }
  522. private func currentTemp(date: Date) -> TempBasal {
  523. let defaultTemp = { () -> TempBasal in
  524. guard let temp = storage.retrieve(OpenAPS.Monitor.tempBasal, as: TempBasal.self) else {
  525. return TempBasal(duration: 0, rate: 0, temp: .absolute, timestamp: Date())
  526. }
  527. let delta = Int((date.timeIntervalSince1970 - temp.timestamp.timeIntervalSince1970) / 60)
  528. let duration = max(0, temp.duration - delta)
  529. return TempBasal(duration: duration, rate: temp.rate, temp: .absolute, timestamp: date)
  530. }()
  531. guard let state = pumpManager?.status.basalDeliveryState else { return defaultTemp }
  532. switch state {
  533. case .active:
  534. return TempBasal(duration: 0, rate: 0, temp: .absolute, timestamp: date)
  535. case let .tempBasal(dose):
  536. let rate = Decimal(dose.unitsPerHour)
  537. let durationMin = max(0, Int((dose.endDate.timeIntervalSince1970 - date.timeIntervalSince1970) / 60))
  538. return TempBasal(duration: durationMin, rate: rate, temp: .absolute, timestamp: date)
  539. default:
  540. return defaultTemp
  541. }
  542. }
  543. private func enactSuggested() -> AnyPublisher<Void, Error> {
  544. guard let suggested = storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self) else {
  545. return Fail(error: APSError.apsError(message: "Suggestion not found")).eraseToAnyPublisher()
  546. }
  547. guard Date().timeIntervalSince(suggested.deliverAt ?? .distantPast) < Config.eхpirationInterval else {
  548. return Fail(error: APSError.apsError(message: "Suggestion expired")).eraseToAnyPublisher()
  549. }
  550. guard let pump = pumpManager else {
  551. return Fail(error: APSError.apsError(message: "Pump not set")).eraseToAnyPublisher()
  552. }
  553. // unable to do temp basal during manual temp basal 😁
  554. if isManualTempBasal {
  555. return Fail(error: APSError.manualBasalTemp(message: "Loop not possible during the manual basal temp"))
  556. .eraseToAnyPublisher()
  557. }
  558. let basalPublisher: AnyPublisher<Void, Error> = Deferred { () -> AnyPublisher<Void, Error> in
  559. if let error = self.verifyStatus() {
  560. return Fail(error: error).eraseToAnyPublisher()
  561. }
  562. guard let rate = suggested.rate, let duration = suggested.duration else {
  563. // It is OK, no temp required
  564. debug(.apsManager, "No temp required")
  565. return Just(()).setFailureType(to: Error.self)
  566. .eraseToAnyPublisher()
  567. }
  568. return pump.enactTempBasal(unitsPerHour: Double(rate), for: TimeInterval(duration * 60)).map { _ in
  569. let temp = TempBasal(duration: duration, rate: rate, temp: .absolute, timestamp: Date())
  570. self.storage.save(temp, as: OpenAPS.Monitor.tempBasal)
  571. return ()
  572. }
  573. .eraseToAnyPublisher()
  574. }.eraseToAnyPublisher()
  575. let bolusPublisher: AnyPublisher<Void, Error> = Deferred { () -> AnyPublisher<Void, Error> in
  576. if let error = self.verifyStatus() {
  577. return Fail(error: error).eraseToAnyPublisher()
  578. }
  579. guard let units = suggested.units else {
  580. // It is OK, no bolus required
  581. debug(.apsManager, "No bolus required")
  582. return Just(()).setFailureType(to: Error.self)
  583. .eraseToAnyPublisher()
  584. }
  585. return pump.enactBolus(units: Double(units), automatic: true).map { _ in
  586. self.bolusProgress.send(0)
  587. return ()
  588. }
  589. .eraseToAnyPublisher()
  590. }.eraseToAnyPublisher()
  591. return basalPublisher.flatMap { bolusPublisher }.eraseToAnyPublisher()
  592. }
  593. private func reportEnacted(received: Bool) {
  594. if let suggestion = storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self), suggestion.deliverAt != nil {
  595. var enacted = suggestion
  596. enacted.timestamp = Date()
  597. enacted.recieved = received
  598. storage.save(enacted, as: OpenAPS.Enact.enacted)
  599. debug(.apsManager, "Suggestion enacted. Received: \(received)")
  600. DispatchQueue.main.async {
  601. self.broadcaster.notify(EnactedSuggestionObserver.self, on: .main) {
  602. $0.enactedSuggestionDidUpdate(enacted)
  603. }
  604. }
  605. nightscout.uploadStatus()
  606. // Update the TDD value
  607. tdd(enacted_: enacted)
  608. // Update statistics
  609. statistics()
  610. }
  611. }
  612. private func tdd(enacted_: Suggestion) {
  613. let tddStartedAt = Date()
  614. let preferences = settingsManager.preferences
  615. let currentTDD = enacted_.tdd ?? 0
  616. // MARK: Fetch data from Core Data: TDD Entity. TEST:
  617. if currentTDD > 0 {
  618. let tenDaysAgo = Date().addingTimeInterval(-10.days.timeInterval)
  619. let twoHoursAgo = Date().addingTimeInterval(-2.hours.timeInterval)
  620. var uniqEvents = [TDD]()
  621. var total: Decimal = 0
  622. var totalAmount: Decimal = 0
  623. var indeces: Int = 0
  624. var nrOfIndeces: Int = 0
  625. coredataContext.performAndWait {
  626. let requestTDD = TDD.fetchRequest() as NSFetchRequest<TDD>
  627. requestTDD.predicate = NSPredicate(format: "timestamp > %@ AND tdd > 0", tenDaysAgo as NSDate)
  628. let sortTDD = NSSortDescriptor(key: "timestamp", ascending: true)
  629. requestTDD.sortDescriptors = [sortTDD]
  630. try? uniqEvents = coredataContext.fetch(requestTDD)
  631. total = uniqEvents.compactMap({ each in each.tdd as? Decimal ?? 0 }).reduce(0, +)
  632. indeces = uniqEvents.count
  633. // Only fetch once. Use same (previous) fetch
  634. let twoHoursArray = uniqEvents.filter({ ($0.timestamp ?? Date()) >= twoHoursAgo })
  635. nrOfIndeces = twoHoursArray.count
  636. totalAmount = twoHoursArray.compactMap({ each in each.tdd as? Decimal ?? 0 }).reduce(0, +)
  637. }
  638. if indeces == 0 {
  639. indeces = 1
  640. }
  641. if nrOfIndeces == 0 {
  642. nrOfIndeces = 1
  643. }
  644. let average2hours = totalAmount / Decimal(nrOfIndeces)
  645. let average14 = total / Decimal(indeces)
  646. let weight = preferences.weightPercentage
  647. let weighted_average = weight * average2hours + (1 - weight) * average14
  648. let averages = TDD_averages(
  649. average_total_data: roundDecimal(average14, 1),
  650. weightedAverage: roundDecimal(weighted_average, 1),
  651. past2hoursAverage: roundDecimal(average2hours, 1),
  652. date: Date()
  653. )
  654. storage.save(averages, as: OpenAPS.Monitor.tdd_averages)
  655. print("Test time of TDD: \(-1 * tddStartedAt.timeIntervalSinceNow) s")
  656. }
  657. }
  658. private func roundDecimal(_ decimal: Decimal, _ digits: Double) -> Decimal {
  659. let rounded = round(Double(decimal) * pow(10, digits)) / pow(10, digits)
  660. return Decimal(rounded)
  661. }
  662. private func roundDouble(_ double: Double, _ digits: Double) -> Double {
  663. let rounded = round(Double(double) * pow(10, digits)) / pow(10, digits)
  664. return rounded
  665. }
  666. private func medianCalculation(array: [Double]) -> Double {
  667. guard !array.isEmpty else {
  668. return 0
  669. }
  670. let sorted = array.sorted()
  671. let length = array.count
  672. if length % 2 == 0 {
  673. return (sorted[length / 2 - 1] + sorted[length / 2]) / 2
  674. }
  675. return sorted[length / 2]
  676. }
  677. // Add to statistics.JSON
  678. private func statistics() {
  679. let statisticsStartedAt = Date()
  680. var testFile: [Statistics] = []
  681. var testIfEmpty = 0
  682. storage.transaction { storage in
  683. testFile = storage.retrieve(OpenAPS.Monitor.statistics, as: [Statistics].self) ?? []
  684. testIfEmpty = testFile.count
  685. }
  686. let updateThisOften = Int(settingsManager.preferences.updateInterval)
  687. // Only run every 30 minutes or according to setting.
  688. if testIfEmpty != 0 {
  689. guard testFile[0].created_at.addingTimeInterval(updateThisOften.minutes.timeInterval) < Date()
  690. else {
  691. return
  692. }
  693. }
  694. let units = settingsManager.settings.units
  695. let preferences = settingsManager.preferences
  696. // MARK: Fetch Carbs from CoreData
  697. var carbs = [Carbohydrates]()
  698. var carbTotal: Decimal = 0
  699. coredataContext.performAndWait {
  700. let requestCarbs = Carbohydrates.fetchRequest() as NSFetchRequest<Carbohydrates>
  701. let daysAgo = Date().addingTimeInterval(-1.days.timeInterval)
  702. requestCarbs.predicate = NSPredicate(format: "carbs > 0 AND date > %@", daysAgo as NSDate)
  703. let sortCarbs = NSSortDescriptor(key: "date", ascending: true)
  704. requestCarbs.sortDescriptors = [sortCarbs]
  705. try? carbs = coredataContext.fetch(requestCarbs)
  706. carbTotal = carbs.map({ carbs in carbs.carbs as? Decimal ?? 0 }).reduce(0, +)
  707. }
  708. // MARK: Fetch TDD from CoreData
  709. var tdds = [TDD]()
  710. var currentTDD: Decimal = 0
  711. coredataContext.performAndWait {
  712. let requestTDD = TDD.fetchRequest() as NSFetchRequest<TDD>
  713. let sort = NSSortDescriptor(key: "timestamp", ascending: false)
  714. requestTDD.sortDescriptors = [sort]
  715. requestTDD.fetchLimit = 1
  716. try? tdds = coredataContext.fetch(requestTDD)
  717. if !tdds.isEmpty {
  718. currentTDD = tdds[0].tdd?.decimalValue ?? 0
  719. }
  720. }
  721. var algo_ = "Oref0"
  722. if preferences.sigmoid, preferences.enableDynamicCR {
  723. algo_ = "Dynamic ISF + CR: Sigmoid"
  724. } else if preferences.sigmoid, !preferences.enableDynamicCR {
  725. algo_ = "Dynamic ISF: Sigmoid"
  726. } else if preferences.useNewFormula, preferences.enableDynamicCR {
  727. algo_ = "Dynamic ISF + CR: Logarithmic"
  728. } else if preferences.useNewFormula, !preferences.sigmoid,!preferences.enableDynamicCR {
  729. algo_ = "Dynamic ISF: Logarithmic"
  730. }
  731. let af = preferences.adjustmentFactor
  732. let insulin_type = preferences.curve
  733. let buildDate = Bundle.main.buildDate
  734. let version = Bundle.main.releaseVersionNumber
  735. let build = Bundle.main.buildVersionNumber
  736. let branch = Bundle.main.infoDictionary?["BuildBranch"] as? String ?? ""
  737. let copyrightNotice_ = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
  738. let pump_ = pumpManager?.localizedTitle ?? ""
  739. let cgm = settingsManager.settings.cgm
  740. let file = OpenAPS.Monitor.statistics
  741. var iPa: Decimal = 75
  742. if preferences.useCustomPeakTime {
  743. iPa = preferences.insulinPeakTime
  744. } else if preferences.curve.rawValue == "rapid-acting" {
  745. iPa = 65
  746. } else if preferences.curve.rawValue == "ultra-rapid" {
  747. iPa = 50
  748. }
  749. // MARK: Fetch LoopStatRecords from CoreData
  750. var lsr = [LoopStatRecord]()
  751. var successRate: Double?
  752. var successNR = 0
  753. var errorNR = 0
  754. var minimumInt = 999.0
  755. var maximumInt = 0.0
  756. var minimumLoopTime = 9999.0
  757. var maximumLoopTime = 0.0
  758. var timeIntervalLoops = 0.0
  759. var previousTimeLoop = Date()
  760. var timeForOneLoop = 0.0
  761. var averageLoopTime = 0.0
  762. var timeForOneLoopArray: [Double] = []
  763. var medianLoopTime = 0.0
  764. var timeIntervalLoopArray: [Double] = []
  765. var medianInterval = 0.0
  766. var averageIntervalLoops = 0.0
  767. coredataContext.performAndWait {
  768. let requestLSR = LoopStatRecord.fetchRequest() as NSFetchRequest<LoopStatRecord>
  769. requestLSR.predicate = NSPredicate(format: "start > %@", Date().addingTimeInterval(-24.hours.timeInterval) as NSDate)
  770. let sortLSR = NSSortDescriptor(key: "start", ascending: false)
  771. requestLSR.sortDescriptors = [sortLSR]
  772. try? lsr = coredataContext.fetch(requestLSR)
  773. if lsr.isNotEmpty {
  774. var i = 0.0
  775. if let loopEnd = lsr[0].end {
  776. previousTimeLoop = loopEnd
  777. }
  778. for each in lsr {
  779. if let loopEnd = each.end {
  780. let loopDuration = each.duration
  781. if each.loopStatus!.contains("Success") {
  782. successNR += 1
  783. } else {
  784. errorNR += 1
  785. }
  786. i += 1
  787. timeIntervalLoops = (previousTimeLoop - (each.start ?? previousTimeLoop)).timeInterval / 60
  788. if timeIntervalLoops > 0.0, i != 1 {
  789. timeIntervalLoopArray.append(timeIntervalLoops)
  790. }
  791. if timeIntervalLoops > maximumInt {
  792. maximumInt = timeIntervalLoops
  793. }
  794. if timeIntervalLoops < minimumInt, i != 1 {
  795. minimumInt = timeIntervalLoops
  796. }
  797. timeForOneLoop = loopDuration
  798. timeForOneLoopArray.append(timeForOneLoop)
  799. if timeForOneLoop >= maximumLoopTime, timeForOneLoop != 0.0 {
  800. maximumLoopTime = timeForOneLoop
  801. }
  802. if timeForOneLoop <= minimumLoopTime, timeForOneLoop != 0.0 {
  803. minimumLoopTime = timeForOneLoop
  804. }
  805. previousTimeLoop = loopEnd
  806. }
  807. }
  808. successRate = (Double(successNR) / Double(i)) * 100
  809. // Average Loop Interval in minutes
  810. let timeOfFirstIndex = lsr[0].start ?? Date()
  811. let lastIndexWithTimestamp = lsr.count - 1
  812. let timeOfLastIndex = lsr[lastIndexWithTimestamp].end ?? Date()
  813. averageLoopTime = (timeOfFirstIndex - timeOfLastIndex).timeInterval / 60 / Double(errorNR + successNR)
  814. // Median values
  815. medianLoopTime = medianCalculation(array: timeForOneLoopArray)
  816. medianInterval = medianCalculation(array: timeIntervalLoopArray)
  817. // Average time interval between loops
  818. averageIntervalLoops = timeIntervalLoopArray.reduce(0, +) / Double(timeIntervalLoopArray.count)
  819. }
  820. }
  821. if minimumInt == 999.0 {
  822. minimumInt = 0.0
  823. }
  824. if minimumLoopTime == 9999.0 {
  825. minimumLoopTime = 0.0
  826. }
  827. var glucose: [Readings] = []
  828. var firstElementTime = Date()
  829. var lastElementTime = Date()
  830. var currentIndexTime = Date()
  831. var bg: Decimal = 0
  832. var bgArray: [Double] = []
  833. var bgArray_1_: [Double] = []
  834. var bgArray_7_: [Double] = []
  835. var bgArray_30_: [Double] = []
  836. var bgArray_90_: [Double] = []
  837. var bgArrayForTIR: [(bg_: Double, date_: Date)] = []
  838. var bgArray_1: [(bg_: Double, date_: Date)] = []
  839. var bgArray_7: [(bg_: Double, date_: Date)] = []
  840. var bgArray_30: [(bg_: Double, date_: Date)] = []
  841. var bgArray_90: [(bg_: Double, date_: Date)] = []
  842. var medianBG = 0.0
  843. var nr_bgs: Decimal = 0
  844. var bg_1: Decimal = 0
  845. var bg_7: Decimal = 0
  846. var bg_30: Decimal = 0
  847. var bg_90: Decimal = 0
  848. var bg_total: Decimal = 0
  849. var j = -1
  850. var conversionFactor: Decimal = 1
  851. if units == .mmolL {
  852. conversionFactor = 0.0555
  853. }
  854. var numberOfDays: Double = 0
  855. var nr1: Decimal = 0
  856. coredataContext.performAndWait {
  857. let requestGFS = Readings.fetchRequest() as NSFetchRequest<Readings>
  858. let sortGlucose = NSSortDescriptor(key: "date", ascending: false)
  859. requestGFS.sortDescriptors = [sortGlucose]
  860. try? glucose = coredataContext.fetch(requestGFS)
  861. // Time In Range (%) and Average Glucose. This will be refactored later after some testing.
  862. let endIndex = glucose.count - 1
  863. firstElementTime = glucose[0].date ?? Date()
  864. lastElementTime = glucose[endIndex].date ?? Date()
  865. currentIndexTime = firstElementTime
  866. numberOfDays = (firstElementTime - lastElementTime).timeInterval / 8.64E4
  867. // Make arrays for median calculations and calculate averages
  868. if endIndex >= 0 {
  869. repeat {
  870. j += 1
  871. if glucose[j].glucose > 0 {
  872. currentIndexTime = glucose[j].date ?? firstElementTime
  873. bg += Decimal(glucose[j].glucose) * conversionFactor
  874. bgArray.append(Double(glucose[j].glucose) * Double(conversionFactor))
  875. bgArrayForTIR.append((Double(glucose[j].glucose), glucose[j].date!))
  876. nr_bgs += 1
  877. if (firstElementTime - currentIndexTime).timeInterval <= 8.64E4 { // 1 day
  878. bg_1 = bg / nr_bgs
  879. bgArray_1 = bgArrayForTIR
  880. bgArray_1_ = bgArray
  881. nr1 = nr_bgs
  882. }
  883. if (firstElementTime - currentIndexTime).timeInterval <= 6.048E5 { // 7 days
  884. bg_7 = bg / nr_bgs
  885. bgArray_7 = bgArrayForTIR
  886. bgArray_7_ = bgArray
  887. }
  888. if (firstElementTime - currentIndexTime).timeInterval <= 2.592E6 { // 30 days
  889. bg_30 = bg / nr_bgs
  890. bgArray_30 = bgArrayForTIR
  891. bgArray_30_ = bgArray
  892. }
  893. if (firstElementTime - currentIndexTime).timeInterval <= 7.776E7 { // 30 days
  894. bg_90 = bg / nr_bgs
  895. bgArray_90 = bgArrayForTIR
  896. bgArray_90_ = bgArray
  897. }
  898. }
  899. } while j != glucose.count - 1
  900. }
  901. }
  902. if nr_bgs > 0 {
  903. // Up to 91 days
  904. bg_total = bg / nr_bgs
  905. }
  906. // Total median
  907. medianBG = medianCalculation(array: bgArray)
  908. func tir(_ array: [(bg_: Double, date_: Date)]) -> (TIR: Double, hypos: Double, hypers: Double) {
  909. var timeInHypo = 0.0
  910. var timeInHyper = 0.0
  911. var hypos = 0.0
  912. var hypers = 0.0
  913. var i = -1
  914. var lastIndex = false
  915. let endIndex = array.count - 1
  916. var hypoLimit = settingsManager.preferences.low
  917. var hyperLimit = settingsManager.preferences.high
  918. if units == .mmolL {
  919. hypoLimit = hypoLimit / 0.0555
  920. hyperLimit = hyperLimit / 0.0555
  921. }
  922. var full_time = 0.0
  923. if endIndex > 0 {
  924. full_time = (array[0].date_ - array[endIndex].date_).timeInterval
  925. }
  926. while i < endIndex {
  927. i += 1
  928. let currentTime = array[i].date_
  929. var previousTime = currentTime
  930. if i + 1 <= endIndex {
  931. previousTime = array[i + 1].date_
  932. } else {
  933. lastIndex = true
  934. }
  935. if array[i].bg_ < Double(hypoLimit), !lastIndex {
  936. timeInHypo += (currentTime - previousTime).timeInterval
  937. } else if array[i].bg_ >= Double(hyperLimit), !lastIndex {
  938. timeInHyper += (currentTime - previousTime).timeInterval
  939. }
  940. }
  941. if timeInHypo == 0.0 {
  942. hypos = 0
  943. } else if full_time != 0.0 { hypos = (timeInHypo / full_time) * 100
  944. }
  945. if timeInHyper == 0.0 {
  946. hypers = 0
  947. } else if full_time != 0.0 { hypers = (timeInHyper / full_time) * 100
  948. }
  949. let TIR = 100 - (hypos + hypers)
  950. return (roundDouble(TIR, 1), roundDouble(hypos, 1), roundDouble(hypers, 1))
  951. }
  952. // HbA1c estimation (%, mmol/mol) 1 day
  953. var NGSPa1CStatisticValue: Decimal = 0.0
  954. var IFCCa1CStatisticValue: Decimal = 0.0
  955. if nr_bgs > 0 {
  956. NGSPa1CStatisticValue = ((bg_1 / conversionFactor) + 46.7) / 28.7 // NGSP (%)
  957. IFCCa1CStatisticValue = 10.929 *
  958. (NGSPa1CStatisticValue - 2.152) // IFCC (mmol/mol) A1C(mmol/mol) = 10.929 * (A1C(%) - 2.15)
  959. }
  960. // 7 days
  961. var NGSPa1CStatisticValue_7: Decimal = 0.0
  962. var IFCCa1CStatisticValue_7: Decimal = 0.0
  963. if nr_bgs > 0 {
  964. NGSPa1CStatisticValue_7 = ((bg_7 / conversionFactor) + 46.7) / 28.7
  965. IFCCa1CStatisticValue_7 = 10.929 * (NGSPa1CStatisticValue_7 - 2.152)
  966. }
  967. // 30 days
  968. var NGSPa1CStatisticValue_30: Decimal = 0.0
  969. var IFCCa1CStatisticValue_30: Decimal = 0.0
  970. if nr_bgs > 0 {
  971. NGSPa1CStatisticValue_30 = ((bg_30 / conversionFactor) + 46.7) / 28.7
  972. IFCCa1CStatisticValue_30 = 10.929 * (NGSPa1CStatisticValue_30 - 2.152)
  973. }
  974. // 90 days
  975. var NGSPa1CStatisticValue_90: Decimal = 0.0
  976. var IFCCa1CStatisticValue_90: Decimal = 0.0
  977. if nr_bgs > 0 {
  978. NGSPa1CStatisticValue_90 = ((bg_90 / conversionFactor) + 46.7) / 28.7
  979. IFCCa1CStatisticValue_90 = 10.929 * (NGSPa1CStatisticValue_90 - 2.152)
  980. }
  981. // Total days
  982. var NGSPa1CStatisticValue_total: Decimal = 0.0
  983. var IFCCa1CStatisticValue_total: Decimal = 0.0
  984. if nr_bgs > 0 {
  985. NGSPa1CStatisticValue_total = ((bg_total / conversionFactor) + 46.7) / 28.7
  986. IFCCa1CStatisticValue_total = 10.929 *
  987. (NGSPa1CStatisticValue_total - 2.152)
  988. }
  989. let median = Durations(
  990. day: roundDecimal(Decimal(medianCalculation(array: bgArray_1_)), 1),
  991. week: roundDecimal(Decimal(medianCalculation(array: bgArray_7_)), 1),
  992. month: roundDecimal(Decimal(medianCalculation(array: bgArray_30_)), 1),
  993. total: roundDecimal(Decimal(medianBG), 1)
  994. )
  995. // MARK: Save to Median to CoreData
  996. let saveMedianToCoreData = BGmedian(context: coredataContext)
  997. saveMedianToCoreData.date = Date()
  998. saveMedianToCoreData.median = median.total as NSDecimalNumber
  999. saveMedianToCoreData.median_1 = median.day as NSDecimalNumber
  1000. saveMedianToCoreData.median_7 = median.week as NSDecimalNumber
  1001. saveMedianToCoreData.median_30 = median.month as NSDecimalNumber
  1002. saveMedianToCoreData.median_90 = roundDecimal(Decimal(medianCalculation(array: bgArray_90_)), 1) as NSDecimalNumber
  1003. coredataContext.perform {
  1004. try? self.coredataContext.save()
  1005. }
  1006. var hbs = Durations(
  1007. day: roundDecimal(NGSPa1CStatisticValue, 1),
  1008. week: roundDecimal(NGSPa1CStatisticValue_7, 1),
  1009. month: roundDecimal(NGSPa1CStatisticValue_30, 1),
  1010. total: roundDecimal(NGSPa1CStatisticValue_total, 1)
  1011. )
  1012. let saveHbA1c = HbA1c(context: coredataContext)
  1013. saveHbA1c.date = Date()
  1014. saveHbA1c.hba1c = NGSPa1CStatisticValue_total as NSDecimalNumber
  1015. saveHbA1c.hba1c_1 = NGSPa1CStatisticValue as NSDecimalNumber
  1016. saveHbA1c.hba1c_7 = NGSPa1CStatisticValue_7 as NSDecimalNumber
  1017. saveHbA1c.hba1c_30 = NGSPa1CStatisticValue_30 as NSDecimalNumber
  1018. saveHbA1c.hba1c_90 = NGSPa1CStatisticValue_90 as NSDecimalNumber
  1019. // MARK: Save to HbA1c to CoreData
  1020. coredataContext.perform {
  1021. try? self.coredataContext.save()
  1022. }
  1023. // Convert to user-preferred unit
  1024. let overrideHbA1cUnit = settingsManager.preferences.overrideHbA1cUnit
  1025. if units == .mmolL {
  1026. // Override if users sets overrideHbA1cUnit: true
  1027. if !overrideHbA1cUnit {
  1028. hbs = Durations(
  1029. day: roundDecimal(IFCCa1CStatisticValue, 1),
  1030. week: roundDecimal(IFCCa1CStatisticValue_7, 1),
  1031. month: roundDecimal(IFCCa1CStatisticValue_30, 1),
  1032. total: roundDecimal(IFCCa1CStatisticValue_total, 1)
  1033. )
  1034. }
  1035. } else if units != .mmolL, overrideHbA1cUnit {
  1036. hbs = Durations(
  1037. day: roundDecimal(IFCCa1CStatisticValue, 1),
  1038. week: roundDecimal(IFCCa1CStatisticValue_7, 1),
  1039. month: roundDecimal(IFCCa1CStatisticValue_30, 1),
  1040. total: roundDecimal(IFCCa1CStatisticValue_total, 1)
  1041. )
  1042. }
  1043. let nrOfCGMReadings = nr1
  1044. let loopstat = LoopCycles(
  1045. loops: successNR + errorNR,
  1046. errors: errorNR,
  1047. readings: Int(nrOfCGMReadings),
  1048. success_rate: Decimal(round(successRate ?? 0)),
  1049. avg_interval: roundDecimal(Decimal(averageIntervalLoops), 1),
  1050. median_interval: roundDecimal(Decimal(medianInterval), 1),
  1051. min_interval: roundDecimal(Decimal(minimumInt), 1),
  1052. max_interval: roundDecimal(Decimal(maximumInt), 1),
  1053. avg_duration: Decimal(roundDouble(averageLoopTime, 2)),
  1054. median_duration: Decimal(roundDouble(medianLoopTime, 2)),
  1055. min_duration: roundDecimal(Decimal(minimumLoopTime), 2),
  1056. max_duration: Decimal(roundDouble(maximumLoopTime, 1))
  1057. )
  1058. // TIR calcs for every case
  1059. var oneDay_: (TIR: Double, hypos: Double, hypers: Double) = (0.0, 0.0, 0.0)
  1060. var sevenDays_: (TIR: Double, hypos: Double, hypers: Double) = (0.0, 0.0, 0.0)
  1061. var thirtyDays_: (TIR: Double, hypos: Double, hypers: Double) = (0.0, 0.0, 0.0)
  1062. var totalDays_: (TIR: Double, hypos: Double, hypers: Double) = (0.0, 0.0, 0.0)
  1063. // Get all TIR calcs for every case
  1064. if nr_bgs > 0 {
  1065. oneDay_ = tir(bgArray_1)
  1066. sevenDays_ = tir(bgArray_7)
  1067. thirtyDays_ = tir(bgArray_30)
  1068. totalDays_ = tir(bgArrayForTIR)
  1069. }
  1070. let tir = Durations(
  1071. day: roundDecimal(Decimal(oneDay_.TIR), 1),
  1072. week: roundDecimal(Decimal(sevenDays_.TIR), 1),
  1073. month: roundDecimal(Decimal(thirtyDays_.TIR), 1),
  1074. total: roundDecimal(Decimal(totalDays_.TIR), 1)
  1075. )
  1076. let hypo = Durations(
  1077. day: Decimal(oneDay_.hypos),
  1078. week: Decimal(sevenDays_.hypos),
  1079. month: Decimal(thirtyDays_.hypos),
  1080. total: Decimal(totalDays_.hypos)
  1081. )
  1082. let hyper = Durations(
  1083. day: Decimal(oneDay_.hypers),
  1084. week: Decimal(sevenDays_.hypers),
  1085. month: Decimal(thirtyDays_.hypers),
  1086. total: Decimal(totalDays_.hypers)
  1087. )
  1088. let TimeInRange = TIRs(TIR: tir, Hypos: hypo, Hypers: hyper)
  1089. let avgs = Durations(
  1090. day: roundDecimal(bg_1, 1),
  1091. week: roundDecimal(bg_7, 1),
  1092. month: roundDecimal(bg_30, 1),
  1093. total: roundDecimal(bg_total, 1)
  1094. )
  1095. let saveAverages = BGaverages(context: coredataContext)
  1096. saveAverages.date = Date()
  1097. saveAverages.average = bg_total as NSDecimalNumber
  1098. saveAverages.average_1 = bg_1 as NSDecimalNumber
  1099. saveAverages.average_7 = bg_7 as NSDecimalNumber
  1100. saveAverages.average_30 = bg_30 as NSDecimalNumber
  1101. saveAverages.average_90 = bg_90 as NSDecimalNumber
  1102. // MARK: Save to HbA1c to CoreData
  1103. coredataContext.perform {
  1104. try? self.coredataContext.save()
  1105. }
  1106. let avg = Averages(Average: avgs, Median: median)
  1107. // MARK: Fetch InsulinDuration from CoreData
  1108. var insulinDistribution = [InsulinDistribution]()
  1109. var insulin = Ins(
  1110. TDD: 0,
  1111. bolus: 0,
  1112. temp_basal: 0,
  1113. scheduled_basal: 0
  1114. )
  1115. coredataContext.performAndWait {
  1116. let requestInsulinDistribution = InsulinDistribution.fetchRequest() as NSFetchRequest<InsulinDistribution>
  1117. let sortInsulin = NSSortDescriptor(key: "date", ascending: false)
  1118. requestInsulinDistribution.sortDescriptors = [sortInsulin]
  1119. requestInsulinDistribution.fetchLimit = 1
  1120. try? insulinDistribution = coredataContext.fetch(requestInsulinDistribution)
  1121. insulin = Ins(
  1122. TDD: roundDecimal(currentTDD, 2),
  1123. bolus: insulinDistribution.first != nil ? ((insulinDistribution[0].bolus ?? 0) as Decimal) : 0,
  1124. temp_basal: insulinDistribution.first != nil ? ((insulinDistribution[0].tempBasal ?? 0) as Decimal) : 0,
  1125. scheduled_basal: insulinDistribution.first != nil ? ((insulinDistribution[0].scheduledBasal ?? 0) as Decimal) : 0
  1126. )
  1127. }
  1128. var sumOfSquares = 0.0
  1129. var sumOfSquares_1 = 0.0
  1130. var sumOfSquares_7 = 0.0
  1131. var sumOfSquares_30 = 0.0
  1132. // Total
  1133. for array in bgArray {
  1134. sumOfSquares += pow(array - Double(bg_total), 2)
  1135. }
  1136. // One day
  1137. for array_1 in bgArray_1_ {
  1138. sumOfSquares_1 += pow(array_1 - Double(bg_1), 2)
  1139. }
  1140. // week
  1141. for array_7 in bgArray_7_ {
  1142. sumOfSquares_7 += pow(array_7 - Double(bg_7), 2)
  1143. }
  1144. // month
  1145. for array_30 in bgArray_30_ {
  1146. sumOfSquares_30 += pow(array_30 - Double(bg_30), 2)
  1147. }
  1148. // Standard deviation and Coefficient of variation
  1149. var sd_total = 0.0
  1150. var cv_total = 0.0
  1151. var sd_1 = 0.0
  1152. var cv_1 = 0.0
  1153. var sd_7 = 0.0
  1154. var cv_7 = 0.0
  1155. var sd_30 = 0.0
  1156. var cv_30 = 0.0
  1157. // Avoid division by zero
  1158. if bg_total > 0 {
  1159. sd_total = sqrt(sumOfSquares / Double(nr_bgs))
  1160. cv_total = sd_total / Double(bg_total) * 100
  1161. }
  1162. if bg_1 > 0 {
  1163. sd_1 = sqrt(sumOfSquares_1 / Double(bgArray_1_.count))
  1164. cv_1 = sd_1 / Double(bg_1) * 100
  1165. }
  1166. if bg_7 > 0 {
  1167. sd_7 = sqrt(sumOfSquares_7 / Double(bgArray_7_.count))
  1168. cv_7 = sd_7 / Double(bg_7) * 100
  1169. }
  1170. if bg_30 > 0 {
  1171. sd_30 = sqrt(sumOfSquares_30 / Double(bgArray_30_.count))
  1172. cv_30 = sd_30 / Double(bg_30) * 100
  1173. }
  1174. // Standard Deviations
  1175. let standardDeviations = Durations(
  1176. day: roundDecimal(Decimal(sd_1), 1),
  1177. week: roundDecimal(Decimal(sd_7), 1),
  1178. month: roundDecimal(Decimal(sd_30), 1),
  1179. total: roundDecimal(Decimal(sd_total), 1)
  1180. )
  1181. // CV = standard deviation / sample mean x 100
  1182. let cvs = Durations(
  1183. day: roundDecimal(Decimal(cv_1), 1),
  1184. week: roundDecimal(Decimal(cv_7), 1),
  1185. month: roundDecimal(Decimal(cv_30), 1),
  1186. total: roundDecimal(Decimal(cv_total), 1)
  1187. )
  1188. let variance = Variance(SD: standardDeviations, CV: cvs)
  1189. let dailystat = Statistics(
  1190. created_at: Date(),
  1191. iPhone: UIDevice.current.getDeviceId,
  1192. iOS: UIDevice.current.getOSInfo,
  1193. Build_Version: version ?? "",
  1194. Build_Number: build ?? "1",
  1195. Branch: branch,
  1196. CopyRightNotice: String(copyrightNotice_.prefix(32)),
  1197. Build_Date: buildDate,
  1198. Algorithm: algo_,
  1199. AdjustmentFactor: af,
  1200. Pump: pump_,
  1201. CGM: cgm.rawValue,
  1202. insulinType: insulin_type.rawValue,
  1203. peakActivityTime: iPa,
  1204. Carbs_24h: carbTotal,
  1205. GlucoseStorage_Days: Decimal(roundDouble(numberOfDays, 1)),
  1206. Statistics: Stats(
  1207. Distribution: TimeInRange,
  1208. Glucose: avg,
  1209. HbA1c: hbs,
  1210. LoopCycles: loopstat,
  1211. Insulin: insulin,
  1212. Variance: variance
  1213. )
  1214. )
  1215. storage.transaction { storage in
  1216. storage.append(dailystat, to: file, uniqBy: \.created_at)
  1217. let uniqeEvents: [Statistics] = storage.retrieve(file, as: [Statistics].self)?
  1218. .filter { $0.created_at.addingTimeInterval(24.hours.timeInterval) > Date() }
  1219. .sorted { $0.created_at > $1.created_at } ?? []
  1220. storage.save(Array(uniqeEvents), as: file)
  1221. }
  1222. nightscout.uploadStatistics(dailystat: dailystat)
  1223. nightscout.uploadPreferences()
  1224. print("Test time of statistics computation: \(-1 * statisticsStartedAt.timeIntervalSinceNow) s")
  1225. }
  1226. private func loopStats(loopStatRecord: LoopStats) {
  1227. let LoopStatsStartedAt = Date()
  1228. coredataContext.perform {
  1229. let nLS = LoopStatRecord(context: self.coredataContext)
  1230. nLS.start = loopStatRecord.start
  1231. nLS.end = loopStatRecord.end ?? Date()
  1232. nLS.loopStatus = loopStatRecord.loopStatus
  1233. nLS.duration = loopStatRecord.duration ?? 0.0
  1234. try? self.coredataContext.save()
  1235. }
  1236. print("Test time of LoopStats computation: \(-1 * LoopStatsStartedAt.timeIntervalSinceNow) s")
  1237. }
  1238. private func processError(_ error: Error) {
  1239. warning(.apsManager, "\(error.localizedDescription)")
  1240. lastError.send(error)
  1241. }
  1242. private func createBolusReporter() {
  1243. bolusReporter = pumpManager?.createBolusProgressReporter(reportingOn: processQueue)
  1244. bolusReporter?.addObserver(self)
  1245. }
  1246. private func updateStatus() {
  1247. debug(.apsManager, "force update status")
  1248. guard let pump = pumpManager else {
  1249. return
  1250. }
  1251. if let omnipod = pump as? OmnipodPumpManager {
  1252. omnipod.getPodStatus { _ in }
  1253. }
  1254. if let omnipodBLE = pump as? OmniBLEPumpManager {
  1255. omnipodBLE.getPodStatus { _ in }
  1256. }
  1257. }
  1258. private func clearBolusReporter() {
  1259. bolusReporter?.removeObserver(self)
  1260. bolusReporter = nil
  1261. processQueue.asyncAfter(deadline: .now() + 0.5) {
  1262. self.bolusProgress.send(nil)
  1263. self.updateStatus()
  1264. }
  1265. }
  1266. }
  1267. private extension PumpManager {
  1268. func enactTempBasal(unitsPerHour: Double, for duration: TimeInterval) -> AnyPublisher<DoseEntry?, Error> {
  1269. Future { promise in
  1270. self.enactTempBasal(unitsPerHour: unitsPerHour, for: duration) { error in
  1271. if let error = error {
  1272. debug(.apsManager, "Temp basal failed: \(unitsPerHour) for: \(duration)")
  1273. promise(.failure(error))
  1274. } else {
  1275. debug(.apsManager, "Temp basal succeded: \(unitsPerHour) for: \(duration)")
  1276. promise(.success(nil))
  1277. }
  1278. }
  1279. }
  1280. .mapError { APSError.pumpError($0) }
  1281. .eraseToAnyPublisher()
  1282. }
  1283. func enactBolus(units: Double, automatic: Bool) -> AnyPublisher<DoseEntry?, Error> {
  1284. Future { promise in
  1285. // convert automatic
  1286. let automaticValue = automatic ? BolusActivationType.automatic : BolusActivationType.manualRecommendationAccepted
  1287. self.enactBolus(units: units, activationType: automaticValue) { error in
  1288. if let error = error {
  1289. debug(.apsManager, "Bolus failed: \(units)")
  1290. promise(.failure(error))
  1291. } else {
  1292. debug(.apsManager, "Bolus succeded: \(units)")
  1293. promise(.success(nil))
  1294. }
  1295. }
  1296. }
  1297. .mapError { APSError.pumpError($0) }
  1298. .eraseToAnyPublisher()
  1299. }
  1300. func cancelBolus() -> AnyPublisher<DoseEntry?, Error> {
  1301. Future { promise in
  1302. self.cancelBolus { result in
  1303. switch result {
  1304. case let .success(dose):
  1305. debug(.apsManager, "Cancel Bolus succeded")
  1306. promise(.success(dose))
  1307. case let .failure(error):
  1308. debug(.apsManager, "Cancel Bolus failed")
  1309. promise(.failure(error))
  1310. }
  1311. }
  1312. }
  1313. .mapError { APSError.pumpError($0) }
  1314. .eraseToAnyPublisher()
  1315. }
  1316. func suspendDelivery() -> AnyPublisher<Void, Error> {
  1317. Future { promise in
  1318. self.suspendDelivery { error in
  1319. if let error = error {
  1320. promise(.failure(error))
  1321. } else {
  1322. promise(.success(()))
  1323. }
  1324. }
  1325. }
  1326. .mapError { APSError.pumpError($0) }
  1327. .eraseToAnyPublisher()
  1328. }
  1329. func resumeDelivery() -> AnyPublisher<Void, Error> {
  1330. Future { promise in
  1331. self.resumeDelivery { error in
  1332. if let error = error {
  1333. promise(.failure(error))
  1334. } else {
  1335. promise(.success(()))
  1336. }
  1337. }
  1338. }
  1339. .mapError { APSError.pumpError($0) }
  1340. .eraseToAnyPublisher()
  1341. }
  1342. }
  1343. extension BaseAPSManager: PumpManagerStatusObserver {
  1344. func pumpManager(_: PumpManager, didUpdate status: PumpManagerStatus, oldStatus _: PumpManagerStatus) {
  1345. let percent = Int((status.pumpBatteryChargeRemaining ?? 1) * 100)
  1346. let battery = Battery(
  1347. percent: percent,
  1348. voltage: nil,
  1349. string: percent > 10 ? .normal : .low,
  1350. display: status.pumpBatteryChargeRemaining != nil
  1351. )
  1352. storage.save(battery, as: OpenAPS.Monitor.battery)
  1353. storage.save(status.pumpStatus, as: OpenAPS.Monitor.status)
  1354. }
  1355. }
  1356. extension BaseAPSManager: DoseProgressObserver {
  1357. func doseProgressReporterDidUpdate(_ doseProgressReporter: DoseProgressReporter) {
  1358. bolusProgress.send(Decimal(doseProgressReporter.progress.percentComplete))
  1359. if doseProgressReporter.progress.isComplete {
  1360. clearBolusReporter()
  1361. }
  1362. }
  1363. }
  1364. extension PumpManagerStatus {
  1365. var pumpStatus: PumpStatus {
  1366. let bolusing = bolusState != .noBolus
  1367. let suspended = basalDeliveryState?.isSuspended ?? true
  1368. let type = suspended ? StatusType.suspended : (bolusing ? .bolusing : .normal)
  1369. return PumpStatus(status: type, bolusing: bolusing, suspended: suspended, timestamp: Date())
  1370. }
  1371. }