AppGroupSource.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import Combine
  2. import Foundation
  3. import LibreTransmitter
  4. struct AppGroupSource: GlucoseSource {
  5. let from: String
  6. func fetch() -> AnyPublisher<[BloodGlucose], Never> {
  7. guard let suiteName = Bundle.main.appGroupSuiteName,
  8. let sharedDefaults = UserDefaults(suiteName: suiteName)
  9. else {
  10. return Just([]).eraseToAnyPublisher()
  11. }
  12. return Just(fetchLastBGs(60, sharedDefaults)).eraseToAnyPublisher()
  13. }
  14. private func fetchLastBGs(_ count: Int, _ sharedDefaults: UserDefaults) -> [BloodGlucose] {
  15. guard let sharedData = sharedDefaults.data(forKey: "latestReadings") else {
  16. return []
  17. }
  18. HeartBeatManager.shared.checkCGMBluetoothTransmitter(sharedUserDefaults: sharedDefaults)
  19. let decoded = try? JSONSerialization.jsonObject(with: sharedData, options: [])
  20. guard let sgvs = decoded as? [AnyObject] else {
  21. return []
  22. }
  23. var results: [BloodGlucose] = []
  24. for sgv in sgvs.prefix(count) {
  25. guard
  26. let glucose = sgv["Value"] as? Int,
  27. let timestamp = sgv["DT"] as? String,
  28. let date = parseDate(timestamp)
  29. else { continue }
  30. var direction: String?
  31. // Dexcom changed the format of trend in 2021 so we accept both String/Int types
  32. if let directionString = sgv["direction"] as? String {
  33. direction = directionString
  34. } else if let intTrend = sgv["trend"] as? Int {
  35. direction = GlucoseTrend(rawValue: intTrend)?.direction
  36. } else if let intTrend = sgv["Trend"] as? Int {
  37. direction = GlucoseTrend(rawValue: intTrend)?.direction
  38. } else if let stringTrend = sgv["trend"] as? String, let intTrend = Int(stringTrend) {
  39. direction = GlucoseTrend(rawValue: intTrend)?.direction
  40. }
  41. guard let direction = direction else { continue }
  42. if let from = sgv["from"] as? String {
  43. guard from == self.from else { continue }
  44. }
  45. results.append(
  46. BloodGlucose(
  47. sgv: glucose,
  48. direction: BloodGlucose.Direction(rawValue: direction),
  49. date: Decimal(Int(date.timeIntervalSince1970 * 1000)),
  50. dateString: date,
  51. unfiltered: nil,
  52. filtered: nil,
  53. noise: nil,
  54. glucose: glucose,
  55. type: "sgv"
  56. )
  57. )
  58. }
  59. return results
  60. }
  61. private func parseDate(_ timestamp: String) -> Date? {
  62. // timestamp looks like "/Date(1462404576000)/"
  63. guard let re = try? NSRegularExpression(pattern: "\\((.*)\\)"),
  64. let match = re.firstMatch(in: timestamp, range: NSMakeRange(0, timestamp.count))
  65. else {
  66. return nil
  67. }
  68. let matchRange = match.range(at: 1)
  69. let epoch = Double((timestamp as NSString).substring(with: matchRange))! / 1000
  70. return Date(timeIntervalSince1970: epoch)
  71. }
  72. func sourceInfo() -> [String: Any]? {
  73. [GlucoseSourceKey.description.rawValue: "Group ID: \(Bundle.main.appGroupSuiteName ?? "Not set"))"]
  74. }
  75. }
  76. public extension Bundle {
  77. var appGroupSuiteName: String? {
  78. object(forInfoDictionaryKey: "AppGroupID") as? String
  79. }
  80. }