Browse Source

Helper terminal methods terminalOK and terminalError

refactor-terminal
Xose Pérez 5 years ago
parent
commit
d47ae2e4a4
13 changed files with 70 additions and 62 deletions
  1. +1
    -1
      code/espurna/config/arduino.h
  2. +6
    -6
      code/espurna/eeprom.ino
  3. +3
    -3
      code/espurna/homeassistant.ino
  4. +2
    -2
      code/espurna/i2c.ino
  5. +6
    -6
      code/espurna/light.ino
  6. +1
    -1
      code/espurna/mqtt.ino
  7. +1
    -1
      code/espurna/nofuss.ino
  8. +2
    -2
      code/espurna/ota.ino
  9. +2
    -2
      code/espurna/relay.ino
  10. +4
    -4
      code/espurna/rfbridge.ino
  11. +9
    -9
      code/espurna/sensor.ino
  12. +27
    -19
      code/espurna/terminal.ino
  13. +6
    -6
      code/espurna/wifi.ino

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

@ -146,7 +146,7 @@
//#define SPIFFS_SUPPORT 1
//#define SSDP_SUPPORT 1
//#define TELNET_SUPPORT 0
#define TERMINAL_SUPPORT 0
//#define TERMINAL_SUPPORT 0
//#define THINGSPEAK_SUPPORT 0
//#define UART_MQTT_SUPPORT 1
//#define WEB_SUPPORT 0


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

@ -65,13 +65,13 @@ 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"));
terminalOK();
});
terminalRegisterCommand(F("EEPROM.COMMIT"), [](Embedis* e) {
const bool res = _eepromCommit();
if (res) {
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
} else {
DEBUG_MSG_P(PSTR("-ERROR\n"));
}
@ -79,22 +79,22 @@ void _eepromInitCommands() {
terminalRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) {
EEPROMr.dump(terminalSerial());
DEBUG_MSG_P(PSTR("\n+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("FLASH.DUMP"), [](Embedis* e) {
if (e->argc < 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
return;
}
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"));
terminalError(F("Sector out of range"));
return;
}
EEPROMr.dump(terminalSerial(), sector);
DEBUG_MSG_P(PSTR("\n+OK\n"));
terminalOK();
});
}


+ 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"));
terminalOK();
});
terminalRegisterCommand(F("HA.SEND"), [](Embedis* e) {
@ -278,7 +278,7 @@ void _haInitCommands() {
#if WEB_SUPPORT
wsSend(_haWebSocketOnSend);
#endif
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("HA.CLEAR"), [](Embedis* e) {
@ -287,7 +287,7 @@ void _haInitCommands() {
#if WEB_SUPPORT
wsSend(_haWebSocketOnSend);
#endif
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
}


+ 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"));
terminalOK();
});
terminalRegisterCommand(F("I2C.CLEAR"), [](Embedis* e) {
i2cClearBus();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
}


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

@ -989,12 +989,12 @@ void _lightInitCommands() {
lightUpdate(true, true);
}
DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness());
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("CHANNEL"), [](Embedis* e) {
if (e->argc < 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
}
int id = String(e->argv[1]).toInt();
if (e->argc > 2) {
@ -1003,7 +1003,7 @@ void _lightInitCommands() {
lightUpdate(true, true);
}
DEBUG_MSG_P(PSTR("Channel #%d: %d\n"), id, lightChannel(id));
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("COLOR"), [](Embedis* e) {
@ -1013,7 +1013,7 @@ void _lightInitCommands() {
lightUpdate(true, true);
}
DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("KELVIN"), [](Embedis* e) {
@ -1023,7 +1023,7 @@ void _lightInitCommands() {
lightUpdate(true, true);
}
DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("MIRED"), [](Embedis* e) {
@ -1033,7 +1033,7 @@ void _lightInitCommands() {
lightUpdate(true, true);
}
DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
}


+ 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"));
terminalOK();
});
}


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

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


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

@ -190,9 +190,9 @@ void _otaInitCommands() {
terminalRegisterCommand(F("OTA"), [](Embedis* e) {
if (e->argc < 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
} else {
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
String url = String(e->argv[1]);
_otaFrom(url);
}


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

@ -985,7 +985,7 @@ void _relayInitCommands() {
terminalRegisterCommand(F("RELAY"), [](Embedis* e) {
if (e->argc < 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
return;
}
int id = String(e->argv[1]).toInt();
@ -1008,7 +1008,7 @@ void _relayInitCommands() {
DEBUG_MSG_P(PSTR("Pulse time: %d\n"), _relays[id].pulse_ms);
}
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
}


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

@ -608,7 +608,7 @@ void _rfbInitCommands() {
terminalRegisterCommand(F("LEARN"), [](Embedis* e) {
if (e->argc < 3) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
return;
}
@ -622,14 +622,14 @@ void _rfbInitCommands() {
rfbLearn(id, status == 1);
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("FORGET"), [](Embedis* e) {
if (e->argc < 3) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
return;
}
@ -643,7 +643,7 @@ void _rfbInitCommands() {
rfbForget(id, status == 1);
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});


+ 9
- 9
code/espurna/sensor.ino View File

@ -282,7 +282,7 @@ void _sensorInitCommands() {
magnitude.global
);
}
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
#if PZEM004T_SUPPORT
terminalRegisterCommand(F("PZ.ADDRESS"), [](Embedis* e) {
@ -292,23 +292,23 @@ void _sensorInitCommands() {
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"));
terminalOK();
} 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"));
terminalOK();
}
} else {
DEBUG_MSG_P(PSTR("-ERROR: Invalid address argument\n"));
terminalError(F("Invalid address argument"));
}
} else {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
}
});
terminalRegisterCommand(F("PZ.RESET"), [](Embedis* e) {
if(e->argc > 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
} else {
unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0;
unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount();
@ -318,12 +318,12 @@ 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"));
terminalOK();
}
});
terminalRegisterCommand(F("PZ.VALUE"), [](Embedis* e) {
if(e->argc > 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
} else {
unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0;
unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount();
@ -337,7 +337,7 @@ void _sensorInitCommands() {
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"));
terminalOK();
}
});
#endif


+ 27
- 19
code/espurna/terminal.ino View File

@ -83,17 +83,17 @@ void _terminalInitCommand() {
terminalRegisterCommand(F("CRASH"), [](Embedis* e) {
crashDump();
crashClear();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
#endif
terminalRegisterCommand(F("COMMANDS"), [](Embedis* e) {
_terminalHelpCommand();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("ERASE.CONFIG"), [](Embedis* e) {
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
resetReason(CUSTOM_RESET_TERMINAL);
_eepromCommit();
ESP.eraseConfig();
@ -102,17 +102,17 @@ void _terminalInitCommand() {
terminalRegisterCommand(F("FACTORY.RESET"), [](Embedis* e) {
resetSettings();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("GPIO"), [](Embedis* e) {
if (e->argc < 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
return;
}
int pin = String(e->argv[1]).toInt();
//if (!gpioValid(pin)) {
// DEBUG_MSG_P(PSTR("-ERROR: Invalid GPIO\n"));
// terminalError(F("Invalid GPIO"));
// return;
//}
if (e->argc > 2) {
@ -120,37 +120,37 @@ 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"));
terminalOK();
});
terminalRegisterCommand(F("HEAP"), [](Embedis* e) {
infoMemory("Heap", getInitialFreeHeap(), getFreeHeap());
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("STACK"), [](Embedis* e) {
infoMemory("Stack", 4096, getFreeStack());
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("HELP"), [](Embedis* e) {
_terminalHelpCommand();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("INFO"), [](Embedis* e) {
info();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("KEYS"), [](Embedis* e) {
_terminalKeysCommand();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("GET"), [](Embedis* e) {
if (e->argc < 2) {
DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
terminalError(F("Wrong arguments"));
return;
}
@ -165,28 +165,28 @@ void _terminalInitCommand() {
DEBUG_MSG_P(PSTR("> %s => \"%s\"\n"), key.c_str(), value.c_str());
}
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("RELOAD"), [](Embedis* e) {
espurnaReload();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("RESET"), [](Embedis* e) {
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
deferredReset(100, CUSTOM_RESET_TERMINAL);
});
terminalRegisterCommand(F("RESET.SAFE"), [](Embedis* e) {
EEPROMr.write(EEPROM_CRASH_COUNTER, SYSTEM_CHECK_MAX);
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
deferredReset(100, CUSTOM_RESET_TERMINAL);
});
terminalRegisterCommand(F("UPTIME"), [](Embedis* e) {
DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime());
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("CONFIG"), [](Embedis* e) {
@ -196,7 +196,7 @@ void _terminalInitCommand() {
String output;
root.printTo(output);
DEBUG_MSG(output.c_str());
DEBUG_MSG_P(PSTR("\n+OK\n"));
});
#if not SETTINGS_AUTOSAVE
@ -249,6 +249,14 @@ void terminalRegisterCommand(const String& name, void (*call)(Embedis*)) {
Embedis::command(name, call);
};
void terminalOK() {
DEBUG_MSG_P(PSTR("+OK\n"));
}
void terminalError(const String& error) {
DEBUG_MSG_P(PSTR("-ERROR: %s\n"), error.c_str());
}
void terminalSetup() {
_serial.callback([](uint8_t ch) {


+ 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"));
terminalOK();
});
terminalRegisterCommand(F("WIFI.RESET"), [](Embedis* e) {
_wifiConfigure();
wifiDisconnect();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
terminalRegisterCommand(F("WIFI.AP"), [](Embedis* e) {
wifiStartAP();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
#if defined(JUSTWIFI_ENABLE_WPS)
terminalRegisterCommand(F("WIFI.WPS"), [](Embedis* e) {
wifiStartWPS();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
#endif // defined(JUSTWIFI_ENABLE_WPS)
#if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
terminalRegisterCommand(F("WIFI.SMARTCONFIG"), [](Embedis* e) {
wifiStartSmartConfig();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
#endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
terminalRegisterCommand(F("WIFI.SCAN"), [](Embedis* e) {
_wifiScan();
DEBUG_MSG_P(PSTR("+OK\n"));
terminalOK();
});
}


Loading…
Cancel
Save