Sensor.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import Foundation
  2. public extension UserDefaults {
  3. private enum Key: String {
  4. case sensor = "no.bjorninge.libre2sensor"
  5. case calibrationMapping = "no.bjorninge.libre2sensor-calibrationmapping"
  6. }
  7. var preSelectedSensor: Sensor? {
  8. get {
  9. if let sensor = object(forKey: Key.sensor.rawValue) as? Data {
  10. let decoder = JSONDecoder()
  11. return try? decoder.decode(Sensor.self, from: sensor)
  12. }
  13. return nil
  14. }
  15. set {
  16. if let newValue = newValue {
  17. let encoder = JSONEncoder()
  18. if let encoded = try? encoder.encode(newValue) {
  19. set(encoded, forKey: Key.sensor.rawValue)
  20. }
  21. } else {
  22. removeObject(forKey: Key.sensor.rawValue)
  23. }
  24. }
  25. }
  26. var calibrationMapping : CalibrationToSensorMapping? {
  27. get {
  28. if let sensor = object(forKey: Key.calibrationMapping.rawValue) as? Data {
  29. let decoder = JSONDecoder()
  30. return try? decoder.decode(CalibrationToSensorMapping.self, from: sensor)
  31. }
  32. return nil
  33. }
  34. set {
  35. if let newValue = newValue {
  36. let encoder = JSONEncoder()
  37. if let encoded = try? encoder.encode(newValue) {
  38. set(encoded, forKey: Key.calibrationMapping.rawValue)
  39. }
  40. } else {
  41. removeObject(forKey: Key.calibrationMapping.rawValue)
  42. }
  43. }
  44. }
  45. }
  46. public struct CalibrationToSensorMapping: Codable {
  47. public let uuid: Data
  48. public let reverseFooterCRC: Int
  49. public init(uuid: Data, reverseFooterCRC: Int) {
  50. self.uuid = uuid
  51. self.reverseFooterCRC = reverseFooterCRC
  52. }
  53. }
  54. public struct Sensor: Codable {
  55. public let uuid: Data
  56. public let patchInfo: Data
  57. // public let calibrationInfo: SensorData.CalibrationInfo
  58. //public let family: SensorFamily
  59. //public let type: SensorType
  60. //public let region: SensorRegion
  61. //public let serial: String?
  62. //public var state: SensorState
  63. public var age: Int? = nil
  64. public var maxAge: Int
  65. // public var lifetime: Int
  66. public var unlockCount: Int
  67. /*
  68. public var unlockCount: Int {
  69. get {
  70. return UserDefaults.standard.integer(forKey: Key.sensorUnlockCount.rawValue)
  71. }
  72. set {
  73. UserDefaults.standard.setValue(newValue, forKey: Key.sensorUnlockCount.rawValue)
  74. }
  75. }*/
  76. /*
  77. public var elapsedLifetime: Int? {
  78. get {
  79. if let remainingLifetime = remainingLifetime {
  80. return max(0, lifetime - remainingLifetime)
  81. }
  82. return nil
  83. }
  84. }
  85. public var remainingLifetime: Int? {
  86. get {
  87. if let age = age {
  88. return max(0, lifetime - age)
  89. }
  90. return nil
  91. }
  92. } */
  93. public init(uuid: Data, patchInfo: Data, maxAge:Int, unlockCount: Int = 0) {
  94. self.uuid = uuid
  95. self.patchInfo = patchInfo
  96. //self.family = SensorFamily(patchInfo: patchInfo)
  97. //self.type = SensorType(patchInfo: patchInfo)
  98. //self.region = SensorRegion(patchInfo: patchInfo)
  99. //self.serial = sensorSerialNumber(sensorUID: self.uuid, sensorFamily: self.family)
  100. //self.state = SensorState(fram: fram)
  101. //self.lifetime = Int(fram[327]) << 8 + Int(fram[326])
  102. self.unlockCount = 0
  103. self.maxAge = maxAge
  104. //self.calibrationInfo = calibrationInfo
  105. }
  106. public var description: String {
  107. return [
  108. "uuid: (\(uuid.hex))",
  109. "patchInfo: (\(patchInfo.hex))",
  110. //"calibrationInfo: (\(calibrationInfo.description))",
  111. //"family: \(family.description)",
  112. //"type: \(type.description)",
  113. //"region: \(region.description)",
  114. //"serial: \(serial ?? "Unknown")",
  115. //"state: \(state.description)",
  116. //"lifetime: \(lifetime.inTime)",
  117. ].joined(separator: ", ")
  118. }
  119. }
  120. fileprivate enum Key: String, CaseIterable {
  121. case sensorUnlockCount = "libre-direct.settings.sensor.unlockCount"
  122. }
  123. /*
  124. fileprivate func sensorSerialNumber(sensorUID: Data, sensorFamily: SensorFamily) -> String? {
  125. let lookupTable = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "T", "U", "V", "W", "X", "Y", "Z"]
  126. guard sensorUID.count == 8 else {
  127. return nil
  128. }
  129. let bytes = Array(sensorUID.reversed().suffix(6))
  130. var fiveBitsArray = [UInt8]()
  131. fiveBitsArray.append(bytes[0] >> 3)
  132. fiveBitsArray.append(bytes[0] << 2 + bytes[1] >> 6)
  133. fiveBitsArray.append(bytes[1] >> 1)
  134. fiveBitsArray.append(bytes[1] << 4 + bytes[2] >> 4)
  135. fiveBitsArray.append(bytes[2] << 1 + bytes[3] >> 7)
  136. fiveBitsArray.append(bytes[3] >> 2)
  137. fiveBitsArray.append(bytes[3] << 3 + bytes[4] >> 5)
  138. fiveBitsArray.append(bytes[4])
  139. fiveBitsArray.append(bytes[5] >> 3)
  140. fiveBitsArray.append(bytes[5] << 2)
  141. return fiveBitsArray.reduce("\(sensorFamily.rawValue)", {
  142. $0 + lookupTable[Int(0x1F & $1)]
  143. })
  144. }
  145. */