diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 7564cb3f..14a63a6b 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -754,7 +754,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return success } - public func sendLocation(destNum: Int64) -> Bool { + public func sendLocation(destNum: Int64, wantAck: Bool) -> Bool { var success = false @@ -764,58 +764,45 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return false } + + var waypointPacket = Location() + waypointPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) + waypointPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) - //let fetchNode: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") - //fetchNode.predicate = NSPredicate(format: "num == %lld", fromNodeNum) + let oneWeekFromNow = Calendar.current.date(byAdding: .day, value: 7, to: Date()) + waypointPacket.expire = UInt32(oneWeekFromNow!.timeIntervalSince1970) + waypointPacket.name = "Test Waypoint" + + + 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! waypointPacket.serializedData() + dataMessage.portnum = PortNum.waypointApp + + meshPacket.decoded = dataMessage - //do { - - // let fetchedNode = try context?.fetch(fetchNode) as! [NodeInfoEntity] + var toRadio: ToRadio! + toRadio = ToRadio() + toRadio.packet = meshPacket + let binaryData: Data = try! toRadio.serializedData() + + if meshLoggingEnabled { MeshLogger.log("📍 Sent a Location Packet from the Apple device GPS to node: \(fromNodeNum)") } + + if connectedPeripheral!.peripheral.state == CBPeripheralState.connected { + connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) + success = true - // if fetchedNode.count == 1 { - - var waypointPacket = Location() - waypointPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) - waypointPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) - waypointPacket.expire = UInt32(LocationHelper.currentTimestamp.timeIntervalSince1970) - waypointPacket.name = "Test Waypoint" - - - 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 - - var dataMessage = DataMessage() - 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() - - if meshLoggingEnabled { MeshLogger.log("📍 Sent a Location Packet from the Apple device GPS to node: \(fromNodeNum)") } - - if connectedPeripheral!.peripheral.state == CBPeripheralState.connected { - - connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) - success = true - - } - // } - - //} catch { - // success = false - //} + } return success } - public func sendPosition(destNum: Int64, wantResponse: Bool) -> Bool { + public func sendPosition(destNum: Int64, wantAck: Bool) -> Bool { var success = false @@ -825,58 +812,43 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return false } + + var positionPacket = Position() + positionPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) + positionPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) + positionPacket.time = UInt32(LocationHelper.currentTimestamp.timeIntervalSince1970) + positionPacket.altitude = Int32(LocationHelper.currentAltitude) - let fetchNode: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") - fetchNode.predicate = NSPredicate(format: "num == %lld", fromNodeNum) - - do { - - let fetchedNode = try context?.fetch(fetchNode) as! [NodeInfoEntity] + // 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 - if fetchedNode.count == 1 { - - var positionPacket = Position() - positionPacket.latitudeI = Int32(LocationHelper.currentLocation.latitude * 1e7) - positionPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) - positionPacket.time = UInt32(LocationHelper.currentTimestamp.timeIntervalSince1970) - positionPacket.altitude = Int32(LocationHelper.currentAltitude) - - // 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 = wantResponse - - var dataMessage = DataMessage() - dataMessage.payload = try! positionPacket.serializedData() - dataMessage.portnum = PortNum.positionApp - - meshPacket.decoded = dataMessage - - var toRadio: ToRadio! - toRadio = ToRadio() - toRadio.packet = meshPacket - let binaryData: Data = try! toRadio.serializedData() - - if meshLoggingEnabled { MeshLogger.log("📍 Sent a Position Packet from the Apple device GPS to node: \(fromNodeNum)") } - - if connectedPeripheral!.peripheral.state == CBPeripheralState.connected { - - connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) - success = true - - } - } + var toRadio: ToRadio! + toRadio = ToRadio() + toRadio.packet = meshPacket + let binaryData: Data = try! toRadio.serializedData() + + if meshLoggingEnabled { MeshLogger.log("📍 Sent a Position Packet from the Apple device GPS to node: \(fromNodeNum)") } + + if connectedPeripheral!.peripheral.state == CBPeripheralState.connected { - } catch { - success = false + connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) + success = true + } return success @@ -890,7 +862,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // Send a position out to the mesh if "share location with the mesh" is enabled in settings if userSettings!.provideLocation { - let success = sendPosition(destNum: connectedPeripheral.num, wantResponse: false) + let success = sendPosition(destNum: connectedPeripheral.num, wantAck: false) if !success { print("Failed to send positon to device") @@ -900,7 +872,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } } - public func sendShutdown(destNum: Int64, wantResponse: Bool) -> Bool { + public func sendShutdown(destNum: Int64) -> Bool { var adminPacket = AdminMessage() adminPacket.shutdownSeconds = 10 @@ -910,7 +882,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Bool { + public func sendReboot(destNum: Int64) -> Bool { var adminPacket = AdminMessage() adminPacket.rebootSeconds = 10 @@ -958,7 +930,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Bool { + public func sendFactoryReset(destNum: Int64) -> Bool { var deviceConfig = Config.DeviceConfig() deviceConfig.factoryReset = true @@ -1010,7 +982,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { + public func saveUser(config: User, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() adminPacket.setOwner = config @@ -1057,7 +1029,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { + public func saveDeviceConfig(config: Config.DeviceConfig, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() adminPacket.setConfig.device = config @@ -1086,7 +1058,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { + public func saveDisplayConfig(config: Config.DisplayConfig, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() adminPacket.setConfig.display = config @@ -1115,7 +1087,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { + public func saveLoRaConfig(config: Config.LoRaConfig, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() adminPacket.setConfig.lora = config @@ -1145,7 +1117,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { + public func savePositionConfig(config: Config.PositionConfig, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() adminPacket.setConfig.position = config @@ -1174,7 +1146,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { + public func saveWiFiConfig(config: Config.WiFiConfig, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() adminPacket.setConfig.wifi = config @@ -1203,7 +1175,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(connectedPeripheral.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { + public func saveCannedMessageModuleConfig(config: ModuleConfig.CannedMessageConfig, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() adminPacket.setModuleConfig.cannedMessage = config @@ -1232,7 +1204,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(fromUser.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { var adminPacket = AdminMessage() - adminPacket.setCannedMessageModulePart1 = messages var meshPacket: MeshPacket = MeshPacket() @@ -1261,7 +1232,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph meshPacket.from = 0 //UInt32(fromUser.num) meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. 0 { @@ -153,7 +153,7 @@ struct DeviceConfig: View { ) { Button("Erase all device settings?", role: .destructive) { - if !bleManager.sendFactoryReset(destNum: bleManager.connectedPeripheral.num, wantResponse: true) { + if !bleManager.sendFactoryReset(destNum: bleManager.connectedPeripheral.num) { print("Factory Reset Failed") } diff --git a/Meshtastic/Views/Settings/Config/DisplayConfig.swift b/Meshtastic/Views/Settings/Config/DisplayConfig.swift index 6fcb7c35..13d9ab13 100644 --- a/Meshtastic/Views/Settings/Config/DisplayConfig.swift +++ b/Meshtastic/Views/Settings/Config/DisplayConfig.swift @@ -213,7 +213,7 @@ struct DisplayConfig: View { dc.autoScreenCarouselSecs = UInt32(screenCarouselInterval) dc.compassNorthTop = compassNorthTop - let adminMessageId = bleManager.saveDisplayConfig(config: dc, fromUser: node!.user!, toUser: node!.user!, wantAck: true) + let adminMessageId = bleManager.saveDisplayConfig(config: dc, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0 { diff --git a/Meshtastic/Views/Settings/Config/LoRaConfig.swift b/Meshtastic/Views/Settings/Config/LoRaConfig.swift index d89a0542..9b30f94e 100644 --- a/Meshtastic/Views/Settings/Config/LoRaConfig.swift +++ b/Meshtastic/Views/Settings/Config/LoRaConfig.swift @@ -261,7 +261,7 @@ struct LoRaConfig: View { lc.region = RegionCodes(rawValue: region)!.protoEnumValue() lc.modemPreset = ModemPresets(rawValue: modemPreset)!.protoEnumValue() - let adminMessageId = bleManager.saveLoRaConfig(config: lc, fromUser: node!.user!, toUser: node!.user!, wantAck: true) + let adminMessageId = bleManager.saveLoRaConfig(config: lc, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0 { diff --git a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift index 9534b7dd..eba68209 100644 --- a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift @@ -320,7 +320,7 @@ struct CannedMessagesConfig: View { cmc.inputbrokerEventCcw = InputEventChars(rawValue: inputbrokerEventCcw)!.protoEnumValue() cmc.inputbrokerEventPress = InputEventChars(rawValue: inputbrokerEventPress)!.protoEnumValue() - let adminMessageId = bleManager.saveCannedMessageModuleConfig(config: cmc, fromUser: node!.user!, toUser: node!.user!, wantAck: true) + let adminMessageId = bleManager.saveCannedMessageModuleConfig(config: cmc, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0 { // Should show a saved successfully alert once I know that to be true diff --git a/Meshtastic/Views/Settings/Config/PositionConfig.swift b/Meshtastic/Views/Settings/Config/PositionConfig.swift index 42ca784d..a2348bea 100644 --- a/Meshtastic/Views/Settings/Config/PositionConfig.swift +++ b/Meshtastic/Views/Settings/Config/PositionConfig.swift @@ -323,7 +323,7 @@ struct PositionConfig: View { pc.gpsAttemptTime = UInt32(gpsAttemptTime) pc.positionBroadcastSecs = UInt32(positionBroadcastSeconds) - let adminMessageId = bleManager.savePositionConfig(config: pc, fromUser: node!.user!, toUser: node!.user!, wantAck: true) + let adminMessageId = bleManager.savePositionConfig(config: pc, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0{ diff --git a/Meshtastic/Views/Settings/Config/WiFiConfig.swift b/Meshtastic/Views/Settings/Config/WiFiConfig.swift index e7cfb5eb..e322a72a 100644 --- a/Meshtastic/Views/Settings/Config/WiFiConfig.swift +++ b/Meshtastic/Views/Settings/Config/WiFiConfig.swift @@ -167,7 +167,7 @@ struct WiFiConfig: View { wifi.psk = self.password wifi.mode = WiFiModes(rawValue: self.mode)?.protoEnumValue() ?? WiFiModes.client.protoEnumValue() - let adminMessageId = bleManager.saveWiFiConfig(config: wifi, fromUser: node!.user!, toUser: node!.user!, wantAck: true) + let adminMessageId = bleManager.saveWiFiConfig(config: wifi, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0 { diff --git a/Meshtastic/Views/Settings/UserConfig.swift b/Meshtastic/Views/Settings/UserConfig.swift index accc224b..3747c71c 100644 --- a/Meshtastic/Views/Settings/UserConfig.swift +++ b/Meshtastic/Views/Settings/UserConfig.swift @@ -113,7 +113,7 @@ struct UserConfig: View { u.shortName = shortName u.longName = longName - let adminMessageId = bleManager.saveUser(config: u, fromUser: node!.user!, toUser: node!.user!, wantAck: true) + let adminMessageId = bleManager.saveUser(config: u, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0 {