Skip to content

Commit e996a86

Browse files
authored
support to numeric-value commands (#6)
Support to numeric-value commands
1 parent 9b5b7a8 commit e996a86

File tree

5 files changed

+69
-38
lines changed

5 files changed

+69
-38
lines changed

examples/ESP8266/ESP8266.ino

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ WiFiClient wifiClient;
2424

2525
void ensureWiFiConnection();
2626
bool onLEDCommand(bool value);
27+
double onDesiredTempCommand(double value);
2728

2829
void setup() {
2930
Serial.begin(9600);
@@ -39,6 +40,8 @@ void setup() {
3940
c4r.declareBoolVariable("LED On", onLEDCommand);
4041
c4r.declareNumericVariable("Uptime");
4142
c4r.declareStringVariable("State");
43+
c4r.declareNumericVariable("DesiredTemp", onDesiredTempCommand);
44+
c4r.setVariable("DesiredTemp", 22.5f);
4245

4346
c4r.publishConfig();
4447

@@ -64,13 +67,15 @@ void loop() {
6467
c4r.publishData();
6568
lastDataSent = currentMillis;
6669

67-
Serial.println("Variables state: ");
68-
Serial.print("LED On = ");
69-
Serial.println(c4r.getBoolValue("LED On"));
70-
Serial.print("Uptime = ");
70+
Serial.println("Variables state:");
71+
Serial.print(" LED = ");
72+
Serial.println(c4r.getBoolValue("LED On") ? "On" : "Off");
73+
Serial.print(" Uptime = ");
7174
Serial.println(c4r.getNumericValue("Uptime"), 0);
72-
Serial.print("State = ");
75+
Serial.print(" State = ");
7376
Serial.println(newEvent);
77+
Serial.print(" Desired Temperature = ");
78+
Serial.println(c4r.getNumericValue("DesiredTemp"), 1);
7479
}
7580

7681
if (currentMillis - lastDiagSent >= diagSendingInterval) {
@@ -85,7 +90,7 @@ void loop() {
8590
c4r.loop();
8691
Serial.print(".");
8792
delay(1000);
88-
}
93+
}
8994
}
9095

9196
void ensureWiFiConnection() {
@@ -100,13 +105,22 @@ void ensureWiFiConnection() {
100105
Serial.print(WiFi.localIP());
101106
WiFi.printDiag(Serial);
102107

103-
// Received signal strength:
104-
long rssi = WiFi.RSSI();
108+
long rssi = WiFi.RSSI(); // Received signal strength
105109
Serial.print("RSSI:");
106-
Serial.println(rssi); }
110+
Serial.println(rssi);
111+
}
107112
}
108113

109114
bool onLEDCommand(bool value) {
115+
Serial.print("LED state set to ");
116+
Serial.println(value);
110117
digitalWrite(ledPin, value ? LOW : HIGH);
111118
return !digitalRead(ledPin);
112119
}
120+
121+
double onDesiredTempCommand(double value) {
122+
Serial.print("Desired temperature set to ");
123+
Serial.println(value, 1);
124+
// Control the heater
125+
return value;
126+
}

src/Cloud4RPi.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2526
Cloud4RPi::~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

282294
void Cloud4RPi::printLogo() {

src/Cloud4RPi.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ friend class C4RMqttCallback;
5757
bool connected();
5858
bool loop();
5959

60-
void declareBoolVariable(const String& varName, C4R_HANDLER_SIGNATURE = NULL);
61-
void declareNumericVariable(const String& varName);
60+
void declareBoolVariable(const String& varName, C4R_BOOL_HANDLER_SIGNATURE = NULL);
61+
void declareNumericVariable(const String& varName, C4R_NUMERIC_HANDLER_SIGNATURE = NULL);
6262
void declareStringVariable(const String& varName);
6363
void declareDiagVariable(const String& varName);
6464

@@ -95,6 +95,6 @@ friend class C4RMqttCallback;
9595
bool publishCore(JsonObject& root, const String& subTopic);
9696
JsonVariant getVariantValue(const String& name, const String& type);
9797
void mqttCallback(char* topic, byte* payload, unsigned int length);
98-
void onCommand(const String& command, bool value);
98+
void onCommand(const String& command, JsonVariant value);
9999
};
100100
#endif // _CLOUD4RPI_H

src/Cloud4RPiVar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
C4RVariableBase::C4RVariableBase(const String& _name) :
44
name(_name) {
5+
56
}
67

78
String C4RVariableBase::getName() {
@@ -65,6 +66,7 @@ void C4RVariableList::add(C4RVariableBase* _variable) {
6566

6667
C4RVariableStorage::C4RVariableStorage() :
6768
list(new C4RVariableList()) {
69+
6870
}
6971

7072
C4RVariableStorage::~C4RVariableStorage() {

src/Cloud4RPiVar.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
#ifdef ESP8266
88
#include <functional>
9-
#define C4R_HANDLER_SIGNATURE std::function<bool(bool)> cmdHandler
9+
#define C4R_BOOL_HANDLER_SIGNATURE std::function<bool(bool)> cmdHandler
10+
#define C4R_NUMERIC_HANDLER_SIGNATURE std::function<double(double)> cmdHandler
11+
#define C4R_T_HANDLER_SIGNATURE std::function<T(T)> cmdHandler
1012
#else
11-
#define C4R_HANDLER_SIGNATURE bool (*cmdHandler)(bool)
13+
#define C4R_BOOL_HANDLER_SIGNATURE bool (*cmdHandler)(bool)
14+
#define C4R_NUMERIC_HANDLER_SIGNATURE double (*cmdHandler)(double)
15+
#define C4R_T_HANDLER_SIGNATURE (*cmdHandler)(T)
1216
#endif
1317

1418

@@ -37,7 +41,7 @@ class C4RVariable : public C4RVariableBase {
3741
private:
3842
String type;
3943
T value;
40-
C4R_HANDLER_SIGNATURE;
44+
C4R_T_HANDLER_SIGNATURE;
4145
public:
4246
C4RVariable(const String& _name, const String& _type, T _value) :
4347
C4RVariableBase(_name),
@@ -49,12 +53,12 @@ class C4RVariable : public C4RVariableBase {
4953
T getValue() { return value; };
5054
void setValue(T _value) { value = _value; }
5155
bool hasHandler() { return cmdHandler != NULL; };
52-
void setHandler(C4R_HANDLER_SIGNATURE) { this->cmdHandler = cmdHandler; }
53-
T handleCommand(T value) {
56+
void setHandler(C4R_T_HANDLER_SIGNATURE) { this->cmdHandler = cmdHandler; }
57+
void handleCommand(T value) {
5458
if (cmdHandler) {
55-
return cmdHandler(value);
59+
T newValue = cmdHandler(value);
60+
setValue(newValue);
5661
}
57-
return NULL;
5862
}
5963
};
6064

@@ -89,7 +93,7 @@ class C4RVariableStorage {
8993
bool exists(const String& varName);
9094

9195
template<typename T>
92-
void declare(const String& varName, const String& varType, C4R_HANDLER_SIGNATURE = NULL) {
96+
void declare(const String& varName, const String& varType, C4R_T_HANDLER_SIGNATURE = NULL) {
9397
C4RVariable<T>* item = new C4RVariable<T>(varName, varType, T());
9498
item->setHandler(cmdHandler);
9599
list->add(item);
@@ -114,15 +118,14 @@ class C4RVariableStorage {
114118
}
115119
}
116120

117-
bool canHandleCommand(const String& command) {
118-
C4RVariableBase* var = list->find(command);
119-
return var && var->hasHandler();
121+
C4RVariableBase* find(const String& name) {
122+
return list->find(name);
120123
}
121124

122125
template<typename T>
123-
T handleCommand(const String& command, T _value) { // optimize double find
126+
void handleCommand(const String& command, T _value) { // optimize double find
124127
C4RVariable<T>* var = (C4RVariable<T>*)list->find(command);
125-
return var->handleCommand(_value);
128+
var->handleCommand(_value);
126129
}
127130
};
128131
#endif // _CLOUD4RPIVAR_H

0 commit comments

Comments
 (0)