heartbeat
This commit is contained in:
parent
558711bb2b
commit
8608583a58
@ -31,8 +31,8 @@ Metro secondTick = Metro(1000);
|
||||
|
||||
|
||||
MqttClient::MqttClient(RequestSender *meterBusMaster) :
|
||||
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
|
||||
m_disconnectState(3), m_disconnectTime(millis())
|
||||
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
|
||||
m_disconnectState(3), m_disconnectTime(millis()), m_uptime(0)
|
||||
{
|
||||
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
||||
m_mbusDevTuple[i].address = 0;
|
||||
@ -51,10 +51,14 @@ void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLe
|
||||
PString buf = PString(strbuf, sizeof(strbuf));
|
||||
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\" }, " <<
|
||||
"\"data\": {" <<
|
||||
"\"uptime\": " << m_uptime <<
|
||||
"}" <<
|
||||
"}" << endl;
|
||||
if (m_disconnectState == 0) {
|
||||
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
|
||||
} else {
|
||||
Serial << "no MQTT connection, message lost: " << endl <<
|
||||
strbuf << endl;
|
||||
}
|
||||
|
||||
// m_server.write(responseBuffer, responseBufferLength);
|
||||
@ -77,19 +81,17 @@ void MqttClient::exec() {
|
||||
// everything fine
|
||||
break;
|
||||
case 1:
|
||||
Serial.println("discState 1");
|
||||
Serial.println("MQTT Connection lost");
|
||||
m_mqttClient.disconnect();
|
||||
m_disconnectTime = millis();
|
||||
m_disconnectState = 2;
|
||||
break;
|
||||
case 2:
|
||||
Serial.println("discState 3");
|
||||
if (m_disconnectTime + 2000 < millis()) {
|
||||
m_disconnectState = 3;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
Serial.println("discState 3");
|
||||
if (m_mqttClient.connect("MeterbusHub")) {
|
||||
Serial << "MQTT connected" << endl;
|
||||
m_mqttClient.subscribe("MeterbusHub.Configuration");
|
||||
@ -106,6 +108,16 @@ void MqttClient::exec() {
|
||||
|
||||
|
||||
if (secondTick.check() == 1) {
|
||||
m_uptime++;
|
||||
|
||||
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
||||
if (m_disconnectState == 0) {
|
||||
m_mqttClient.publish("MeterbusHub.Heartbeat", (char*)msg.c_str());
|
||||
} else {
|
||||
Serial << "no MQTT connection, message lost: " << msg << endl;
|
||||
}
|
||||
|
||||
|
||||
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
||||
m_mbusDevTuple[i].timer -= 1;
|
||||
if (m_mbusDevTuple[i].timer == 0) {
|
||||
@ -117,20 +129,20 @@ void MqttClient::exec() {
|
||||
|
||||
|
||||
|
||||
// m_client = m_server.available();
|
||||
// if (m_client) {
|
||||
// uint16_t sendBufLen = 0;
|
||||
// uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
|
||||
// if (sendBuffer != 0) {
|
||||
// int chi;
|
||||
// while ((chi = m_client.read()) != -1) {
|
||||
// char ch = (char) chi;
|
||||
// *(sendBuffer + sendBufLen) = ch;
|
||||
// sendBufLen++;
|
||||
// }
|
||||
// m_meterBusMaster->sendBufferReady(sendBufLen, this);
|
||||
// }
|
||||
// }
|
||||
// m_client = m_server.available();
|
||||
// if (m_client) {
|
||||
// uint16_t sendBufLen = 0;
|
||||
// uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
|
||||
// if (sendBuffer != 0) {
|
||||
// int chi;
|
||||
// while ((chi = m_client.read()) != -1) {
|
||||
// char ch = (char) chi;
|
||||
// *(sendBuffer + sendBufLen) = ch;
|
||||
// sendBufLen++;
|
||||
// }
|
||||
// m_meterBusMaster->sendBufferReady(sendBufLen, this);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
@ -35,6 +35,7 @@ private:
|
||||
PubSubClient m_mqttClient;
|
||||
uint8_t m_disconnectState;
|
||||
uint32_t m_disconnectTime;
|
||||
uint32_t m_uptime;
|
||||
mbusDevTuple_t m_mbusDevTuple[NUM_OF_DEVICES];
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user