Skip to content

Commit f303ba7

Browse files
authored
Merge pull request #2 from loopandlearn/pr_patches_update
Pr patches update
2 parents dcbfc33 + 363e1c4 commit f303ba7

File tree

7 files changed

+2112
-0
lines changed

7 files changed

+2112
-0
lines changed

2002/dev_2002.patch

Lines changed: 946 additions & 0 deletions
Large diffs are not rendered by default.
File renamed without changes.

2002/pr2002_profile_light.patch

Lines changed: 946 additions & 0 deletions
Large diffs are not rendered by default.

dexcom_sage/dev_g6g7_sage.patch

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
Submodule CGMBLEKit 2d1b8f1..5e73129:
2+
diff --git a/CGMBLEKit/CGMBLEKit/TransmitterManager.swift b/CGMBLEKit/CGMBLEKit/TransmitterManager.swift
3+
index 4942087..26d2181 100644
4+
--- a/CGMBLEKit/CGMBLEKit/TransmitterManager.swift
5+
+++ b/CGMBLEKit/CGMBLEKit/TransmitterManager.swift
6+
@@ -9,7 +9,19 @@ import HealthKit
7+
import LoopKit
8+
import ShareClient
9+
import os.log
10+
-
11+
+import CommonCrypto
12+
+
13+
+extension String {
14+
+ func sha1() -> String {
15+
+ let data = Data(self.utf8)
16+
+ var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
17+
+ data.withUnsafeBytes {
18+
+ _ = CC_SHA1($0.baseAddress, CC_LONG(data.count), &digest)
19+
+ }
20+
+ let hexBytes = digest.map { String(format: "%02hhx", $0) }
21+
+ return hexBytes.joined()
22+
+ }
23+
+}
24+
25+
public struct TransmitterManagerState: RawRepresentable, Equatable {
26+
public typealias RawValue = CGMManager.RawStateValue
27+
@@ -301,6 +313,74 @@ public class TransmitterManager: TransmitterDelegate {
28+
logDeviceCommunication("Error: \(error)", type: .error)
29+
}
30+
31+
+ //Patch for detecting a new G6 sensor
32+
+ private func detectNewSensor() {
33+
+ do { // Silence all errors and return
34+
+ if let latestReading = latestReading {
35+
+ let date = latestReading.sessionStartDate
36+
+ let tenDaysInSeconds = 864000.0
37+
+ let timeIntervalSinceNow = date.timeIntervalSinceNow
38+
+ let now = Date()
39+
+
40+
+ //Check that the sessionStartDate is reasonable
41+
+ if timeIntervalSinceNow > -tenDaysInSeconds && date <= now {
42+
+
43+
+ //Read the last date sent to nightscout
44+
+ let interval = UserDefaults.standard.double(forKey: "lastSessionStartDate")
45+
+ let lastsessionStartDate = Date(timeIntervalSinceReferenceDate: interval)
46+
+
47+
+ //Check if the last value we sent to nightscout is not the same (with some margin for rounding errors)
48+
+ if abs(date.timeIntervalSince(lastsessionStartDate)) > 60 {
49+
+ let keychain = KeychainManager()
50+
+ let credentials = try keychain.getInternetCredentials(account: "NightscoutAPI")
51+
+
52+
+ let formatter = ISO8601DateFormatter()
53+
+ formatter.timeZone = TimeZone(abbreviation: "UTC")
54+
+ let dateString = formatter.string(from: date)
55+
+
56+
+ let json: [String: Any] = [
57+
+ "enteredBy": "Loop",
58+
+ "created_at": dateString,
59+
+ "eventType": "Sensor Start",
60+
+ "secret": credentials.password.sha1()
61+
+ ]
62+
+
63+
+ guard var urlComponents = URLComponents(url: credentials.url, resolvingAgainstBaseURL: false) else {return }
64+
+ if urlComponents.path.hasSuffix("/") {
65+
+ urlComponents.path = String(urlComponents.path.dropLast())
66+
+ }
67+
+ urlComponents.path += "/api/v1/treatments.json"
68+
+
69+
+ guard let apiURL = urlComponents.url else { return }
70+
+
71+
+ var request = URLRequest(url: apiURL)
72+
+ request.httpMethod = "POST"
73+
+ request.setValue("application/json", forHTTPHeaderField: "Content-Type")
74+
+ request.httpBody = try? JSONSerialization.data(withJSONObject: json)
75+
+
76+
+ let task = URLSession.shared.dataTask(with: request) { data, response, error in
77+
+ if let error = error {
78+
+ // Handle network error
79+
+ print("Error: \(error)")
80+
+ return
81+
+ }
82+
+
83+
+ guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
84+
+ // Handle server error
85+
+ print("Error: Invalid response")
86+
+ return
87+
+ }
88+
+ UserDefaults.standard.set(date.timeIntervalSinceReferenceDate, forKey: "lastSessionStartDate")
89+
+ }
90+
+ task.resume()
91+
+ }
92+
+ }
93+
+ }
94+
+ } catch {
95+
+ return
96+
+ }
97+
+ }
98+
+
99+
public func transmitter(_ transmitter: Transmitter, didRead glucose: Glucose) {
100+
guard glucose != latestReading else {
101+
updateDelegate(with: .noData)
102+
@@ -309,6 +389,8 @@ public class TransmitterManager: TransmitterDelegate {
103+
104+
latestReading = glucose
105+
106+
+ detectNewSensor()
107+
+
108+
logDeviceCommunication("New reading: \(glucose.readDate)", type: .receive)
109+
110+
guard glucose.state.hasReliableGlucose else {
111+
Submodule G7SensorKit f7c8c4f..97e2ef0:
112+
diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
113+
index d5c0b46..a1b3455 100644
114+
--- a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
115+
+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
116+
@@ -10,7 +10,20 @@ import Foundation
117+
import CoreBluetooth
118+
import HealthKit
119+
import os.log
120+
-
121+
+import LoopKit
122+
+import CommonCrypto
123+
+
124+
+extension String {
125+
+ func sha1() -> String {
126+
+ let data = Data(self.utf8)
127+
+ var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
128+
+ data.withUnsafeBytes {
129+
+ _ = CC_SHA1($0.baseAddress, CC_LONG(data.count), &digest)
130+
+ }
131+
+ let hexBytes = digest.map { String(format: "%02hhx", $0) }
132+
+ return hexBytes.joined()
133+
+ }
134+
+}
135+
136+
public protocol G7SensorDelegate: AnyObject {
137+
func sensorDidConnect(_ sensor: G7Sensor, name: String)
138+
@@ -122,6 +135,73 @@ public final class G7Sensor: G7BluetoothManagerDelegate {
139+
return bluetoothManager.isConnected
140+
}
141+
142+
+ //Patch for detecting a new G6 sensor
143+
+ private func detectNewSensor() {
144+
+ do { // Silence all errors and return
145+
+ if let date = self.activationDate {
146+
+ let tenDaysInSeconds = 864000.0
147+
+ let timeIntervalSinceNow = date.timeIntervalSinceNow
148+
+ let now = Date()
149+
+
150+
+ //Check that the sessionStartDate is reasonable
151+
+ if timeIntervalSinceNow > -tenDaysInSeconds && date <= now {
152+
+
153+
+ //Read the last date sent to nightscout
154+
+ let interval = UserDefaults.standard.double(forKey: "lastG7SessionStartDate")
155+
+ let lastsessionStartDate = Date(timeIntervalSinceReferenceDate: interval)
156+
+
157+
+ //Check if the last value we sent to nightscout is not the same (with some margin for rounding errors)
158+
+ if abs(date.timeIntervalSince(lastsessionStartDate)) > 60 {
159+
+ let keychain = KeychainManager()
160+
+ let credentials = try keychain.getInternetCredentials(account: "NightscoutAPI")
161+
+
162+
+ let formatter = ISO8601DateFormatter()
163+
+ formatter.timeZone = TimeZone(abbreviation: "UTC")
164+
+ let dateString = formatter.string(from: date)
165+
+
166+
+ let json: [String: Any] = [
167+
+ "enteredBy": "Loop",
168+
+ "created_at": dateString,
169+
+ "eventType": "Sensor Start",
170+
+ "secret": credentials.password.sha1()
171+
+ ]
172+
+
173+
+ guard var urlComponents = URLComponents(url: credentials.url, resolvingAgainstBaseURL: false) else {return }
174+
+ if urlComponents.path.hasSuffix("/") {
175+
+ urlComponents.path = String(urlComponents.path.dropLast())
176+
+ }
177+
+ urlComponents.path += "/api/v1/treatments.json"
178+
+
179+
+ guard let apiURL = urlComponents.url else { return }
180+
+
181+
+ var request = URLRequest(url: apiURL)
182+
+ request.httpMethod = "POST"
183+
+ request.setValue("application/json", forHTTPHeaderField: "Content-Type")
184+
+ request.httpBody = try? JSONSerialization.data(withJSONObject: json)
185+
+
186+
+ let task = URLSession.shared.dataTask(with: request) { data, response, error in
187+
+ if let error = error {
188+
+ // Handle network error
189+
+ print("Error: \(error)")
190+
+ return
191+
+ }
192+
+
193+
+ guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
194+
+ // Handle server error
195+
+ print("Error: Invalid response")
196+
+ return
197+
+ }
198+
+ UserDefaults.standard.set(date.timeIntervalSinceReferenceDate, forKey: "lastG7SessionStartDate")
199+
+ }
200+
+ task.resume()
201+
+ }
202+
+ }
203+
+ }
204+
+ } catch {
205+
+ return
206+
+ }
207+
+ }
208+
+
209+
private func handleGlucoseMessage(message: G7GlucoseMessage, peripheralManager: G7PeripheralManager) {
210+
activationDate = Date().addingTimeInterval(-TimeInterval(message.messageTimestamp))
211+
peripheralManager.perform { (peripheral) in
212+
@@ -147,6 +227,8 @@ public final class G7Sensor: G7BluetoothManagerDelegate {
213+
self.activationDate = activationDate
214+
self.delegate?.sensor(self, didRead: message)
215+
self.bluetoothManager.stopScanning()
216+
+
217+
+ self.detectNewSensor()
218+
}
219+
}
220+
} else if sensorID != nil {

0 commit comments

Comments
 (0)