configuration partly done

This commit is contained in:
hg
2015-05-11 22:36:58 +02:00
parent 4e84527a4f
commit fe3f5d4b84
6 changed files with 254 additions and 17 deletions

View File

@ -11,12 +11,94 @@
#include <PString.h>
#include <Streaming.h>
#include <Metro.h>
#include <WString.h>
#include <Print.h>
#include "cmd.h"
#include "config.h"
byte MQTT_BROKER[] = { 172, 16, 2, 16 };
char MQTT_BROKER_DEFAULT[] = "192.168.75.1";
const uint16_t MQTT_PORT = 1883;
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
configWrite(CONFIG_DEVICES, sizeof(mc->m_mbusDevTuple), (char*)mc->m_mbusDevTuple);
res = "done";
} 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;
while (true);
res = "done";
}
return res;
}
void callback(char* topic, byte* payload, unsigned int length) {
}
@ -24,16 +106,10 @@ void callback(char* topic, byte* payload, unsigned int length) {
Metro secondTick = Metro(1000);
MqttClient::MqttClient(RequestSender *meterBusMaster) :
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
MqttClient::MqttClient(RequestSender *meterBusMaster) : m_mqttConfig(this),
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(m_client),
m_disconnectState(3), m_disconnectTime(millis()), m_uptime(0), m_deviceIdx(0)
{
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
}
void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLength, uint8_t token) {
@ -67,7 +143,7 @@ void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLe
if (m_disconnectState == 0) {
//Serial << "publishing " << strbuf << endl;
//Serial << "length: " << buf.length() << endl;
m_mqttClient.publish("MeterbusHub/Measurement", strbuf);
m_mqttClient.publish("IoT/MeterbusHub/Measurement", strbuf);
} else {
Serial << "no MQTT connection, message lost: " << endl <<
strbuf << endl;
@ -80,13 +156,34 @@ void MqttClient::sendError(uint8_t code, uint8_t token) {
if (m_disconnectState == 0) {
//Serial << "publishing " << msg << endl;
//Serial << "length: " << msg.length() << endl;
m_mqttClient.publish("MeterbusHub/Measurement", (char*)msg.c_str());
m_mqttClient.publish("IoT/MeterbusHub/Measurement", (char*)msg.c_str());
} else {
Serial << "no MQTT connection, message lost: " << msg << endl;
}
}
void MqttClient::begin() {
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);
}
void MqttClient::exec() {
@ -133,9 +230,10 @@ void MqttClient::exec() {
m_uptime++;
//Serial << "Tick " << m_uptime << endl;
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
byte wdogCnt = WDOG_RSTCNT;
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String(", \"watchdogCnt\": ") + wdogCnt + String("}}");
if (m_disconnectState == 0) {
m_mqttClient.publish("MeterbusHub/Heartbeat", (char*)msg.c_str());
m_mqttClient.publish("IoT/MeterbusHub/Heartbeat", (char*)msg.c_str());
} else {
Serial << "no MQTT connection, message lost: " << msg << endl;
}