Skip to content

Commit

Permalink
Save settings and preferences and profiles to stat database (#698)
Browse files Browse the repository at this point in the history
* Share settings and profiles

* Share Settings and Preferences
  • Loading branch information
Jon-b-m authored May 21, 2024
1 parent 61cf62e commit c740ea1
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 35 deletions.
1 change: 1 addition & 0 deletions FreeAPS/Sources/Models/NightscoutPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import Foundation
struct NightscoutPreferences: JSON {
var report = "preferences"
let preferences: Preferences?
let enteredBy: String
}
3 changes: 2 additions & 1 deletion FreeAPS/Sources/Models/NightscoutSettings.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

struct NightscoutSettings: JSON {
let report = "settings"
var report = "settings"
let settings: FreeAPSSettings?
let enteredBy: String
}
2 changes: 1 addition & 1 deletion FreeAPS/Sources/Models/NightscoutStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ struct NightscoutProfileStore: JSON {
let startDate: Date
let mills: Int
let units: String
let enteredBy: String
var enteredBy: String
let store: [String: ScheduledNightscoutProfile]
}
46 changes: 32 additions & 14 deletions FreeAPS/Sources/Services/Network/NightscoutAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,20 +559,18 @@ extension NightscoutAPI {
}

func uploadPrefs(_ prefs: NightscoutPreferences) -> AnyPublisher<Void, Swift.Error> {
let statURL = IAPSconfig.statURL
var components = URLComponents()
components.scheme = url.scheme
components.host = url.host
components.port = url.port
components.path = Config.statusPath
components.scheme = statURL.scheme
components.host = statURL.host
components.port = statURL.port
components.path = Config.sharePath

var request = URLRequest(url: components.url!)
request.allowsConstrainedNetworkAccess = false
request.timeoutInterval = Config.timeout
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

if let secret = secret {
request.addValue(secret.sha1(), forHTTPHeaderField: "api-secret")
}
request.httpBody = try! JSONCoding.encoder.encode(prefs)
request.httpMethod = "POST"

Expand All @@ -583,20 +581,18 @@ extension NightscoutAPI {
}

func uploadSettings(_ settings: NightscoutSettings) -> AnyPublisher<Void, Swift.Error> {
let statURL = IAPSconfig.statURL
var components = URLComponents()
components.scheme = url.scheme
components.host = url.host
components.port = url.port
components.path = Config.statusPath
components.scheme = statURL.scheme
components.host = statURL.host
components.port = statURL.port
components.path = Config.sharePath

var request = URLRequest(url: components.url!)
request.allowsConstrainedNetworkAccess = false
request.timeoutInterval = Config.timeout
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

if let secret = secret {
request.addValue(secret.sha1(), forHTTPHeaderField: "api-secret")
}
request.httpBody = try! JSONCoding.encoder.encode(settings)
request.httpMethod = "POST"

Expand Down Expand Up @@ -630,6 +626,28 @@ extension NightscoutAPI {
.eraseToAnyPublisher()
}

func uploadSettingsToDatabase(_ profile: NightscoutProfileStore) -> AnyPublisher<Void, Swift.Error> {
let statURL = IAPSconfig.statURL
var components = URLComponents()
components.scheme = statURL.scheme
components.host = statURL.host
components.port = statURL.port
components.path = Config.sharePath

var request = URLRequest(url: components.url!)
request.allowsConstrainedNetworkAccess = false
request.timeoutInterval = Config.timeout
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

request.httpBody = try! JSONCoding.encoder.encode(profile)
request.httpMethod = "POST"

return service.run(request)
.retry(Config.retryCount)
.map { _ in () }
.eraseToAnyPublisher()
}

func uploadPreferences(_ preferences: Preferences) -> AnyPublisher<Void, Swift.Error> {
var components = URLComponents()
components.scheme = url.scheme
Expand Down
73 changes: 54 additions & 19 deletions FreeAPS/Sources/Services/Network/NightscoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
settingsManager.settings.isUploadEnabled
}

private var isStatsUploadEnabled: Bool {
settingsManager.settings.uploadStats
}

private var isUploadGlucoseEnabled: Bool {
settingsManager.settings.uploadGlucose
}
Expand Down Expand Up @@ -472,10 +476,10 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {

func uploadPreferences(_ preferences: Preferences) {
let prefs = NightscoutPreferences(
preferences: settingsManager.preferences
preferences: settingsManager.preferences, enteredBy: getIdentifier()
)

guard let nightscout = nightscoutAPI, isUploadEnabled else {
guard let nightscout = nightscoutAPI, isStatsUploadEnabled else {
return
}

Expand All @@ -484,7 +488,7 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
.sink { completion in
switch completion {
case .finished:
debug(.nightscout, "Preferences uploaded")
debug(.nightscout, "Preferences uploaded to database")
self.storage.save(preferences, as: OpenAPS.Nightscout.uploadedPreferences)
case let .failure(error):
debug(.nightscout, error.localizedDescription)
Expand All @@ -496,10 +500,10 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {

func uploadSettings(_ settings: FreeAPSSettings) {
let sets = NightscoutSettings(
settings: settingsManager.settings
settings: settingsManager.settings, enteredBy: getIdentifier()
)

guard let nightscout = nightscoutAPI, isUploadEnabled else {
guard let nightscout = nightscoutAPI, isStatsUploadEnabled else {
return
}

Expand All @@ -508,7 +512,7 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
.sink { completion in
switch completion {
case .finished:
debug(.nightscout, "Settings uploaded")
debug(.nightscout, "Settings uploaded to database")
self.storage.save(settings, as: OpenAPS.Nightscout.uploadedSettings)
case let .failure(error):
debug(.nightscout, error.localizedDescription)
Expand Down Expand Up @@ -730,7 +734,7 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
store: [defaultProfile: ps]
)

guard let nightscout = nightscoutAPI, isNetworkReachable, isUploadEnabled else {
guard let nightscout = nightscoutAPI, isNetworkReachable, isUploadEnabled || isStatsUploadEnabled else {
return
}

Expand All @@ -754,18 +758,39 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
{
NSLog("NightscoutManager uploadProfile, no profile change")
} else {
processQueue.async {
nightscout.uploadProfile(p)
.sink { completion in
switch completion {
case .finished:
self.storage.save(p, as: OpenAPS.Nightscout.uploadedProfile)
debug(.nightscout, "Profile uploaded")
case let .failure(error):
debug(.nightscout, error.localizedDescription)
}
} receiveValue: {}
.store(in: &self.lifetime)
if isUploadEnabled {
processQueue.async {
nightscout.uploadProfile(p)
.sink { completion in
switch completion {
case .finished:
self.storage.save(p, as: OpenAPS.Nightscout.uploadedProfile)
debug(.nightscout, "Profile uploaded")
case let .failure(error):
debug(.nightscout, error.localizedDescription)
}
} receiveValue: {}
.store(in: &self.lifetime)
}
}
if isStatsUploadEnabled {
var q = p
q.enteredBy = getIdentifier()
processQueue.async {
nightscout.uploadSettingsToDatabase(q)
.sink { completion in
switch completion {
case .finished:
debug(.nightscout, "Profiles uploaded to database")
if !self.isUploadEnabled {
self.storage.save(p, as: OpenAPS.Nightscout.uploadedProfile)
}
case let .failure(error):
debug(.nightscout, error.localizedDescription)
}
} receiveValue: {}
.store(in: &self.lifetime)
}
}
}
}
Expand All @@ -775,6 +800,16 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
uploadTreatments(glucoseStorage.nightscoutCGMStateNotUploaded(), fileToSave: OpenAPS.Nightscout.uploadedCGMState)
}

private func getIdentifier() -> String {
var identfier = keychain.getValue(String.self, forKey: IAPSconfig.id) ?? ""
guard identfier.count > 1 else {
identfier = UUID().uuidString
keychain.setValue(identfier, forKey: IAPSconfig.id)
return identfier
}
return identfier
}

func uploadManualGlucose() {
uploadTreatments(
glucoseStorage.nightscoutManualGlucoseNotUploaded(),
Expand Down

0 comments on commit c740ea1

Please sign in to comment.