From 98506ca883390b5821c4b45e61af62ed402c7ce0 Mon Sep 17 00:00:00 2001 From: hg Date: Wed, 13 May 2015 21:07:59 +0200 Subject: [PATCH] mq add done --- MqttClient.cpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/MqttClient.cpp b/MqttClient.cpp index 1deab9b..4debdb7 100644 --- a/MqttClient.cpp +++ b/MqttClient.cpp @@ -92,18 +92,39 @@ String MqttConfig::exec(String params) { if ((tokenStr != 0) && (*tokenStr != 0) && (addressStr != 0) && (*addressStr != 0) && (periodStr != 0) && (*periodStr != 0)) { - *out << "Token: " << tokenStr << endl; - *out << "Address: " << addressStr << endl; - *out << "Period: " << periodStr << endl; + 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)); - res = "done"; + 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"; + } } else { *out << "not enough arguments" << endl; res = "failure"; } - // configWrite(CONFIG_DEVICES, sizeof(mc->m_mbusDevTuple), (char*)mc->m_mbusDevTuple); } else if (params.startsWith("del ")) { // delete an mbus client, params: index int space = params.indexOf(' ');