mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
Working mqtt client proxy
This commit is contained in:
parent
5929268171
commit
a08b49b72a
2 changed files with 15 additions and 19 deletions
|
|
@ -13,7 +13,8 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
|
|||
// MqttClientProxyManagerDelegate
|
||||
func onMqttConnected() {
|
||||
mqttManager.status = .connected
|
||||
print("MQTT Connected")
|
||||
print("📲 Mqtt Client Proxy onMqttConnected now subscribing to \(mqttManager.topic).")
|
||||
mqttManager.mqttClientProxy?.subscribe(mqttManager.topic)
|
||||
}
|
||||
|
||||
func onMqttDisconnected() {
|
||||
|
|
@ -25,14 +26,13 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
|
|||
|
||||
print("onMqttMessageReceived")
|
||||
if message.topic.contains("/stat/") {
|
||||
// return
|
||||
return
|
||||
}
|
||||
var proxyMessage = MqttClientProxyMessage()
|
||||
proxyMessage.topic = message.topic
|
||||
proxyMessage.data = Data(message.payload)
|
||||
proxyMessage.retained = message.retained
|
||||
|
||||
|
||||
var toRadio: ToRadio!
|
||||
toRadio = ToRadio()
|
||||
toRadio.mqttClientProxyMessage = proxyMessage
|
||||
|
|
@ -435,13 +435,13 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
|
|||
|
||||
// Publish mqttClientProxyMessages received on the from radio
|
||||
if decodedInfo.payloadVariant == FromRadio.OneOf_PayloadVariant.mqttClientProxyMessage(decodedInfo.mqttClientProxyMessage) {
|
||||
var message = CocoaMQTTMessage (
|
||||
let message = CocoaMQTTMessage (
|
||||
topic: decodedInfo.mqttClientProxyMessage.topic,
|
||||
payload: [UInt8](decodedInfo.mqttClientProxyMessage.data),
|
||||
retained: decodedInfo.mqttClientProxyMessage.retained
|
||||
)
|
||||
print("📲 Publish Mqtt client proxy message received on FromRadio to the Mqtt server \(message)")
|
||||
mqttManager.mqttClient?.publish(message)
|
||||
mqttManager.mqttClientProxy?.publish(message)
|
||||
}
|
||||
|
||||
switch decodedInfo.packet.decoded.portnum {
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class MqttClientProxyManager {
|
|||
weak var delegate: MqttClientProxyManagerDelegate?
|
||||
var status = ConnectionStatus.none
|
||||
|
||||
var mqttClient: CocoaMQTT?
|
||||
var mqttClientProxy: CocoaMQTT?
|
||||
|
||||
var topic = "msh/2/c"
|
||||
|
||||
|
|
@ -80,8 +80,8 @@ class MqttClientProxyManager {
|
|||
|
||||
let clientId = "MeshtasticAppleMqttProxy-" + String(ProcessInfo().processIdentifier)
|
||||
|
||||
mqttClient = CocoaMQTT(clientID: clientId, host: host, port: UInt16(port))
|
||||
if let mqttClient = mqttClient {
|
||||
mqttClientProxy = CocoaMQTT(clientID: clientId, host: host, port: UInt16(port))
|
||||
if let mqttClient = mqttClientProxy {
|
||||
|
||||
mqttClient.username = username
|
||||
mqttClient.password = password
|
||||
|
|
@ -105,26 +105,26 @@ class MqttClientProxyManager {
|
|||
}
|
||||
|
||||
func subscribe(topic: String, qos: MqttQos) {
|
||||
let qos = CocoaMQTTQoS(rawValue :UInt8(qos.rawValue))!
|
||||
mqttClient?.subscribe(topic, qos: qos)
|
||||
print("📲 MQTT Client Proxy subscribed to: " + topic)
|
||||
let qos = CocoaMQTTQoS(rawValue :UInt8(qos.rawValue))!
|
||||
mqttClientProxy?.subscribe(topic, qos: qos)
|
||||
}
|
||||
|
||||
func unsubscribe(topic: String) {
|
||||
mqttClient?.unsubscribe(topic)
|
||||
mqttClientProxy?.unsubscribe(topic)
|
||||
print("📲 MQTT Client Proxy unsubscribe for: " + topic)
|
||||
}
|
||||
|
||||
func publish(message: String, topic: String, qos: MqttQos) {
|
||||
let qos = CocoaMQTTQoS(rawValue :UInt8(qos.rawValue))!
|
||||
mqttClient?.publish(topic, withString: message, qos: qos)
|
||||
mqttClientProxy?.publish(topic, withString: message, qos: qos)
|
||||
print("📲 MQTT Client Proxy publish for: " + topic)
|
||||
}
|
||||
|
||||
func disconnect() {
|
||||
//MqttSettings.shared.isConnected = false
|
||||
|
||||
if let client = mqttClient {
|
||||
if let client = mqttClientProxy {
|
||||
status = .disconnecting
|
||||
client.disconnect()
|
||||
print("📲 MQTT Client Proxy Disconnected")
|
||||
|
|
@ -141,8 +141,6 @@ extension MqttClientProxyManager: CocoaMQTTDelegate {
|
|||
print("📲 MQTT Client Proxy didConnectAck: \(ack)")
|
||||
if ack == .accept {
|
||||
delegate?.onMqttConnected()
|
||||
print("📲 MQTT Client Proxy subscribed to: " + topic)
|
||||
|
||||
} else {
|
||||
// Connection error
|
||||
var errorDescription = "Unknown Error"
|
||||
|
|
@ -165,7 +163,7 @@ extension MqttClientProxyManager: CocoaMQTTDelegate {
|
|||
print(errorDescription)
|
||||
delegate?.onMqttError(message: errorDescription)
|
||||
|
||||
self.disconnect() // Stop reconnecting
|
||||
//self.disconnect() // Stop reconnecting
|
||||
//mqttSettings.isConnected = false // Disable automatic connect on start
|
||||
}
|
||||
|
||||
|
|
@ -190,10 +188,8 @@ extension MqttClientProxyManager: CocoaMQTTDelegate {
|
|||
func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
|
||||
print("📲 MQTT Client Proxy didPublishAck from MqttClientProxyManager: \(id)")
|
||||
}
|
||||
|
||||
|
||||
|
||||
func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
|
||||
public func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
|
||||
delegate?.onMqttMessageReceived(message: message)
|
||||
print("📲 MQTT Client Proxy message received on topic: \(message.topic)")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue