NetMeterbusMaster/MqttClient.cpp

344 lines
10 KiB
C++
Raw Normal View History

2015-05-08 23:40:36 +02:00
/*
* MqttClient.cpp
*
* Created on: 08.05.2015
* Author: wn
*/
#include "MqttClient.h"
#include <PubSubClient.h>
2015-05-09 00:08:54 +02:00
#include <PString.h>
#include <Streaming.h>
2015-05-09 16:31:49 +02:00
#include <Metro.h>
2015-05-11 22:36:58 +02:00
#include <WString.h>
#include <Print.h>
#include "cmd.h"
#include "config.h"
#include "reset.h"
2015-05-09 00:08:54 +02:00
2015-05-11 22:36:58 +02:00
char MQTT_BROKER_DEFAULT[] = "192.168.75.1";
2015-05-09 00:08:54 +02:00
const uint16_t MQTT_PORT = 1883;
2015-05-08 23:40:36 +02:00
2015-05-11 22:36:58 +02:00
String MqttConfig::exec(String params) {
String res = "failed";
Print *out = m_server;
MqttClient *mc = (MqttClient*)m_mqttClient;
if (params.equalsIgnoreCase("help")) {
*out << "help ..... this help page" << endl;
*out << "show ..... shows the whole configuration" << endl;
*out << "broker ... set the broker's address" << endl;
*out << " followed by a restart, you need to" << endl;
*out << " about a minute" << endl;
*out << "add ...... add an mbus client, params: token address period" << endl;
*out << "del ...... delete an mbus client, params: index" << endl;
*out << "reset .... reset configuration" << endl;
*out << " followed by a restart, you need to" << endl;
*out << " about a minute" << endl;
*out << endl;
res = "done";
} else if (params.equalsIgnoreCase("show")) {
// show the whole configuration
*out << "Clients:" << endl;
*out << "(index, token, address, period)" << endl;
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
*out << i << ": " << mc->m_mbusDevTuple[i].token << ", " <<
mc->m_mbusDevTuple[i].address << ", " <<
mc->m_mbusDevTuple[i].queryPeriod << endl;
}
*out << "Broker: " << mc->m_mqttBroker << endl;
*out << "Watchdog resets: " << WDOG_RSTCNT << endl;
res = "done";
} else if (params.startsWith("broker ")) {
// set the broker's address
int space = params.indexOf(' ');
if (space == -1) {
res = "missing argument";
} else {
String broker = params.substring(space+1);
strcpy(mc->m_mqttBroker, broker.c_str());
configWrite(CONFIG_BROKER, sizeof(mc->m_mqttBroker), (char*)mc->m_mqttBroker);
*out << "Stopping gateway, wait for reset" << endl;
while (true);
res = "done";
}
} else if (params.startsWith("add ")) {
// add an mbus client, params: token address period
2015-05-13 17:37:05 +02:00
char paramBuf[64];
char *paramPtr;
strncpy(paramBuf, params.c_str(), sizeof(paramBuf));
paramPtr = paramBuf;
char *tokenStr = 0;
char *addressStr = 0;
char *periodStr = 0;
if ((paramPtr != 0) && (*paramPtr != 0)){
// command
strsep(&paramPtr, " ");
}
if ((paramPtr != 0) && (*paramPtr != 0)){
tokenStr = strsep(&paramPtr, " ");
}
if ((paramPtr != 0) && (*paramPtr != 0)){
addressStr = strsep(&paramPtr, " ");
}
if ((paramPtr != 0) && (*paramPtr != 0)){
periodStr = strsep(&paramPtr, " ");
}
2015-05-11 22:36:58 +02:00
2015-05-13 17:37:05 +02:00
if ((tokenStr != 0) && (*tokenStr != 0) &&
(addressStr != 0) && (*addressStr != 0) &&
(periodStr != 0) && (*periodStr != 0)) {
2015-05-13 21:07:59 +02:00
int token = atoi(tokenStr);
bool validToken = ! ((token <= 0) || (token > 250));
int address = atoi(addressStr);
bool validAddress = ! ((address <= 0) || (address > 250));
int period = atoi(periodStr);
bool validPeriod = ! ((period < 10) || (period > 3600));
2015-05-13 17:37:05 +02:00
2015-05-13 21:07:59 +02:00
if (validToken && validAddress && validPeriod) {
uint8_t i = 0;
while (mc->m_mbusDevTuple[i].address != 0) {
i++;
}
mc->m_mbusDevTuple[i] = { (uint8_t)token, (uint8_t)address, (uint16_t)period };
configWrite(CONFIG_DEVICES, sizeof(mc->m_mbusDevTuple), (char*)mc->m_mbusDevTuple);
res = "done";
} else {
if (! validToken) {
*out << "Invalid token " << tokenStr << endl;
}
if (! validAddress) {
*out << "Invalid address " << addressStr << endl;
}
if (! validPeriod) {
*out << "Invalid period " << periodStr << endl;
}
res = "failure";
}
2015-05-13 17:37:05 +02:00
} else {
*out << "not enough arguments" << endl;
res = "failure";
}
2015-05-11 22:36:58 +02:00
} else if (params.startsWith("del ")) {
// delete an mbus client, params: index
int space = params.indexOf(' ');
if (space == -1) {
res = "missing argument";
} else {
String arg0 = params.substring(space);
int idx = atoi(arg0.c_str());
if (idx < 0 || idx >= NUM_OF_DEVICES) {
res = "illegal index";
} else {
*out << "Index " << arg0 << " (" << idx << ") to be deleted" << endl;
mc->m_mbusDevTuple[idx] = {0, 0, 0, 0};
configWrite(CONFIG_DEVICES, sizeof(mc->m_mbusDevTuple), (char*)mc->m_mbusDevTuple);
res = "done";
}
}
} else if (params.equalsIgnoreCase("reset")) {
configReset();
*out << "Stopping gateway, wait for reset" << endl;
resetDevice();
2015-05-11 22:36:58 +02:00
res = "done";
}
return res;
}
2015-05-08 23:40:36 +02:00
void callback(char* topic, byte* payload, unsigned int length) {
}
2015-05-09 16:31:49 +02:00
Metro secondTick = Metro(1000);
2015-05-11 22:36:58 +02:00
MqttClient::MqttClient(RequestSender *meterBusMaster) : m_mqttConfig(this),
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(m_client),
2015-05-09 20:26:31 +02:00
m_disconnectState(3), m_disconnectTime(millis()), m_uptime(0), m_deviceIdx(0)
2015-05-08 23:40:36 +02:00
{
}
2015-05-09 20:26:31 +02:00
void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLength, uint8_t token) {
2015-05-09 23:15:45 +02:00
char strbuf[1024];
2015-05-08 23:40:36 +02:00
memset(strbuf, sizeof(strbuf), 0);
PString buf = PString(strbuf, sizeof(strbuf));
2015-05-09 18:55:15 +02:00
2015-05-09 23:15:45 +02:00
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\", " <<
2015-05-25 22:49:20 +02:00
"\"token\": " << token <<
2015-05-09 23:15:45 +02:00
"}, " <<
"\"data\": {" <<
2015-05-09 20:26:31 +02:00
"\"uptime\": " << m_uptime << ", " <<
2015-05-09 18:55:15 +02:00
"\"telegram\": \"";
2015-05-09 20:26:31 +02:00
uint16_t i = 0;
while (true) {
if (responseBuffer[i] <= 0x0f) {
buf.print("0");
}
buf.print(responseBuffer[i], HEX);
buf.print(" ");
i++;
if (i == responseBufferLength) {
break;
}
}
2015-05-09 18:55:15 +02:00
buf << "\"}}";
2015-05-08 23:40:36 +02:00
if (m_disconnectState == 0) {
2015-05-09 23:48:09 +02:00
//Serial << "publishing " << strbuf << endl;
//Serial << "length: " << buf.length() << endl;
2015-05-25 22:49:20 +02:00
m_mqttClient.publish("IoT/Measurement/MeterbusHub", strbuf);
2015-05-09 17:45:17 +02:00
} else {
Serial << "no MQTT connection, message lost: " << endl <<
strbuf << endl;
2015-05-08 23:40:36 +02:00
}
}
2015-05-09 20:26:31 +02:00
void MqttClient::sendError(uint8_t code, uint8_t token) {
2015-05-09 23:15:45 +02:00
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\", \"error\": ")
+ code + String(", \"token\": ") + token + String(" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
if (m_disconnectState == 0) {
2015-05-09 23:48:09 +02:00
//Serial << "publishing " << msg << endl;
//Serial << "length: " << msg.length() << endl;
2015-05-25 22:49:20 +02:00
m_mqttClient.publish("IoT/Failure/MeterbusHub", (char*)msg.c_str());
2015-05-09 23:15:45 +02:00
} else {
Serial << "no MQTT connection, message lost: " << msg << endl;
}
2015-05-08 23:40:36 +02:00
}
2015-05-11 22:36:58 +02:00
void MqttClient::begin(CmdServer *cmdServer) {
m_mqttConfig.registerYourself(cmdServer);
if (! configIsValid()) {
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
m_mbusDevTuple[i] = { 0, 0, 0, 0 };
}
m_mbusDevTuple[0] = { 1, 0x53, 10, 0 }; // light meter
m_mbusDevTuple[1] = { 2, 32, 10, 0 }; // electrity
configWrite(CONFIG_DEVICES, sizeof(m_mbusDevTuple), (char*)m_mbusDevTuple);
strcpy(m_mqttBroker, MQTT_BROKER_DEFAULT);
configWrite(CONFIG_BROKER, sizeof(m_mqttBroker), (char*)m_mqttBroker);
}
configRead(CONFIG_DEVICES, sizeof(m_mbusDevTuple), (char*)m_mbusDevTuple);
configRead(CONFIG_BROKER, sizeof(m_mqttBroker), (char*)m_mqttBroker);
m_mqttClient = PubSubClient(m_mqttBroker, MQTT_PORT, callback, m_client);
2015-05-08 23:40:36 +02:00
}
void MqttClient::exec() {
2015-05-09 23:15:45 +02:00
//Serial << "*** a" << endl;
2015-05-08 23:40:36 +02:00
if ((m_disconnectState == 0) && (! m_mqttClient.loop())) {
m_disconnectState = 1;
}
2015-05-09 23:15:45 +02:00
//Serial << "*** b, " << m_disconnectState << endl;
2015-05-08 23:40:36 +02:00
switch (m_disconnectState) {
case 0:
// Serial.println("discState 0");
// everything fine
break;
case 1:
2015-05-09 17:45:17 +02:00
Serial.println("MQTT Connection lost");
2015-05-08 23:40:36 +02:00
m_mqttClient.disconnect();
m_disconnectTime = millis();
m_disconnectState = 2;
break;
case 2:
if (m_disconnectTime + 2000 < millis()) {
m_disconnectState = 3;
}
break;
case 3:
2015-05-09 23:15:45 +02:00
Serial << "Trying to re-connect" << endl;
2015-05-08 23:40:36 +02:00
if (m_mqttClient.connect("MeterbusHub")) {
2015-05-09 00:08:54 +02:00
Serial << "MQTT connected" << endl;
2015-05-08 23:40:36 +02:00
m_disconnectTime = millis();
m_disconnectState = 0;
} else {
2015-05-10 18:32:11 +02:00
Serial << "no success" << endl;
2015-05-08 23:40:36 +02:00
m_disconnectState = 1;
}
break;
default:
m_disconnectState = 0;
break;
}
2015-05-09 23:15:45 +02:00
//Serial << "*** c" << endl;
2015-05-09 16:31:49 +02:00
if (secondTick.check() == 1) {
2015-05-09 17:45:17 +02:00
m_uptime++;
2015-05-09 23:48:09 +02:00
//Serial << "Tick " << m_uptime << endl;
2015-05-09 17:45:17 +02:00
2015-05-11 22:36:58 +02:00
byte wdogCnt = WDOG_RSTCNT;
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String(", \"watchdogCnt\": ") + wdogCnt + String("}}");
2015-05-10 18:32:11 +02:00
if (m_disconnectState == 0) {
2015-05-25 22:49:20 +02:00
m_mqttClient.publish("IoT/Heartbeat/MeterbusHub", (char*)msg.c_str());
2015-05-10 18:32:11 +02:00
} else {
Serial << "no MQTT connection, message lost: " << msg << endl;
}
2015-05-09 17:45:17 +02:00
2015-05-09 16:31:49 +02:00
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
2015-05-09 20:26:31 +02:00
if ((m_mbusDevTuple[i].address != 0) && (m_mbusDevTuple[i].timer != 0)) {
m_mbusDevTuple[i].timer -= 1;
2015-05-09 23:48:09 +02:00
// if (m_mbusDevTuple[i].timer == 0) {
// Serial << "Device " << m_mbusDevTuple[i].token << " ready for request" << endl;
// } else {
// Serial << "Device " << m_mbusDevTuple[i].token << " not ready for request, timer: " << m_mbusDevTuple[i].timer << endl;
// }
2015-05-09 20:26:31 +02:00
}
}
2015-05-09 18:55:15 +02:00
2015-05-09 23:15:45 +02:00
//Serial << "while in" << endl;
while (true) {
2015-05-09 20:26:31 +02:00
if ((m_mbusDevTuple[m_deviceIdx].address != 0) && (m_mbusDevTuple[m_deviceIdx].timer == 0)) {
Serial << "Issue request for device " << m_mbusDevTuple[m_deviceIdx].token << endl;
2015-05-09 18:55:15 +02:00
uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
2015-05-09 20:26:31 +02:00
if (sendBuffer != 0) {
Serial << "send buffer ready" << endl;
2015-05-09 23:15:45 +02:00
m_mbusDevTuple[m_deviceIdx].timer = m_mbusDevTuple[m_deviceIdx].queryPeriod;
2015-05-09 20:26:31 +02:00
sendBuffer[0] = 0x10;
sendBuffer[1] = 0x5b;
sendBuffer[2] = m_mbusDevTuple[m_deviceIdx].address;
sendBuffer[3] = (uint8_t)(sendBuffer[1] + sendBuffer[2]);
sendBuffer[4] = 0x16;
m_meterBusMaster->sendBufferReady(5, m_mbusDevTuple[m_deviceIdx].token, this);
} else {
Serial << "no send buffer ready" << endl;
}
2015-05-09 23:15:45 +02:00
break;
} else {
2015-05-09 23:48:09 +02:00
//Serial << "Trying " << m_deviceIdx << ", " << m_mbusDevTuple[m_deviceIdx].token << endl;
2015-05-09 23:15:45 +02:00
m_deviceIdx++;
if (m_deviceIdx >= NUM_OF_DEVICES) {
m_deviceIdx = 0;
}
break;
2015-05-09 16:31:49 +02:00
}
}
2015-05-09 23:15:45 +02:00
//Serial << "while out" << endl;
2015-05-09 16:31:49 +02:00
}
2015-05-09 23:15:45 +02:00
//Serial << "*** d" << endl;
2015-05-09 16:31:49 +02:00
2015-05-08 23:40:36 +02:00
}