Fix duplicate contact bug, reduce mqtt client proxy logging

This commit is contained in:
Garth Vander Houwen 2023-10-10 21:41:15 -07:00
parent 628edad092
commit 9799ed9330
5 changed files with 42 additions and 34 deletions

View file

@ -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()

View file

@ -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

View file

@ -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 {

View file

@ -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")

View file

@ -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