mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2025-02-19 23:46:02 +01:00
loopPing added
check for returning diag messages on the main channel
This commit is contained in:
parent
3c89d713fd
commit
e51279b202
@ -50,8 +50,39 @@ void cantorDecode(int32_t c, int *a, int *b)
|
|||||||
*a = w - *b;
|
*a = w - *b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief callback used from DIAG to send diag messages to the broker / clients
|
||||||
|
*
|
||||||
|
* @param msg
|
||||||
|
* @param length
|
||||||
|
*/
|
||||||
|
void mqttDiag(const char *msg, const int length)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (MQTTInterface::get()->getState() == CONNECTED)
|
||||||
|
{
|
||||||
|
// if not connected all goes only to Serial;
|
||||||
|
// if CONNECTED we have at least the root topic subscribed to
|
||||||
|
auto mqSocket = MQTTInterface::get()->getActive();
|
||||||
|
char topic[MAXTSTR];
|
||||||
|
memset(topic, 0, MAXTSTR);
|
||||||
|
|
||||||
|
if (mqSocket == 0)
|
||||||
|
{ // send to root topic of the commandstation as it doen't concern a specific client at this point
|
||||||
|
sprintf(topic, "%s", MQTTInterface::get()->getClientID());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sprintf(topic, "%s/%ld/diag", MQTTInterface::get()->getClientID(), MQTTInterface::get()->getClients()[mqSocket].topic);
|
||||||
|
}
|
||||||
|
// Serial.print(" ---- MQTT pub to: "); Serial.print(topic); Serial.print(" Msg: "); Serial.print(msg);
|
||||||
|
MQTTInterface::get()->publish(topic, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MQTTInterface::setup()
|
void MQTTInterface::setup()
|
||||||
{
|
{
|
||||||
|
StringLogger::get().addDiagWriter(mqttDiag);
|
||||||
singleton = new MQTTInterface();
|
singleton = new MQTTInterface();
|
||||||
|
|
||||||
if (!singleton->connected)
|
if (!singleton->connected)
|
||||||
@ -97,6 +128,7 @@ void mqttCallback(char *topic, byte *payload, unsigned int length)
|
|||||||
{
|
{
|
||||||
case '<': // Recieved a DCC-EX Command
|
case '<': // Recieved a DCC-EX Command
|
||||||
{
|
{
|
||||||
|
if (payload[1] == '*') { return;} // it's a bounced diag message
|
||||||
const char s[2] = "/"; // topic delimiter is /
|
const char s[2] = "/"; // topic delimiter is /
|
||||||
char *token;
|
char *token;
|
||||||
byte mqsocket;
|
byte mqsocket;
|
||||||
@ -538,8 +570,22 @@ void MQTTInterface::loop()
|
|||||||
|
|
||||||
|
|
||||||
bool showonce = false;
|
bool showonce = false;
|
||||||
|
auto s = millis();
|
||||||
|
void loopPing(int interval)
|
||||||
|
{
|
||||||
|
auto c = millis();
|
||||||
|
if (c - s > 2000)
|
||||||
|
{
|
||||||
|
DIAG(F("loop alive")); // ping every 2 sec
|
||||||
|
s = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void MQTTInterface::loop2()
|
void MQTTInterface::loop2()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
loopPing(2000); // ping every 2 sec
|
||||||
// Connection impossible so just don't do anything
|
// Connection impossible so just don't do anything
|
||||||
if (singleton->mqState == CONNECTION_FAILED)
|
if (singleton->mqState == CONNECTION_FAILED)
|
||||||
{
|
{
|
||||||
|
@ -178,34 +178,33 @@ private:
|
|||||||
char clientID[(CLIENTIDSIZE * 2) + 1]; // unique ID of the commandstation; not to confused with the connectionID
|
char clientID[(CLIENTIDSIZE * 2) + 1]; // unique ID of the commandstation; not to confused with the connectionID
|
||||||
csmqttclient_t clients[MAXMQTTCONNECTIONS]; // array of connected mqtt clients
|
csmqttclient_t clients[MAXMQTTCONNECTIONS]; // array of connected mqtt clients
|
||||||
char connectID[MAXCONNECTID]; // clientId plus possible prefix if required by the broker
|
char connectID[MAXCONNECTID]; // clientId plus possible prefix if required by the broker
|
||||||
uint8_t subscriberid = 0; // id assigned to a mqtt client when recieving the inital handshake; +1 at each connection
|
byte subscriberid = 0; // id assigned to a mqtt client when recieving the inital handshake; +1 at each connection
|
||||||
|
byte activeSubscriber = 0; // if its 0 no active Subscriber; set as soon as we recieve a command of go into processing on the CS
|
||||||
|
|
||||||
bool connected = false; // set to true if the ethernet connection is available
|
bool connected = false; // set to true if the ethernet connection is available
|
||||||
MQTTInterfaceState mqState = INIT; // Status of the MQBroker connection
|
MQTTInterfaceState mqState = INIT; // Status of the MQBroker connection
|
||||||
RingStream *outboundRing; // Buffer for collecting the results from the command parser
|
RingStream *outboundRing; // Buffer for collecting the results from the command parser
|
||||||
PubSubClient *mqttClient; // PubSub Endpoint for data exchange
|
PubSubClient *mqttClient; // PubSub Endpoint for data exchange
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static MQTTInterface *get() noexcept
|
static MQTTInterface *get() noexcept { return singleton;}
|
||||||
{
|
|
||||||
return singleton;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean subscribe(const char *topic);
|
boolean subscribe(const char *topic);
|
||||||
|
void publish(const char *topic, const char *payload);
|
||||||
|
|
||||||
void publish(const char *topic, const char *payload);
|
ObjectPool<csmsg_t, MAXPOOLSIZE> *getPool() { return &pool; };
|
||||||
|
Queue<int> *getIncomming() { return ∈ };
|
||||||
ObjectPool<csmsg_t, MAXPOOLSIZE> *getPool() { return &pool; };
|
Queue<int> *getSubscriptionQueue() { return &subscriberQueue; };
|
||||||
Queue<int> *getIncomming() { return ∈ };
|
MQTTInterfaceState getState() { return mqState; };
|
||||||
Queue<int> *getSubscriptionQueue() { return &subscriberQueue; };
|
byte getActive() { return activeSubscriber; };
|
||||||
|
void setActive(byte mqSocket) { activeSubscriber = mqSocket; };
|
||||||
char *getClientID() { return clientID; };
|
char *getClientID() { return clientID; };
|
||||||
uint8_t getClientSize() { return subscriberid; }
|
uint8_t getClientSize() { return subscriberid; };
|
||||||
|
|
||||||
// initalized to 0 so that the first id comming back is 1
|
// initalized to 0 so that the first id comming back is 1
|
||||||
// index 0 in the clients array is not used therefore
|
// index 0 in the clients array is not used therefore
|
||||||
//! improvement here to be done to save some bytes
|
|
||||||
|
|
||||||
uint8_t obtainSubscriberID()
|
uint8_t obtainSubscriberID()
|
||||||
{
|
{
|
||||||
if (subscriberid == MAXMQTTCONNECTIONS)
|
if (subscriberid == MAXMQTTCONNECTIONS)
|
||||||
@ -216,7 +215,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
csmqttclient_t *getClients() { return clients; };
|
csmqttclient_t *getClients() { return clients; };
|
||||||
RingStream *getRingStream() { return outboundRing; }; // debug only
|
RingStream *getRingStream() { return outboundRing; };
|
||||||
|
|
||||||
static void setup();
|
static void setup();
|
||||||
static void loop();
|
static void loop();
|
||||||
@ -224,4 +223,5 @@ public:
|
|||||||
~MQTTInterface() = default;
|
~MQTTInterface() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user