diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 693ad4a8..71802686 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -147,7 +147,7 @@ bool settingsRestoreJson(JsonObject& data); // Terminal // ----------------------------------------------------------------------------- #if TERMINAL_SUPPORT - void terminalRegisterCommand(const String& name, void (*call)(Embedis*)); + void terminalRegisterCommand(const String& name, bool (*call)(Embedis*)); void terminalInject(void *data, size_t len); Stream & terminalSerial(); #endif diff --git a/code/espurna/eeprom.ino b/code/espurna/eeprom.ino index 36853e30..cdb0ed5e 100644 --- a/code/espurna/eeprom.ino +++ b/code/espurna/eeprom.ino @@ -65,36 +65,34 @@ void _eepromInitCommands() { DEBUG_MSG_P(PSTR("[MAIN] Commits done: %lu\n"), _eeprom_commit_count); DEBUG_MSG_P(PSTR("[MAIN] Last result: %s\n"), _eeprom_last_commit_result ? "OK" : "ERROR"); } - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("EEPROM.COMMIT"), [](Embedis* e) { const bool res = _eepromCommit(); - if (res) { - DEBUG_MSG_P(PSTR("+OK\n")); - } else { - DEBUG_MSG_P(PSTR("-ERROR\n")); - } + if (res) return true; + DEBUG_MSG_P(PSTR("-ERROR\n")); + return false; }); terminalRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) { EEPROMr.dump(terminalSerial()); - DEBUG_MSG_P(PSTR("\n+OK\n")); + return true; }); terminalRegisterCommand(F("FLASH.DUMP"), [](Embedis* e) { if (e->argc < 2) { DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; + return false; } uint32_t sector = String(e->argv[1]).toInt(); uint32_t max = ESP.getFlashChipSize() / SPI_FLASH_SEC_SIZE; if (sector >= max) { DEBUG_MSG_P(PSTR("-ERROR: Sector out of range\n")); - return; + return false; } EEPROMr.dump(terminalSerial(), sector); - DEBUG_MSG_P(PSTR("\n+OK\n")); + return true; }); } diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index 300f237b..61fbaecd 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -269,7 +269,7 @@ void _haInitCommands() { terminalRegisterCommand(F("HA.CONFIG"), [](Embedis* e) { DEBUG_MSG(_haGetConfig().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("HA.SEND"), [](Embedis* e) { @@ -278,7 +278,7 @@ void _haInitCommands() { #if WEB_SUPPORT wsSend(_haWebSocketOnSend); #endif - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("HA.CLEAR"), [](Embedis* e) { @@ -287,7 +287,7 @@ void _haInitCommands() { #if WEB_SUPPORT wsSend(_haWebSocketOnSend); #endif - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); } diff --git a/code/espurna/i2c.ino b/code/espurna/i2c.ino index b9e6a84b..4c2d590a 100644 --- a/code/espurna/i2c.ino +++ b/code/espurna/i2c.ino @@ -355,12 +355,12 @@ void i2cCommands() { terminalRegisterCommand(F("I2C.SCAN"), [](Embedis* e) { i2cScan(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("I2C.CLEAR"), [](Embedis* e) { i2cClearBus(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); } diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 738d1d12..3012e42d 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -978,7 +978,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("CHANNEL"), [](Embedis* e) { @@ -992,7 +992,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Channel #%d: %d\n"), id, lightChannel(id)); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("COLOR"), [](Embedis* e) { @@ -1002,7 +1002,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("KELVIN"), [](Embedis* e) { @@ -1012,7 +1012,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("MIRED"), [](Embedis* e) { @@ -1022,7 +1022,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); } diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 12e8b99e..0fa5f5d5 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -353,7 +353,7 @@ void _mqttInitCommands() { terminalRegisterCommand(F("MQTT.RESET"), [](Embedis* e) { _mqttConfigure(); mqttDisconnect(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); } diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index ad9fd4d2..6cccebf1 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -75,8 +75,8 @@ void _nofussConfigure() { void _nofussInitCommands() { terminalRegisterCommand(F("NOFUSS"), [](Embedis* e) { - DEBUG_MSG_P(PSTR("+OK\n")); nofussRun(); + return true; }); } diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index bf366f8c..d0cc465e 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -189,13 +189,16 @@ void _otaFrom(String url) { void _otaInitCommands() { terminalRegisterCommand(F("OTA"), [](Embedis* e) { + if (e->argc < 2) { DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - } else { - DEBUG_MSG_P(PSTR("+OK\n")); - String url = String(e->argv[1]); - _otaFrom(url); + return false } + + String url = String(e->argv[1]); + _otaFrom(url); + return true; + }); } diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 338ecbba..bebb3a18 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -986,12 +986,12 @@ void _relayInitCommands() { terminalRegisterCommand(F("RELAY"), [](Embedis* e) { if (e->argc < 2) { DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; + return false; } int id = String(e->argv[1]).toInt(); if (id >= relayCount()) { DEBUG_MSG_P(PSTR("-ERROR: Wrong relayID (%d)\n"), id); - return; + return false; } if (e->argc > 2) { @@ -1008,7 +1008,7 @@ void _relayInitCommands() { DEBUG_MSG_P(PSTR("Pulse time: %d\n"), _relays[id].pulse_ms); } - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); } diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index 3524fb06..fc24ff09 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -609,20 +609,20 @@ void _rfbInitCommands() { if (e->argc < 3) { DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; + return false; } int id = String(e->argv[1]).toInt(); if (id >= relayCount()) { DEBUG_MSG_P(PSTR("-ERROR: Wrong relayID (%d)\n"), id); - return; + return false; } int status = String(e->argv[2]).toInt(); rfbLearn(id, status == 1); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); @@ -630,20 +630,20 @@ void _rfbInitCommands() { if (e->argc < 3) { DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; + return false; } int id = String(e->argv[1]).toInt(); if (id >= relayCount()) { DEBUG_MSG_P(PSTR("-ERROR: Wrong relayID (%d)\n"), id); - return; + return false; } int status = String(e->argv[2]).toInt(); rfbForget(id, status == 1); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index cce02dda..af06c9ce 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -271,6 +271,7 @@ void _sensorAPISetup() { #if TERMINAL_SUPPORT void _sensorInitCommands() { + terminalRegisterCommand(F("MAGNITUDES"), [](Embedis* e) { for (unsigned char i=0; i<_magnitudes.size(); i++) { sensor_magnitude_t magnitude = _magnitudes[i]; @@ -282,34 +283,40 @@ void _sensorInitCommands() { magnitude.global ); } - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); + #if PZEM004T_SUPPORT - terminalRegisterCommand(F("PZ.ADDRESS"), [](Embedis* e) { - if (e->argc == 1) { - DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n")); - unsigned char dev_count = pzem004t_sensor->getAddressesCount(); - for(unsigned char dev = 0; dev < dev_count; dev++) { - DEBUG_MSG_P(PSTR("Device %d/%s\n"), dev, pzem004t_sensor->getAddress(dev).c_str()); - } - DEBUG_MSG_P(PSTR("+OK\n")); - } else if(e->argc == 2) { - IPAddress addr; - if (addr.fromString(String(e->argv[1]))) { - if(pzem004t_sensor->setDeviceAddress(&addr)) { - DEBUG_MSG_P(PSTR("+OK\n")); + + terminalRegisterCommand(F("PZ.ADDRESS"), [](Embedis* e) { + if (e->argc == 1) { + DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n")); + unsigned char dev_count = pzem004t_sensor->getAddressesCount(); + for(unsigned char dev = 0; dev < dev_count; dev++) { + DEBUG_MSG_P(PSTR("Device %d/%s\n"), dev, pzem004t_sensor->getAddress(dev).c_str()); + } + return true; + } else if(e->argc == 2) { + IPAddress addr; + if (addr.fromString(String(e->argv[1]))) { + if (pzem004t_sensor->setDeviceAddress(&addr)) { + return true; + } } - } else { DEBUG_MSG_P(PSTR("-ERROR: Invalid address argument\n")); + } else { + DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); } - } else { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - } - }); - terminalRegisterCommand(F("PZ.RESET"), [](Embedis* e) { - if(e->argc > 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - } else { + return false; + }); + + terminalRegisterCommand(F("PZ.RESET"), [](Embedis* e) { + + if (e->argc > 2) { + DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + return false; + } + unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0; unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount(); DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n")); @@ -318,32 +325,39 @@ void _sensorInitCommands() { setSetting("pzEneTotal", dev, offset); DEBUG_MSG_P(PSTR("Device %d/%s - Offset: %s\n"), dev, pzem004t_sensor->getAddress(dev).c_str(), String(offset).c_str()); } - DEBUG_MSG_P(PSTR("+OK\n")); - } - }); - terminalRegisterCommand(F("PZ.VALUE"), [](Embedis* e) { - if(e->argc > 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - } else { + return true; + + }); + + terminalRegisterCommand(F("PZ.VALUE"), [](Embedis* e) { + + if(e->argc > 2) { + DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + return false; + } + unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0; unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount(); DEBUG_MSG_P(PSTR("[SENSOR] PZEM004T\n")); for(unsigned char dev = init; dev < limit; dev++) { - DEBUG_MSG_P(PSTR("Device %d/%s - Current: %s Voltage: %s Power: %s Energy: %s\n"), // - dev, - pzem004t_sensor->getAddress(dev).c_str(), - String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_CURRENT_INDEX)).c_str(), - String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_VOLTAGE_INDEX)).c_str(), - String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_POWER_ACTIVE_INDEX)).c_str(), - String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_ENERGY_INDEX)).c_str()); + DEBUG_MSG_P( + PSTR("Device %d/%s - Current: %s Voltage: %s Power: %s Energy: %s\n"), // + dev, + pzem004t_sensor->getAddress(dev).c_str(), + String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_CURRENT_INDEX)).c_str(), + String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_VOLTAGE_INDEX)).c_str(), + String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_POWER_ACTIVE_INDEX)).c_str(), + String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_ENERGY_INDEX)).c_str() + ); } - DEBUG_MSG_P(PSTR("+OK\n")); - } - }); - #endif + return true; + + }); + + #endif // PZEM004T_SUPPORT } -#endif +#endif // TERMINAL_SUPPORT void _sensorTick() { for (unsigned char i=0; i<_sensors.size(); i++) { diff --git a/code/espurna/terminal.ino b/code/espurna/terminal.ino index 6164f38c..27f73723 100644 --- a/code/espurna/terminal.ino +++ b/code/espurna/terminal.ino @@ -83,13 +83,13 @@ void _terminalInitCommand() { terminalRegisterCommand(F("CRASH"), [](Embedis* e) { crashDump(); crashClear(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); #endif terminalRegisterCommand(F("COMMANDS"), [](Embedis* e) { _terminalHelpCommand(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("ERASE.CONFIG"), [](Embedis* e) { @@ -102,13 +102,13 @@ void _terminalInitCommand() { terminalRegisterCommand(F("FACTORY.RESET"), [](Embedis* e) { resetSettings(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("GPIO"), [](Embedis* e) { if (e->argc < 2) { DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; + return false; } int pin = String(e->argv[1]).toInt(); //if (!gpioValid(pin)) { @@ -120,38 +120,39 @@ void _terminalInitCommand() { digitalWrite(pin, state); } DEBUG_MSG_P(PSTR("GPIO %d is %s\n"), pin, digitalRead(pin) == HIGH ? "HIGH" : "LOW"); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("HEAP"), [](Embedis* e) { infoMemory("Heap", getInitialFreeHeap(), getFreeHeap()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("STACK"), [](Embedis* e) { infoMemory("Stack", 4096, getFreeStack()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("HELP"), [](Embedis* e) { _terminalHelpCommand(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("INFO"), [](Embedis* e) { info(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("KEYS"), [](Embedis* e) { _terminalKeysCommand(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("GET"), [](Embedis* e) { + if (e->argc < 2) { DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; + return false; } for (unsigned char i = 1; i < e->argc; i++) { @@ -165,28 +166,29 @@ void _terminalInitCommand() { DEBUG_MSG_P(PSTR("> %s => \"%s\"\n"), key.c_str(), value.c_str()); } - DEBUG_MSG_P(PSTR("+OK\n")); + return true; + }); terminalRegisterCommand(F("RELOAD"), [](Embedis* e) { espurnaReload(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("RESET"), [](Embedis* e) { - DEBUG_MSG_P(PSTR("+OK\n")); deferredReset(100, CUSTOM_RESET_TERMINAL); + return true; }); terminalRegisterCommand(F("RESET.SAFE"), [](Embedis* e) { EEPROMr.write(EEPROM_CRASH_COUNTER, SYSTEM_CHECK_MAX); - DEBUG_MSG_P(PSTR("+OK\n")); deferredReset(100, CUSTOM_RESET_TERMINAL); + return true; }); terminalRegisterCommand(F("UPTIME"), [](Embedis* e) { DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime()); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("CONFIG"), [](Embedis* e) { @@ -196,13 +198,13 @@ void _terminalInitCommand() { String output; root.printTo(output); DEBUG_MSG(output.c_str()); - DEBUG_MSG_P(PSTR("\n+OK\n")); + return true; }); #if not SETTINGS_AUTOSAVE terminalRegisterCommand(F("SAVE"), [](Embedis* e) { eepromCommit(); - DEBUG_MSG_P(PSTR("\n+OK\n")); + return true; }); #endif @@ -245,8 +247,11 @@ Stream & terminalSerial() { return (Stream &) _serial; } -void terminalRegisterCommand(const String& name, void (*call)(Embedis*)) { - Embedis::command(name, call); +void terminalRegisterCommand(const String& name, bool (*callback)(Embedis*)) { + Embedis::command(name, [callback](Embedis* e) { + bool ret = callback(e); + if (ret) DEBUG_MSG_P(PSTR("+OK\n")); + ); }; void terminalSetup() { diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index 78dd79ab..f2448727 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -387,37 +387,37 @@ void _wifiInitCommands() { terminalRegisterCommand(F("WIFI"), [](Embedis* e) { wifiDebug(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("WIFI.RESET"), [](Embedis* e) { _wifiConfigure(); wifiDisconnect(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); terminalRegisterCommand(F("WIFI.AP"), [](Embedis* e) { wifiStartAP(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); #if defined(JUSTWIFI_ENABLE_WPS) terminalRegisterCommand(F("WIFI.WPS"), [](Embedis* e) { wifiStartWPS(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); #endif // defined(JUSTWIFI_ENABLE_WPS) #if defined(JUSTWIFI_ENABLE_SMARTCONFIG) terminalRegisterCommand(F("WIFI.SMARTCONFIG"), [](Embedis* e) { wifiStartSmartConfig(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG) terminalRegisterCommand(F("WIFI.SCAN"), [](Embedis* e) { _wifiScan(); - DEBUG_MSG_P(PSTR("+OK\n")); + return true; }); }