-
Notifications
You must be signed in to change notification settings - Fork 1
/
siteChange.patch
89 lines (82 loc) · 3.3 KB
/
siteChange.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
From 5e41c1a524155666d80074e8558e0d4f9f549928 Mon Sep 17 00:00:00 2001
From: bjorkert <[email protected]>
Date: Sat, 25 Feb 2023 19:43:25 +0100
Subject: [PATCH] siteChange
---
OmniBLE/PumpManager/OmniBLEPumpManager.swift | 52 ++++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git a/OmniBLE/PumpManager/OmniBLEPumpManager.swift b/OmniBLE/PumpManager/OmniBLEPumpManager.swift
index 90feb09..549b375 100644
--- a/OmniBLE/PumpManager/OmniBLEPumpManager.swift
+++ b/OmniBLE/PumpManager/OmniBLEPumpManager.swift
@@ -874,6 +874,56 @@ extension OmniBLEPumpManager {
#endif
}
+ private func SiteChange()
+ {
+ do { // Silence all errors and return
+ let keychain = KeychainManager()
+ let credentials = try keychain.getInternetCredentials(account: "NightscoutAPI")
+
+ let date = Date()
+ let formatter = ISO8601DateFormatter()
+ formatter.timeZone = TimeZone(abbreviation: "UTC")
+ let dateString = formatter.string(from: date)
+
+ let json: [String: Any] = [
+ "enteredBy": "Loop",
+ "timestamp": dateString,
+ "eventType": "Site Change",
+ "secret": credentials.password.sha1()
+ ]
+
+ guard var urlComponents = URLComponents(url: credentials.url, resolvingAgainstBaseURL: false) else {return }
+ if urlComponents.path.hasSuffix("/") {
+ urlComponents.path = String(urlComponents.path.dropLast())
+ }
+ urlComponents.path += "/api/v1/treatments.json"
+
+ guard let apiURL = urlComponents.url else { return }
+
+ var request = URLRequest(url: apiURL)
+ request.httpMethod = "POST"
+ request.setValue("application/json", forHTTPHeaderField: "Content-Type")
+ request.httpBody = try? JSONSerialization.data(withJSONObject: json)
+
+ let task = URLSession.shared.dataTask(with: request) { data, response, error in
+ if let error = error {
+ // Handle network error
+ print("Error: \(error)")
+ return
+ }
+
+ guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
+ // Handle server error
+ print("Error: Invalid response")
+ return
+ }
+ }
+ task.resume()
+ } catch {
+ return
+ }
+ }
+
// Called on the main thread
public func insertCannula(completion: @escaping (Result<TimeInterval,OmniBLEPumpManagerError>) -> Void) {
@@ -894,6 +944,7 @@ extension OmniBLEPumpManager {
var podState = state.podState
podState?.setupProgress = .completed
state.updatePodStateFromPodComms(podState)
+ self.SiteChange()
return .success(mockDelay)
})
@@ -944,6 +995,7 @@ extension OmniBLEPumpManager {
]
let finishWait = try session.insertCannula(optionalAlerts: alerts)
+ self.SiteChange()
completion(.success(finishWait))
} catch let error {
completion(.failure(.communication(error)))
--
2.38.1