From af90966900d65e5d30cbe95fb8fb42e19514ce40 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Mon, 6 Feb 2023 18:45:03 -0800 Subject: [PATCH] Fix getNodeInfo method that was the source of nil nodes --- Meshtastic/Persistence/QueryCoreData.swift | 6 +- .../Views/Map/Custom/MapViewSwiftUI.swift | 9 ++- Meshtastic/Views/Nodes/NodeDetail.swift | 12 ++-- Meshtastic/Views/Nodes/NodeMap.swift | 5 +- .../Settings/Config/BluetoothConfig.swift | 26 ++++---- .../Views/Settings/Config/DeviceConfig.swift | 34 ++++++----- .../Views/Settings/Config/DisplayConfig.swift | 36 +++++------ .../Views/Settings/Config/LoRaConfig.swift | 30 +++++----- .../Config/Module/CannedMessagesConfig.swift | 4 +- .../Module/ExternalNotificationConfig.swift | 52 ++++++++-------- .../Settings/Config/Module/MQTTConfig.swift | 38 ++++++------ .../Config/Module/RangeTestConfig.swift | 26 ++++---- .../Settings/Config/Module/SerialConfig.swift | 38 ++++++------ .../Config/Module/TelemetryConfig.swift | 36 +++++------ .../Views/Settings/Config/NetworkConfig.swift | 32 +++++----- .../Settings/Config/PositionConfig.swift | 59 ++++++++++--------- Meshtastic/Views/Settings/Settings.swift | 2 +- Meshtastic/Views/Settings/UserConfig.swift | 16 ++--- 18 files changed, 245 insertions(+), 216 deletions(-) diff --git a/Meshtastic/Persistence/QueryCoreData.swift b/Meshtastic/Persistence/QueryCoreData.swift index cb30f04f..a026ad8d 100644 --- a/Meshtastic/Persistence/QueryCoreData.swift +++ b/Meshtastic/Persistence/QueryCoreData.swift @@ -7,7 +7,7 @@ import CoreData -public func getNodeInfo(id: Int64, context: NSManagedObjectContext) -> NodeInfoEntity { +public func getNodeInfo(id: Int64, context: NSManagedObjectContext) -> NodeInfoEntity? { let fetchNodeInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(id)) @@ -18,9 +18,9 @@ public func getNodeInfo(id: Int64, context: NSManagedObjectContext) -> NodeInfoE return fetchNodeInfo[0] } } catch { - return NodeInfoEntity(context: context) + return nil } - return NodeInfoEntity(context: context) + return nil } public func getUser(id: Int64, context: NSManagedObjectContext) -> UserEntity { diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index d57b4cf2..fd68f7fe 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -79,9 +79,12 @@ struct MapViewSwiftUI: UIViewRepresentable { self.loadedLastUpdatedLocalMapFile = self.lastUpdatedLocalMapFile } } - mapView.removeAnnotations(mapView.annotations) - mapView.addAnnotations(positions) - mapView.addAnnotations(waypoints) + + DispatchQueue.main.async { + mapView.removeAnnotations(mapView.annotations) + mapView.addAnnotations(positions) + mapView.addAnnotations(waypoints) + } } func makeCoordinator() -> MapCoordinator { diff --git a/Meshtastic/Views/Nodes/NodeDetail.swift b/Meshtastic/Views/Nodes/NodeDetail.swift index 4d33d11e..51e34164 100644 --- a/Meshtastic/Views/Nodes/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/NodeDetail.swift @@ -33,7 +33,7 @@ struct NodeDetail: View { @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "name", ascending: false)], predicate: NSPredicate( format: "expire == nil || expire >= %@", Date() as NSDate - ), animation: .easeIn) + ), animation: .none) private var waypoints: FetchedResults /// The current weather condition for the city. @@ -52,7 +52,7 @@ struct NodeDetail: View { GeometryReader { bounds in VStack { if node.positions?.count ?? 0 > 0 { - let mostRecent = node.positions?.lastObject as! PositionEntity + // let mostRecent = node.positions?.lastObject as! PositionEntity ZStack { let annotations = node.positions?.array as! [PositionEntity] ZStack { @@ -373,7 +373,7 @@ struct NodeDetail: View { isPresented: $showingShutdownConfirm ) { Button("Shutdown Node?", role: .destructive) { - if !bleManager.sendShutdown(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo!.adminIndex) { + if !bleManager.sendShutdown(fromUser: connectedNode!.user!, toUser: node.user!, adminIndex: connectedNode!.myInfo!.adminIndex) { print("Shutdown Failed") } } @@ -393,7 +393,7 @@ struct NodeDetail: View { isPresented: $showingRebootConfirm ) { Button("reboot.node", role: .destructive) { - if !bleManager.sendReboot(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo!.adminIndex) { + if !bleManager.sendReboot(fromUser: connectedNode!.user!, toUser: node.user!, adminIndex: connectedNode!.myInfo!.adminIndex) { print("Reboot Failed") } } @@ -440,9 +440,9 @@ struct NodeDetail: View { if bleManager.connectedPeripheral != nil { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.user != nil && node.user != nil && connectedNode.myInfo != nil { + if connectedNode != nil && connectedNode!.user != nil && node.user != nil && connectedNode!.myInfo != nil { - let adminMessageId = bleManager.requestDeviceMetadata(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo!.adminIndex, context: context) + let adminMessageId = bleManager.requestDeviceMetadata(fromUser: connectedNode!.user!, toUser: node.user!, adminIndex: connectedNode!.myInfo!.adminIndex, context: context) } } diff --git a/Meshtastic/Views/Nodes/NodeMap.swift b/Meshtastic/Views/Nodes/NodeMap.swift index f1f095a2..079b9b9e 100644 --- a/Meshtastic/Views/Nodes/NodeMap.swift +++ b/Meshtastic/Views/Nodes/NodeMap.swift @@ -29,14 +29,15 @@ struct NodeMap: View { } } } + //&& nodePosition != nil @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "time", ascending: false)], - predicate: NSPredicate(format: "time >= %@", Calendar.current.startOfDay(for: Date()) as NSDate), animation: .easeIn) + predicate: NSPredicate(format: "time >= %@", Calendar.current.startOfDay(for: Date()) as NSDate), animation: .none) private var positions: FetchedResults @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "name", ascending: false)], predicate: NSPredicate( format: "expire == nil || expire >= %@", Date() as NSDate - ), animation: .easeIn) + ), animation: .none) private var waypoints: FetchedResults @State private var mapType: MKMapType? diff --git a/Meshtastic/Views/Settings/Config/BluetoothConfig.swift b/Meshtastic/Views/Settings/Config/BluetoothConfig.swift index 33d01896..854d53c1 100644 --- a/Meshtastic/Views/Settings/Config/BluetoothConfig.swift +++ b/Meshtastic/Views/Settings/Config/BluetoothConfig.swift @@ -100,16 +100,18 @@ struct BluetoothConfig: View { let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) Button(buttonText) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var bc = Config.BluetoothConfig() - bc.enabled = enabled - bc.mode = BluetoothModes(rawValue: mode)?.protoEnumValue() ?? Config.BluetoothConfig.PairingMode.randomPin - bc.fixedPin = UInt32(fixedPin) ?? 123456 - let adminMessageId = bleManager.saveBluetoothConfig(config: bc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + var bc = Config.BluetoothConfig() + bc.enabled = enabled + bc.mode = BluetoothModes(rawValue: mode)?.protoEnumValue() ?? Config.BluetoothConfig.PairingMode.randomPin + bc.fixedPin = UInt32(fixedPin) ?? 123456 + let adminMessageId = bleManager.saveBluetoothConfig(config: bc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } message: { @@ -131,8 +133,8 @@ struct BluetoothConfig: View { if bleManager.connectedPeripheral != nil && node?.bluetoothConfig == nil { print("empty bluetooth config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestBluetoothConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestBluetoothConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/DeviceConfig.swift b/Meshtastic/Views/Settings/Config/DeviceConfig.swift index 5786dce7..b979464d 100644 --- a/Meshtastic/Views/Settings/Config/DeviceConfig.swift +++ b/Meshtastic/Views/Settings/Config/DeviceConfig.swift @@ -163,21 +163,23 @@ struct DeviceConfig: View { let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) Button(buttonText) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var dc = Config.DeviceConfig() - dc.role = DeviceRoles(rawValue: deviceRole)!.protoEnumValue() - dc.serialEnabled = serialEnabled - dc.debugLogEnabled = debugLogEnabled - dc.buttonGpio = UInt32(buttonGPIO) - dc.buzzerGpio = UInt32(buzzerGPIO) - - let adminMessageId = bleManager.saveDeviceConfig(config: dc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + var dc = Config.DeviceConfig() + dc.role = DeviceRoles(rawValue: deviceRole)!.protoEnumValue() + dc.serialEnabled = serialEnabled + dc.debugLogEnabled = debugLogEnabled + dc.buttonGpio = UInt32(buttonGPIO) + dc.buzzerGpio = UInt32(buzzerGPIO) + + let adminMessageId = bleManager.saveDeviceConfig(config: dc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } - } + } } message: { Text("config.save.confirm") @@ -203,8 +205,8 @@ struct DeviceConfig: View { if bleManager.connectedPeripheral != nil && node?.deviceConfig == nil { print("empty device config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestDeviceConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestDeviceConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/DisplayConfig.swift b/Meshtastic/Views/Settings/Config/DisplayConfig.swift index fc2e2836..f9da9c3e 100644 --- a/Meshtastic/Views/Settings/Config/DisplayConfig.swift +++ b/Meshtastic/Views/Settings/Config/DisplayConfig.swift @@ -120,22 +120,24 @@ struct DisplayConfig: View { let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) Button(buttonText) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var dc = Config.DisplayConfig() - dc.gpsFormat = GpsFormats(rawValue: gpsFormat)!.protoEnumValue() - dc.screenOnSecs = UInt32(screenOnSeconds) - dc.autoScreenCarouselSecs = UInt32(screenCarouselInterval) - dc.compassNorthTop = compassNorthTop - dc.flipScreen = flipScreen - dc.oled = OledTypes(rawValue: oledType)!.protoEnumValue() - dc.displaymode = DisplayModes(rawValue: displayMode)!.protoEnumValue() - - let adminMessageId = bleManager.saveDisplayConfig(config: dc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { + if connectedNode != nil { + var dc = Config.DisplayConfig() + dc.gpsFormat = GpsFormats(rawValue: gpsFormat)!.protoEnumValue() + dc.screenOnSecs = UInt32(screenOnSeconds) + dc.autoScreenCarouselSecs = UInt32(screenCarouselInterval) + dc.compassNorthTop = compassNorthTop + dc.flipScreen = flipScreen + dc.oled = OledTypes(rawValue: oledType)!.protoEnumValue() + dc.displaymode = DisplayModes(rawValue: displayMode)!.protoEnumValue() - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + let adminMessageId = bleManager.saveDisplayConfig(config: dc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } @@ -162,8 +164,8 @@ struct DisplayConfig: View { if bleManager.connectedPeripheral != nil && node?.displayConfig == nil { print("empty display config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestDisplayConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestDisplayConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/LoRaConfig.swift b/Meshtastic/Views/Settings/Config/LoRaConfig.swift index 40ef0ea0..cbc0f286 100644 --- a/Meshtastic/Views/Settings/Config/LoRaConfig.swift +++ b/Meshtastic/Views/Settings/Config/LoRaConfig.swift @@ -81,18 +81,20 @@ struct LoRaConfig: View { let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) Button(buttonText) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var lc = Config.LoRaConfig() - lc.hopLimit = UInt32(hopLimit) - lc.region = RegionCodes(rawValue: region)!.protoEnumValue() - lc.modemPreset = ModemPresets(rawValue: modemPreset)!.protoEnumValue() - lc.usePreset = true - lc.txEnabled = true - let adminMessageId = bleManager.saveLoRaConfig(config: lc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + var lc = Config.LoRaConfig() + lc.hopLimit = UInt32(hopLimit) + lc.region = RegionCodes(rawValue: region)!.protoEnumValue() + lc.modemPreset = ModemPresets(rawValue: modemPreset)!.protoEnumValue() + lc.usePreset = true + lc.txEnabled = true + let adminMessageId = bleManager.saveLoRaConfig(config: lc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } message: { @@ -119,8 +121,8 @@ struct LoRaConfig: View { if bleManager.connectedPeripheral != nil && node?.loRaConfig == nil { print("empty lora config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestLoRaConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestLoRaConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift index f2add109..f3cb7af4 100644 --- a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift @@ -292,8 +292,8 @@ struct CannedMessagesConfig: View { if bleManager.connectedPeripheral != nil && node?.cannedMessageConfig == nil { print("empty canned messages module config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestCannedMessagesModuleConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestCannedMessagesModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift b/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift index 76d2b738..f7255fdd 100644 --- a/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift @@ -155,29 +155,31 @@ struct ExternalNotificationConfig: View { titleVisibility: .visible ) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown") - let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) - Button(buttonText) { - var enc = ModuleConfig.ExternalNotificationConfig() - enc.enabled = enabled - enc.alertBell = alertBell - enc.alertBellBuzzer = alertBellBuzzer - enc.alertBellVibra = alertBellVibra - enc.alertMessage = alertMessage - enc.alertMessageBuzzer = alertMessageBuzzer - enc.alertMessageVibra = alertMessageVibra - enc.active = active - enc.output = UInt32(output) - enc.outputBuzzer = UInt32(outputBuzzer) - enc.outputVibra = UInt32(outputVibra) - enc.outputMs = UInt32(outputMilliseconds) - enc.usePwm = usePWM - let adminMessageId = bleManager.saveExternalNotificationModuleConfig(config: enc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) - if adminMessageId > 0{ - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown") + let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) + Button(buttonText) { + var enc = ModuleConfig.ExternalNotificationConfig() + enc.enabled = enabled + enc.alertBell = alertBell + enc.alertBellBuzzer = alertBellBuzzer + enc.alertBellVibra = alertBellVibra + enc.alertMessage = alertMessage + enc.alertMessageBuzzer = alertMessageBuzzer + enc.alertMessageVibra = alertMessageVibra + enc.active = active + enc.output = UInt32(output) + enc.outputBuzzer = UInt32(outputBuzzer) + enc.outputVibra = UInt32(outputVibra) + enc.outputMs = UInt32(outputMilliseconds) + enc.usePwm = usePWM + let adminMessageId = bleManager.saveExternalNotificationModuleConfig(config: enc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0{ + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } @@ -211,8 +213,8 @@ struct ExternalNotificationConfig: View { if bleManager.connectedPeripheral != nil && node?.externalNotificationConfig == nil { print("empty external notification module config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestExternalNotificationModuleConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestExternalNotificationModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift b/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift index e3855377..ec2d9735 100644 --- a/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift @@ -144,22 +144,24 @@ struct MQTTConfig: View { titleVisibility: .visible ) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown") - let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) - Button(buttonText) { - var mqtt = ModuleConfig.MQTTConfig() - mqtt.enabled = self.enabled - mqtt.address = self.address - mqtt.username = self.username - mqtt.password = self.password - mqtt.encryptionEnabled = self.encryptionEnabled - mqtt.jsonEnabled = self.jsonEnabled - let adminMessageId = bleManager.saveMQTTConfig(config: mqtt, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown") + let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) + Button(buttonText) { + var mqtt = ModuleConfig.MQTTConfig() + mqtt.enabled = self.enabled + mqtt.address = self.address + mqtt.username = self.username + mqtt.password = self.password + mqtt.encryptionEnabled = self.encryptionEnabled + mqtt.jsonEnabled = self.jsonEnabled + let adminMessageId = bleManager.saveMQTTConfig(config: mqtt, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } @@ -185,8 +187,8 @@ struct MQTTConfig: View { if bleManager.connectedPeripheral != nil && node?.telemetryConfig == nil { print("empty mqtt module config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestMqttModuleConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestMqttModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift b/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift index a66d926e..e3e9ef94 100644 --- a/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift @@ -67,16 +67,18 @@ struct RangeTestConfig: View { Button(buttonText) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var rtc = ModuleConfig.RangeTestConfig() - rtc.enabled = enabled - rtc.save = save - rtc.sender = UInt32(sender) - let adminMessageId = bleManager.saveRangeTestModuleConfig(config: rtc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + var rtc = ModuleConfig.RangeTestConfig() + rtc.enabled = enabled + rtc.save = save + rtc.sender = UInt32(sender) + let adminMessageId = bleManager.saveRangeTestModuleConfig(config: rtc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } @@ -99,8 +101,8 @@ struct RangeTestConfig: View { if bleManager.connectedPeripheral != nil && node?.rangeTestConfig == nil { print("empty range test module config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestRangeTestModuleConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestRangeTestModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift b/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift index 4678ca60..2eb1d9e8 100644 --- a/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift @@ -122,22 +122,24 @@ struct SerialConfig: View { let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) Button(buttonText) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var sc = ModuleConfig.SerialConfig() - sc.enabled = enabled - sc.echo = echo - sc.rxd = UInt32(rxd) - sc.txd = UInt32(txd) - sc.baud = SerialBaudRates(rawValue: baudRate)!.protoEnumValue() - sc.timeout = UInt32(timeout) - sc.mode = SerialModeTypes(rawValue: mode)!.protoEnumValue() - - let adminMessageId = bleManager.saveSerialModuleConfig(config: sc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) - - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + var sc = ModuleConfig.SerialConfig() + sc.enabled = enabled + sc.echo = echo + sc.rxd = UInt32(rxd) + sc.txd = UInt32(txd) + sc.baud = SerialBaudRates(rawValue: baudRate)!.protoEnumValue() + sc.timeout = UInt32(timeout) + sc.mode = SerialModeTypes(rawValue: mode)!.protoEnumValue() + + let adminMessageId = bleManager.saveSerialModuleConfig(config: sc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) + + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } @@ -166,8 +168,8 @@ struct SerialConfig: View { if bleManager.connectedPeripheral != nil && node?.serialConfig == nil { print("empty serial module config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestSerialModuleConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestSerialModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } diff --git a/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift b/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift index acd96ad0..fad65cd7 100644 --- a/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift @@ -78,21 +78,23 @@ struct TelemetryConfig: View { titleVisibility: .visible ) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown") - let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) - Button(buttonText) { - var tc = ModuleConfig.TelemetryConfig() - tc.deviceUpdateInterval = UInt32(deviceUpdateInterval) - tc.environmentUpdateInterval = UInt32(environmentUpdateInterval) - tc.environmentMeasurementEnabled = environmentMeasurementEnabled - tc.environmentScreenEnabled = environmentScreenEnabled - tc.environmentDisplayFahrenheit = environmentDisplayFahrenheit - let adminMessageId = bleManager.saveTelemetryModuleConfig(config: tc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown") + let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) + Button(buttonText) { + var tc = ModuleConfig.TelemetryConfig() + tc.deviceUpdateInterval = UInt32(deviceUpdateInterval) + tc.environmentUpdateInterval = UInt32(environmentUpdateInterval) + tc.environmentMeasurementEnabled = environmentMeasurementEnabled + tc.environmentScreenEnabled = environmentScreenEnabled + tc.environmentDisplayFahrenheit = environmentDisplayFahrenheit + let adminMessageId = bleManager.saveTelemetryModuleConfig(config: tc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } @@ -117,8 +119,8 @@ struct TelemetryConfig: View { if bleManager.connectedPeripheral != nil && node?.telemetryConfig == nil { print("empty telemetry module config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestTelemetryModuleConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestTelemetryModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/NetworkConfig.swift b/Meshtastic/Views/Settings/Config/NetworkConfig.swift index db670c27..33744b64 100644 --- a/Meshtastic/Views/Settings/Config/NetworkConfig.swift +++ b/Meshtastic/Views/Settings/Config/NetworkConfig.swift @@ -111,19 +111,21 @@ struct NetworkConfig: View { let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) Button(buttonText) { let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var network = Config.NetworkConfig() - network.wifiEnabled = self.wifiEnabled - network.wifiSsid = self.wifiSsid - network.wifiPsk = self.wifiPsk - network.ethEnabled = self.ethEnabled - //network.addressMode = Config.NetworkConfig.AddressMode.dhcp - - let adminMessageId = bleManager.saveNetworkConfig(config: network, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + if connectedNode != nil { + var network = Config.NetworkConfig() + network.wifiEnabled = self.wifiEnabled + network.wifiSsid = self.wifiSsid + network.wifiPsk = self.wifiPsk + network.ethEnabled = self.ethEnabled + //network.addressMode = Config.NetworkConfig.AddressMode.dhcp + + let adminMessageId = bleManager.saveNetworkConfig(config: network, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } message: { @@ -148,8 +150,8 @@ struct NetworkConfig: View { if bleManager.connectedPeripheral != nil && node?.positionConfig == nil { print("empty network config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestNetworkConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestNetworkConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Config/PositionConfig.swift b/Meshtastic/Views/Settings/Config/PositionConfig.swift index 7c7af9ea..bec9b268 100644 --- a/Meshtastic/Views/Settings/Config/PositionConfig.swift +++ b/Meshtastic/Views/Settings/Config/PositionConfig.swift @@ -207,34 +207,37 @@ struct PositionConfig: View { Button(buttonText) { if fixedPosition { - _ = bleManager.sendPosition(destNum: bleManager.connectedPeripheral.num, wantResponse: false) + _ = bleManager.sendPosition(destNum: node!.num, wantResponse: true) } let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var pc = Config.PositionConfig() - pc.positionBroadcastSmartEnabled = smartPositionEnabled - pc.gpsEnabled = deviceGpsEnabled - pc.fixedPosition = fixedPosition - pc.gpsUpdateInterval = UInt32(gpsUpdateInterval) - pc.gpsAttemptTime = UInt32(gpsAttemptTime) - pc.positionBroadcastSecs = UInt32(positionBroadcastSeconds) - var pf : PositionFlags = [] - if includeAltitude { pf.insert(.Altitude) } - if includeAltitudeMsl { pf.insert(.AltitudeMsl) } - if includeGeoidalSeparation { pf.insert(.GeoidalSeparation) } - if includeDop { pf.insert(.Dop) } - if includeHvdop { pf.insert(.Hvdop) } - if includeSatsinview { pf.insert(.Satsinview) } - if includeSeqNo { pf.insert(.SeqNo) } - if includeTimestamp { pf.insert(.Timestamp) } - if includeSpeed { pf.insert(.Speed) } - if includeHeading { pf.insert(.Heading) } - pc.positionFlags = UInt32(pf.rawValue) - let adminMessageId = bleManager.savePositionConfig(config: pc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true - // for now just disable the button after a successful save - hasChanges = false - goBack() + + if connectedNode != nil { + var pc = Config.PositionConfig() + pc.positionBroadcastSmartEnabled = smartPositionEnabled + pc.gpsEnabled = deviceGpsEnabled + pc.fixedPosition = fixedPosition + pc.gpsUpdateInterval = UInt32(gpsUpdateInterval) + pc.gpsAttemptTime = UInt32(gpsAttemptTime) + pc.positionBroadcastSecs = UInt32(positionBroadcastSeconds) + var pf : PositionFlags = [] + if includeAltitude { pf.insert(.Altitude) } + if includeAltitudeMsl { pf.insert(.AltitudeMsl) } + if includeGeoidalSeparation { pf.insert(.GeoidalSeparation) } + if includeDop { pf.insert(.Dop) } + if includeHvdop { pf.insert(.Hvdop) } + if includeSatsinview { pf.insert(.Satsinview) } + if includeSeqNo { pf.insert(.SeqNo) } + if includeTimestamp { pf.insert(.Timestamp) } + if includeSpeed { pf.insert(.Speed) } + if includeHeading { pf.insert(.Heading) } + pc.positionFlags = UInt32(pf.rawValue) + let adminMessageId = bleManager.savePositionConfig(config: pc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + // Should show a saved successfully alert once I know that to be true + // for now just disable the button after a successful save + hasChanges = false + goBack() + } } } } @@ -279,8 +282,8 @@ struct PositionConfig: View { if bleManager.connectedPeripheral != nil && node?.positionConfig == nil { print("empty position config") let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - if connectedNode.id > 0 { - _ = bleManager.requestPositionConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) + if connectedNode != nil && connectedNode!.num > 0 { + _ = bleManager.requestPositionConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) } } } diff --git a/Meshtastic/Views/Settings/Settings.swift b/Meshtastic/Views/Settings/Settings.swift index 9d11d10f..a36e79bb 100644 --- a/Meshtastic/Views/Settings/Settings.swift +++ b/Meshtastic/Views/Settings/Settings.swift @@ -57,7 +57,7 @@ struct Settings: View { if node?.myInfo?.adminIndex ?? 0 > 0 { Section("Configure") { Picker("Configuring Node", selection: $selectedNode) { - if connectedNodeNum == 0 { + if selectedNode == 0 { Text("Connect to a Node").tag(0) } ForEach(nodes) { node in diff --git a/Meshtastic/Views/Settings/UserConfig.swift b/Meshtastic/Views/Settings/UserConfig.swift index dd2aecb0..e645c370 100644 --- a/Meshtastic/Views/Settings/UserConfig.swift +++ b/Meshtastic/Views/Settings/UserConfig.swift @@ -96,13 +96,15 @@ struct UserConfig: View { let connectedUser = getUser(id: bleManager.connectedPeripheral.num, context: context) let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) - var u = User() - u.shortName = shortName - u.longName = longName - let adminMessageId = bleManager.saveUser(config: u, fromUser: connectedUser, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0) - if adminMessageId > 0 { - hasChanges = false - goBack() + if connectedNode != nil { + var u = User() + u.shortName = shortName + u.longName = longName + let adminMessageId = bleManager.saveUser(config: u, fromUser: connectedUser, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) + if adminMessageId > 0 { + hasChanges = false + goBack() + } } } } message: {