mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
update
This commit is contained in:
parent
caaad92887
commit
7442e3452e
59
DccMQTT.cpp
59
DccMQTT.cpp
|
@ -28,40 +28,49 @@
|
|||
#include "defines.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <EthernetInterface.h>
|
||||
#include <PubSubClient.h> // Base (sync) MQTT library
|
||||
|
||||
#include <DccMQTT.h>
|
||||
|
||||
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
|
||||
// *
|
||||
// */
|
||||
|
||||
|
||||
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;
|
||||
// mqttDccExParser = p;
|
||||
}
|
||||
|
||||
|
||||
|
|
28
DccMQTT.h
28
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<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()
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user