GlucoseSimulatorSource.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /// Glucose source - Blood Glucose Simulator
  2. ///
  3. /// Source publish fake data about glucose's level, creates ascending and descending trends
  4. ///
  5. /// Enter point of Source is GlucoseSimulatorSource.fetch method. Method is called from FetchGlucoseManager module.
  6. /// Not more often than a specified period (default - 300 seconds), it returns a Combine-publisher that publishes data on glucose values (global type BloodGlucose). If there is no up-to-date data (or the publication period has not passed yet), then a publisher of type Empty is returned, otherwise it returns a publisher of type Just.
  7. ///
  8. /// Simulator composition
  9. /// ===================
  10. ///
  11. /// class GlucoseSimulatorSource - main class
  12. /// protocol BloodGlucoseGenerator
  13. /// - IntelligentGenerator: BloodGlucoseGenerator
  14. // TODO: Every itteration trend make two steps, but must only one
  15. // TODO: Trend's value sticks to max and min Glucose value (in Glucose Generator)
  16. // TODO: Add reaction to insulin
  17. // TODO: Add probability to set trend's target value. Middle values must have more probability, than max and min.
  18. import Combine
  19. import Foundation
  20. import LoopKitUI
  21. // MARK: - Glucose simulator
  22. final class GlucoseSimulatorSource: GlucoseSource {
  23. var cgmManager: CGMManagerUI?
  24. var glucoseManager: FetchGlucoseManager?
  25. var cgmType: CGMType = .simulator
  26. private enum Config {
  27. // min time period to publish data
  28. static let workInterval: TimeInterval = 300
  29. // default BloodGlucose item at first run
  30. // 288 = 1 day * 24 hours * 60 minites * 60 seconds / workInterval
  31. static let defaultBGItems = 288
  32. }
  33. @Persisted(key: "GlucoseSimulatorLastGlucose") private var lastGlucose = 100
  34. @Persisted(key: "GlucoseSimulatorLastFetchDate") private var lastFetchDate: Date! = nil
  35. init() {
  36. if lastFetchDate == nil {
  37. var lastDate = Date()
  38. for _ in 1 ... Config.defaultBGItems {
  39. lastDate = lastDate.addingTimeInterval(-Config.workInterval)
  40. }
  41. lastFetchDate = lastDate
  42. }
  43. }
  44. private lazy var generator: BloodGlucoseGenerator = {
  45. IntelligentGenerator(
  46. currentGlucose: lastGlucose
  47. )
  48. }()
  49. private var canGenerateNewValues: Bool {
  50. guard let lastDate = lastFetchDate else { return true }
  51. if Calendar.current.dateComponents([.second], from: lastDate, to: Date()).second! >= Int(Config.workInterval) {
  52. return true
  53. } else {
  54. return false
  55. }
  56. }
  57. func fetch(_: DispatchTimer?) -> AnyPublisher<[BloodGlucose], Never> {
  58. guard canGenerateNewValues else {
  59. return Just([]).eraseToAnyPublisher()
  60. }
  61. let glucoses = generator.getBloodGlucoses(
  62. startDate: lastFetchDate,
  63. finishDate: Date(),
  64. withInterval: Config.workInterval
  65. )
  66. if let lastItem = glucoses.last {
  67. lastGlucose = lastItem.glucose!
  68. lastFetchDate = Date()
  69. }
  70. return Just(glucoses).eraseToAnyPublisher()
  71. }
  72. func fetchIfNeeded() -> AnyPublisher<[BloodGlucose], Never> {
  73. fetch(nil)
  74. }
  75. }
  76. // MARK: - Glucose generator
  77. protocol BloodGlucoseGenerator {
  78. func getBloodGlucoses(startDate: Date, finishDate: Date, withInterval: TimeInterval) -> [BloodGlucose]
  79. }
  80. class IntelligentGenerator: BloodGlucoseGenerator {
  81. private enum Config {
  82. // max and min glucose of trend's target
  83. static let maxGlucose = 320
  84. static let minGlucose = 45
  85. }
  86. // target glucose of trend
  87. @Persisted(key: "GlucoseSimulatorTargetValue") private var trendTargetValue = 100
  88. // how many steps left in current trend
  89. @Persisted(key: "GlucoseSimulatorTargetSteps") private var trendStepsLeft = 1
  90. // direction of last step
  91. @Persisted(key: "GlucoseSimulatorDirection") private var trandsStepDirection = BloodGlucose.Direction.flat.rawValue
  92. var currentGlucose: Int
  93. let startup = Date()
  94. init(currentGlucose: Int) {
  95. self.currentGlucose = currentGlucose
  96. }
  97. func getBloodGlucoses(startDate: Date, finishDate: Date, withInterval interval: TimeInterval) -> [BloodGlucose] {
  98. var result = [BloodGlucose]()
  99. var _currentDate = startDate
  100. while _currentDate <= finishDate {
  101. result.append(getNextBloodGlucose(forDate: _currentDate))
  102. _currentDate = _currentDate.addingTimeInterval(interval)
  103. }
  104. return result
  105. }
  106. // get next glucose's value in current trend
  107. private func getNextBloodGlucose(forDate date: Date) -> BloodGlucose {
  108. let previousGlucose = currentGlucose
  109. makeStepInTrend()
  110. trandsStepDirection = getDirection(fromGlucose: previousGlucose, toGlucose: currentGlucose).rawValue
  111. let glucose = BloodGlucose(
  112. _id: UUID().uuidString,
  113. sgv: currentGlucose,
  114. direction: BloodGlucose.Direction(rawValue: trandsStepDirection),
  115. date: Decimal(Int(date.timeIntervalSince1970) * 1000),
  116. dateString: date,
  117. unfiltered: Decimal(currentGlucose),
  118. filtered: nil,
  119. noise: nil,
  120. glucose: currentGlucose,
  121. type: nil,
  122. activationDate: startup,
  123. sessionStartDate: startup,
  124. transmitterID: "SIMULATOR"
  125. )
  126. return glucose
  127. }
  128. private func setNewRandomTarget() {
  129. guard trendTargetValue > 0 else {
  130. trendTargetValue = Array(80 ... 110).randomElement()!
  131. return
  132. }
  133. let difference = (Array(-50 ... -20) + Array(20 ... 50)).randomElement()!
  134. let _value = trendTargetValue + difference
  135. if _value <= Config.minGlucose {
  136. trendTargetValue = Config.minGlucose
  137. } else if _value >= Config.maxGlucose {
  138. trendTargetValue = Config.maxGlucose
  139. } else {
  140. trendTargetValue = _value
  141. }
  142. }
  143. private func setNewRandomSteps() {
  144. trendStepsLeft = Array(3 ... 8).randomElement()!
  145. }
  146. private func getDirection(fromGlucose from: Int, toGlucose to: Int) -> BloodGlucose.Direction {
  147. BloodGlucose.Direction(trend: Int(to - from))
  148. }
  149. private func generateNewTrend() {
  150. setNewRandomTarget()
  151. setNewRandomSteps()
  152. }
  153. private func makeStepInTrend() {
  154. currentGlucose +=
  155. Int(Double((trendTargetValue - currentGlucose) / trendStepsLeft) * [0.3, 0.6, 1, 1.3, 1.6, 2.0].randomElement()!)
  156. trendStepsLeft -= 1
  157. if trendStepsLeft == 0 {
  158. generateNewTrend()
  159. }
  160. }
  161. func sourceInfo() -> [String: Any]? {
  162. [GlucoseSourceKey.description.rawValue: "Glucose simulator"]
  163. }
  164. }