1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-03 12:55:03 +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,6 +28,7 @@
#include "defines.h"
#include <Arduino.h>
#include <EthernetInterface.h>
#include <PubSubClient.h> // Base (sync) MQTT library
#include <DccMQTT.h>
@ -35,33 +36,41 @@
void DccMQTT::setup()
{
// IPAddress server(MQTT_BROKER_ADDRESS);
// EthernetClient ethClient = ETHNetwork::getServer().available();
char _csidMsg[64]{'\0'}; //!< string buffer for the serialized message to return
mqttClient.setServer(server, MQTT_BROKER_PORT); // Initalize MQ broker
// // 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 ..."));
// 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
// 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 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
*
*/
// /**
// * @todo set the connect status with a retained message on the $connected topic /admin/<csid>/$connected as used in the connect
// *
// */
mqttDccExParser = p;
// mqttDccExParser = p;
}

View File

@ -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<DccMQTTMsg> in;
Queue<DccMQTTMsg> 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()
};