From da382614dd0ab3070586b831a549d2b87b711e48 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 13 Nov 2022 09:25:00 -0800 Subject: [PATCH] Assorted app cleanup --- Meshtastic.xcodeproj/project.pbxproj | 2 + Meshtastic/Helpers/BLEManager.swift | 58 +++++-------------- Meshtastic/Helpers/MeshPackets.swift | 38 +++++------- Meshtastic/Info.plist | 2 + Meshtastic/Persistence/Persistence.swift | 2 + Meshtastic/Views/Helpers/BatteryGauge.swift | 5 -- .../Views/Messages/ChannelMessageList.swift | 2 +- .../Views/Messages/UserMessageList.swift | 2 +- Meshtastic/Views/Nodes/NodeDetail.swift | 14 +++-- .../Config/Module/CannedMessagesConfig.swift | 17 ------ 10 files changed, 45 insertions(+), 97 deletions(-) diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index e2c2b813..962893b8 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -959,6 +959,7 @@ ENABLE_PREVIEWS = YES; INFOPLIST_FILE = Meshtastic/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = Meshtastic; + INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities"; IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -991,6 +992,7 @@ ENABLE_PREVIEWS = YES; INFOPLIST_FILE = Meshtastic/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = Meshtastic; + INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities"; IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 238eb167..52ff7951 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -505,9 +505,9 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph if myInfo != nil { connectedPeripheral.num = myInfo!.myNodeNum - connectedPeripheral.firmwareVersion = myInfo!.firmwareVersion ?? "Unknown" - connectedPeripheral.name = myInfo!.bleName ?? "Unknown" - connectedPeripheral.longName = myInfo!.bleName ?? "Unknown" + connectedPeripheral.firmwareVersion = myInfo?.firmwareVersion ?? "Unknown" + connectedPeripheral.name = myInfo?.bleName ?? "Unknown" + connectedPeripheral.longName = myInfo?.bleName ?? "Unknown" } } } @@ -759,51 +759,33 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return success } - public func sendLocation(destNum: Int64, wantAck: Bool) -> Bool { + public func sendWaypoint(destNum: Int64, name: String, wantAck: Bool) -> Bool { var success = false - let fromNodeNum = connectedPeripheral.num - if fromNodeNum <= 0 || (LocationHelper.currentLocation.latitude == LocationHelper.DefaultLocation.latitude && LocationHelper.currentLocation.longitude == LocationHelper.DefaultLocation.longitude) { - return false } - var positionPacket = Position() - positionPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) - positionPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) - positionPacket.satsInView = UInt32(LocationHelper.satsInView) - positionPacket.altitude = Int32(LocationHelper.currentAltitude) - positionPacket.timestamp = UInt32(LocationHelper.currentTimestamp.timeIntervalSince1970) - positionPacket.groundSpeed = UInt32(LocationHelper.currentSpeed) - if LocationHelper.currentHeading > 0 { - positionPacket.groundTrack = UInt32(LocationHelper.currentHeading) - } - - //var waypointPacket = Waypoint() - //waypointPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) - //waypointPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) - - //let oneWeekFromNow = Calendar.current.date(byAdding: .day, value: 7, to: Date()) - //waypointPacket.expire = UInt32(oneWeekFromNow!.timeIntervalSince1970) - //waypointPacket.name = "Test Waypoint" + var waypointPacket = Waypoint() + waypointPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) + waypointPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) + let oneWeekFromNow = Calendar.current.date(byAdding: .day, value: 7, to: Date()) + waypointPacket.expire = UInt32(oneWeekFromNow!.timeIntervalSince1970) + waypointPacket.name = name var meshPacket = MeshPacket() meshPacket.to = UInt32(destNum) meshPacket.from = 0 // Send 0 as from from phone to device to avoid warning about client trying to set node num meshPacket.wantAck = true//wantAck - var dataMessage = DataMessage() - dataMessage.payload = try! positionPacket.serializedData() - dataMessage.portnum = PortNum.positionApp - + dataMessage.payload = try! waypointPacket.serializedData() + dataMessage.portnum = PortNum.waypointApp meshPacket.decoded = dataMessage - var toRadio: ToRadio! toRadio = ToRadio() toRadio.packet = meshPacket let binaryData: Data = try! toRadio.serializedData() - MeshLogger.log("📍 Sent a Location Packet from the Apple device GPS to node: \(fromNodeNum)") + MeshLogger.log("📍 Sent a Waypoint Packet from the Apple device GPS to node: \(fromNodeNum)") if connectedPeripheral!.peripheral.state == CBPeripheralState.connected { connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) @@ -815,14 +797,10 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph public func sendPosition(destNum: Int64, wantAck: Bool) -> Bool { var success = false - let fromNodeNum = connectedPeripheral.num - if fromNodeNum <= 0 || (LocationHelper.currentLocation.latitude == LocationHelper.DefaultLocation.latitude && LocationHelper.currentLocation.longitude == LocationHelper.DefaultLocation.longitude) { - return false } - var positionPacket = Position() positionPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) positionPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) @@ -830,23 +808,19 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph positionPacket.timestamp = UInt32(LocationHelper.currentTimestamp.timeIntervalSince1970) positionPacket.altitude = Int32(LocationHelper.currentAltitude) positionPacket.satsInView = UInt32(LocationHelper.satsInView) - // Get Errors without some speed if LocationHelper.currentSpeed >= 5 { positionPacket.groundSpeed = UInt32(LocationHelper.currentSpeed) positionPacket.groundTrack = UInt32(LocationHelper.currentHeading) } - var meshPacket = MeshPacket() meshPacket.to = UInt32(destNum) meshPacket.from = 0 // Send 0 as from from phone to device to avoid warning about client trying to set node num meshPacket.wantAck = wantAck - var dataMessage = DataMessage() dataMessage.payload = try! positionPacket.serializedData() dataMessage.portnum = PortNum.positionApp - meshPacket.decoded = dataMessage var toRadio: ToRadio! @@ -854,15 +828,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph toRadio.packet = meshPacket let binaryData: Data = try! toRadio.serializedData() - if connectedPeripheral!.peripheral.state == CBPeripheralState.connected { - connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) success = true - MeshLogger.log("📍 Sent a Share Location Position Packet from the Apple device GPS to node: \(fromNodeNum)") - + MeshLogger.log("📍 Sent a Position Packet from the Apple device GPS to node: \(fromNodeNum)") } - return success } diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index cd1a4aea..03c41d4b 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -224,51 +224,37 @@ func localConfig (config: Config, context:NSManagedObjectContext, nodeNum: Int64 if config.payloadVariant == Config.OneOf_PayloadVariant.network(config.network) { MeshLogger.log("📶 Network config received \(String(nodeNum))") - print(try! config.network.jsonString()) - let fetchNodeInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(nodeNum)) do { let fetchedNode = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity] - // Found a node, save WiFi Config if !fetchedNode.isEmpty { - if fetchedNode[0].networkConfig == nil { - let newNetworkConfig = NetworkConfigEntity(context: context) - newNetworkConfig.wifiSsid = config.network.wifiSsid newNetworkConfig.wifiPsk = config.network.wifiPsk fetchedNode[0].networkConfig = newNetworkConfig - } else { - fetchedNode[0].networkConfig?.wifiSsid = config.network.wifiSsid fetchedNode[0].networkConfig?.wifiPsk = config.network.wifiPsk } do { - try context.save() - MeshLogger.log("💾 Updated WiFi Config for node number: \(String(nodeNum))") + MeshLogger.log("💾 Updated Network Config for node number: \(String(nodeNum))") } catch { - context.rollback() - let nsError = error as NSError print("💥 Error Updating Core Data WiFiConfigEntity: \(nsError)") } } else { - - print("💥 No Nodes found in local database matching node number \(nodeNum) unable to save WiFi Config") + print("💥 No Nodes found in local database matching node number \(nodeNum) unable to save Network Config") } - } catch { - let nsError = error as NSError print("💥 Fetching node for core data WiFiConfigEntity failed: \(nsError)") } @@ -1047,7 +1033,18 @@ func nodeInfoAppPacket (packet: MeshPacket, context: NSManagedObjectContext) { } func adminAppPacket (packet: MeshPacket, context: NSManagedObjectContext) { - print(try! packet.decoded.jsonString()) + + //print(packet.payloadVariant.debugDescription) + + if let messages = try? CannedMessageModuleConfig(serializedData: packet.decoded.payload) { + //let adminMessageId = bleManager.saveCannedMessageModuleMessages(messages: messages, fromUser: node!.user!, toUser: node!.user!, wantResponse: true) + //if adminMessageId > 0 { + + //} + //print(messages) + } else { + //print(try! packet.decoded.jsonString()) + } } @@ -1103,20 +1100,15 @@ func positionPacket (packet: MeshPacket, context: NSManagedObjectContext) { print("💥 Error Saving NodeInfoEntity from POSITION_APP \(nsError)") } } - } else { - print("💥 Empty POSITION_APP Packet") print(try! packet.jsonString()) if let dataMessage = try? DataMessage(serializedData: packet.decoded.payload) { - print(dataMessage) - + //print(dataMessage) } } } - } catch { - print("💥 Error Fetching NodeInfoEntity for POSITION_APP") } } diff --git a/Meshtastic/Info.plist b/Meshtastic/Info.plist index 126bae5b..55f1bdb3 100644 --- a/Meshtastic/Info.plist +++ b/Meshtastic/Info.plist @@ -75,6 +75,8 @@ armv7 + UIStatusBarStyle + UISupportedInterfaceOrientations UIInterfaceOrientationPortrait diff --git a/Meshtastic/Persistence/Persistence.swift b/Meshtastic/Persistence/Persistence.swift index fad13db3..325e4d45 100644 --- a/Meshtastic/Persistence/Persistence.swift +++ b/Meshtastic/Persistence/Persistence.swift @@ -78,8 +78,10 @@ extension NSManagedObjectContext { /// - Throws: An error if anything went wrong executing the batch deletion. public func executeAndMergeChanges(using batchDeleteRequest: NSBatchDeleteRequest) throws { batchDeleteRequest.resultType = .resultTypeObjectIDs + let result = try execute(batchDeleteRequest) as? NSBatchDeleteResult let changes: [AnyHashable: Any] = [NSDeletedObjectsKey: result?.result as? [NSManagedObjectID] ?? []] + NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [self]) } } diff --git a/Meshtastic/Views/Helpers/BatteryGauge.swift b/Meshtastic/Views/Helpers/BatteryGauge.swift index 6a996bd9..503d7c68 100644 --- a/Meshtastic/Views/Helpers/BatteryGauge.swift +++ b/Meshtastic/Views/Helpers/BatteryGauge.swift @@ -6,9 +6,7 @@ // import SwiftUI -#if canImport(Charts) import Charts -#endif struct BatteryGauge: View { @@ -19,8 +17,6 @@ struct BatteryGauge: View { var body: some View { VStack { - - #if !targetEnvironment(macCatalyst) if batteryLevel == 0.0 { // Plugged in Image(systemName: "powerplug") @@ -50,7 +46,6 @@ struct BatteryGauge: View { .tint(gradient) .gaugeStyle(.accessoryCircular) } - #endif } } } diff --git a/Meshtastic/Views/Messages/ChannelMessageList.swift b/Meshtastic/Views/Messages/ChannelMessageList.swift index 48ea6eaa..01860ea5 100644 --- a/Meshtastic/Views/Messages/ChannelMessageList.swift +++ b/Meshtastic/Views/Messages/ChannelMessageList.swift @@ -297,7 +297,7 @@ struct ChannelMessageList: View { focusedField = nil replyMessageId = 0 if sendPositionWithMessage { - if bleManager.sendLocation(destNum: Int64(channel.index), wantAck: true) { + if bleManager.sendPosition(destNum: Int64(channel.index), wantAck: true) { print("Location Sent") } } diff --git a/Meshtastic/Views/Messages/UserMessageList.swift b/Meshtastic/Views/Messages/UserMessageList.swift index 0cc30403..6ebea97b 100644 --- a/Meshtastic/Views/Messages/UserMessageList.swift +++ b/Meshtastic/Views/Messages/UserMessageList.swift @@ -300,7 +300,7 @@ struct UserMessageList: View { focusedField = nil replyMessageId = 0 if sendPositionWithMessage { - if bleManager.sendLocation(destNum: user.num, wantAck: true) { + if bleManager.sendPosition(destNum: user.num, wantAck: true) { print("Location Sent") } } diff --git a/Meshtastic/Views/Nodes/NodeDetail.swift b/Meshtastic/Views/Nodes/NodeDetail.swift index 6016d931..6ab47349 100644 --- a/Meshtastic/Views/Nodes/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/NodeDetail.swift @@ -56,7 +56,7 @@ struct NodeDetail: View { ) } .ignoresSafeArea(.all, edges: [.leading, .trailing]) - .frame(idealWidth: bounds.size.width, minHeight: bounds.size.height / 1.70) + .frame(idealWidth: bounds.size.width, minHeight: bounds.size.height / 2) } Text(mostRecent.satsInView > 0 ? "Sats: \(mostRecent.satsInView)" : " ") @@ -84,12 +84,12 @@ struct NodeDetail: View { Image(hwModelString) .resizable() .aspectRatio(contentMode: .fill) - .frame(width: 75, height: 75) + .frame(width: 100, height: 100) .cornerRadius(5) Text(String(hwModelString)) .foregroundColor(.gray) - .font(.title).fixedSize() + .font(.largeTitle).fixedSize() } } @@ -140,7 +140,9 @@ struct NodeDetail: View { .symbolRenderingMode(.hierarchical) Text("User Id:").font(.title) } - Text(node.user?.userId ?? "??????").font(.title).foregroundColor(.gray) + //Text(node.user?.userId ?? "??????").font(.title).foregroundColor(.gray) + Text("!\(String(format:"%02x", node.num))") + .font(.title).foregroundColor(.gray) } Divider() VStack { @@ -178,7 +180,7 @@ struct NodeDetail: View { } DateTimeText(dateTime: node.lastHeard) - .font(.title) + .font(.title3) .foregroundColor(.gray) } } @@ -202,7 +204,7 @@ struct NodeDetail: View { Image(node.user!.hwModel ?? "UNSET") .resizable() - .frame(width: 50, height: 50) + .frame(width: 75, height: 75) .cornerRadius(5) Text(String(node.user!.hwModel ?? "UNSET")) diff --git a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift index 27b661b0..65148707 100644 --- a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift @@ -313,69 +313,52 @@ struct CannedMessagesConfig: View { hasChanges = true } .onChange(of: enabled) { newEnabled in - if node != nil && node!.cannedMessageConfig != nil { - if newEnabled != node!.cannedMessageConfig!.enabled { hasChanges = true } } } .onChange(of: sendBell) { newBell in - if node != nil && node!.cannedMessageConfig != nil { - if newBell != node!.cannedMessageConfig!.sendBell { hasChanges = true } } } .onChange(of: rotary1Enabled) { newRot1 in - if node != nil && node!.cannedMessageConfig != nil { - if newRot1 != node!.cannedMessageConfig!.rotary1Enabled { hasChanges = true } } } .onChange(of: updown1Enabled) { newUpDown in - if node != nil && node!.cannedMessageConfig != nil { - if newUpDown != node!.cannedMessageConfig!.updown1Enabled { hasChanges = true } } } .onChange(of: inputbrokerPinA) { newPinA in - if node != nil && node!.cannedMessageConfig != nil { - if newPinA != node!.cannedMessageConfig!.inputbrokerPinA { hasChanges = true } } } .onChange(of: inputbrokerPinB) { newPinB in if node != nil && node!.cannedMessageConfig != nil { - if newPinB != node!.cannedMessageConfig!.inputbrokerPinB { hasChanges = true } } } .onChange(of: inputbrokerPinPress) { newPinPress in if node != nil && node!.cannedMessageConfig != nil { - if newPinPress != node!.cannedMessageConfig!.inputbrokerPinPress { hasChanges = true } } } .onChange(of: inputbrokerEventCw) { newKeyA in if node != nil && node!.cannedMessageConfig != nil { - if newKeyA != node!.cannedMessageConfig!.inputbrokerEventCw { hasChanges = true } } } .onChange(of: inputbrokerEventCcw) { newKeyB in - if node != nil && node!.cannedMessageConfig != nil { - if newKeyB != node!.cannedMessageConfig!.inputbrokerEventCcw { hasChanges = true } } } .onChange(of: inputbrokerEventPress) { newKeyPress in - if node != nil && node!.cannedMessageConfig != nil { - if newKeyPress != node!.cannedMessageConfig!.inputbrokerEventPress { hasChanges = true } } }