From 9799ed9330476f5b021ccbc55bcc91c8edf20aab Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 10 Oct 2023 21:41:15 -0700 Subject: [PATCH] Fix duplicate contact bug, reduce mqtt client proxy logging --- Meshtastic/Extensions/Date.swift | 3 --- Meshtastic/Helpers/BLEManager.swift | 6 ++--- Meshtastic/Helpers/MeshPackets.swift | 23 +++++++++++-------- .../Helpers/Mqtt/MqttClientProxyManager.swift | 23 +++++++++++++------ Meshtastic/Persistence/UpdateCoreData.swift | 21 +++++++++-------- 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/Meshtastic/Extensions/Date.swift b/Meshtastic/Extensions/Date.swift index 29a60320..224f0b03 100644 --- a/Meshtastic/Extensions/Date.swift +++ b/Meshtastic/Extensions/Date.swift @@ -8,9 +8,6 @@ import Foundation extension Date { - static var currentTimeStamp: Int64 { - return Int64(Date().timeIntervalSince1970 * 1000) - } func formattedDate(format: String) -> String { let dateformat = DateFormatter() diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 351c899e..30c83cd9 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -290,7 +290,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate func onMqttMessageReceived(message: CocoaMQTTMessage) { - print("📲 Mqtt Client Proxy onMqttMessageReceived for topic: \(message.topic)") + if message.topic.contains("/stat/") { return } @@ -305,7 +305,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate let binaryData: Data = try! toRadio.serializedData() if connectedPeripheral?.peripheral.state ?? CBPeripheralState.disconnected == CBPeripheralState.connected { connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) - print("📲 Sent Mqtt client proxy message to the connected device.") + } } @@ -443,7 +443,6 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate payload: [UInt8](decodedInfo.mqttClientProxyMessage.data), retained: decodedInfo.mqttClientProxyMessage.retained ) - print("📲 Publish Mqtt client proxy message received on FromRadio to the Mqtt server \(message)") mqttManager.mqttClientProxy?.publish(message) } @@ -870,7 +869,6 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) success = true let logString = String.localizedStringWithFormat("mesh.log.sharelocation %@".localized, String(fromNodeNum)) - print(positionPacket) MeshLogger.log("📍 \(logString)") } return success diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index eca33e91..1ae63a83 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -258,6 +258,7 @@ func nodeInfoPacket (nodeInfo: NodeInfo, channel: UInt32, context: NSManagedObje newNode.lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(nodeInfo.lastHeard))) newNode.snr = nodeInfo.snr if nodeInfo.hasUser { + let newUser = UserEntity(context: context) newUser.userId = nodeInfo.user.id newUser.num = Int64(nodeInfo.num) @@ -333,16 +334,18 @@ func nodeInfoPacket (nodeInfo: NodeInfo, channel: UInt32, context: NSManagedObje fetchedNode[0].user!.longName = nodeInfo.user.longName fetchedNode[0].user!.shortName = nodeInfo.user.shortName fetchedNode[0].user!.hwModel = String(describing: nodeInfo.user.hwModel).uppercased() - } else { - let newUser = UserEntity(context: context) - newUser.num = Int64(nodeInfo.num) - let userId = String(format:"%2X", nodeInfo.num) - newUser.userId = "!\(userId)" - let last4 = String(userId.suffix(4)) - newUser.longName = "Meshtastic \(last4)" - newUser.shortName = last4 - newUser.hwModel = "UNSET" - fetchedNode[0].user = newUser + } else { + if (fetchedNode[0].user == nil) { + let newUser = UserEntity(context: context) + newUser.num = Int64(nodeInfo.num) + let userId = String(format:"%2X", nodeInfo.num) + newUser.userId = "!\(userId)" + let last4 = String(userId.suffix(4)) + newUser.longName = "Meshtastic \(last4)" + newUser.shortName = last4 + newUser.hwModel = "UNSET" + fetchedNode[0].user = newUser + } } if nodeInfo.hasDeviceMetrics { diff --git a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift index 4df407b5..2d7dc6c1 100644 --- a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift +++ b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift @@ -22,6 +22,7 @@ class MqttClientProxyManager { weak var delegate: MqttClientProxyManagerDelegate? var mqttClientProxy: CocoaMQTT? var topic = "msh/2/c" + var debugLog = false func connectFromConfigSettings(node: NodeInfoEntity) { let defaultServerAddress = "mqtt.meshtastic.org" let useSsl = node.mqttConfig?.tlsEnabled == true @@ -58,9 +59,9 @@ class MqttClientProxyManager { mqttClient.password = password mqttClient.keepAlive = 60 mqttClient.cleanSession = cleanSession -#if DEBUG - mqttClient.logLevel = .debug -#endif + if debugLog { + mqttClient.logLevel = .debug + } mqttClient.willMessage = CocoaMQTTMessage(topic: "/will", string: "dieout") mqttClient.autoReconnect = true mqttClient.delegate = self @@ -82,7 +83,9 @@ class MqttClientProxyManager { } func publish(message: String, topic: String, qos: CocoaMQTTQoS) { mqttClientProxy?.publish(topic, withString: message, qos: qos) - print("📲 MQTT Client Proxy publish for: " + topic) + if debugLog { + print("📲 MQTT Client Proxy publish for: " + topic) + } } func disconnect() { if let client = mqttClientProxy { @@ -130,15 +133,21 @@ extension MqttClientProxyManager: CocoaMQTTDelegate { delegate?.onMqttDisconnected() } func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) { - print("📲 MQTT Client Proxy didPublishMessage from MqttClientProxyManager: \(message)") + if debugLog { + print("📲 MQTT Client Proxy didPublishMessage from MqttClientProxyManager: \(message)") + } } func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) { - print("📲 MQTT Client Proxy didPublishAck from MqttClientProxyManager: \(id)") + if debugLog { + print("📲 MQTT Client Proxy didPublishAck from MqttClientProxyManager: \(id)") + } } public func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) { delegate?.onMqttMessageReceived(message: message) - print("📲 MQTT Client Proxy message received on topic: \(message.topic)") + if debugLog { + print("📲 MQTT Client Proxy message received on topic: \(message.topic)") + } } func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) { print("📲 MQTT Client Proxy didSubscribeTopics: \(success.allKeys.count) topics. failed: \(failed.count) topics") diff --git a/Meshtastic/Persistence/UpdateCoreData.swift b/Meshtastic/Persistence/UpdateCoreData.swift index 6ed295d5..7c79d265 100644 --- a/Meshtastic/Persistence/UpdateCoreData.swift +++ b/Meshtastic/Persistence/UpdateCoreData.swift @@ -179,15 +179,17 @@ func upsertNodeInfoPacket (packet: MeshPacket, context: NSManagedObjectContext) fetchedNode[0].user!.shortName = nodeInfoMessage.user.shortName fetchedNode[0].user!.hwModel = String(describing: nodeInfoMessage.user.hwModel).uppercased() } else { - let newUser = UserEntity(context: context) - newUser.num = Int64(nodeInfoMessage.num) - let userId = String(format:"%2X", nodeInfoMessage.num) - newUser.userId = "!\(userId)" - let last4 = String(userId.suffix(4)) - newUser.longName = "Meshtastic \(last4)" - newUser.shortName = last4 - newUser.hwModel = "UNSET" - fetchedNode[0].user! = newUser + if (fetchedNode[0].user == nil) { + let newUser = UserEntity(context: context) + newUser.num = Int64(nodeInfoMessage.num) + let userId = String(format:"%2X", nodeInfoMessage.num) + newUser.userId = "!\(userId)" + let last4 = String(userId.suffix(4)) + newUser.longName = "Meshtastic \(last4)" + newUser.shortName = last4 + newUser.hwModel = "UNSET" + fetchedNode[0].user! = newUser + } } } do { @@ -235,7 +237,6 @@ func upsertPositionPacket (packet: MeshPacket, context: NSManagedObjectContext) position.latest = false } } - print("Incoming position message: \n \(positionMessage)") let position = PositionEntity(context: context) position.latest = true position.snr = packet.rxSnr