1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00
This commit is contained in:
Gregor Baues 2021-04-27 14:20:49 +02:00
parent caaad92887
commit 7442e3452e
2 changed files with 50 additions and 37 deletions

View File

@ -28,40 +28,49 @@
#include "defines.h" #include "defines.h"
#include <Arduino.h> #include <Arduino.h>
#include <EthernetInterface.h>
#include <PubSubClient.h> // Base (sync) MQTT library #include <PubSubClient.h> // Base (sync) MQTT library
#include <DccMQTT.h> #include <DccMQTT.h>
void DccMQTT::setup() 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/<csid>/$connected as used in the connect
// *
// */
// mqttDccExParser = p;
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/<csid>/$connected as used in the connect
*
*/
mqttDccExParser = p;
} }

View File

@ -21,14 +21,11 @@ struct DccMQTTMsg {
}; };
enum DccMQTTState {
INIT,
// IPAddress server(MQTT_BROKER_ADDRESS); CONFIGURED, // server/client objects set
// EthernetClient ethClient = ETHNetwork::getServer().available(); CONNECTED // mqtt broker is connected
};
// // MQTT connection
// PubSubClient mqttClient(ethClient);
// PubSubClient *DccMQTT::mqClient = &mqttClient;
class DccMQTT class DccMQTT
@ -40,25 +37,32 @@ private:
DccMQTT(const DccMQTT&); // non construction-copyable DccMQTT(const DccMQTT&); // non construction-copyable
DccMQTT& operator=( const DccMQTT& ); // non copyable DccMQTT& operator=( const DccMQTT& ); // non copyable
EthernetClient *ethClient; // TCP Client object for the MQ Connection EthernetClient ethClient; // TCP Client object for the MQ Connection
IPAddress *server; // MQTT server object IPAddress server; // MQTT server object
PubSubClient *mqttClient; // PubSub Endpoint for data exchange PubSubClient mqttClient; // PubSub Endpoint for data exchange
// EthernetClient ethClient = ETHNetwork::getServer().available(); // EthernetClient ethClient = ETHNetwork::getServer().available();
Queue<DccMQTTMsg> in; Queue<DccMQTTMsg> in;
Queue<DccMQTTMsg> out; Queue<DccMQTTMsg> out;
DccMQTTState mqState = INIT;
public: public:
static DccMQTT *get() noexcept { static DccMQTT *get() noexcept {
return &singleton; 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 setup(); // called at setup in the main ino file
void loop(); void loop();
~DccMQTT() = default; ~DccMQTT() = default;
DccMQTT()
}; };