OrefFunction.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 makeProfile
  20. case iob
  21. case meal
  22. // since we're removing some keys from our Profile that exist in Javascript
  23. // we need to let the difference function know which keys to ignore when
  24. // calculating differences
  25. func keysToIgnore() -> Set<String> {
  26. switch self {
  27. case .makeProfile:
  28. return Set(["calc_glucose_noise", "enableEnliteBgproxy", "exercise_mode", "offline_hotspot"])
  29. case .iob:
  30. // we're only checking the first result for now
  31. return Set(stride(from: 1, to: 48, by: 1).map { String("[\($0)]") })
  32. case .meal:
  33. // These aren't used by downstream calculations, so we
  34. // can ignore them in our comparison
  35. return Set(["maxDeviation", "minDeviation", "allDeviations", "bwCarbs", "bwFound", "journalCarbs", "nsCarbs"])
  36. }
  37. }
  38. // Some values might be slightly different due to Double vs Decimal
  39. // and minor algorithmic differences
  40. func approximateMatchingNumbers() -> [String: Double] {
  41. switch self {
  42. case .makeProfile:
  43. return [:]
  44. case .iob:
  45. // for iob we can get rounding errors because of Double vs Decimal
  46. // so we leave a little extra room for our comparisons
  47. return [
  48. "iob": 0.1,
  49. "activity": 0.01,
  50. "basaliob": 0.25,
  51. "bolusiob": 0.25,
  52. "netbasalinsulin": 0.25,
  53. "bolusinsulin": 0.25,
  54. // Please see this issue for context on duration:
  55. // https://github.com/nightscout/Trio-dev/issues/453
  56. "duration": 120
  57. ]
  58. case .meal:
  59. return [
  60. "carbs": 0.1,
  61. "mealCOB": 10,
  62. "currentDeviation": 1,
  63. "slopeFromMaxDeviation": 0.25,
  64. "slopeFromMinDeviation": 0.25,
  65. "lastCarbTime": 1
  66. ]
  67. }
  68. }
  69. func returnType() -> ReturnType {
  70. switch self {
  71. case .makeProfile:
  72. return .dictionary
  73. case .iob:
  74. return .array
  75. case .meal:
  76. return .dictionary
  77. }
  78. }
  79. }