Browse Source

Wrap embeddis commands and move feedback to caller

refactor-terminal
Xose Pérez 6 years ago
parent
commit
7ad0e3a02b
13 changed files with 124 additions and 104 deletions
  1. +1
    -1
      code/espurna/config/prototypes.h
  2. +8
    -10
      code/espurna/eeprom.ino
  3. +3
    -3
      code/espurna/homeassistant.ino
  4. +2
    -2
      code/espurna/i2c.ino
  5. +5
    -5
      code/espurna/light.ino
  6. +1
    -1
      code/espurna/mqtt.ino
  7. +1
    -1
      code/espurna/nofuss.ino
  8. +7
    -4
      code/espurna/ota.ino
  9. +3
    -3
      code/espurna/relay.ino
  10. +6
    -6
      code/espurna/rfbridge.ino
  11. +56
    -42
      code/espurna/sensor.ino
  12. +25
    -20
      code/espurna/terminal.ino
  13. +6
    -6
      code/espurna/wifi.ino

+ 1
- 1
code/espurna/config/prototypes.h View File

@ -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


+ 8
- 10
code/espurna/eeprom.ino View File

@ -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;
});
}


+ 3
- 3
code/espurna/homeassistant.ino View File

@ -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;
});
}


+ 2
- 2
code/espurna/i2c.ino View File

@ -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;
});
}


+ 5
- 5
code/espurna/light.ino View File

@ -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;
});
}


+ 1
- 1
code/espurna/mqtt.ino View File

@ -353,7 +353,7 @@ void _mqttInitCommands() {
terminalRegisterCommand(F("MQTT.RESET"), [](Embedis* e) {
_mqttConfigure();
mqttDisconnect();
DEBUG_MSG_P(PSTR("+OK\n"));
return true;
});
}


+ 1
- 1
code/espurna/nofuss.ino View File

@ -75,8 +75,8 @@ void _nofussConfigure() {
void _nofussInitCommands() {
terminalRegisterCommand(F("NOFUSS"), [](Embedis* e) {
DEBUG_MSG_P(PSTR("+OK\n"));
nofussRun();
return true;
});
}


+ 7
- 4
code/espurna/ota.ino View File

@ -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;
});
}


+ 3
- 3
code/espurna/relay.ino View File

@ -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;
});
}


+ 6
- 6
code/espurna/rfbridge.ino View File

@ -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;
});


+ 56
- 42
code/espurna/sensor.ino View File

@ -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++) {


+ 25
- 20
code/espurna/terminal.ino View File

@ -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() {


+ 6
- 6
code/espurna/wifi.ino View File

@ -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;
});
}


Loading…
Cancel
Save