From 8dc1210607fdb6fce02faabfa58b30720d93a536 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Thu, 21 Dec 2023 10:59:09 -0800 Subject: [PATCH] Trace route cleanup, smarter positions for phone locations on ios 17 --- Meshtastic/Helpers/BLEManager.swift | 5 ++-- Meshtastic/Helpers/LocationsHandler.swift | 31 +++++++++++++++++++++- Meshtastic/Views/Nodes/TraceRouteLog.swift | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index ea8d8942..cffe109b 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -664,8 +664,9 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate if hopNode != nil { hopNodes.append(traceRouteHop) } - routeString += "\(hopNode?.user?.longName ?? "unknown".localized) --> " + routeString += "\(hopNode?.user?.longName ?? "unknown".localized) -->" } + routeString += traceRoute?.node?.user?.longName ?? "unknown".localized traceRoute?.routeText = routeString traceRoute?.hops = NSOrderedSet(array: hopNodes) do { @@ -676,8 +677,6 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate let nsError = error as NSError print("💥 Error Updating Core Data TraceRouteHOp: \(nsError)") } - - routeString += "\(decodedInfo.packet.from)" let logString = String.localizedStringWithFormat("mesh.log.traceroute.received.route %@".localized, routeString) MeshLogger.log("🪧 \(logString)") } diff --git a/Meshtastic/Helpers/LocationsHandler.swift b/Meshtastic/Helpers/LocationsHandler.swift index 9a0fd68f..cd9707af 100644 --- a/Meshtastic/Helpers/LocationsHandler.swift +++ b/Meshtastic/Helpers/LocationsHandler.swift @@ -16,6 +16,8 @@ import CoreLocation static let shared = LocationsHandler() // Create a single, shared instance of the object. private let manager: CLLocationManager private var background: CLBackgroundActivitySession? + var locationsArray: [CLLocation] + var enableSmartPosition: Bool @Published var lastLocation = CLLocation() @Published var isStationary = false @@ -36,6 +38,9 @@ import CoreLocation private init() { self.manager = CLLocationManager() // Creating a location manager instance is safe to call here in `MainActor`. + locationsArray = [CLLocation]() + enableSmartPosition = true + self.manager.distanceFilter = 5 } func startLocationUpdates() { @@ -53,7 +58,16 @@ import CoreLocation self.lastLocation = loc self.isStationary = update.isStationary self.count += 1 - print("Location \(self.count): \(self.lastLocation)") + var locationAdded: Bool + if enableSmartPosition { + locationAdded = addLocation(loc) + } else { + locationsArray.append(loc) + locationAdded = true + } + if !locationAdded { + print("Bad Location \(self.count): \(loc)") + } } } } catch { @@ -68,6 +82,21 @@ import CoreLocation self.updatesStarted = false } + func addLocation(_ location: CLLocation) -> Bool { + let age = -location.timestamp.timeIntervalSinceNow + if age > 10 { + return false + } + if location.horizontalAccuracy < 0 { + return false + } + if location.horizontalAccuracy > 100 { + return false + } + locationsArray.append(location) + return true + } + static let DefaultLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090) static var satsInView: Int { diff --git a/Meshtastic/Views/Nodes/TraceRouteLog.swift b/Meshtastic/Views/Nodes/TraceRouteLog.swift index 7b525932..874b4660 100644 --- a/Meshtastic/Views/Nodes/TraceRouteLog.swift +++ b/Meshtastic/Views/Nodes/TraceRouteLog.swift @@ -34,7 +34,7 @@ struct TraceRouteLog: View { List(node.traceRoutes?.reversed() as? [TraceRouteEntity] ?? [], id: \.self, selection: $selectedRoute) { route in Label { - Text("\(route.time?.formatted() ?? "unknown".localized) - \(route.response ? (route.hops?.count == 0 && route.response ? "Direct" : "\(route.hops?.count == 0) Hops") : "No Response")") + Text("\(route.time?.formatted() ?? "unknown".localized) - \(route.response ? (route.hops?.count == 0 && route.response ? "Direct" : "\(route.hops?.count ?? 0) Hops") : "No Response")") } icon: { Image(systemName: route.response ? (route.hops?.count == 0 && route.response ? "person.line.dotted.person" : "point.3.connected.trianglepath.dotted") : "person.slash") .symbolRenderingMode(.hierarchical)