From 345e3467e782527f348af18d3ff2a758f61c58a8 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 8 Oct 2023 13:06:10 -0700 Subject: [PATCH] Dummy up users if they are nil to prevent Unknown from being half the nodes on MQTT --- Meshtastic/Helpers/MeshPackets.swift | 22 ++++++++++++++++++++- Meshtastic/Persistence/UpdateCoreData.swift | 10 ++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index b01808fa..b2083e4f 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -265,6 +265,16 @@ func nodeInfoPacket (nodeInfo: NodeInfo, channel: UInt32, context: NSManagedObje newUser.shortName = nodeInfo.user.shortName newUser.hwModel = String(describing: nodeInfo.user.hwModel).uppercased() newNode.user = newUser + } 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" + newNode.user = newUser } if nodeInfo.position.longitudeI > 0 || nodeInfo.position.latitudeI > 0 && (nodeInfo.position.latitudeI != 373346000 && nodeInfo.position.longitudeI != -1220090000) { @@ -306,7 +316,7 @@ func nodeInfoPacket (nodeInfo: NodeInfo, channel: UInt32, context: NSManagedObje } catch { print("💥 Fetch MyInfo Error") } - } else if nodeInfo.hasUser && nodeInfo.num > 0 { + } else if nodeInfo.num > 0 { fetchedNode[0].id = Int64(nodeInfo.num) fetchedNode[0].num = Int64(nodeInfo.num) @@ -323,6 +333,16 @@ 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 } if nodeInfo.hasDeviceMetrics { diff --git a/Meshtastic/Persistence/UpdateCoreData.swift b/Meshtastic/Persistence/UpdateCoreData.swift index 18c9463d..6ed295d5 100644 --- a/Meshtastic/Persistence/UpdateCoreData.swift +++ b/Meshtastic/Persistence/UpdateCoreData.swift @@ -178,6 +178,16 @@ func upsertNodeInfoPacket (packet: MeshPacket, context: NSManagedObjectContext) fetchedNode[0].user!.longName = nodeInfoMessage.user.longName 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 } } do {