From 7442e3452ed92e5f7a765fbbf3e9ac9e86de8b3f Mon Sep 17 00:00:00 2001 From: Gregor Baues Date: Tue, 27 Apr 2021 14:20:49 +0200 Subject: [PATCH] update --- DccMQTT.cpp | 59 ++++++++++++++++++++++++++++++----------------------- DccMQTT.h | 28 ++++++++++++++----------- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/DccMQTT.cpp b/DccMQTT.cpp index 7b74bc5..0c18584 100644 --- a/DccMQTT.cpp +++ b/DccMQTT.cpp @@ -28,40 +28,49 @@ #include "defines.h" #include +#include #include // Base (sync) MQTT library #include void DccMQTT::setup() { + + // IPAddress server(MQTT_BROKER_ADDRESS); +// EthernetClient ethClient = ETHNetwork::getServer().available(); + +// // MQTT connection +// PubSubClient mqttClient(ethClient); +// PubSubClient *DccMQTT::mqClient = &mqttClient; + + server = IPAddress(MQTT_BROKER_ADDRESS); + + // char _csidMsg[64]{'\0'}; //!< string buffer for the serialized message to return + // mqttClient.setServer(server, MQTT_BROKER_PORT); // Initalize MQ broker + + + // DBG(F("MQTT Client : Server ok ...")); + // mqttClient.setCallback(mqttCallback); // Initalize callback function for incomming messages + // DBG(F("MQTT Client : Callback set ...")); + + // DccMQTT::setDeviceID(); // set the unique device ID to bu used for creating / listening to topic + + // /** + // * @todo check for connection failure + // */ + // reconnect(); // inital connection as well as reconnects + // DccMQTT::subscribe(); // set up all subscriptionn + // INFO(F("MQTT subscriptons done...")); + // sprintf_P(_csidMsg, csidfmt, DccMQTT::getDeviceID()); + // mqttClient.publish(DccMQTT::topics[ADMIN], _csidMsg); // say hello to the broker and the API who listens to this topic - char _csidMsg[64]{'\0'}; //!< string buffer for the serialized message to return - mqttClient.setServer(server, MQTT_BROKER_PORT); // Initalize MQ broker + // /** + // * @todo set the connect status with a retained message on the $connected topic /admin//$connected as used in the connect + // * + // */ - - DBG(F("MQTT Client : Server ok ...")); - mqttClient.setCallback(mqttCallback); // Initalize callback function for incomming messages - DBG(F("MQTT Client : Callback set ...")); - - DccMQTT::setDeviceID(); // set the unique device ID to bu used for creating / listening to topic - - /** - * @todo check for connection failure - */ - reconnect(); // inital connection as well as reconnects - DccMQTT::subscribe(); // set up all subscriptionn - INFO(F("MQTT subscriptons done...")); - sprintf_P(_csidMsg, csidfmt, DccMQTT::getDeviceID()); - mqttClient.publish(DccMQTT::topics[ADMIN], _csidMsg); // say hello to the broker and the API who listens to this topic - - - /** - * @todo set the connect status with a retained message on the $connected topic /admin//$connected as used in the connect - * - */ - - mqttDccExParser = p; + // mqttDccExParser = p; } diff --git a/DccMQTT.h b/DccMQTT.h index d178c7f..2027fb8 100644 --- a/DccMQTT.h +++ b/DccMQTT.h @@ -21,14 +21,11 @@ struct DccMQTTMsg { }; - - -// IPAddress server(MQTT_BROKER_ADDRESS); -// EthernetClient ethClient = ETHNetwork::getServer().available(); - -// // MQTT connection -// PubSubClient mqttClient(ethClient); -// PubSubClient *DccMQTT::mqClient = &mqttClient; +enum DccMQTTState { + INIT, + CONFIGURED, // server/client objects set + CONNECTED // mqtt broker is connected +}; class DccMQTT @@ -40,25 +37,32 @@ private: DccMQTT(const DccMQTT&); // non construction-copyable DccMQTT& operator=( const DccMQTT& ); // non copyable - EthernetClient *ethClient; // TCP Client object for the MQ Connection - IPAddress *server; // MQTT server object - PubSubClient *mqttClient; // PubSub Endpoint for data exchange + EthernetClient ethClient; // TCP Client object for the MQ Connection + IPAddress server; // MQTT server object + PubSubClient mqttClient; // PubSub Endpoint for data exchange // EthernetClient ethClient = ETHNetwork::getServer().available(); - + Queue in; Queue out; + DccMQTTState mqState = INIT; + public: static DccMQTT *get() noexcept { return &singleton; } + bool isConfigured() { return mqState == CONFIGURED; }; + bool isConnected() { return mqState == CONNECTED; }; + void setState(DccMQTTState s) { mqState = s; }; + void setup(); // called at setup in the main ino file void loop(); ~DccMQTT() = default; + DccMQTT() };