Commit 6d71a41e authored by Eric Duminil's avatar Eric Duminil
Browse files

Refactor

parent 3546cc81
...@@ -104,7 +104,7 @@ namespace sensor { ...@@ -104,7 +104,7 @@ namespace sensor {
Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF."); Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF.");
sensor_commands::defineCallback("co2", setCO2forDebugging, " 1500 (Sets co2 level, for debugging purposes)"); sensor_commands::defineCallback("co2", setCO2forDebugging, " 1500 (Sets co2 level, for debugging purposes)");
sensor_commands::defineCallback("timer", setTimer, " 30 (Sets measurement interval)"); sensor_commands::defineCallback("timer", setTimer, " 30 (Sets measurement interval, in s)");
sensor_commands::defineCallback("calibrate", calibrateSensorToSpecificPPM, sensor_commands::defineCallback("calibrate", calibrateSensorToSpecificPPM,
" 600 (Starts calibration process, to given ppm)"); " 600 (Starts calibration process, to given ppm)");
sensor_commands::defineCallback("calibrate!", calibrateSensorRightNow, " 600 (Calibrates right now, to given ppm)"); sensor_commands::defineCallback("calibrate!", calibrateSensorRightNow, " 600 (Calibrates right now, to given ppm)");
......
...@@ -125,7 +125,7 @@ namespace csv_writer { ...@@ -125,7 +125,7 @@ namespace csv_writer {
showFilesystemContent(); showFilesystemContent();
Serial.println(); Serial.println();
sensor_commands::defineCallback("csv", setCSVinterval, " 60 (Sets CSV writing interval)"); sensor_commands::defineCallback("csv", setCSVinterval, " 60 (Sets CSV writing interval, in s)");
// sensor_commands::defineCallback("format_filesystem", FS_LIB.format); // sensor_commands::defineCallback("format_filesystem", FS_LIB.format);
} }
......
...@@ -44,7 +44,7 @@ namespace mqtt { ...@@ -44,7 +44,7 @@ namespace mqtt {
// mqttClient.setSocketTimeout(config::mqtt_timeout); //NOTE: somehow doesn't seem to have any effect on connect() // mqttClient.setSocketTimeout(config::mqtt_timeout); //NOTE: somehow doesn't seem to have any effect on connect()
mqttClient.setServer(config::mqtt_server, config::mqtt_port); mqttClient.setServer(config::mqtt_server, config::mqtt_port);
sensor_commands::defineCallback("mqtt", setMQTTinterval, " 60 (Sets MQTT sending interval)"); sensor_commands::defineCallback("mqtt", setMQTTinterval, " 60 (Sets MQTT sending interval, in s)");
} }
void publish(const String &timestamp, int16_t co2, float temperature, float humidity) { void publish(const String &timestamp, int16_t co2, float temperature, float humidity) {
......
...@@ -25,11 +25,7 @@ namespace sensor_commands { ...@@ -25,11 +25,7 @@ namespace sensor_commands {
} }
} }
bool startsWith(const char *a, const char *b) { uint8_t parseCommand(const char *command, char *function_name, int32_t &parameter) {
return !strncmp(a, b, strlen(b));
}
bool parseCommand(const char *command, char *function_name, int32_t &parameter) {
char split_command[MAX_COMMAND_SIZE]; char split_command[MAX_COMMAND_SIZE];
strlcpy(split_command, command, MAX_COMMAND_SIZE); strlcpy(split_command, command, MAX_COMMAND_SIZE);
Serial.print(F("Received : '")); Serial.print(F("Received : '"));
...@@ -44,21 +40,22 @@ namespace sensor_commands { ...@@ -44,21 +40,22 @@ namespace sensor_commands {
} }
strlcpy(function_name, part1, MAX_COMMAND_SIZE); strlcpy(function_name, part1, MAX_COMMAND_SIZE);
arg = strtok(NULL, " "); arg = strtok(NULL, " ");
uint8_t code = 0;
if (arg) { if (arg) {
char *end; char *end;
parameter = strtol(arg, &end, 10); parameter = strtol(arg, &end, 10);
if (*end) { if (*end) {
// Second argument isn't a number // Second argument isn't a number
parameter = -9999; code = 2;
} }
} else { } else {
parameter = -9999; // No parameter
code = 2;
} }
return 0; return code;
} }
void listAvailableCallbacks() { void listAvailableCallbacks() {
Serial.println(F("Message not supported. Available commands :"));
for (uint8_t i = 0; i < callbacks_count; i++) { for (uint8_t i = 0; i < callbacks_count; i++) {
Serial.print(" "); Serial.print(" ");
Serial.print(callbacks[i].name); Serial.print(callbacks[i].name);
...@@ -70,7 +67,7 @@ namespace sensor_commands { ...@@ -70,7 +67,7 @@ namespace sensor_commands {
void run(const char *command) { void run(const char *command) {
char function_name[MAX_COMMAND_SIZE]; char function_name[MAX_COMMAND_SIZE];
int32_t parameter; int32_t parameter = 0;
parseCommand(command, function_name, parameter); parseCommand(command, function_name, parameter);
for (uint8_t i = 0; i < callbacks_count; i++) { for (uint8_t i = 0; i < callbacks_count; i++) {
...@@ -84,6 +81,7 @@ namespace sensor_commands { ...@@ -84,6 +81,7 @@ namespace sensor_commands {
return; return;
} }
} }
Serial.println(F("Message not supported. Available commands :"));
listAvailableCallbacks(); listAvailableCallbacks();
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment