Skip to content

Commit

Permalink
Publisher example: Add a setting for logging LocationHistoryData JSON
Browse files Browse the repository at this point in the history
While testing a fix for #425, I wanted to have access to the location
history data in a log message.

This adds a switch in a new "Developer settings" section of the Settings
page, which when turned on will emit a JSON serialization of the
location history data when it’s received from the SDK. It’s turned off
by default, because it's only for my specific use case.
  • Loading branch information
lawrence-forooghian committed Dec 15, 2022
1 parent 86b3a9f commit e946fff
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Combine
import AblyAssetTrackingPublisher
import Foundation
import Logging

class ObservablePublisher: ObservableObject {
private let publisher: AblyAssetTrackingPublisher.Publisher
private let logger: Logger?
let configInfo: PublisherConfigInfo
let locationHistoryDataHandler: LocationHistoryDataHandlerProtocol?

Expand All @@ -22,11 +24,12 @@ class ObservablePublisher: ObservableObject {
@Published private(set) var lastError: ErrorInformation?

// After initialising an ObservablePublisher instance, you need to manually set the publisher’s delegate to the created instance.
init(publisher: AblyAssetTrackingPublisher.Publisher, configInfo: PublisherConfigInfo, locationHistoryDataHandler: LocationHistoryDataHandlerProtocol? = nil) {
init(publisher: AblyAssetTrackingPublisher.Publisher, configInfo: PublisherConfigInfo, locationHistoryDataHandler: LocationHistoryDataHandlerProtocol? = nil, logger: Logger? = nil) {
self.publisher = publisher
self.configInfo = configInfo
self.routingProfile = publisher.routingProfile
self.locationHistoryDataHandler = locationHistoryDataHandler
self.logger = logger
}

func stop(completion: @escaping ResultHandler<Void>) {
Expand Down Expand Up @@ -89,6 +92,19 @@ extension ObservablePublisher: PublisherDelegate {
}

func publisher(sender: AblyAssetTrackingPublisher.Publisher, didFinishRecordingLocationHistoryData locationHistoryData: LocationHistoryData) {
if SettingsModel.shared.logLocationHistoryJSON {
do {
let jsonData = try JSONEncoder().encode(locationHistoryData)
if let jsonString = String(data: jsonData, encoding: .utf8) {
logger?.log(level: .debug, "Received location history data: \(jsonString)")
} else {
logger?.log(level: .error, "Failed to convert location history data to string")
}
} catch {
logger?.log(level: .error, "Failed to serialize location history data to JSON: \(error.localizedDescription)")
}
}

locationHistoryDataHandler?.handleLocationHistoryData(locationHistoryData)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ class SettingsModel {
}
}

var logLocationHistoryJSON: Bool {
get {
UserDefaults.standard.bool(forKey: "logLocationHistoryJSON")
}
set {
UserDefaults.standard.set(newValue, forKey: "logLocationHistoryJSON")
}
}

var vehicleProfile: VehicleProfile {
get {
guard let profile: VehicleProfile = UserDefaults.standard.get("vehicleProfile") else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ struct SettingsView: View {
} header: {
Text("Other settings")
}

Section {
Toggle(isOn: $viewModel.logLocationHistoryJSON) {
Text("Log location history JSON")
Text("Causes the app to emit a `debug` level log message when a `LocationHistoryData` is received from the Asset Tracking SDK. The log message will contain a JSON serialization of this history data.")
}
} header: {
Text("Developer settings")
}
}
.listStyle(.grouped)
.navigationBarTitle("Settings")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class CreatePublisherViewModel: ObservableObject {

let configInfo = ObservablePublisher.PublisherConfigInfo(areRawLocationsEnabled: areRawLocationsEnabled, constantResolution: constantResolution)

let observablePublisher = ObservablePublisher(publisher: publisher, configInfo: configInfo, locationHistoryDataHandler: locationHistoryDataHandler)
let observablePublisher = ObservablePublisher(publisher: publisher, configInfo: configInfo, locationHistoryDataHandler: locationHistoryDataHandler, logger: logger)
publisher.delegate = observablePublisher

return observablePublisher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class SettingsViewModel: ObservableObject {
}
}

@Published var logLocationHistoryJSON: Bool = SettingsModel.shared.logLocationHistoryJSON {
didSet {
SettingsModel.shared.logLocationHistoryJSON = logLocationHistoryJSON
}
}

@Published var defaultResolutionMinimumDisplacement: String = "\(SettingsModel.shared.defaultResolution.minimumDisplacement)"
@Published var defaultResolutionDesiredInterval: String = "\(SettingsModel.shared.defaultResolution.desiredInterval)"

Expand Down

0 comments on commit e946fff

Please sign in to comment.