From 7cd048ef2269b6ab984417679e83daf0ed703eea Mon Sep 17 00:00:00 2001 From: Nathaniel Hamming Date: Wed, 18 Sep 2024 08:39:23 -0300 Subject: [PATCH 1/3] report time zone changes --- TidepoolServiceKit/TidepoolService.swift | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/TidepoolServiceKit/TidepoolService.swift b/TidepoolServiceKit/TidepoolService.swift index b1a1b1f..c7abf21 100644 --- a/TidepoolServiceKit/TidepoolService.swift +++ b/TidepoolServiceKit/TidepoolService.swift @@ -88,6 +88,7 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject { await tapi.addObserver(self) deviceLogUploader = DeviceLogUploader(api: tapi) await deviceLogUploader?.setDelegate(remoteDataServiceDelegate) + observeTimeZoneChanges() } public init?(rawState: RawStateValue) { @@ -485,6 +486,33 @@ extension TidepoolService: RemoteDataService { return (created, updated, lastControllerSettingsDatum, lastCGMSettingsDatum, lastPumpSettingsDatum) } + + private func uploadTimeZoneChangeData(from fromTimeZone: TimeZone, to toTimeZone: TimeZone, method: TTimeChangeDeviceEventDatum.Method = .automatic, at date: Date = Date()) async throws { + guard let userId = userId, let hostIdentifier = hostIdentifier, let hostVersion = hostVersion else { + throw TidepoolServiceError.configuration + } + + let timeZoneChangeData = TTimeChangeDeviceEventDatum(time: date, + from: TTimeChangeDeviceEventDatum.Info(timeZoneName: fromTimeZone.identifier), + to: TTimeChangeDeviceEventDatum.Info(timeZoneName: toTimeZone.identifier), + method: method) + let _ = try await createData([timeZoneChangeData]) + } + + private func observeTimeZoneChanges() { + NotificationCenter.default.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: .main) { notification in + if let previousTimeZone = notification.object as? TimeZone { + let currentTimeZone = TimeZone.current + Task { + do { + try await self.uploadTimeZoneChangeData(from: previousTimeZone, to: currentTimeZone) + } catch { + self.log.error("Failed to upload time zone change data - %{public}@", error.localizedDescription) + } + } + } + } + } private func createData(_ data: [TDatum]) async throws -> Bool { if let error = error { From 1485b9751331ba595933e406ed54cbc43acd9329 Mon Sep 17 00:00:00 2001 From: Nathaniel Hamming Date: Wed, 18 Sep 2024 10:45:03 -0300 Subject: [PATCH 2/3] removed unneeded print statement --- TidepoolServiceKit/DeviceLogUploader.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/TidepoolServiceKit/DeviceLogUploader.swift b/TidepoolServiceKit/DeviceLogUploader.swift index 61b5a18..f9061ec 100644 --- a/TidepoolServiceKit/DeviceLogUploader.swift +++ b/TidepoolServiceKit/DeviceLogUploader.swift @@ -19,7 +19,7 @@ actor DeviceLogUploader { private var delegate: RemoteDataServiceDelegate? - private var logChunkDuration = TimeInterval(hours: 1) + private var logChunkDuration = TimeInterval(minutes: 5)//TimeInterval(hours: 1) func setDelegate(_ delegate: RemoteDataServiceDelegate?) { self.delegate = delegate @@ -91,10 +91,8 @@ actor DeviceLogUploader { do { let metatdata = try await api.uploadDeviceLogs(logs: data, start: start, end: end) log.default("metadata: %@", String(describing: metatdata)) - print("hi") } catch { log.error("error uploading device logs:: %@", String(describing: error)) - print("hi") } } } From f43252fc195567c093e57da75e35c37af888e826 Mon Sep 17 00:00:00 2001 From: Nathaniel Hamming Date: Wed, 18 Sep 2024 14:10:49 -0300 Subject: [PATCH 3/3] set device log uploader when remoteDataServiceDelegate is set --- TidepoolServiceKit/DeviceLogUploader.swift | 2 +- TidepoolServiceKit/TidepoolService.swift | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/TidepoolServiceKit/DeviceLogUploader.swift b/TidepoolServiceKit/DeviceLogUploader.swift index f9061ec..4ed7344 100644 --- a/TidepoolServiceKit/DeviceLogUploader.swift +++ b/TidepoolServiceKit/DeviceLogUploader.swift @@ -19,7 +19,7 @@ actor DeviceLogUploader { private var delegate: RemoteDataServiceDelegate? - private var logChunkDuration = TimeInterval(minutes: 5)//TimeInterval(hours: 1) + private var logChunkDuration = TimeInterval(hours: 1) func setDelegate(_ delegate: RemoteDataServiceDelegate?) { self.delegate = delegate diff --git a/TidepoolServiceKit/TidepoolService.swift b/TidepoolServiceKit/TidepoolService.swift index c7abf21..3089277 100644 --- a/TidepoolServiceKit/TidepoolService.swift +++ b/TidepoolServiceKit/TidepoolService.swift @@ -48,8 +48,14 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject { public weak var stateDelegate: StatefulPluggableDelegate? - public weak var remoteDataServiceDelegate: RemoteDataServiceDelegate? - + public weak var remoteDataServiceDelegate: RemoteDataServiceDelegate? { + didSet { + Task { + await setDeviceLogUploaderDelegate() + } + } + } + public lazy var sessionStorage: SessionStorage = KeychainManager() public let tapi: TAPI = TAPI(clientId: BuildDetails.default.tidepoolServiceClientId, redirectURL: BuildDetails.default.tidepoolServiceRedirectURL) @@ -72,6 +78,10 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject { private let tidepoolKitLog = OSLog(category: "TidepoolKit") private var deviceLogUploader: DeviceLogUploader? + + private func setDeviceLogUploaderDelegate() async { + await deviceLogUploader?.setDelegate(remoteDataServiceDelegate) + } public init(hostIdentifier: String, hostVersion: String) { self.id = UUID().uuidString @@ -87,7 +97,7 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject { await tapi.setLogging(self) await tapi.addObserver(self) deviceLogUploader = DeviceLogUploader(api: tapi) - await deviceLogUploader?.setDelegate(remoteDataServiceDelegate) + await setDeviceLogUploaderDelegate() observeTimeZoneChanges() }