Move position timer so it invalidates fewer times, update sendPosition so that it always wants and ack and want response can be adjusted by the use case. Currently only positions sent from a dm will want response.

This commit is contained in:
Garth Vander Houwen 2022-12-21 12:50:27 -08:00
parent 1dad62d123
commit 43f515b7bb
4 changed files with 21 additions and 22 deletions

View file

@ -527,19 +527,6 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject {
MeshLogger.log("💥 Error Deleting the All - Broadcast User")
}
// MARK: Share Location Position Update Timer
// Use context to pass the radio name with the timer
// Use a RunLoop to prevent the timer from running on the main UI thread
if userSettings?.provideLocation ?? false {
if positionTimer != nil {
positionTimer!.invalidate()
}
positionTimer = Timer.scheduledTimer(timeInterval: TimeInterval((userSettings?.provideLocationInterval ?? 900)), target: self, selector: #selector(positionTimerFired), userInfo: context, repeats: true)
if positionTimer != nil {
RunLoop.current.add(positionTimer!, forMode: .common)
}
}
if decodedInfo.configCompleteID != 0 && decodedInfo.configCompleteID == configNonce {
invalidVersion = false
lastConnectionError = ""
@ -548,6 +535,19 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject {
MeshLogger.log("🤜 BLE Config Complete Packet Id: \(decodedInfo.configCompleteID)")
peripherals.removeAll(where: { $0.peripheral.state == CBPeripheralState.disconnected })
// Config conplete returns so we don't read the characteristic again
// MARK: Share Location Position Update Timer
// Use context to pass the radio name with the timer
// Use a RunLoop to prevent the timer from running on the main UI thread
if userSettings?.provideLocation ?? false {
if positionTimer != nil {
positionTimer!.invalidate()
}
positionTimer = Timer.scheduledTimer(timeInterval: TimeInterval((userSettings?.provideLocationInterval ?? 900)), target: self, selector: #selector(positionTimerFired), userInfo: context, repeats: true)
if positionTimer != nil {
RunLoop.current.add(positionTimer!, forMode: .common)
}
}
return
}
@ -711,7 +711,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject {
return success
}
public func sendPosition(destNum: Int64, wantAck: Bool) -> Bool {
public func sendPosition(destNum: Int64, wantResponse: Bool) -> Bool {
var success = false
let fromNodeNum = connectedPeripheral.num
@ -734,14 +734,13 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject {
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
if destNum != emptyNodeNum {
dataMessage.wantResponse = true
}
//if destNum != emptyNodeNum {
dataMessage.wantResponse = wantResponse
//}
meshPacket.decoded = dataMessage
var toRadio: ToRadio!
@ -765,7 +764,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject {
// 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, wantAck: false)
let success = sendPosition(destNum: connectedPeripheral.num, wantResponse: false)
if !success {
print("Failed to send positon to device")

View file

@ -336,7 +336,7 @@ struct ChannelMessageList: View {
focusedField = nil
replyMessageId = 0
if sendPositionWithMessage {
if bleManager.sendPosition(destNum: Int64(channel.index), wantAck: true) {
if bleManager.sendPosition(destNum: Int64(channel.index), wantResponse: false) {
print("Location Sent")
}
}

View file

@ -333,7 +333,7 @@ struct UserMessageList: View {
focusedField = nil
replyMessageId = 0
if sendPositionWithMessage {
if bleManager.sendPosition(destNum: user.num, wantAck: true) {
if bleManager.sendPosition(destNum: user.num, wantResponse: true) {
print("Location Sent")
}
}

View file

@ -208,7 +208,7 @@ struct PositionConfig: View {
Button("Save Position Config to \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")?") {
if fixedPosition {
_ = bleManager.sendPosition(destNum: bleManager.connectedPeripheral.num, wantAck: true)
_ = bleManager.sendPosition(destNum: bleManager.connectedPeripheral.num, wantResponse: false)
}
var pc = Config.PositionConfig()