From dc53ef743ed800b9e97d75b261ebc0a22197989c Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 15 Aug 2023 19:20:59 -0700 Subject: [PATCH] Delete unnecessary MQTT nonsense --- Meshtastic/Helpers/BLEManager.swift | 5 ++- .../Helpers/Mqtt/MqttClientProxyManager.swift | 42 ++----------------- 2 files changed, 7 insertions(+), 40 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 8d149579..4a510d41 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -12,14 +12,14 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate // MqttClientProxyManagerDelegate func onMqttConnected() { - mqttManager.status = .connected + //mqttManager.status = .connected mqttProxyConnected = true print("📲 Mqtt Client Proxy onMqttConnected now subscribing to \(mqttManager.topic).") mqttManager.mqttClientProxy?.subscribe(mqttManager.topic) } func onMqttDisconnected() { - mqttManager.status = .disconnected + // mqttManager.status = .disconnected mqttProxyConnected = false print("MQTT Disconnected") } @@ -47,6 +47,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate } func onMqttError(message: String) { + mqttProxyConnected = false print("MQTT Error") } diff --git a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift index af22fcd1..e4a27531 100644 --- a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift +++ b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift @@ -16,31 +16,12 @@ protocol MqttClientProxyManagerDelegate: AnyObject { } class MqttClientProxyManager { - enum ConnectionStatus { - case connecting - case connected - case disconnecting - case disconnected - case error - case none - } - - enum MqttQos: Int { - case atMostOnce = 0 - case atLeastOnce = 1 - case exactlyOnce = 2 - } // Singleton Instance static let shared = MqttClientProxyManager() - private static let defaultKeepAliveInterval: Int32 = 60 - weak var delegate: MqttClientProxyManagerDelegate? - var status = ConnectionStatus.none - var mqttClientProxy: CocoaMQTT? - var topic = "msh/2/c" private init() { @@ -81,8 +62,6 @@ class MqttClientProxyManager { return } - status = .connecting - let clientId = "MeshtasticAppleMqttProxy-" + String(ProcessInfo().processIdentifier) mqttClientProxy = CocoaMQTT(clientID: clientId, host: host, port: UInt16(port)) @@ -103,17 +82,14 @@ class MqttClientProxyManager { let success = mqttClient.connect() if !success { delegate?.onMqttError(message: "Mqtt connect error") - status = .error } } else { delegate?.onMqttError(message: "Mqtt initialization error") - status = .error } } - func subscribe(topic: String, qos: MqttQos) { + func subscribe(topic: String, qos: CocoaMQTTQoS) { print("📲 MQTT Client Proxy subscribed to: " + topic) - let qos = CocoaMQTTQoS(rawValue :UInt8(qos.rawValue))! mqttClientProxy?.subscribe(topic, qos: qos) } @@ -122,21 +98,16 @@ class MqttClientProxyManager { print("📲 MQTT Client Proxy unsubscribe for: " + topic) } - func publish(message: String, topic: String, qos: MqttQos) { - let qos = CocoaMQTTQoS(rawValue :UInt8(qos.rawValue))! + func publish(message: String, topic: String, qos: CocoaMQTTQoS) { mqttClientProxy?.publish(topic, withString: message, qos: qos) print("📲 MQTT Client Proxy publish for: " + topic) } func disconnect() { - //MqttSettings.shared.isConnected = false if let client = mqttClientProxy { - status = .disconnecting client.disconnect() print("📲 MQTT Client Proxy Disconnected") - } else { - status = .disconnected } } } @@ -170,21 +141,16 @@ extension MqttClientProxyManager: CocoaMQTTDelegate { print(errorDescription) delegate?.onMqttError(message: errorDescription) - //self.disconnect() // Stop reconnecting - //mqttSettings.isConnected = false // Disable automatic connect on start + self.disconnect() } - - self.status = ack == .accept ? ConnectionStatus.connected : ConnectionStatus.error // Set AFTER sending onMqttError (so the delegate can detect that was an error while establishing connection) } func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) { print("mqttDidDisconnect: \(err?.localizedDescription ?? "")") - if let error = err, status == .connecting { + if let error = err { delegate?.onMqttError(message: error.localizedDescription) } - - status = err == nil ? .disconnected : .error delegate?.onMqttDisconnected() }