@@ -20,13 +20,14 @@ Cloud4RPi::Cloud4RPi(const String &_deviceToken, const String &_server, int _por
2020 jsonBufferSize(JSON_BUFFER_SIZE),
2121 variables(new C4RVariableStorage()),
2222 diagnostics(new C4RVariableStorage()) {
23+
2324}
2425
2526Cloud4RPi::~Cloud4RPi () {
2627 if (mqttClient != NULL ) {
2728 delete mqttClient;
2829 mqttClient = NULL ;
29- };
30+ }
3031
3132 if (variables != NULL ) {
3233 delete variables;
@@ -90,15 +91,15 @@ bool Cloud4RPi::ensureConnection(int maxReconnectAttempts, int reconnectTimeout)
9091 return true ;
9192}
9293
93- void Cloud4RPi::declareBoolVariable (const String& varName, C4R_HANDLER_SIGNATURE ) {
94+ void Cloud4RPi::declareBoolVariable (const String& varName, C4R_BOOL_HANDLER_SIGNATURE ) {
9495 if (!isVariableExists (varName)) {
9596 variables->declare <bool >(varName, VAR_TYPE_BOOL, cmdHandler);
9697 }
9798}
9899
99- void Cloud4RPi::declareNumericVariable (const String& varName) {
100+ void Cloud4RPi::declareNumericVariable (const String& varName, C4R_NUMERIC_HANDLER_SIGNATURE ) {
100101 if (!isVariableExists (varName)) {
101- variables->declare <double >(varName, VAR_TYPE_NUMERIC);
102+ variables->declare <double >(varName, VAR_TYPE_NUMERIC, cmdHandler );
102103 }
103104}
104105
@@ -262,21 +263,32 @@ void Cloud4RPi::mqttCallback(char* topic, byte* payload, unsigned int length) {
262263 return ;
263264 }
264265 for (JsonObject::iterator item=root.begin (); item!=root.end (); ++item) {
265- String key = item->key ;
266- bool value = item->value ; // TODO other types!
267- this ->onCommand (key, value);
266+ this ->onCommand (item->key , item->value );
268267 }
269268}
270- void Cloud4RPi::onCommand (const String& command, bool value) {
271- if (variables->canHandleCommand (command)) {
272- bool newValue = variables->handleCommand <bool >(command, value);
273- setVariable (command, newValue);
274- publishData ();
275- } else {
269+
270+ void Cloud4RPi::onCommand (const String& command, JsonVariant value) {
271+ C4RVariableBase* item = variables->find (command);
272+ if (!item) {
273+ CLOUD4RPI_PRINT (" Variable ''" );
274+ CLOUD4RPI_PRINT (command);
275+ CLOUD4RPI_PRINTLN (" ' not found." );
276+ return ;
277+ }
278+ if (!item->hasHandler ()) {
276279 CLOUD4RPI_PRINT (" No handler for '" );
277280 CLOUD4RPI_PRINT (command);
278281 CLOUD4RPI_PRINTLN (" ' command." );
282+ return ;
283+ }
284+ String type = item->getType ();
285+ if (type == VAR_TYPE_BOOL) {
286+ variables->handleCommand <bool >(command, value.as <bool >());
287+ }
288+ if (type == VAR_TYPE_NUMERIC) {
289+ variables->handleCommand <double >(command, value.as <double >());
279290 }
291+ publishData ();
280292}
281293
282294void Cloud4RPi::printLogo () {
0 commit comments