Broker fix

This commit is contained in:
Mit4el
2024-12-03 20:15:16 +03:00
parent 889eb5f85b
commit 322925fa5a

View File

@@ -7,12 +7,13 @@ namespace _Broker
{ {
#define DEF_PORT 1883 #define DEF_PORT 1883
bool _global_debug = false;
// MqttBroker broker(1883); // MqttBroker broker(1883);
class myPicoMQTT : public PicoMQTT::Server class myPicoMQTT : public PicoMQTT::Server
{ {
private: private:
bool _debug; //bool _debug;
String _user; String _user;
String _pass; String _pass;
@@ -27,15 +28,15 @@ namespace _Broker
_pass = pass; _pass = pass;
} }
void setDebug(bool debug) /* void setDebug(bool debug)
{ {
_debug = debug; _debug = debug;
} }
*/
protected: protected:
void on_connected(const char *client_id) void on_connected(const char *client_id)
{ {
if (_debug) if (_Broker::_global_debug)
{ {
Serial.print("[BrokerMQTT], Client connected: "); Serial.print("[BrokerMQTT], Client connected: ");
Serial.println(client_id); Serial.println(client_id);
@@ -43,7 +44,7 @@ namespace _Broker
} }
void on_disconnected(const char *client_id) void on_disconnected(const char *client_id)
{ {
if (_debug) if (_Broker::_global_debug)
{ {
// SerialPrint("i", "BrokerMQTT", "Client disconnected: " + client_id); // SerialPrint("i", "BrokerMQTT", "Client disconnected: " + client_id);
@@ -53,7 +54,7 @@ namespace _Broker
} }
void on_subscribe(const char *client_id, const char *topic) void on_subscribe(const char *client_id, const char *topic)
{ {
if (_debug) if (_Broker::_global_debug)
{ {
// SerialPrint("i", "BrokerMQTT", "Client " + client_id + ", subscribe: " + topic); // SerialPrint("i", "BrokerMQTT", "Client " + client_id + ", subscribe: " + topic);
@@ -65,7 +66,7 @@ namespace _Broker
} }
void on_unsubscribe(const char *client_id, const char *topic) void on_unsubscribe(const char *client_id, const char *topic)
{ {
if (_debug) if (_Broker::_global_debug)
{ {
// SerialPrint("i", "BrokerMQTT", "Client " + client_id + ", unsubscribe: " + topic); // SerialPrint("i", "BrokerMQTT", "Client " + client_id + ", unsubscribe: " + topic);
@@ -163,7 +164,8 @@ namespace _Broker
_port = DEF_PORT; _port = DEF_PORT;
picoMqtt = new myPicoMQTT(_port); picoMqtt = new myPicoMQTT(_port);
picoMqtt->begin(); picoMqtt->begin();
picoMqtt->setDebug(_debug); //picoMqtt->setDebug(_debug);
_global_debug = _debug;
picoMqtt->setAuth(_user, _pass); picoMqtt->setAuth(_user, _pass);
if (_brige) if (_brige)
{ {
@@ -182,12 +184,12 @@ namespace _Broker
{ {
picoMqtt->subscribe("#", [](const char *topic, const char *message) picoMqtt->subscribe("#", [](const char *topic, const char *message)
{ clientMqtt->publish(topic, message); { clientMqtt->publish(topic, message);
if (_debug) if (_Broker::_global_debug)
SerialPrint("i", F("BrigeMQTT"), "Client publish, topic: " + String(topic) + " msg: " + String(message) ); }); SerialPrint("i", F("BrigeMQTT"), "Client publish, topic: " + String(topic) + " msg: " + String(message) ); });
clientMqtt->subscribe("#", [](const char *topic, const char *message) clientMqtt->subscribe("#", [](const char *topic, const char *message)
{ picoMqtt->publish(topic, message); { picoMqtt->publish(topic, message);
if (_debug) if (_Broker::_global_debug)
SerialPrint("i", F("BrigeMQTT"), "Server publish, topic: " + String(topic) + " msg: " + String(message) ); }); SerialPrint("i", F("BrigeMQTT"), "Server publish, topic: " + String(topic) + " msg: " + String(message) ); });
} }
// picoMqtt.begin(); // picoMqtt.begin();