PumpHistoryStorage.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import Foundation
  2. import LoopKit
  3. import SwiftDate
  4. import Swinject
  5. protocol PumpHistoryObserver {
  6. func pumpHistoryDidUpdate(_ events: [PumpHistoryEvent])
  7. }
  8. protocol PumpHistoryStorage {
  9. func storePumpEvents(_ events: [NewPumpEvent])
  10. func storeEvents(_ events: [PumpHistoryEvent])
  11. func storeJournalCarbs(_ carbs: Int)
  12. func recent() -> [PumpHistoryEvent]
  13. func nightscoutTretmentsNotUploaded() -> [NigtscoutTreatment]
  14. func saveCancelTempEvents()
  15. }
  16. final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
  17. private let processQueue = DispatchQueue(label: "BasePumpHistoryStorage.processQueue")
  18. @Injected() private var storage: FileStorage!
  19. @Injected() private var broadcaster: Broadcaster!
  20. init(resolver: Resolver) {
  21. injectServices(resolver)
  22. }
  23. func storePumpEvents(_ events: [NewPumpEvent]) {
  24. processQueue.async {
  25. let eventsToStore = events.flatMap { event -> [PumpHistoryEvent] in
  26. let id = event.raw.md5String
  27. switch event.type {
  28. case .bolus:
  29. guard let dose = event.dose else { return [] }
  30. let amount = Decimal(string: dose.unitsInDeliverableIncrements.description)
  31. let minutes = Int((dose.endDate - dose.startDate).timeInterval / 60)
  32. return [PumpHistoryEvent(
  33. id: id,
  34. type: .bolus,
  35. timestamp: event.date,
  36. amount: amount,
  37. duration: minutes,
  38. durationMin: nil,
  39. rate: nil,
  40. temp: nil,
  41. carbInput: nil
  42. )]
  43. case .tempBasal:
  44. guard let dose = event.dose else { return [] }
  45. let rate = Decimal(dose.unitsPerHour)
  46. let minutes = (dose.endDate - dose.startDate).timeInterval / 60
  47. let delivered = dose.deliveredUnits
  48. let date = event.date
  49. let isCancel = delivered != nil //! event.isMutable && delivered != nil
  50. guard !isCancel else { return [] }
  51. return [
  52. PumpHistoryEvent(
  53. id: id,
  54. type: .tempBasalDuration,
  55. timestamp: date,
  56. amount: nil,
  57. duration: nil,
  58. durationMin: Int(round(minutes)),
  59. rate: nil,
  60. temp: nil,
  61. carbInput: nil
  62. ),
  63. PumpHistoryEvent(
  64. id: "_" + id,
  65. type: .tempBasal,
  66. timestamp: date,
  67. amount: nil,
  68. duration: nil,
  69. durationMin: nil,
  70. rate: rate,
  71. temp: .absolute,
  72. carbInput: nil
  73. )
  74. ]
  75. case .suspend:
  76. return [
  77. PumpHistoryEvent(
  78. id: id,
  79. type: .pumpSuspend,
  80. timestamp: event.date,
  81. amount: nil,
  82. duration: nil,
  83. durationMin: nil,
  84. rate: nil,
  85. temp: nil,
  86. carbInput: nil
  87. )
  88. ]
  89. case .resume:
  90. return [
  91. PumpHistoryEvent(
  92. id: id,
  93. type: .pumpResume,
  94. timestamp: event.date,
  95. amount: nil,
  96. duration: nil,
  97. durationMin: nil,
  98. rate: nil,
  99. temp: nil,
  100. carbInput: nil
  101. )
  102. ]
  103. case .rewind:
  104. return [
  105. PumpHistoryEvent(
  106. id: id,
  107. type: .rewind,
  108. timestamp: event.date,
  109. amount: nil,
  110. duration: nil,
  111. durationMin: nil,
  112. rate: nil,
  113. temp: nil,
  114. carbInput: nil
  115. )
  116. ]
  117. case .prime:
  118. return [
  119. PumpHistoryEvent(
  120. id: id,
  121. type: .prime,
  122. timestamp: event.date,
  123. amount: nil,
  124. duration: nil,
  125. durationMin: nil,
  126. rate: nil,
  127. temp: nil,
  128. carbInput: nil
  129. )
  130. ]
  131. case .alarm:
  132. return [
  133. PumpHistoryEvent(
  134. id: id,
  135. type: .pumpAlarm,
  136. timestamp: event.date,
  137. note: event.title
  138. )
  139. ]
  140. default:
  141. return []
  142. }
  143. }
  144. self.storeEvents(eventsToStore)
  145. }
  146. }
  147. func storeJournalCarbs(_ carbs: Int) {
  148. processQueue.async {
  149. let eventsToStore = [
  150. PumpHistoryEvent(
  151. id: UUID().uuidString,
  152. type: .journalCarbs,
  153. timestamp: Date(),
  154. amount: nil,
  155. duration: nil,
  156. durationMin: nil,
  157. rate: nil,
  158. temp: nil,
  159. carbInput: carbs
  160. )
  161. ]
  162. self.storeEvents(eventsToStore)
  163. }
  164. }
  165. func storeEvents(_ events: [PumpHistoryEvent]) {
  166. processQueue.async {
  167. let file = OpenAPS.Monitor.pumpHistory
  168. var uniqEvents: [PumpHistoryEvent] = []
  169. self.storage.transaction { storage in
  170. storage.append(events, to: file, uniqBy: \.id)
  171. uniqEvents = storage.retrieve(file, as: [PumpHistoryEvent].self)?
  172. .filter { $0.timestamp.addingTimeInterval(1.days.timeInterval) > Date() }
  173. .sorted { $0.timestamp > $1.timestamp } ?? []
  174. storage.save(Array(uniqEvents), as: file)
  175. }
  176. self.broadcaster.notify(PumpHistoryObserver.self, on: self.processQueue) {
  177. $0.pumpHistoryDidUpdate(uniqEvents)
  178. }
  179. }
  180. }
  181. func recent() -> [PumpHistoryEvent] {
  182. storage.retrieve(OpenAPS.Monitor.pumpHistory, as: [PumpHistoryEvent].self)?.reversed() ?? []
  183. }
  184. func nightscoutTretmentsNotUploaded() -> [NigtscoutTreatment] {
  185. let events = recent()
  186. guard !events.isEmpty else { return [] }
  187. let temps: [NigtscoutTreatment] = events.reduce([]) { result, event in
  188. var result = result
  189. switch event.type {
  190. case .tempBasal:
  191. result.append(NigtscoutTreatment(
  192. duration: nil,
  193. rawDuration: nil,
  194. rawRate: event,
  195. absolute: event.rate,
  196. rate: event.rate,
  197. eventType: .nsTempBasal,
  198. createdAt: event.timestamp,
  199. enteredBy: NigtscoutTreatment.local,
  200. bolus: nil,
  201. insulin: nil,
  202. notes: nil,
  203. carbs: nil,
  204. targetTop: nil,
  205. targetBottom: nil
  206. ))
  207. case .tempBasalDuration:
  208. if var last = result.popLast(), last.eventType == .nsTempBasal, last.createdAt == event.timestamp {
  209. last.duration = event.durationMin
  210. last.rawDuration = event
  211. result.append(last)
  212. }
  213. default: break
  214. }
  215. return result
  216. }
  217. let bolusesAndCarbs = events.compactMap { event -> NigtscoutTreatment? in
  218. switch event.type {
  219. case .bolus:
  220. return NigtscoutTreatment(
  221. duration: event.duration,
  222. rawDuration: nil,
  223. rawRate: nil,
  224. absolute: nil,
  225. rate: nil,
  226. eventType: .bolus,
  227. createdAt: event.timestamp,
  228. enteredBy: NigtscoutTreatment.local,
  229. bolus: event,
  230. insulin: event.amount,
  231. notes: nil,
  232. carbs: nil,
  233. targetTop: nil,
  234. targetBottom: nil
  235. )
  236. case .journalCarbs:
  237. return NigtscoutTreatment(
  238. duration: nil,
  239. rawDuration: nil,
  240. rawRate: nil,
  241. absolute: nil,
  242. rate: nil,
  243. eventType: .nsCarbCorrection,
  244. createdAt: event.timestamp,
  245. enteredBy: NigtscoutTreatment.local,
  246. bolus: nil,
  247. insulin: nil,
  248. notes: nil,
  249. carbs: Decimal(event.carbInput ?? 0),
  250. targetTop: nil,
  251. targetBottom: nil
  252. )
  253. default: return nil
  254. }
  255. }
  256. let misc = events.compactMap { event -> NigtscoutTreatment? in
  257. switch event.type {
  258. case .prime:
  259. return NigtscoutTreatment(
  260. duration: event.duration,
  261. rawDuration: nil,
  262. rawRate: nil,
  263. absolute: nil,
  264. rate: nil,
  265. eventType: .nsSiteChange,
  266. createdAt: event.timestamp,
  267. enteredBy: NigtscoutTreatment.local,
  268. bolus: event,
  269. insulin: nil,
  270. notes: nil,
  271. carbs: nil,
  272. targetTop: nil,
  273. targetBottom: nil
  274. )
  275. case .rewind:
  276. return NigtscoutTreatment(
  277. duration: nil,
  278. rawDuration: nil,
  279. rawRate: nil,
  280. absolute: nil,
  281. rate: nil,
  282. eventType: .nsInsulinChange,
  283. createdAt: event.timestamp,
  284. enteredBy: NigtscoutTreatment.local,
  285. bolus: nil,
  286. insulin: nil,
  287. notes: nil,
  288. carbs: nil,
  289. targetTop: nil,
  290. targetBottom: nil
  291. )
  292. case .pumpAlarm:
  293. return NigtscoutTreatment(
  294. duration: 30, // minutes
  295. rawDuration: nil,
  296. rawRate: nil,
  297. absolute: nil,
  298. rate: nil,
  299. eventType: .nsAnnouncement,
  300. createdAt: event.timestamp,
  301. enteredBy: NigtscoutTreatment.local,
  302. bolus: nil,
  303. insulin: nil,
  304. notes: "Alarm \(String(describing: event.note)) \(event.type)",
  305. carbs: nil,
  306. targetTop: nil,
  307. targetBottom: nil
  308. )
  309. default: return nil
  310. }
  311. }
  312. let uploaded = storage.retrieve(OpenAPS.Nightscout.uploadedPumphistory, as: [NigtscoutTreatment].self) ?? []
  313. let treatments = Array(Set([bolusesAndCarbs, temps, misc].flatMap { $0 }).subtracting(Set(uploaded)))
  314. return treatments.sorted { $0.createdAt! > $1.createdAt! }
  315. }
  316. func saveCancelTempEvents() {
  317. let basalID = UUID().uuidString
  318. let date = Date()
  319. let events = [
  320. PumpHistoryEvent(
  321. id: basalID,
  322. type: .tempBasalDuration,
  323. timestamp: date,
  324. amount: nil,
  325. duration: nil,
  326. durationMin: 0,
  327. rate: nil,
  328. temp: nil,
  329. carbInput: nil
  330. ),
  331. PumpHistoryEvent(
  332. id: "_" + basalID,
  333. type: .tempBasal,
  334. timestamp: date,
  335. amount: nil,
  336. duration: nil,
  337. durationMin: nil,
  338. rate: 0,
  339. temp: .absolute,
  340. carbInput: nil
  341. )
  342. ]
  343. storeEvents(events)
  344. }
  345. }