From 61a98a09b9498dd8811df2cc20dcb9fef1e5bbeb Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 10 Mar 2024 20:17:54 -0700 Subject: [PATCH] Handle topic change keep less positions in memory --- Meshtastic.xcodeproj/project.pbxproj | 4 +- Meshtastic/Extensions/UserDefaults.swift | 9 ++++ Meshtastic/Helpers/BLEManager.swift | 1 + Meshtastic/Helpers/LocationsHandler.swift | 44 +++++++++---------- .../Helpers/Mqtt/MqttClientProxyManager.swift | 9 ++-- Meshtastic/Views/Settings/AppSettings.swift | 6 +++ Meshtastic/Views/Settings/Firmware.swift | 2 +- 7 files changed, 47 insertions(+), 28 deletions(-) diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index e54ac7fc..6cf4d0e2 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -1572,7 +1572,7 @@ CODE_SIGN_ENTITLEMENTS = Meshtastic/Meshtastic.entitlements; "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 841; + CURRENT_PROJECT_VERSION = 842; DEVELOPMENT_ASSET_PATHS = "\"Meshtastic/Preview Content\""; DEVELOPMENT_TEAM = GCH7VS5Y9R; ENABLE_PREVIEWS = YES; @@ -1606,7 +1606,7 @@ CODE_SIGN_ENTITLEMENTS = Meshtastic/Meshtastic.entitlements; "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 841; + CURRENT_PROJECT_VERSION = 842; DEVELOPMENT_ASSET_PATHS = "\"Meshtastic/Preview Content\""; DEVELOPMENT_TEAM = GCH7VS5Y9R; ENABLE_PREVIEWS = YES; diff --git a/Meshtastic/Extensions/UserDefaults.swift b/Meshtastic/Extensions/UserDefaults.swift index 1963ed4f..39cc9b89 100644 --- a/Meshtastic/Extensions/UserDefaults.swift +++ b/Meshtastic/Extensions/UserDefaults.swift @@ -28,6 +28,7 @@ extension UserDefaults { case detectionSensorRole case enableSmartPosition case modemPreset + case firmwareVersion } func reset() { @@ -211,4 +212,12 @@ extension UserDefaults { UserDefaults.standard.set(newValue, forKey: "modemPreset") } } + static var firmwareVersion: String { + get { + UserDefaults.standard.string(forKey: "firmwareVersion") ?? "0.0.0" + } + set { + UserDefaults.standard.set(newValue, forKey: "firmwareVersion") + } + } } diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index cf663f10..5d85d325 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -582,6 +582,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate nowKnown = true connectedVersion = String(version.dropLast()) appState.firmwareVersion = connectedVersion + UserDefaults.firmwareVersion = connectedVersion } let supportedVersion = connectedVersion == "0.0.0" || self.minimumVersion.compare(connectedVersion, options: .numeric) == .orderedAscending || minimumVersion.compare(connectedVersion, options: .numeric) == .orderedSame if !supportedVersion { diff --git a/Meshtastic/Helpers/LocationsHandler.swift b/Meshtastic/Helpers/LocationsHandler.swift index 82714d62..6ec44b90 100644 --- a/Meshtastic/Helpers/LocationsHandler.swift +++ b/Meshtastic/Helpers/LocationsHandler.swift @@ -60,14 +60,10 @@ import CoreLocation self.isStationary = update.isStationary var locationAdded: Bool - if enableSmartPosition { - locationAdded = addLocation(loc) - //print("Added Location \(self.count): \(loc)") - } else { - locationsArray.append(loc) - locationAdded = true - } - if locationAdded { + locationAdded = addLocation(loc, smartPostion: enableSmartPosition) + if !isRecording && locationAdded { + self.count = 1 + } else if locationAdded && isRecording { self.count += 1 } } @@ -84,19 +80,21 @@ import CoreLocation self.updatesStarted = false } - func addLocation(_ location: CLLocation) -> Bool { - let age = -location.timestamp.timeIntervalSinceNow - if age > 10 { - print("Bad Location \(self.count): Too Old \(age) seconds ago \(location)") - return false - } - if location.horizontalAccuracy < 0 { - print("Bad Location \(self.count): Horizontal Accuracy: \(location.horizontalAccuracy) \(location)") - return false - } - if location.horizontalAccuracy > 25 { - print("Bad Location \(self.count): Horizontal Accuracy: \(location.horizontalAccuracy) \(location)") - return false + func addLocation(_ location: CLLocation, smartPostion: Bool) -> Bool { + if smartPostion { + let age = -location.timestamp.timeIntervalSinceNow + if age > 10 { + print("Bad Location \(self.count): Too Old \(age) seconds ago \(location)") + return false + } + if location.horizontalAccuracy < 0 { + print("Bad Location \(self.count): Horizontal Accuracy: \(location.horizontalAccuracy) \(location)") + return false + } + if location.horizontalAccuracy > 25 { + print("Bad Location \(self.count): Horizontal Accuracy: \(location.horizontalAccuracy) \(location)") + return false + } } if isRecording { if let lastLocation = locationsArray.last { @@ -107,8 +105,10 @@ import CoreLocation elevationGain += gain } } + locationsArray.append(location) + } else { + locationsArray = [location] } - locationsArray.append(location) return true } diff --git a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift index ce3bcf49..5a1a33d4 100644 --- a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift +++ b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift @@ -21,7 +21,7 @@ class MqttClientProxyManager { private static let defaultKeepAliveInterval: Int32 = 60 weak var delegate: MqttClientProxyManagerDelegate? var mqttClientProxy: CocoaMQTT? - var topic = "msh/2/e" + var topic = "msh" var debugLog = false func connectFromConfigSettings(node: NodeInfoEntity) { let defaultServerAddress = "mqtt.meshtastic.org" @@ -36,13 +36,16 @@ class MqttClientProxyManager { defaultServerPort = Int(fullHost.components(separatedBy: ":")[1]) ?? (useSsl ? 8883 : 1883) } } + let minimumVersion = "2.3.0" + let latestVersion = minimumVersion.compare(UserDefaults.firmwareVersion, options: .numeric) == .orderedSame + if let host = host { let port = defaultServerPort let username = node.mqttConfig?.username let password = node.mqttConfig?.password let root = node.mqttConfig?.root?.count ?? 0 > 0 ? node.mqttConfig?.root : "msh" - let prefix = root! + "/2/e" - topic = prefix + "/#" + let prefix = root! + topic = prefix + (latestVersion ? "/2/e" : "/2/c") + "/#" let qos = CocoaMQTTQoS(rawValue: UInt8(1))! connect(host: host, port: port, useSsl: useSsl, username: username, password: password, topic: topic, qos: qos, cleanSession: true) } diff --git a/Meshtastic/Views/Settings/AppSettings.swift b/Meshtastic/Views/Settings/AppSettings.swift index 9e559b62..cd9ef8f4 100644 --- a/Meshtastic/Views/Settings/AppSettings.swift +++ b/Meshtastic/Views/Settings/AppSettings.swift @@ -24,9 +24,15 @@ struct AppSettings: View { Label("appsettings.provide.location", systemImage: "location.circle.fill") } .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + Text("Use your phone's gps to provide a location to your node. Must have location access and precise location enabled for Meshtastic in Settings.") + .font(.caption2) + .foregroundColor(.gray) if provideLocation { Toggle(isOn: $enableSmartPosition) { Label("appsettings.smartposition", systemImage: "brain") + Text("Will only send a position to the phone if it is recent and of high horizontal accuracy.") + .font(.caption2) + .foregroundColor(.gray) } .toggleStyle(SwitchToggleStyle(tint: .accentColor)) VStack { diff --git a/Meshtastic/Views/Settings/Firmware.swift b/Meshtastic/Views/Settings/Firmware.swift index 21aa3b82..98b29957 100644 --- a/Meshtastic/Views/Settings/Firmware.swift +++ b/Meshtastic/Views/Settings/Firmware.swift @@ -12,7 +12,7 @@ struct Firmware: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager var node: NodeInfoEntity? - @State var minimumVersion = "2.2.21" + @State var minimumVersion = "2.3.0" @State var version = "" @State private var currentDevice: DeviceHardware? @State private var latestStable: FirmwareRelease?