OrefFunction.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import Foundation
  2. /// After the port from Javascript to Swift is complete, we should remove the logging module:
  3. /// https://github.com/nightscout/Trio-dev/issues/293
  4. enum OrefFunctionResult {
  5. case success(RawJSON)
  6. case failure(Error)
  7. func returnOrThrow() throws -> RawJSON {
  8. switch self {
  9. case let .success(json): return json
  10. case let .failure(error): throw error
  11. }
  12. }
  13. }
  14. enum OrefFunction: String, Codable {
  15. enum ReturnType {
  16. case array
  17. case dictionary
  18. }
  19. case autosens
  20. case iob
  21. case meal
  22. case makeProfile
  23. case determineBasal
  24. // since we're removing some keys from our Profile that exist in Javascript
  25. // we need to let the difference function know which keys to ignore when
  26. // calculating differences
  27. func keysToIgnore() -> Set<String> {
  28. switch self {
  29. case .makeProfile:
  30. return Set(["calc_glucose_noise", "enableEnliteBgproxy", "exercise_mode", "offline_hotspot"])
  31. case .iob:
  32. // we're only checking the first result for now
  33. return Set(stride(from: 1, to: 48, by: 1).map { String("[\($0)]") })
  34. case .meal:
  35. // These aren't used by downstream calculations, so we
  36. // can ignore them in our comparison
  37. return Set(["maxDeviation", "minDeviation", "allDeviations", "bwCarbs", "bwFound", "journalCarbs", "nsCarbs"])
  38. case .autosens:
  39. return Set(["deviationsUnsorted"])
  40. case .determineBasal:
  41. // FIXME: Adjust as we go
  42. return Set([
  43. // Not calculating yet
  44. "id",
  45. "units",
  46. "insulinReq",
  47. "rate",
  48. "duration",
  49. "deliverAt",
  50. "carbsReq",
  51. "temp",
  52. "reservoir",
  53. "ISF",
  54. "current_target",
  55. "TDD",
  56. "insulinForManualBolus",
  57. "manualBolusErrorString",
  58. "minDelta",
  59. "CR",
  60. "received",
  61. "reason",
  62. // in JS but not in Swift
  63. "tick",
  64. "BGI",
  65. "target_bg",
  66. "deviation",
  67. // in Swift but not in JS
  68. "timestamp",
  69. "minGuardBG",
  70. "minPredBG"
  71. ])
  72. }
  73. }
  74. // Some values might be slightly different due to Double vs Decimal
  75. // and minor algorithmic differences
  76. func approximateMatchingNumbers() -> [String: Double] {
  77. switch self {
  78. case .makeProfile:
  79. return [:]
  80. case .iob:
  81. // for iob we can get rounding errors because of Double vs Decimal
  82. // so we leave a little extra room for our comparisons
  83. return [
  84. "iob": 0.1,
  85. "activity": 0.01,
  86. "basaliob": 0.25,
  87. "bolusiob": 0.25,
  88. "netbasalinsulin": 0.25,
  89. "bolusinsulin": 0.25,
  90. // Please see this issue for context on duration:
  91. // https://github.com/nightscout/Trio-dev/issues/453
  92. "duration": 120
  93. ]
  94. case .meal:
  95. return [
  96. "carbs": 0.1,
  97. "mealCOB": 10,
  98. "currentDeviation": 1,
  99. "slopeFromMaxDeviation": 0.25,
  100. "slopeFromMinDeviation": 0.25,
  101. "lastCarbTime": 1
  102. ]
  103. case .autosens:
  104. return [
  105. "ratio": 0.011,
  106. "newisf": 1.5
  107. ]
  108. case .determineBasal:
  109. return [:]
  110. }
  111. }
  112. func returnType() -> ReturnType {
  113. switch self {
  114. case .makeProfile:
  115. return .dictionary
  116. case .iob:
  117. return .array
  118. case .meal:
  119. return .dictionary
  120. case .autosens:
  121. return .dictionary
  122. case .determineBasal:
  123. return .dictionary
  124. }
  125. }
  126. }