Tasmota/tasmota/tasmota_support/support_command.ino

2678 lines
101 KiB
Arduino
Raw Normal View History

/*
support_command.ino - command support for Tasmota
2021-01-01 12:44:04 +00:00
Copyright (C) 2021 Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const char kTasmotaCommands[] PROGMEM = "|" // No prefix
// SetOptions synonyms
D_SO_WIFINOSLEEP "|"
// Other commands
2022-03-19 11:49:16 +00:00
D_CMND_UPGRADE "|" D_CMND_UPLOAD "|" D_CMND_OTAURL "|" D_CMND_SERIALLOG "|" D_CMND_RESTART "|"
2022-05-04 09:07:24 +01:00
#ifndef FIRMWARE_MINIMAL_ONLY
2022-03-19 11:49:16 +00:00
D_CMND_BACKLOG "|" D_CMND_DELAY "|" D_CMND_POWER "|" D_CMND_STATUS "|" D_CMND_STATE "|" D_CMND_SLEEP "|"
D_CMND_POWERONSTATE "|" D_CMND_PULSETIME "|" D_CMND_BLINKTIME "|" D_CMND_BLINKCOUNT "|" D_CMND_SAVEDATA "|"
D_CMND_SO "|" D_CMND_SETOPTION "|" D_CMND_TEMPERATURE_RESOLUTION "|" D_CMND_HUMIDITY_RESOLUTION "|" D_CMND_PRESSURE_RESOLUTION "|" D_CMND_POWER_RESOLUTION "|"
D_CMND_VOLTAGE_RESOLUTION "|" D_CMND_FREQUENCY_RESOLUTION "|" D_CMND_CURRENT_RESOLUTION "|" D_CMND_ENERGY_RESOLUTION "|" D_CMND_WEIGHT_RESOLUTION "|"
D_CMND_MODULE "|" D_CMND_MODULES "|" D_CMND_GPIO "|" D_CMND_GPIOS "|" D_CMND_TEMPLATE "|" D_CMND_PWM "|" D_CMND_PWMFREQUENCY "|" D_CMND_PWMRANGE "|"
D_CMND_BUTTONDEBOUNCE "|" D_CMND_SWITCHDEBOUNCE "|" D_CMND_SYSLOG "|" D_CMND_LOGHOST "|" D_CMND_LOGPORT "|"
D_CMND_SERIALBUFFER "|" D_CMND_SERIALSEND "|" D_CMND_BAUDRATE "|" D_CMND_SERIALCONFIG "|" D_CMND_SERIALDELIMITER "|"
D_CMND_IPADDRESS "|" D_CMND_NTPSERVER "|" D_CMND_AP "|" D_CMND_SSID "|" D_CMND_PASSWORD "|" D_CMND_HOSTNAME "|" D_CMND_WIFICONFIG "|" D_CMND_WIFI "|" D_CMND_DNSTIMEOUT "|"
2021-04-08 16:57:37 +01:00
D_CMND_DEVICENAME "|" D_CMND_FN "|" D_CMND_FRIENDLYNAME "|" D_CMND_SWITCHMODE "|" D_CMND_INTERLOCK "|" D_CMND_TELEPERIOD "|" D_CMND_RESET "|" D_CMND_TIME "|" D_CMND_TIMEZONE "|" D_CMND_TIMESTD "|"
D_CMND_TIMEDST "|" D_CMND_ALTITUDE "|" D_CMND_LEDPOWER "|" D_CMND_LEDSTATE "|" D_CMND_LEDMASK "|" D_CMND_LEDPWM_ON "|" D_CMND_LEDPWM_OFF "|" D_CMND_LEDPWM_MODE "|"
2022-08-03 06:32:55 +01:00
D_CMND_WIFIPOWER "|" D_CMND_TEMPOFFSET "|" D_CMND_HUMOFFSET "|" D_CMND_SPEEDUNIT "|" D_CMND_GLOBAL_TEMP "|" D_CMND_GLOBAL_HUM"|" D_CMND_GLOBAL_PRESS "|" D_CMND_SWITCHTEXT "|" D_CMND_WIFISCAN "|" D_CMND_WIFITEST "|"
#ifdef USE_I2C
D_CMND_I2CSCAN "|" D_CMND_I2CDRIVER "|"
#endif
#ifdef USE_DEVICE_GROUPS
2021-02-10 03:22:43 +00:00
D_CMND_DEVGROUP_NAME "|"
2020-04-07 19:25:58 +01:00
#ifdef USE_DEVICE_GROUPS_SEND
D_CMND_DEVGROUP_SEND "|"
#endif // USE_DEVICE_GROUPS_SEND
D_CMND_DEVGROUP_SHARE "|" D_CMND_DEVGROUPSTATUS "|" D_CMND_DEVGROUP_TIE "|"
#endif // USE_DEVICE_GROUPS
2022-01-23 11:29:06 +00:00
D_CMND_SETSENSOR "|" D_CMND_SENSOR "|" D_CMND_DRIVER "|" D_CMND_JSON
#ifdef ESP32
"|Info|"
#if defined(SOC_TOUCH_VERSION_1) || defined(SOC_TOUCH_VERSION_2)
D_CMND_TOUCH_CAL "|" D_CMND_TOUCH_THRES "|"
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
D_CMND_CPU_FREQUENCY
2020-11-28 11:46:17 +00:00
#endif // ESP32
2022-05-04 09:07:24 +01:00
#endif //FIRMWARE_MINIMAL_ONLY
;
SO_SYNONYMS(kTasmotaSynonyms,
127,
);
void (* const TasmotaCommand[])(void) PROGMEM = {
2022-03-19 11:49:16 +00:00
&CmndUpgrade, &CmndUpgrade, &CmndOtaUrl, &CmndSeriallog, &CmndRestart,
2022-05-04 09:07:24 +01:00
#ifndef FIRMWARE_MINIMAL_ONLY
2022-03-19 11:49:16 +00:00
&CmndBacklog, &CmndDelay, &CmndPower, &CmndStatus, &CmndState, &CmndSleep,
&CmndPowerOnState, &CmndPulsetime, &CmndBlinktime, &CmndBlinkcount, &CmndSavedata,
2021-01-26 11:03:08 +00:00
&CmndSetoption, &CmndSetoption, &CmndTemperatureResolution, &CmndHumidityResolution, &CmndPressureResolution, &CmndPowerResolution,
&CmndVoltageResolution, &CmndFrequencyResolution, &CmndCurrentResolution, &CmndEnergyResolution, &CmndWeightResolution,
&CmndModule, &CmndModules, &CmndGpio, &CmndGpios, &CmndTemplate, &CmndPwm, &CmndPwmfrequency, &CmndPwmrange,
&CmndButtonDebounce, &CmndSwitchDebounce, &CmndSyslog, &CmndLoghost, &CmndLogport,
&CmndSerialBuffer, &CmndSerialSend, &CmndBaudrate, &CmndSerialConfig, &CmndSerialDelimiter,
&CmndIpAddress, &CmndNtpServer, &CmndAp, &CmndSsid, &CmndPassword, &CmndHostname, &CmndWifiConfig, &CmndWifi, &CmndDnsTimeout,
2021-04-08 16:57:37 +01:00
&CmndDevicename, &CmndFriendlyname, &CmndFriendlyname, &CmndSwitchMode, &CmndInterlock, &CmndTeleperiod, &CmndReset, &CmndTime, &CmndTimezone, &CmndTimeStd,
&CmndTimeDst, &CmndAltitude, &CmndLedPower, &CmndLedState, &CmndLedMask, &CmndLedPwmOn, &CmndLedPwmOff, &CmndLedPwmMode,
2022-08-03 06:32:55 +01:00
&CmndWifiPower, &CmndTempOffset, &CmndHumOffset, &CmndSpeedUnit, &CmndGlobalTemp, &CmndGlobalHum, &CmndGlobalPress, &CmndSwitchText, &CmndWifiScan, &CmndWifiTest,
#ifdef USE_I2C
2021-12-30 08:39:29 +00:00
&CmndI2cScan, &CmndI2cDriver,
#endif
#ifdef USE_DEVICE_GROUPS
2021-02-10 03:22:43 +00:00
&CmndDevGroupName,
2020-04-07 19:25:58 +01:00
#ifdef USE_DEVICE_GROUPS_SEND
&CmndDevGroupSend,
#endif // USE_DEVICE_GROUPS_SEND
2021-02-10 03:22:43 +00:00
&CmndDevGroupShare, &CmndDevGroupStatus, &CmndDevGroupTie,
#endif // USE_DEVICE_GROUPS
2022-01-23 11:29:06 +00:00
&CmndSetSensor, &CmndSensor, &CmndDriver, &CmndJson
#ifdef ESP32
, &CmndInfo,
#if defined(SOC_TOUCH_VERSION_1) || defined(SOC_TOUCH_VERSION_2)
&CmndTouchCal, &CmndTouchThres,
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
&CmndCpuFrequency
2020-11-28 11:46:17 +00:00
#endif // ESP32
2022-05-04 09:07:24 +01:00
#endif //FIRMWARE_MINIMAL_ONLY
};
2019-10-22 16:29:21 +01:00
const char kWifiConfig[] PROGMEM =
D_WCFG_0_RESTART "||" D_WCFG_2_WIFIMANAGER "||" D_WCFG_4_RETRY "|" D_WCFG_5_WAIT "|" D_WCFG_6_SERIAL "|" D_WCFG_7_WIFIMANAGER_RESET_ONLY;
/********************************************************************************************/
2022-08-03 06:32:55 +01:00
#ifndef FIRMWARE_MINIMAL_ONLY
void CmndWifiScan(void)
{
if (XdrvMailbox.data_len > 0) {
if ( !Wifi.scan_state || Wifi.scan_state > 7 ) {
ResponseCmndChar(D_JSON_SCANNING);
Wifi.scan_state = 6;
} else {
ResponseCmndChar(D_JSON_BUSY);
}
} else {
if ( !Wifi.scan_state ) {
ResponseCmndChar(D_JSON_NOT_STARTED);
} else if ( Wifi.scan_state >= 1 && Wifi.scan_state <= 5 ) {
ResponseCmndChar(D_JSON_BUSY);
} else if ( Wifi.scan_state >= 6 && Wifi.scan_state <= 7 ) {
ResponseCmndChar(D_JSON_SCANNING);
} else { //show scan result
Response_P(PSTR("{\"" D_CMND_WIFISCAN "\":"));
if (WiFi.scanComplete() > 0) {
// Sort networks by RSSI
uint32_t indexes[WiFi.scanComplete()];
for (uint32_t i = 0; i < WiFi.scanComplete(); i++) {
indexes[i] = i;
}
for (uint32_t i = 0; i < WiFi.scanComplete(); i++) {
for (uint32_t j = i + 1; j < WiFi.scanComplete(); j++) {
if (WiFi.RSSI(indexes[j]) > WiFi.RSSI(indexes[i])) {
std::swap(indexes[i], indexes[j]);
}
}
}
delay(0);
2022-08-03 10:39:51 +01:00
ResponseAppend_P(PSTR("{"));
2022-08-03 06:32:55 +01:00
for (uint32_t i = 0; i < WiFi.scanComplete(); i++) {
2022-08-07 13:23:27 +01:00
ResponseAppend_P(PSTR("\"" D_STATUS5_NETWORK "%d\":{\"" D_SSID "\":\"%s\",\"" D_BSSID "\":\"%s\",\"" D_CHANNEL
"\":\"%d\",\"" D_JSON_SIGNAL "\":\"%d\",\"" D_RSSI "\":\"%d\",\"" D_JSON_ENCRYPTION "\":\"%s\"}"),
2022-08-03 06:32:55 +01:00
i+1,
WiFi.SSID(indexes[i]).c_str(),
WiFi.BSSIDstr(indexes[i]).c_str(),
WiFi.channel(indexes[i]),
WiFi.RSSI(indexes[i]),
WifiGetRssiAsQuality(WiFi.RSSI(indexes[i])),
WifiEncryptionType(indexes[i]).c_str());
2022-08-03 06:32:55 +01:00
if ( ResponseSize() < ResponseLength() + 300 ) { break; }
2022-08-07 13:23:27 +01:00
if ( i < WiFi.scanComplete() -1 ) { ResponseAppend_P(PSTR(",")); }
2022-08-03 06:32:55 +01:00
//AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_WIFI "MAX SIZE: %d, SIZE: %d"),ResponseSize(),ResponseLength());
}
ResponseJsonEnd();
} else {
ResponseAppend_P(PSTR("\"" D_NO_NETWORKS_FOUND "\""));
}
ResponseJsonEnd();
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR(D_CMND_WIFISCAN));
}
}
}
void CmndWifiTest(void)
{
// Test WIFI Connection to Router if Tasmota is in AP mode since in AP mode, a STA connection can be established
// at the same time for testing the connection.
#ifdef USE_WEBSERVER
if (!WifiIsInManagerMode()) { ResponseCmndError(); return; }
if ( (XdrvMailbox.data_len > 0) ) {
if (Wifi.wifiTest != WIFI_TESTING) { // Start Test
char* pos = strchr(XdrvMailbox.data, '+');
if (pos != nullptr) {
char ssid_test[XdrvMailbox.data_len];
char pswd_test[XdrvMailbox.data_len];
subStr(ssid_test, XdrvMailbox.data, "+", 1);
subStr(pswd_test, XdrvMailbox.data, "+", 2);
ResponseCmndIdxChar(D_JSON_TESTING);
//Response_P(PSTR("{\"%s%d\":{\"Network\":\"%s,\"PASS\":\"%s\"}}"), XdrvMailbox.command, XdrvMailbox.index, ssid_test, pswd_test);
if (WIFI_NOT_TESTING == Wifi.wifiTest) {
if (MAX_WIFI_OPTION == Wifi.old_wificonfig) { Wifi.old_wificonfig = Settings->sta_config; }
TasmotaGlobal.wifi_state_flag = Settings->sta_config = WIFI_MANAGER;
Wifi.save_data_counter = TasmotaGlobal.save_data_counter;
}
Wifi.wifi_test_counter = 9; // seconds to test user's proposed AP
Wifi.wifiTest = WIFI_TESTING;
TasmotaGlobal.save_data_counter = 0; // Stop auto saving data - Updating Settings
Settings->save_data = 0;
TasmotaGlobal.sleep = 0; // Disable sleep
TasmotaGlobal.restart_flag = 0; // No restart
TasmotaGlobal.ota_state_flag = 0; // No OTA
Wifi.wifi_Test_Restart = false;
Wifi.wifi_Test_Save_SSID2 = false;
if (0 == XdrvMailbox.index) { Wifi.wifi_Test_Restart = true; } // If WifiTest is successful, save data on SSID1 and restart
if (2 == XdrvMailbox.index) { Wifi.wifi_Test_Save_SSID2 = true; } // If WifiTest is successful, save data on SSID2
SettingsUpdateText(Wifi.wifi_Test_Save_SSID2 ? SET_STASSID2 : SET_STASSID1, ssid_test);
SettingsUpdateText(Wifi.wifi_Test_Save_SSID2 ? SET_STAPWD2 : SET_STAPWD1, pswd_test);
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CONNECTING_TO_AP " %s " D_AS " %s ..."),
SettingsText(Wifi.wifi_Test_Save_SSID2 ? SET_STASSID2 : SET_STASSID1), TasmotaGlobal.hostname);
WiFi.begin(SettingsText(Wifi.wifi_Test_Save_SSID2 ? SET_STASSID2 : SET_STASSID1), SettingsText(Wifi.wifi_Test_Save_SSID2 ? SET_STAPWD2 : SET_STAPWD1));
}
} else {
ResponseCmndChar(D_JSON_BUSY);
}
} else {
switch (Wifi.wifiTest) {
case WIFI_TESTING:
ResponseCmndChar(D_JSON_TESTING);
break;
case WIFI_NOT_TESTING:
ResponseCmndChar(D_JSON_NOT_STARTED);
break;
case WIFI_TEST_FINISHED:
ResponseCmndChar(Wifi.wifi_test_AP_TIMEOUT ? D_CONNECT_FAILED_AP_TIMEOUT : D_JSON_SUCCESSFUL);
break;
case WIFI_TEST_FINISHED_BAD:
switch (WiFi.status()) {
case WL_CONNECTED:
ResponseCmndChar(D_CONNECT_FAILED_NO_IP_ADDRESS);
break;
case WL_NO_SSID_AVAIL:
ResponseCmndChar(D_CONNECT_FAILED_AP_NOT_REACHED);
break;
case WL_CONNECT_FAILED:
ResponseCmndChar(D_CONNECT_FAILED_WRONG_PASSWORD);
break;
default: // WL_IDLE_STATUS and WL_DISCONNECTED - SSId in range but no answer from the router
ResponseCmndChar(D_CONNECT_FAILED_AP_TIMEOUT);
}
break;
}
}
#else
ResponseCmndError();
#endif //USE_WEBSERVER
}
#endif // not defined FIRMWARE_MINIMAL_ONLY
2021-01-31 15:54:28 +00:00
void ResponseCmndNumber(int value) {
2019-08-03 12:01:34 +01:00
Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, value);
}
2021-01-31 15:54:28 +00:00
void ResponseCmndFloat(float value, uint32_t decimals) {
2021-01-26 15:26:00 +00:00
Response_P(PSTR("{\"%s\":%*_f}"), XdrvMailbox.command, decimals, &value); // Return float value without quotes
}
void ResponseCmndIdxFloat(float value, uint32_t decimals) {
Response_P(PSTR("{\"%s%d\":%*_f}"), XdrvMailbox.command, XdrvMailbox.index, decimals, &value); // Return float value without quotes
}
2021-01-31 15:54:28 +00:00
void ResponseCmndIdxNumber(int value) {
2019-08-03 12:01:34 +01:00
Response_P(S_JSON_COMMAND_INDEX_NVALUE, XdrvMailbox.command, XdrvMailbox.index, value);
}
2021-01-31 15:54:28 +00:00
void ResponseCmndChar_P(const char* value) {
2020-05-06 07:56:09 +01:00
Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, value);
2020-03-16 17:55:58 +00:00
}
2021-01-31 15:54:28 +00:00
void ResponseCmndChar(const char* value) {
2020-06-19 11:33:31 +01:00
Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, EscapeJSONString(value).c_str());
2019-08-03 12:01:34 +01:00
}
2021-01-31 15:54:28 +00:00
void ResponseCmndStateText(uint32_t value) {
2019-08-03 12:01:34 +01:00
ResponseCmndChar(GetStateText(value));
}
2021-01-31 15:54:28 +00:00
void ResponseCmndDone(void) {
ResponseCmndChar_P(PSTR(D_JSON_DONE));
2019-08-03 12:01:34 +01:00
}
2021-01-31 15:54:28 +00:00
void ResponseCmndError(void) {
ResponseCmndChar_P(PSTR(D_JSON_ERROR));
}
2021-02-16 15:21:46 +00:00
void ResponseCmndFailed(void) {
ResponseCmndChar_P(PSTR(D_JSON_FAILED));
}
2021-01-31 15:54:28 +00:00
void ResponseCmndIdxChar(const char* value) {
2020-06-19 11:33:31 +01:00
Response_P(S_JSON_COMMAND_INDEX_SVALUE, XdrvMailbox.command, XdrvMailbox.index, EscapeJSONString(value).c_str());
2019-08-03 12:01:34 +01:00
}
2021-01-31 15:54:28 +00:00
void ResponseCmndIdxError(void) {
ResponseCmndIdxChar(PSTR(D_JSON_ERROR));
}
void ResponseCmndAll(uint32_t text_index, uint32_t count) {
uint32_t real_index = text_index;
2020-10-30 11:29:48 +00:00
ResponseClear();
#ifdef MQTT_DATA_STRING
for (uint32_t i = 0; i < count; i++) {
if ((SET_MQTT_GRP_TOPIC == text_index) && (1 == i)) { real_index = SET_MQTT_GRP_TOPIC2 -1; }
ResponseAppend_P(PSTR("%c\"%s%d\":\"%s\""), (i)?',':'{', XdrvMailbox.command, i +1, EscapeJSONString(SettingsText(real_index +i)).c_str());
}
ResponseJsonEnd();
#else
2021-02-05 16:05:13 +00:00
bool jsflg = false;
for (uint32_t i = 0; i < count; i++) {
if ((SET_MQTT_GRP_TOPIC == text_index) && (1 == i)) { real_index = SET_MQTT_GRP_TOPIC2 -1; }
2021-02-05 16:05:13 +00:00
if ((ResponseAppend_P(PSTR("%c\"%s%d\":\"%s\""), (jsflg)?',':'{', XdrvMailbox.command, i +1, EscapeJSONString(SettingsText(real_index +i)).c_str()) > (MAX_LOGSZ - TOPSZ)) || (i == count -1)) {
ResponseJsonEnd();
2021-04-07 14:07:05 +01:00
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, XdrvMailbox.command);
2021-02-05 16:05:13 +00:00
ResponseClear();
jsflg = false;
} else {
jsflg = true;
}
}
#endif
}
2019-08-03 12:01:34 +01:00
/********************************************************************************************/
void ExecuteCommand(const char *cmnd, uint32_t source)
{
// cmnd: "status 0" = stopic "status" and svalue " 0"
// cmnd: "var1 =1" = stopic "var1" and svalue " =1"
// cmnd: "var1=1" = stopic "var1" and svalue "=1"
2021-06-25 16:09:53 +01:00
SHOW_FREE_MEM(PSTR("ExecuteCommand"));
ShowSource(source);
2022-11-13 11:55:18 +00:00
TasmotaGlobal.last_command_source = source;
const char *pos = cmnd;
while (*pos && isspace(*pos)) {
pos++; // Skip all spaces
}
const char *start = pos;
// Get a command. Commands can only use letters, digits and underscores
while (*pos && (isalpha(*pos) || isdigit(*pos) || '_' == *pos || '/' == *pos)) {
2019-11-03 14:37:33 +00:00
if ('/' == *pos) { // Skip possible cmnd/tasmota/ preamble
start = pos + 1;
}
pos++;
}
if ('\0' == *start || pos <= start) {
return; // Did not find any command to execute
}
uint32_t size = pos - start;
char stopic[size + 2]; // with leader '/' and end '\0'
stopic[0] = '/';
memcpy(stopic+1, start, size);
stopic[size+1] = '\0';
char svalue[strlen(pos) +1]; // pos point to the start of parameters
strlcpy(svalue, pos, sizeof(svalue));
CommandHandler(stopic, svalue, strlen(svalue));
}
/********************************************************************************************/
// topicBuf: /power1 dataBuf: toggle = Console command
// topicBuf: cmnd/tasmota/power1 dataBuf: toggle = Mqtt command using topic
// topicBuf: cmnd/tasmotas/power1 dataBuf: toggle = Mqtt command using a group topic
// topicBuf: cmnd/DVES_83BB10_fb/power1 dataBuf: toggle = Mqtt command using fallback topic
2019-07-24 12:09:42 +01:00
2023-02-05 14:13:44 +00:00
void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len) {
2021-06-25 16:09:53 +01:00
SHOW_FREE_MEM(PSTR("CommandHandler"));
bool grpflg = false;
uint32_t real_index = SET_MQTT_GRP_TOPIC;
for (uint32_t i = 0; i < MAX_GROUP_TOPICS; i++) {
if (1 == i) { real_index = SET_MQTT_GRP_TOPIC2 -1; }
2020-04-10 13:38:00 +01:00
char *group_topic = SettingsText(real_index +i);
if (*group_topic && strstr(topicBuf, group_topic) != nullptr) {
grpflg = true;
break;
}
}
char stemp1[TOPSZ];
GetFallbackTopic_P(stemp1, ""); // Full Fallback topic = cmnd/DVES_xxxxxxxx_fb/
2020-10-30 11:29:48 +00:00
TasmotaGlobal.fallback_topic_flag = (!strncmp(topicBuf, stemp1, strlen(stemp1)));
char *type = strrchr(topicBuf, '/'); // Last part of received topic is always the command (type)
uint32_t index = 1;
bool user_index = false;
if (type != nullptr) {
type++;
2019-10-30 13:08:43 +00:00
uint32_t i;
int nLen; // strlen(type)
char *s = type;
for (nLen = 0; *s; s++, nLen++) {
*s=toupper(*s);
}
i = nLen;
if (i > 0) { // may be 0
while (isdigit(type[i-1])) {
i--;
}
}
if (i < nLen) {
index = atoi(type + i);
user_index = true;
}
type[i] = '\0';
2023-02-05 14:13:44 +00:00
bool binary_data = (index > 199); // Suppose binary data on topic index > 199
if (!binary_data) {
bool keep_spaces = ((strstr_P(type, PSTR("SERIALSEND")) != nullptr) && (index > 9)); // Do not skip leading spaces on (s)serialsend10 and up
if (!keep_spaces) {
while (*dataBuf && isspace(*dataBuf)) {
dataBuf++; // Skip leading spaces in data
data_len--;
}
}
2021-05-12 16:10:50 +01:00
}
2023-02-05 14:13:44 +00:00
int32_t payload = -99;
if (!binary_data) {
if (!strcmp(dataBuf,"?")) { data_len = 0; }
2022-02-18 15:13:55 +00:00
2023-02-05 14:13:44 +00:00
char *p;
payload = strtol(dataBuf, &p, 0); // decimal, octal (0) or hex (0x)
if (p == dataBuf) { payload = -99; }
int temp_payload = GetStateNumber(dataBuf);
if (temp_payload > -1) { payload = temp_payload; }
}
2022-02-18 15:13:55 +00:00
2023-02-05 14:13:44 +00:00
AddLog(LOG_LEVEL_DEBUG, PSTR("CMD: Grp %d, Cmd '%s', Idx %d, Len %d, Pld %d, Data '%s'"),
grpflg, type, index, data_len, payload, (binary_data) ? HexToString((uint8_t*)dataBuf, data_len).c_str() : dataBuf);
Response_P(PSTR("{\"" D_JSON_COMMAND "\":\"" D_JSON_ERROR "\"}"));
2021-06-11 17:14:12 +01:00
if (Settings->ledstate &0x02) { TasmotaGlobal.blinks++; }
2020-11-27 13:43:45 +00:00
// TasmotaGlobal.backlog_timer = millis() + (100 * MIN_BACKLOG_DELAY);
2021-06-11 17:14:12 +01:00
TasmotaGlobal.backlog_timer = millis() + Settings->param[P_BACKLOG_DELAY]; // SetOption34
char command[CMDSZ] = { 0 };
XdrvMailbox.command = command;
XdrvMailbox.index = index;
XdrvMailbox.data_len = data_len;
XdrvMailbox.payload = payload;
XdrvMailbox.grpflg = grpflg;
XdrvMailbox.usridx = user_index;
XdrvMailbox.topic = type;
XdrvMailbox.data = dataBuf;
2019-09-24 07:51:09 +01:00
#ifdef USE_SCRIPT_SUB_COMMAND
// allow overwrite tasmota cmds
2019-09-30 09:25:02 +01:00
if (!Script_SubCmd()) {
if (!DecodeCommand(kTasmotaCommands, TasmotaCommand, kTasmotaSynonyms)) {
2019-09-30 09:25:02 +01:00
if (!XdrvCall(FUNC_COMMAND)) {
if (!XsnsCall(FUNC_COMMAND)) {
2019-09-24 07:51:09 +01:00
type = nullptr; // Unknown command
2019-09-30 09:25:02 +01:00
}
2019-09-24 07:51:09 +01:00
}
}
}
2020-11-28 11:46:17 +00:00
#else // USE_SCRIPT_SUB_COMMAND
if (!DecodeCommand(kTasmotaCommands, TasmotaCommand, kTasmotaSynonyms)) {
if (!XdrvCall(FUNC_COMMAND)) {
if (!XsnsCall(FUNC_COMMAND)) {
type = nullptr; // Unknown command
}
}
}
2020-11-28 11:46:17 +00:00
#endif // USE_SCRIPT_SUB_COMMAND
2019-09-24 07:51:09 +01:00
}
if (type == nullptr) {
2020-10-29 11:39:44 +00:00
TasmotaGlobal.blinks = 201;
2020-01-14 15:57:55 +00:00
snprintf_P(stemp1, sizeof(stemp1), PSTR(D_JSON_COMMAND));
Response_P(PSTR("{\"" D_JSON_COMMAND "\":\"" D_JSON_UNKNOWN "\"}"));
2020-01-14 15:57:55 +00:00
type = (char*)stemp1;
}
2021-05-23 13:42:27 +01:00
if (ResponseLength()) {
2020-10-05 11:23:07 +01:00
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, type);
}
2020-10-30 11:29:48 +00:00
TasmotaGlobal.fallback_topic_flag = false;
}
/********************************************************************************************/
void CmndBacklog(void) {
// Backlog command1;command2;.. Execute commands in sequence with a delay in between set with SetOption34
// Backlog0 command1;command2;.. Execute commands in sequence with no delay
if (XdrvMailbox.data_len) {
if (0 == XdrvMailbox.index) {
TasmotaGlobal.backlog_nodelay = true;
}
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
char *blcommand = strtok(XdrvMailbox.data, ";");
#ifdef SUPPORT_IF_STATEMENT
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
while ((blcommand != nullptr) && (backlog.size() < MAX_BACKLOG))
#else
2020-10-30 11:29:48 +00:00
uint32_t bl_pointer = (!TasmotaGlobal.backlog_pointer) ? MAX_BACKLOG -1 : TasmotaGlobal.backlog_pointer;
bl_pointer--;
2020-10-30 11:29:48 +00:00
while ((blcommand != nullptr) && (TasmotaGlobal.backlog_index != bl_pointer))
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
#endif
{
// Ignore semicolon (; = end of single command) between brackets {}
char *next = strchr(blcommand, '\0') +1; // Prepare for next ;
while ((next != nullptr) && (ChrCount(blcommand, "{") != ChrCount(blcommand, "}"))) { // Check for valid {} pair
next--; // Select end of line
*next = ';'; // Restore ; removed by strtok()
next = strtok(nullptr, ";"); // Point to begin of next string up to next ; or nullptr
}
// Skip unnecessary command Backlog at start of blcommand
while(true) {
blcommand = Trim(blcommand);
2021-05-03 10:00:48 +01:00
if (0 == strncasecmp_P(blcommand, PSTR(D_CMND_BACKLOG), strlen(D_CMND_BACKLOG))) {
blcommand += strlen(D_CMND_BACKLOG);
} else {
break;
}
}
2021-05-03 10:00:48 +01:00
// Do not allow command Reset in backlog
if ((*blcommand != '\0') && (strncasecmp_P(blcommand, PSTR(D_CMND_RESET), strlen(D_CMND_RESET)) != 0)) {
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
#ifdef SUPPORT_IF_STATEMENT
if (backlog.size() < MAX_BACKLOG) {
backlog.add(blcommand);
}
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
#else
TasmotaGlobal.backlog[TasmotaGlobal.backlog_index] = blcommand;
TasmotaGlobal.backlog_index++;
if (TasmotaGlobal.backlog_index >= MAX_BACKLOG) {
TasmotaGlobal.backlog_index = 0;
}
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
#endif
}
blcommand = strtok(nullptr, ";");
}
2019-08-03 12:01:34 +01:00
// ResponseCmndChar(D_JSON_APPENDED);
2020-10-30 11:29:48 +00:00
ResponseClear();
2020-11-27 13:43:45 +00:00
TasmotaGlobal.backlog_timer = millis();
} else {
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
bool blflag = BACKLOG_EMPTY;
#ifdef SUPPORT_IF_STATEMENT
backlog.clear();
#else
2020-10-30 11:29:48 +00:00
TasmotaGlobal.backlog_pointer = TasmotaGlobal.backlog_index;
Support If in Rules The major features of IF statement are: - Support IF, ELSEIF, ELSE - Support not only comparison but also using logical expression as condition - Support run multiple commands - Support nested IF statement - All the commands run by IF statement will go to the BACKLOG! - No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM! Extended Backus-Naur Form of IF statement: <if-statement> ::= IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF <logical-expression> := <comparison-expression> | (<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} | "(" <logical-expression ")" {(AND | OR) <logical expression>} <comparison-expression> ::= <math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression> <statement-list> ::= <statement> {";" <statement>} <statement> ::= <Sonoff-Tasmota-command> | <if-statement> In English: If statement support 3 format: 1. IF (<condition>) <statement-list> ENDIF 2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF 3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF <condition> is a logical expression which can be: 1. A comparison expression for example: VAR1 >= 10 2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example: UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1 3. Parenthesis can be used to change the priority of logical expression. For example: UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1) <statement-list> can be: 1. A Sonoff-Tasmota command. For example: ledpower on 2. A IF statement ("IF .... ENDIF") 3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example: Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on 4. Do not need to lead with "BACKLOG" for multiple commands.
2019-09-09 16:24:27 +01:00
#endif
2021-01-18 20:48:04 +00:00
ResponseCmndChar(blflag ? PSTR(D_JSON_EMPTY) : PSTR(D_JSON_ABORTED));
}
}
2022-01-23 11:29:06 +00:00
void CmndJson(void) {
// Json {"template":{"NAME":"Dummy","GPIO":[320,0,321],"FLAG":0,"BASE":18},"power":2,"HSBColor":"51,97,100","Channel":[100,85,3]}
//
// Escape lower level tokens and add quotes around it
// Input:
// {"template":{"NAME":"Dummy","GPIO":[320,0,321],"FLAG":0,"BASE":18},"power":2,"HSBColor":"51,97,100","Channel":[100,85,3]}
// Output (escaped subtokens):
// {"template":"{\"NAME\":\"Dummy\",\"GPIO\":[320,0,321],\"FLAG\":0,\"BASE\":18}","power":2,"HSBColor":"51,97,100","Channel":[100,85,3]}
uint32_t bracket = 0;
String data_buf("");
data_buf.reserve(XdrvMailbox.data_len); // We need at least the same amount of characters
for (uint32_t index = 0; index < XdrvMailbox.data_len; index++) {
char c = (char)XdrvMailbox.data[index];
if (c == '{') {
bracket++;
if (2 == bracket) { data_buf += '"'; } // Add start quote
}
if (bracket > 1) {
if (c == '\"') { data_buf += '\\'; } // Escape any quote within second level token
}
data_buf += c;
if (c == '}') {
bracket--;
if (1 == bracket) { data_buf += '"'; } // Add end quote
}
}
JsonParser parser((char*)data_buf.c_str());
2022-01-23 11:29:06 +00:00
JsonParserObject root = parser.getRootObject();
if (root) {
// Convert to backlog commands
// Input (escaped subtokens):
// {"template":"{\"NAME\":\"Dummy\",\"GPIO\":[320,0,321],\"FLAG\":0,\"BASE\":18}","power":2,"HSBColor":"51,97,100","Channel":[100,85,3]}
// Output:
// template {"NAME":"Dummy","GPIO":[320,0,321],"FLAG":0,"BASE":18};power 2;HSBColor 51,97,100;Channel1 100;Channel2 85;Channel3 3
String backlog; // We might need a larger string than XdrvMailbox.data_len accomodating decoded arrays
2022-01-23 11:29:06 +00:00
for (auto command_key : root) {
const char *command = command_key.getStr();
JsonParserToken parameters = command_key.getValue();
if (parameters.isArray()) {
JsonParserArray parameter_arr = parameters.getArray();
uint32_t index = 1;
for (auto value : parameter_arr) {
if (backlog.length()) { backlog += ";"; }
backlog += command;
backlog += index++;
backlog += " ";
backlog += value.getStr(); // Channel1 100;Channel2 85;Channel3 3
}
} else if (parameters.isObject()) { // Should have been escaped
// AddLog(LOG_LEVEL_DEBUG, PSTR("JSN: Object"));
2022-01-23 11:29:06 +00:00
} else {
if (backlog.length()) { backlog += ";"; }
backlog += command;
backlog += " ";
backlog += parameters.getStr(); // HSBColor 51,97,100
}
}
XdrvMailbox.data = (char*)backlog.c_str(); // Backlog commands
XdrvMailbox.data_len = 1; // Any data
XdrvMailbox.index = 0; // Backlog0 - no delay
CmndBacklog();
} else {
ResponseCmndChar(PSTR(D_JSON_EMPTY));
}
}
void CmndDelay(void)
{
if ((XdrvMailbox.payload >= (MIN_BACKLOG_DELAY / 100)) && (XdrvMailbox.payload <= 3600)) {
2020-11-27 13:43:45 +00:00
TasmotaGlobal.backlog_timer = millis() + (100 * XdrvMailbox.payload);
}
uint32_t bl_delay = 0;
2020-11-27 13:43:45 +00:00
long bl_delta = TimePassedSince(TasmotaGlobal.backlog_timer);
if (bl_delta < 0) { bl_delay = (bl_delta *-1) / 100; }
2019-08-03 12:01:34 +01:00
ResponseCmndNumber(bl_delay);
}
void CmndPower(void)
{
2020-10-30 11:29:48 +00:00
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= TasmotaGlobal.devices_present)) {
if ((XdrvMailbox.payload < POWER_OFF) || (XdrvMailbox.payload > POWER_BLINK_STOP)) {
XdrvMailbox.payload = POWER_SHOW_STATE;
}
2021-06-11 17:14:12 +01:00
// Settings->flag.device_index_enable = XdrvMailbox.usridx; // SetOption26 - Switch between POWER or POWER1
ExecuteCommandPower(XdrvMailbox.index, XdrvMailbox.payload, SRC_IGNORE);
2020-10-30 11:29:48 +00:00
ResponseClear();
}
else if (0 == XdrvMailbox.index) {
if ((XdrvMailbox.payload < POWER_OFF) || (XdrvMailbox.payload > POWER_TOGGLE)) {
XdrvMailbox.payload = POWER_SHOW_STATE;
}
SetAllPower(XdrvMailbox.payload, SRC_IGNORE);
2021-06-11 17:14:12 +01:00
if (Settings->flag3.hass_tele_on_power) { // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT
2020-08-14 15:43:50 +01:00
MqttPublishTeleState();
}
2020-10-30 11:29:48 +00:00
ResponseClear();
}
}
2021-04-07 14:07:05 +01:00
void CmndStatusResponse(uint32_t index) {
2021-05-24 16:59:47 +01:00
static String all_status = (const char*) nullptr;
2021-06-02 17:33:33 +01:00
if (0 == XdrvMailbox.index) { // Command status0
2021-05-24 16:59:47 +01:00
if (99 == index) {
all_status.replace("}{", ",");
char cmnd_status[10]; // STATUS11
snprintf_P(cmnd_status, sizeof(cmnd_status), PSTR(D_CMND_STATUS "0"));
MqttPublishPayloadPrefixTopicRulesProcess_P(STAT, cmnd_status, all_status.c_str(), Settings->flag5.mqtt_status_retain);
2021-05-24 16:59:47 +01:00
all_status = (const char*) nullptr;
2021-06-02 17:33:33 +01:00
} else {
if (0 == index) { all_status = ""; }
all_status += ResponseData();
2021-05-24 16:59:47 +01:00
}
}
else if (index < 99) {
char cmnd_status[10]; // STATUS11
char number[4] = { 0 };
snprintf_P(cmnd_status, sizeof(cmnd_status), PSTR(D_CMND_STATUS "%s"), (index) ? itoa(index, number, 10) : "");
MqttPublishPrefixTopicRulesProcess_P(STAT, cmnd_status, Settings->flag5.mqtt_status_retain);
2021-05-24 16:59:47 +01:00
}
2021-04-07 14:07:05 +01:00
}
void CmndStatus(void)
{
2020-11-02 14:21:32 +00:00
int32_t payload = XdrvMailbox.payload;
if (0 == XdrvMailbox.index) { payload = 0; } // All status messages in one MQTT message (status0)
2021-05-24 16:59:47 +01:00
if (payload > MAX_STATUS) { return; } // {"Command":"Error"}
2021-06-11 17:14:12 +01:00
if (!Settings->flag.mqtt_enabled && (6 == payload)) { return; } // SetOption3 - Enable MQTT
2020-10-30 11:29:48 +00:00
if (!TasmotaGlobal.energy_driver && (9 == payload)) { return; }
if (!CrashFlag() && (12 == payload)) { return; }
2021-06-11 17:14:12 +01:00
if (!Settings->flag3.shutter_mode && (13 == payload)) { return; }
char stemp[200];
char stemp2[TOPSZ];
2020-11-02 14:21:32 +00:00
if ((0 == payload) || (-99 == payload)) {
2020-10-30 11:29:48 +00:00
uint32_t maxfn = (TasmotaGlobal.devices_present > MAX_FRIENDLYNAMES) ? MAX_FRIENDLYNAMES : (!TasmotaGlobal.devices_present) ? 1 : TasmotaGlobal.devices_present;
#ifdef USE_SONOFF_IFAN
if (IsModuleIfan()) { maxfn = 1; }
#endif // USE_SONOFF_IFAN
stemp[0] = '\0';
for (uint32_t i = 0; i < maxfn; i++) {
2020-06-19 11:33:31 +01:00
snprintf_P(stemp, sizeof(stemp), PSTR("%s%s\"%s\"" ), stemp, (i > 0 ? "," : ""), EscapeJSONString(SettingsText(SET_FRIENDLYNAME1 +i)).c_str());
}
stemp2[0] = '\0';
2023-02-06 10:45:28 +00:00
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
2021-06-11 17:14:12 +01:00
snprintf_P(stemp2, sizeof(stemp2), PSTR("%s%s%d" ), stemp2, (i > 0 ? "," : ""), Settings->switchmode[i]);
}
Response_P(PSTR("{\"" D_CMND_STATUS "\":{\"" D_CMND_MODULE "\":%d,\"" D_CMND_DEVICENAME "\":\"%s\",\"" D_CMND_FRIENDLYNAME "\":[%s],\"" D_CMND_TOPIC "\":\"%s\",\""
D_CMND_BUTTONTOPIC "\":\"%s\",\"" D_CMND_POWER "\":%d,\"" D_CMND_POWERONSTATE "\":%d,\"" D_CMND_LEDSTATE "\":%d,\""
D_CMND_LEDMASK "\":\"%04X\",\"" D_CMND_SAVEDATA "\":%d,\"" D_JSON_SAVESTATE "\":%d,\"" D_CMND_SWITCHTOPIC "\":\"%s\",\""
D_CMND_SWITCHMODE "\":[%s],\"" D_CMND_BUTTONRETAIN "\":%d,\"" D_CMND_SWITCHRETAIN "\":%d,\"" D_CMND_SENSORRETAIN "\":%d,\"" D_CMND_POWERRETAIN "\":%d,\""
D_CMND_INFORETAIN "\":%d,\"" D_CMND_STATERETAIN "\":%d,\"" D_CMND_STATUSRETAIN "\":%d}}"),
2020-10-30 11:29:48 +00:00
ModuleNr(), EscapeJSONString(SettingsText(SET_DEVICENAME)).c_str(), stemp, TasmotaGlobal.mqtt_topic,
2021-06-11 17:14:12 +01:00
SettingsText(SET_MQTT_BUTTON_TOPIC), TasmotaGlobal.power, Settings->poweronstate, Settings->ledstate,
Settings->ledmask, Settings->save_data,
Settings->flag.save_state, // SetOption0 - Save power state and use after restart
SettingsText(SET_MQTT_SWITCH_TOPIC),
2019-11-03 11:33:36 +00:00
stemp2,
2021-06-11 17:14:12 +01:00
Settings->flag.mqtt_button_retain, // CMND_BUTTONRETAIN
Settings->flag.mqtt_switch_retain, // CMND_SWITCHRETAIN
Settings->flag.mqtt_sensor_retain, // CMND_SENSORRETAIN
Settings->flag.mqtt_power_retain, // CMND_POWERRETAIN
Settings->flag5.mqtt_info_retain, // CMND_INFORETAIN
Settings->flag5.mqtt_state_retain, // CMND_STATERETAIN
Settings->flag5.mqtt_status_retain // CMND_STATUSRETAIN
);
2021-05-24 16:59:47 +01:00
CmndStatusResponse(0);
}
if ((0 == payload) || (1 == payload)) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS1_PARAMETER "\":{\"" D_JSON_BAUDRATE "\":%d,\"" D_CMND_SERIALCONFIG "\":\"%s\",\"" D_CMND_GROUPTOPIC "\":\"%s\",\"" D_CMND_OTAURL "\":\"%s\",\""
D_JSON_RESTARTREASON "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\",\"" D_JSON_STARTUPUTC "\":\"%s\",\"" D_CMND_SLEEP "\":%d,\""
2020-04-19 15:58:13 +01:00
D_JSON_CONFIG_HOLDER "\":%d,\"" D_JSON_BOOTCOUNT "\":%d,\"BCResetTime\":\"%s\",\"" D_JSON_SAVECOUNT "\":%d"
#ifdef ESP8266
",\"" D_JSON_SAVEADDRESS "\":\"%X\""
#endif
"}}"),
2020-10-28 16:32:07 +00:00
TasmotaGlobal.baudrate, GetSerialConfig().c_str(), SettingsText(SET_MQTT_GRP_TOPIC), SettingsText(SET_OTAURL),
2021-06-11 17:14:12 +01:00
GetResetReason().c_str(), GetUptime().c_str(), GetDateAndTime(DT_RESTART).c_str(), Settings->sleep,
Settings->cfg_holder, Settings->bootcount, GetDateAndTime(DT_BOOTCOUNT).c_str(), Settings->save_flag
2020-04-19 15:58:13 +01:00
#ifdef ESP8266
, GetSettingsAddress()
#endif
);
2021-04-07 14:07:05 +01:00
CmndStatusResponse(1);
}
if ((0 == payload) || (2 == payload)) {
2022-10-04 16:52:15 +01:00
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS2_FIRMWARE "\":{\"" D_JSON_VERSION "\":\"%s%s%s\",\"" D_JSON_BUILDDATETIME "\":\"%s\""
2020-04-19 15:58:13 +01:00
#ifdef ESP8266
",\"" D_JSON_BOOTVERSION "\":%d"
#endif
",\"" D_JSON_COREVERSION "\":\"" ARDUINO_CORE_RELEASE "\",\"" D_JSON_SDKVERSION "\":\"%s\","
2022-04-24 10:24:01 +01:00
"\"CpuFrequency\":%d,\"Hardware\":\"%s\""
"%s}}"),
2022-10-04 16:52:15 +01:00
TasmotaGlobal.version, TasmotaGlobal.image_name, GetCodeCores().c_str(), GetBuildDateAndTime().c_str()
2020-04-19 15:58:13 +01:00
#ifdef ESP8266
, ESP.getBootVersion()
#endif
, ESP.getSdkVersion(),
2022-04-24 10:24:01 +01:00
ESP.getCpuFreqMHz(), GetDeviceHardwareRevision().c_str(),
2019-12-11 09:49:57 +00:00
GetStatistics().c_str());
2021-04-07 14:07:05 +01:00
CmndStatusResponse(2);
}
if ((0 == payload) || (3 == payload)) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS3_LOGGING "\":{\"" D_CMND_SERIALLOG "\":%d,\"" D_CMND_WEBLOG "\":%d,\"" D_CMND_MQTTLOG "\":%d,\"" D_CMND_SYSLOG "\":%d,\""
D_CMND_LOGHOST "\":\"%s\",\"" D_CMND_LOGPORT "\":%d,\"" D_CMND_SSID "\":[\"%s\",\"%s\"],\"" D_CMND_TELEPERIOD "\":%d,\""
D_JSON_RESOLUTION "\":\"%08X\",\"" D_CMND_SETOPTION "\":[\"%08X\",\"%s\",\"%08X\",\"%08X\",\"%08X\",\"%08X\"]}}"),
2021-06-11 17:14:12 +01:00
Settings->seriallog_level, Settings->weblog_level, Settings->mqttlog_level, Settings->syslog_level,
SettingsText(SET_SYSLOG_HOST), Settings->syslog_port, EscapeJSONString(SettingsText(SET_STASSID1)).c_str(), EscapeJSONString(SettingsText(SET_STASSID2)).c_str(), Settings->tele_period,
Settings->flag2.data, Settings->flag.data, ToHex_P((unsigned char*)Settings->param, PARAM8_SIZE, stemp2, sizeof(stemp2)),
Settings->flag3.data, Settings->flag4.data, Settings->flag5.data, Settings->flag6.data);
2021-04-07 14:07:05 +01:00
CmndStatusResponse(3);
}
if ((0 == payload) || (4 == payload)) {
2021-02-08 10:34:29 +00:00
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS4_MEMORY "\":{\"" D_JSON_PROGRAMSIZE "\":%d,\"" D_JSON_FREEMEMORY "\":%d,\"" D_JSON_HEAPSIZE "\":%d,\""
2020-05-12 14:42:16 +01:00
#ifdef ESP32
D_JSON_STACKLOWMARK "\":%d,\"" D_JSON_PSRMAXMEMORY "\":%d,\"" D_JSON_PSRFREEMEMORY "\":%d,\""
2020-11-28 11:46:17 +00:00
#endif // ESP32
2020-04-19 15:58:13 +01:00
D_JSON_PROGRAMFLASHSIZE "\":%d,\"" D_JSON_FLASHSIZE "\":%d"
",\"" D_JSON_FLASHCHIPID "\":\"%06X\""
2022-08-21 13:21:17 +01:00
",\"FlashFrequency\":%d,\"" D_JSON_FLASHMODE "\":\"%s\""),
2022-05-05 16:52:35 +01:00
ESP_getSketchSize()/1024, ESP_getFreeSketchSpace()/1024, ESP_getFreeHeap1024(),
2020-05-12 14:42:16 +01:00
#ifdef ESP32
uxTaskGetStackHighWaterMark(nullptr) / 1024, ESP.getPsramSize()/1024, ESP.getFreePsram()/1024,
ESP_getFlashChipMagicSize()/1024, ESP.getFlashChipSize()/1024
2020-11-28 11:46:17 +00:00
#endif // ESP32
#ifdef ESP8266
ESP_getFlashChipSize()/1024, ESP.getFlashChipRealSize()/1024
#endif // ESP8266
2022-05-09 21:43:27 +01:00
, ESP_getFlashChipId()
2022-08-21 13:21:17 +01:00
, ESP.getFlashChipSpeed()/1000000, ESP_getFlashChipMode().c_str());
2020-10-28 10:37:59 +00:00
ResponseAppendFeatures();
XsnsDriverState();
ResponseAppend_P(PSTR(",\"Sensors\":"));
XsnsSensorState(0);
ResponseJsonEndEnd();
2021-04-07 14:07:05 +01:00
CmndStatusResponse(4);
}
if ((0 == payload) || (5 == payload)) {
2022-12-10 12:44:16 +00:00
#ifdef USE_IPV6
if (5 == payload) { WifiDumpAddressesIPv6(); }
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS5_NETWORK "\":{\"" D_CMND_HOSTNAME "\":\"%s\",\""
D_CMND_IPADDRESS "\":\"%_I\",\"" D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\""
D_JSON_DNSSERVER "1\":\"%s\",\"" D_JSON_DNSSERVER "2\":\"%s\",\""
D_JSON_MAC "\":\"%s\""
",\"" D_JSON_IP6_GLOBAL "\":\"%s\",\"" D_JSON_IP6_LOCAL "\":\"%s\""),
TasmotaGlobal.hostname,
(uint32_t)WiFi.localIP(), Settings->ipv4_address[1], Settings->ipv4_address[2],
DNSGetIPStr(0).c_str(), DNSGetIPStr(1).c_str(),
WiFi.macAddress().c_str()
,WifiGetIPv6Str().c_str(), WifiGetIPv6LinkLocalStr().c_str());
#else // USE_IPV6
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS5_NETWORK "\":{\"" D_CMND_HOSTNAME "\":\"%s\",\""
D_CMND_IPADDRESS "\":\"%_I\",\"" D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\""
D_JSON_DNSSERVER "1\":\"%_I\",\"" D_JSON_DNSSERVER "2\":\"%_I\",\""
D_JSON_MAC "\":\"%s\""),
TasmotaGlobal.hostname,
(uint32_t)WiFi.localIP(), Settings->ipv4_address[1], Settings->ipv4_address[2],
Settings->ipv4_address[3], Settings->ipv4_address[4],
WiFi.macAddress().c_str());
2022-12-10 12:44:16 +00:00
#endif // USE_IPV6
2022-04-12 15:28:32 +01:00
#ifdef USE_TASMESH
ResponseAppend_P(PSTR(",\"SoftAPMac\":\"%s\""), WiFi.softAPmacAddress().c_str());
#endif // USE_TASMESH
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#ifdef USE_IPV6
ResponseAppend_P(PSTR(",\"Ethernet\":{\"" D_CMND_HOSTNAME "\":\"%s\",\""
D_CMND_IPADDRESS "\":\"%_I\",\"" D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\""
D_JSON_DNSSERVER "1\":\"%s\",\"" D_JSON_DNSSERVER "2\":\"%s\",\""
D_JSON_MAC "\":\"%s\",\"" D_JSON_IP6_GLOBAL "\":\"%s\",\"" D_JSON_IP6_LOCAL "\":\"%s\"}"),
EthernetHostname(),
(uint32_t)EthernetLocalIP(), Settings->eth_ipv4_address[1], Settings->eth_ipv4_address[2],
DNSGetIPStr(0).c_str(), DNSGetIPStr(1).c_str(),
EthernetMacAddress().c_str(),
EthernetGetIPv6Str().c_str(), EthernetGetIPv6LinkLocalStr().c_str());
#else // USE_IPV6
ResponseAppend_P(PSTR(",\"Ethernet\":{\"" D_CMND_HOSTNAME "\":\"%s\",\""
D_CMND_IPADDRESS "\":\"%_I\",\"" D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\""
D_JSON_DNSSERVER "1\":\"%_I\",\"" D_JSON_DNSSERVER "2\":\"%_I\",\""
D_JSON_MAC "\":\"%s\"}"),
EthernetHostname(),
(uint32_t)EthernetLocalIP(), Settings->eth_ipv4_address[1], Settings->eth_ipv4_address[2],
Settings->eth_ipv4_address[3], Settings->eth_ipv4_address[4],
2023-01-13 10:47:58 +00:00
EthernetMacAddress().c_str());
2022-12-10 12:44:16 +00:00
#endif // USE_IPV6
#endif // USE_ETHERNET
ResponseAppend_P(PSTR(",\"" D_CMND_WEBSERVER "\":%d,\"HTTP_API\":%d,\"" D_CMND_WIFICONFIG "\":%d,\"" D_CMND_WIFIPOWER "\":%s}}"),
Settings->webserver, Settings->flag5.disable_referer_chk, Settings->sta_config, WifiGetOutputPower().c_str());
2021-04-07 14:07:05 +01:00
CmndStatusResponse(5);
}
2021-06-11 17:14:12 +01:00
if (((0 == payload) || (6 == payload)) && Settings->flag.mqtt_enabled) { // SetOption3 - Enable MQTT
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS6_MQTT "\":{\"" D_CMND_MQTTHOST "\":\"%s\",\"" D_CMND_MQTTPORT "\":%d,\"" D_CMND_MQTTCLIENT D_JSON_MASK "\":\"%s\",\""
D_CMND_MQTTCLIENT "\":\"%s\",\"" D_CMND_MQTTUSER "\":\"%s\",\"" D_JSON_MQTT_COUNT "\":%d,\"MAX_PACKET_SIZE\":%d,\"KEEPALIVE\":%d,\"SOCKET_TIMEOUT\":%d}}"),
2021-06-11 17:14:12 +01:00
SettingsText(SET_MQTT_HOST), Settings->mqtt_port, EscapeJSONString(SettingsText(SET_MQTT_CLIENT)).c_str(),
TasmotaGlobal.mqtt_client, EscapeJSONString(SettingsText(SET_MQTT_USER)).c_str(), MqttConnectCount(), MQTT_MAX_PACKET_SIZE, Settings->mqtt_keepalive, Settings->mqtt_socket_timeout);
2021-04-07 14:07:05 +01:00
CmndStatusResponse(6);
}
if ((0 == payload) || (7 == payload)) {
2021-06-11 17:14:12 +01:00
if (99 == Settings->timezone) {
snprintf_P(stemp, sizeof(stemp), PSTR("%d" ), Settings->timezone);
} else {
snprintf_P(stemp, sizeof(stemp), PSTR("\"%s\"" ), GetTimeZone().c_str());
}
#if defined(USE_TIMERS) && defined(USE_SUNRISE)
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS7_TIME "\":{\"" D_JSON_UTC_TIME "\":\"%s\",\"" D_JSON_LOCAL_TIME "\":\"%s\",\"" D_JSON_STARTDST "\":\"%s\",\""
D_JSON_ENDDST "\":\"%s\",\"" D_CMND_TIMEZONE "\":%s,\"" D_JSON_SUNRISE "\":\"%s\",\"" D_JSON_SUNSET "\":\"%s\"}}"),
GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_LOCALNOTZ).c_str(), GetDateAndTime(DT_DST).c_str(),
GetDateAndTime(DT_STD).c_str(), stemp, GetSun(0).c_str(), GetSun(1).c_str());
#else
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS7_TIME "\":{\"" D_JSON_UTC_TIME "\":\"%s\",\"" D_JSON_LOCAL_TIME "\":\"%s\",\"" D_JSON_STARTDST "\":\"%s\",\""
D_JSON_ENDDST "\":\"%s\",\"" D_CMND_TIMEZONE "\":%s}}"),
GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_LOCALNOTZ).c_str(), GetDateAndTime(DT_DST).c_str(),
GetDateAndTime(DT_STD).c_str(), stemp);
#endif // USE_TIMERS and USE_SUNRISE
2021-04-07 14:07:05 +01:00
CmndStatusResponse(7);
}
#if defined(USE_ENERGY_SENSOR) && defined(USE_ENERGY_MARGIN_DETECTION)
2020-10-30 11:29:48 +00:00
if (TasmotaGlobal.energy_driver) {
if ((0 == payload) || (9 == payload)) {
2023-01-26 14:26:10 +00:00
EnergyMarginStatus();
2021-04-07 14:07:05 +01:00
CmndStatusResponse(9);
}
}
#endif // USE_ENERGY_MARGIN_DETECTION
if ((0 == payload) || (8 == payload) || (10 == payload)) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS10_SENSOR "\":"));
2021-11-21 17:54:13 +00:00
MqttShowSensor(true);
ResponseJsonEnd();
2021-04-07 14:07:05 +01:00
CmndStatusResponse((8 == payload) ? 8 : 10);
}
if ((0 == payload) || (11 == payload)) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS11_STATUS "\":"));
MqttShowState();
ResponseJsonEnd();
2021-04-07 14:07:05 +01:00
CmndStatusResponse(11);
}
2019-10-10 12:39:52 +01:00
2019-12-28 13:54:26 +00:00
if (CrashFlag()) {
if ((0 == payload) || (12 == payload)) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS12_STATUS "\":"));
CrashDump();
ResponseJsonEnd();
2021-04-07 14:07:05 +01:00
CmndStatusResponse(12);
}
}
2020-08-20 16:58:58 +01:00
2020-08-20 15:24:17 +01:00
#ifdef USE_SHUTTER
2021-06-11 17:14:12 +01:00
if (Settings->flag3.shutter_mode) {
2020-08-20 16:58:58 +01:00
if ((0 == payload) || (13 == payload)) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS13_SHUTTER "\":{"));
2020-08-20 16:58:58 +01:00
for (uint32_t i = 0; i < MAX_SHUTTERS; i++) {
2021-06-11 17:14:12 +01:00
if (0 == Settings->shutter_startrelay[i]) { break; }
2020-08-20 16:58:58 +01:00
if (i > 0) { ResponseAppend_P(PSTR(",")); }
ResponseAppend_P(PSTR("\"" D_STATUS13_SHUTTER "%d\":{\"Relay1\":%d,\"Relay2\":%d,\"Open\":%d,\"Close\":%d,"
"\"50perc\":%d,\"Delay\":%d,\"Opt\":\"%s\","
"\"Calib\":[%d,%d,%d,%d,%d],"
"\"Mode\":\"%d\"}"),
2021-06-11 17:14:12 +01:00
i, Settings->shutter_startrelay[i], Settings->shutter_startrelay[i] +1, Settings->shutter_opentime[i], Settings->shutter_closetime[i],
Settings->shutter_set50percent[i], Settings->shutter_motordelay[i], GetBinary8(Settings->shutter_options[i], 4).c_str(),
Settings->shuttercoeff[0][i], Settings->shuttercoeff[1][i], Settings->shuttercoeff[2][i], Settings->shuttercoeff[3][i], Settings->shuttercoeff[4][i],
Settings->shutter_mode);
}
ResponseJsonEndEnd();
2021-04-07 14:07:05 +01:00
CmndStatusResponse(13);
2020-08-20 15:24:17 +01:00
}
}
#endif
2020-08-20 16:58:58 +01:00
2021-05-24 16:59:47 +01:00
CmndStatusResponse(99);
2020-10-30 11:29:48 +00:00
ResponseClear();
}
void CmndState(void)
{
2020-10-30 11:29:48 +00:00
ResponseClear();
MqttShowState();
2021-06-11 17:14:12 +01:00
if (Settings->flag3.hass_tele_on_power) { // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), Settings->flag5.mqtt_state_retain);
}
#ifdef USE_HOME_ASSISTANT
2021-06-11 17:14:12 +01:00
if (Settings->flag.hass_discovery) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59)
HAssPublishStatus();
}
#endif // USE_HOME_ASSISTANT
}
void CmndTempOffset(void)
{
if (XdrvMailbox.data_len > 0) {
int value = (int)(CharToFloat(XdrvMailbox.data) * 10);
if ((value > -127) && (value < 127)) {
2021-06-11 17:14:12 +01:00
Settings->temp_comp = value;
}
}
2021-06-11 17:14:12 +01:00
ResponseCmndFloat((float)(Settings->temp_comp) / 10, 1);
}
2020-03-16 17:29:55 +00:00
void CmndHumOffset(void)
{
if (XdrvMailbox.data_len > 0) {
int value = (int)(CharToFloat(XdrvMailbox.data) * 10);
if ((value > -101) && (value < 101)) {
2021-06-11 17:14:12 +01:00
Settings->hum_comp = value;
2020-03-16 17:29:55 +00:00
}
}
2021-06-11 17:14:12 +01:00
ResponseCmndFloat((float)(Settings->hum_comp) / 10, 1);
2020-03-16 17:29:55 +00:00
}
void CmndGlobalTemp(void) {
if (2 == XdrvMailbox.index) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 251)) {
Settings->global_sensor_index[0] = XdrvMailbox.payload;
TasmotaGlobal.user_globals[0] = 0;
}
ResponseCmndIdxNumber(Settings->global_sensor_index[0]);
} else {
if (XdrvMailbox.data_len > 0) {
// Set temperature based on SO8 (Celsius or Fahrenheit)
float temperature = ConvertTempToCelsius(CharToFloat(XdrvMailbox.data));
// Temperature is now Celsius
if ((temperature >= -50.0f) && (temperature <= 100.0f)) {
TasmotaGlobal.temperature_celsius = temperature;
2022-06-22 23:06:05 +01:00
TasmotaGlobal.global_update = TasmotaGlobal.uptime;
TasmotaGlobal.user_globals[0] = 1;
}
}
ResponseCmndFloat(TasmotaGlobal.temperature_celsius, 1);
}
}
void CmndGlobalHum(void) {
if (2 == XdrvMailbox.index) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 251)) {
Settings->global_sensor_index[1] = XdrvMailbox.payload;
TasmotaGlobal.user_globals[1] = 0;
}
ResponseCmndIdxNumber(Settings->global_sensor_index[1]);
} else {
if (XdrvMailbox.data_len > 0) {
float humidity = CharToFloat(XdrvMailbox.data);
if ((humidity >= 0.0f) && (humidity <= 100.0f)) {
TasmotaGlobal.humidity = humidity;
2022-06-22 23:06:05 +01:00
TasmotaGlobal.global_update = TasmotaGlobal.uptime;
TasmotaGlobal.user_globals[1] = 1;
}
}
ResponseCmndFloat(TasmotaGlobal.humidity, 1);
}
}
void CmndGlobalPress(void) {
if (2 == XdrvMailbox.index) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 251)) {
Settings->global_sensor_index[2] = XdrvMailbox.payload;
TasmotaGlobal.user_globals[2] = 0;
}
ResponseCmndIdxNumber(Settings->global_sensor_index[2]);
} else {
if (XdrvMailbox.data_len > 0) {
// Set pressure based on SO24 (hPa or mmHg (or inHg based on SO139))
float pressure = ConvertHgToHpa(CharToFloat(XdrvMailbox.data));
// Pressure is now hPa
if ((pressure >= 0.0f) && (pressure <= 1200.0f)) {
TasmotaGlobal.pressure_hpa = pressure;
2022-06-22 23:06:05 +01:00
TasmotaGlobal.global_update = TasmotaGlobal.uptime;
TasmotaGlobal.user_globals[2] = 1;
}
}
ResponseCmndFloat(TasmotaGlobal.pressure_hpa, 1);
}
}
void CmndSleep(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 251)) {
2021-06-11 17:14:12 +01:00
Settings->sleep = XdrvMailbox.payload;
2020-10-29 15:16:34 +00:00
TasmotaGlobal.sleep = XdrvMailbox.payload;
WiFiSetSleepMode();
}
2021-06-11 17:14:12 +01:00
Response_P(S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, XdrvMailbox.command, Settings->sleep, TasmotaGlobal.sleep);
2019-10-22 17:34:41 +01:00
}
void CmndUpgrade(void)
{
// Check if the payload is numerically 1, and had no trailing chars.
// e.g. "1foo" or "1.2.3" could fool us.
// Check if the version we have been asked to upgrade to is higher than our current version.
// We also need at least 3 chars to make a valid version number string.
if (((1 == XdrvMailbox.data_len) && (1 == XdrvMailbox.payload)) || ((XdrvMailbox.data_len >= 3) && NewerVersion(XdrvMailbox.data))) {
2020-10-29 11:21:24 +00:00
TasmotaGlobal.ota_state_flag = 3;
char stemp1[TOPSZ];
2020-10-30 11:29:48 +00:00
Response_P(PSTR("{\"%s\":\"" D_JSON_VERSION " %s " D_JSON_FROM " %s\"}"), XdrvMailbox.command, TasmotaGlobal.version, GetOtaUrl(stemp1, sizeof(stemp1)));
}
#if defined(ESP32) && defined(USE_WEBCLIENT_HTTPS)
else if (EspSingleOtaPartition() && !EspRunningFactoryPartition() && (1 == XdrvMailbox.data_len) && (2 == XdrvMailbox.payload)) {
TasmotaGlobal.ota_factory = true;
TasmotaGlobal.ota_state_flag = 3;
2022-05-11 13:45:09 +01:00
ResponseCmndChar(PSTR("Safeboot"));
}
#endif // ESP32 and WEBCLIENT_HTTPS
else {
2020-10-30 11:29:48 +00:00
Response_P(PSTR("{\"%s\":\"" D_JSON_ONE_OR_GT "\"}"), XdrvMailbox.command, TasmotaGlobal.version);
}
}
void CmndOtaUrl(void)
{
if (XdrvMailbox.data_len > 0) {
2020-05-04 19:00:05 +01:00
SettingsUpdateText(SET_OTAURL, (SC_DEFAULT == Shortcut()) ? PSTR(OTA_URL) : XdrvMailbox.data);
}
ResponseCmndChar(SettingsText(SET_OTAURL));
}
void CmndSeriallog(void)
{
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_DEBUG_MORE)) {
#ifdef ESP32
if (tasconsole_serial) {
#endif // ESP32
2021-06-11 17:14:12 +01:00
Settings->flag.mqtt_serial = 0; // CMND_SERIALSEND and CMND_SERIALLOG
#ifdef ESP32
}
#endif // ESP32
SetTasConlog(XdrvMailbox.payload);
}
2021-06-11 17:14:12 +01:00
Response_P(S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, XdrvMailbox.command, Settings->seriallog_level, TasmotaGlobal.seriallog_level);
}
2021-05-03 10:00:48 +01:00
void CmndRestart(void)
{
switch (XdrvMailbox.payload) {
case 1:
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
2021-01-12 18:31:15 +00:00
ResponseCmndChar(PSTR(D_JSON_RESTARTING));
break;
case 2:
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
2020-10-30 11:29:48 +00:00
TasmotaGlobal.restart_halt = true;
2021-01-12 18:31:15 +00:00
ResponseCmndChar(PSTR(D_JSON_HALTING));
break;
#ifdef ESP32
case 3:
if (EspPrepSwitchPartition(2)) { // Toggle partition between safeboot and production
TasmotaGlobal.restart_flag = 2;
ResponseCmndChar(PSTR("Switching"));
}
break;
#endif // ESP32
case -1:
CmndCrash(); // force a crash
break;
2019-12-28 12:14:51 +00:00
case -2:
CmndWDT();
break;
2019-12-28 13:54:26 +00:00
case -3:
CmndBlockedLoop();
break;
case 99:
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_RESTARTING));
EspRestart();
break;
default:
2020-03-16 17:55:58 +00:00
ResponseCmndChar_P(PSTR(D_JSON_ONE_TO_RESTART));
}
}
void CmndPowerOnState(void)
{
#ifdef ESP8266
2020-10-30 11:29:48 +00:00
if (TasmotaGlobal.module_type != MOTOR)
#endif // ESP8266
{
/* 0 = Keep relays off after power on
* 1 = Turn relays on after power on, if PulseTime set wait for PulseTime seconds, and turn relays off
* 2 = Toggle relays after power on
* 3 = Set relays to last saved state after power on
* 4 = Turn relays on and disable any relay control (used for Sonoff Pow to always measure power)
* 5 = Keep relays off after power on, if PulseTime set wait for PulseTime seconds, and turn relays on
*/
if ((XdrvMailbox.payload >= POWER_ALL_OFF) && (XdrvMailbox.payload <= POWER_ALL_OFF_PULSETIME_ON)) {
2021-06-11 17:14:12 +01:00
Settings->poweronstate = XdrvMailbox.payload;
if (POWER_ALL_ALWAYS_ON == Settings->poweronstate) {
2020-10-30 11:29:48 +00:00
for (uint32_t i = 1; i <= TasmotaGlobal.devices_present; i++) {
ExecuteCommandPower(i, POWER_ON, SRC_IGNORE);
}
}
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->poweronstate);
}
}
void CmndPulsetime(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_PULSETIMERS)) {
/*
uint32_t items = 1;
if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) {
items = MAX_PULSETIMERS;
} else {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 65536)) {
2021-06-11 17:14:12 +01:00
Settings->pulse_timer[XdrvMailbox.index -1] = XdrvMailbox.payload; // 0 - 65535
SetPulseTimer(XdrvMailbox.index -1, XdrvMailbox.payload);
}
}
2020-10-30 11:29:48 +00:00
ResponseClear();
for (uint32_t i = 0; i < items; i++) {
uint32_t index = (1 == items) ? XdrvMailbox.index : i +1;
ResponseAppend_P(PSTR("%c\"%s%d\":{\"" D_JSON_SET "\":%d,\"" D_JSON_REMAINING "\":%d}"),
(i) ? ',' : '{',
XdrvMailbox.command, index,
2021-06-11 17:14:12 +01:00
Settings->pulse_timer[index -1], GetPulseTimer(index -1));
}
ResponseJsonEnd();
*/
if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) {
Response_P(PSTR("{\"" D_CMND_PULSETIME "\":{\"" D_JSON_SET "\":["));
for (uint32_t i = 0; i < MAX_PULSETIMERS; i++) {
ResponseAppend_P(PSTR("%s%d"), (i)?",":"", Settings->pulse_timer[i]);
}
ResponseAppend_P(PSTR("],\"" D_JSON_REMAINING "\":["));
for (uint32_t i = 0; i < MAX_PULSETIMERS; i++) {
ResponseAppend_P(PSTR("%s%d"), (i)?",":"", GetPulseTimer(i));
}
ResponseAppend_P(PSTR("]}}"));
} else {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 65536)) {
Settings->pulse_timer[XdrvMailbox.index -1] = XdrvMailbox.payload; // 0 - 65535
SetPulseTimer(XdrvMailbox.index -1, XdrvMailbox.payload);
}
uint32_t index = XdrvMailbox.index;
Response_P(PSTR("{\"%s%d\":{\"" D_JSON_SET "\":%d,\"" D_JSON_REMAINING "\":%d}}"),
XdrvMailbox.command, index,
Settings->pulse_timer[index -1], GetPulseTimer(index -1));
}
}
}
void CmndBlinktime(void)
{
if ((XdrvMailbox.payload > 1) && (XdrvMailbox.payload <= 3600)) {
2021-06-11 17:14:12 +01:00
Settings->blinktime = XdrvMailbox.payload;
2020-10-28 16:32:07 +00:00
if (TasmotaGlobal.blink_timer > 0) {
TasmotaGlobal.blink_timer = millis() + (100 * XdrvMailbox.payload);
}
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->blinktime);
}
void CmndBlinkcount(void)
{
2022-06-03 14:15:25 +01:00
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 32000)) {
2021-06-11 17:14:12 +01:00
Settings->blinkcount = XdrvMailbox.payload; // 0 - 65535
if (TasmotaGlobal.blink_counter) { TasmotaGlobal.blink_counter = Settings->blinkcount *2; }
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->blinkcount);
}
void CmndSavedata(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3600)) {
2021-06-11 17:14:12 +01:00
Settings->save_data = XdrvMailbox.payload;
TasmotaGlobal.save_data_counter = Settings->save_data;
}
SettingsSaveAll();
char stemp1[TOPSZ];
2021-06-11 17:14:12 +01:00
if (Settings->save_data > 1) {
snprintf_P(stemp1, sizeof(stemp1), PSTR(D_JSON_EVERY " %d " D_UNIT_SECOND), Settings->save_data);
}
2021-06-11 17:14:12 +01:00
ResponseCmndChar((Settings->save_data > 1) ? stemp1 : GetStateText(Settings->save_data));
}
2021-01-26 11:03:08 +00:00
void CmndSetoption(void) {
2020-04-21 09:03:34 +01:00
snprintf_P(XdrvMailbox.command, CMDSZ, PSTR(D_CMND_SETOPTION)); // Rename result shortcut command SO to SetOption
2021-01-25 16:44:17 +00:00
CmndSetoptionBase(1);
2021-01-25 16:35:47 +00:00
}
// Code called by SetOption and by Berry
bool SetoptionDecode(uint32_t index, uint32_t *ptype, uint32_t *pindex) {
if (index < 178) {
2021-06-11 17:14:12 +01:00
if (index <= 31) { // SetOption0 .. 31 = Settings->flag
*ptype = 2;
*pindex = index; // 0 .. 31
}
2021-06-11 17:14:12 +01:00
else if (index <= 49) { // SetOption32 .. 49 = Settings->param
*ptype = 1;
*pindex = index -32; // 0 .. 17 (= PARAM8_SIZE -1)
}
2021-06-11 17:14:12 +01:00
else if (index <= 81) { // SetOption50 .. 81 = Settings->flag3
*ptype = 3;
*pindex = index -50; // 0 .. 31
}
else if (index <= 113) { // SetOption82 .. 113 = Settings->flag4
*ptype = 4;
*pindex = index -82; // 0 .. 31
2019-10-31 11:12:48 +00:00
}
else if (index <= 145) { // SetOption114 .. 145 = Settings->flag5
*ptype = 5;
*pindex = index -114; // 0 .. 31
}
else { // SetOption146 .. 177 = Settings->flag6
*ptype = 6;
*pindex = index -146; // 0 .. 31
}
return true;
}
return false;
}
uint32_t GetOption(uint32_t index) {
uint32_t ptype;
uint32_t pindex;
if (SetoptionDecode(index, &ptype, &pindex)) {
if (1 == ptype) {
2021-06-11 17:14:12 +01:00
return Settings->param[pindex];
} else {
2021-06-11 17:14:12 +01:00
uint32_t flag = Settings->flag.data;
if (3 == ptype) {
2021-06-11 17:14:12 +01:00
flag = Settings->flag3.data;
}
else if (4 == ptype) {
2021-06-11 17:14:12 +01:00
flag = Settings->flag4.data;
}
else if (5 == ptype) {
2021-06-11 17:14:12 +01:00
flag = Settings->flag5.data;
}
else if (6 == ptype) {
flag = Settings->flag6.data;
}
return bitRead(flag, pindex);
2020-08-13 15:49:11 +01:00
}
} else {
return 0; // fallback
}
}
void CmndSetoptionBase(bool indexed) {
// Allow a command to access a single SetOption by it's command name
// indexed = 0 : No index will be returned attached to the command
// {"ClockDirection":"OFF"}
// indexed = 1 : The SetOption index will be returned with the command
// {"SetOption16":"OFF"}
uint32_t ptype;
uint32_t pindex;
if (SetoptionDecode(XdrvMailbox.index, &ptype, &pindex)) {
2019-10-31 12:18:00 +00:00
if (XdrvMailbox.payload >= 0) {
2019-10-31 12:18:00 +00:00
if (1 == ptype) { // SetOption32 .. 49
uint32_t param_low = 0;
uint32_t param_high = 255;
switch (pindex) {
case P_HOLD_TIME:
case P_MAX_POWER_RETRY:
case P_BISTABLE_PULSE:
param_low = 1;
param_high = 250;
break;
}
if ((XdrvMailbox.payload >= param_low) && (XdrvMailbox.payload <= param_high)) {
2021-06-11 17:14:12 +01:00
Settings->param[pindex] = XdrvMailbox.payload;
#ifdef USE_LIGHT
if (P_RGB_REMAP == pindex) {
LightUpdateColorMapping();
2020-10-29 11:58:22 +00:00
TasmotaGlobal.restart_flag = 2; // SetOption37 needs a reboot in most cases
}
#endif
#if (defined(USE_IR_REMOTE) && defined(USE_IR_RECEIVE)) || defined(USE_IR_REMOTE_FULL)
if (P_IR_UNKNOW_THRESHOLD == pindex) {
IrReceiveUpdateThreshold(); // SetOption38
}
if (P_IR_TOLERANCE == pindex) {
IrReceiveUpdateTolerance(); // SetOption44
}
#endif
#ifdef ROTARY_V1
if (P_ROTARY_MAX_STEP == pindex) {
RotaryInitMaxSteps(); // SetOption43
}
#endif
2019-10-31 12:18:00 +00:00
} else {
ptype = 99; // Command Error
}
} else {
if (XdrvMailbox.payload <= 1) {
if (2 == ptype) { // SetOption0 .. 31
switch (pindex) {
case 5: // mqtt_power_retain (CMND_POWERRETAIN)
case 6: // mqtt_button_retain (CMND_BUTTONRETAIN)
case 7: // mqtt_switch_retain (CMND_SWITCHRETAIN)
case 9: // mqtt_sensor_retain (CMND_SENSORRETAIN)
case 14: // interlock (CMND_INTERLOCK)
case 22: // mqtt_serial (SerialSend and SerialLog)
case 23: // mqtt_serial_raw (SerialSend)
case 25: // knx_enabled (Web config)
case 27: // knx_enable_enhancement (Web config)
ptype = 99; // Command Error
break; // Ignore command SetOption
case 3: // mqtt
case 15: // pwm_control
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
2019-10-31 12:18:00 +00:00
default:
2021-06-11 17:14:12 +01:00
bitWrite(Settings->flag.data, pindex, XdrvMailbox.payload);
2019-10-31 12:18:00 +00:00
}
if (12 == pindex) { // stop_flash_rotate
2020-10-30 11:29:48 +00:00
TasmotaGlobal.stop_flash_rotate = XdrvMailbox.payload;
2019-10-31 12:18:00 +00:00
SettingsSave(2);
}
#ifdef USE_HOME_ASSISTANT
if ((19 == pindex) || (30 == pindex) || (114 == pindex)) {
2019-10-31 12:18:00 +00:00
HAssDiscover(); // Delayed execution to provide enough resources during hass_discovery or hass_light
}
#endif // USE_HOME_ASSISTANT
#ifdef USE_TASMOTA_DISCOVERY
if (19 == pindex) {
TasRediscover();
}
#endif // USE_TASMOTA_DISCOVERY
2019-10-31 12:18:00 +00:00
}
else if (3 == ptype) { // SetOption50 .. 81
2021-06-11 17:14:12 +01:00
bitWrite(Settings->flag3.data, pindex, XdrvMailbox.payload);
2019-10-31 12:18:00 +00:00
switch (pindex) {
case 5: // SetOption55
if (0 == XdrvMailbox.payload) {
2020-10-29 11:58:22 +00:00
TasmotaGlobal.restart_flag = 2; // Disable mDNS needs restart
2019-10-31 12:18:00 +00:00
}
break;
case 10: // SetOption60 enable or disable traditional sleep
WiFiSetSleepMode(); // Update WiFi sleep mode accordingly
break;
case 18: // SetOption68 for multi-channel PWM, requires a reboot
case 25: // SetOption75 grouptopic change
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
2019-10-31 12:18:00 +00:00
break;
}
}
else if (4 == ptype) { // SetOption82 .. 113
2021-06-11 17:14:12 +01:00
bitWrite(Settings->flag4.data, pindex, XdrvMailbox.payload);
switch (pindex) {
2020-12-29 18:31:27 +00:00
#ifdef USE_LIGHT
case 0: // SetOption 82 - (Alexa) Reduced CT range for Alexa (1)
setAlexaCTRange();
break;
#endif
case 3: // SetOption85 - Enable Device Groups
case 6: // SetOption88 - PWM Dimmer Buttons control remote devices
2020-06-29 15:01:55 +01:00
case 15: // SetOption97 - Set Baud rate for TuyaMCU serial communication (0 = 9600 or 1 = 115200)
case 20: // SetOption102 - Set Baud rate for Teleinfo serial communication (0 = 1200 or 1 = 9600)
case 21: // SetOption103 - Enable TLS mode (requires TLS version)
case 22: // SetOption104 - No Retain - disable all MQTT retained messages, some brokers don't support it: AWS IoT, Losant
case 24: // SetOption106 - Virtual CT - Creates a virtual White ColorTemp for RGBW lights
case 25: // SetOption107 - Virtual CT Channel - signals whether the hardware white is cold CW (true) or warm WW (false)
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
break;
#ifdef USE_PWM_DIMMER
2022-08-10 18:14:58 +01:00
case 5: // SetOption87 - (PWM Dimmer) Turn red LED on (1) when powered off
TasmotaGlobal.restore_powered_off_led_counter = 1;
break;
#endif // USE_PWM_DIMMER
}
2019-10-31 12:18:00 +00:00
}
2020-08-13 15:49:11 +01:00
else if (5 == ptype) { // SetOption114 .. 145
2021-06-11 17:14:12 +01:00
bitWrite(Settings->flag5.data, pindex, XdrvMailbox.payload);
switch (pindex) {
case 1: // SetOption115 - Enable ESP32 MI32
if (0 == XdrvMailbox.payload) {
TasmotaGlobal.restart_flag = 2;
}
break;
case 18: // SetOption132 - TLS Fingerprint
TasmotaGlobal.restart_flag = 2;
break;
}
2020-08-13 15:49:11 +01:00
}
else if (6 == ptype) { // SetOption146 .. 177
bitWrite(Settings->flag6.data, pindex, XdrvMailbox.payload);
switch (pindex) {
case 5: // SetOption151 - Matter enabled
TasmotaGlobal.restart_flag = 2;
break;
}
}
2019-10-31 12:18:00 +00:00
} else {
ptype = 99; // Command Error
}
}
}
2019-10-31 12:18:00 +00:00
if (ptype < 99) {
2019-10-31 12:18:00 +00:00
if (1 == ptype) {
2021-01-25 16:44:17 +00:00
if (indexed) {
2021-06-11 17:14:12 +01:00
ResponseCmndIdxNumber(Settings->param[pindex]);
2021-01-25 16:44:17 +00:00
} else {
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->param[pindex]);
2021-01-25 16:44:17 +00:00
}
2019-10-31 11:12:48 +00:00
} else {
2021-06-11 17:14:12 +01:00
uint32_t flag = Settings->flag.data;
2019-10-31 12:18:00 +00:00
if (3 == ptype) {
2021-06-11 17:14:12 +01:00
flag = Settings->flag3.data;
2019-10-31 11:12:48 +00:00
}
2019-10-31 12:18:00 +00:00
else if (4 == ptype) {
2021-06-11 17:14:12 +01:00
flag = Settings->flag4.data;
2019-10-31 11:12:48 +00:00
}
2020-08-13 15:49:11 +01:00
else if (5 == ptype) {
2021-06-11 17:14:12 +01:00
flag = Settings->flag5.data;
2020-08-13 15:49:11 +01:00
}
else if (6 == ptype) {
flag = Settings->flag6.data;
}
2021-01-25 16:44:17 +00:00
if (indexed) {
ResponseCmndIdxChar(GetStateText(bitRead(flag, pindex)));
} else {
ResponseCmndChar(GetStateText(bitRead(flag, pindex)));
}
2019-10-31 11:12:48 +00:00
}
}
}
}
void CmndTemperatureResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.temperature_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.temperature_resolution);
}
void CmndHumidityResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.humidity_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.humidity_resolution);
}
void CmndPressureResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.pressure_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.pressure_resolution);
}
void CmndPowerResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.wattage_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.wattage_resolution);
}
void CmndVoltageResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.voltage_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.voltage_resolution);
}
void CmndFrequencyResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.frequency_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.frequency_resolution);
}
void CmndCurrentResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.current_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.current_resolution);
}
void CmndEnergyResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 5)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.energy_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.energy_resolution);
}
void CmndWeightResolution(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.weight_resolution = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.weight_resolution);
}
void CmndSpeedUnit(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.speed_conversion = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->flag2.speed_conversion);
}
void CmndModule(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= MAXMODULE)) {
bool present = false;
if (0 == XdrvMailbox.payload) {
XdrvMailbox.payload = USER_MODULE;
present = true;
} else {
XdrvMailbox.payload--;
present = ValidTemplateModule(XdrvMailbox.payload);
}
if (present) {
if (XdrvMailbox.index == 2) {
2021-06-11 17:14:12 +01:00
Settings->fallback_module = XdrvMailbox.payload;
} else {
2021-06-11 17:14:12 +01:00
Settings->last_module = Settings->module;
Settings->module = XdrvMailbox.payload;
SetModuleType();
2021-06-11 17:14:12 +01:00
if (Settings->last_module != XdrvMailbox.payload) {
for (uint32_t i = 0; i < nitems(Settings->my_gp.io); i++) {
Settings->my_gp.io[i] = GPIO_NONE;
}
}
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
}
}
}
2021-06-11 17:14:12 +01:00
uint8_t module_real = Settings->module;
uint8_t module_number = ModuleNr();
if (XdrvMailbox.index == 2) {
2021-06-11 17:14:12 +01:00
module_real = Settings->fallback_module;
module_number = (USER_MODULE == Settings->fallback_module) ? 0 : Settings->fallback_module +1;
strcat(XdrvMailbox.command, "2");
}
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, module_number, AnyModuleName(module_real).c_str());
}
void CmndModules(void)
{
uint32_t midx = USER_MODULE;
#ifdef MQTT_DATA_STRING
Response_P(PSTR("{\"" D_CMND_MODULES "\":{"));
for (uint32_t i = 0; i <= sizeof(kModuleNiceList); i++) {
if (i > 0) {
midx = pgm_read_byte(kModuleNiceList + i -1);
ResponseAppend_P(PSTR(","));
}
uint32_t j = i ? midx +1 : 0;
ResponseAppend_P(PSTR("\"%d\":\"%s\""), j, AnyModuleName(midx).c_str());
}
ResponseJsonEndEnd();
#else
uint32_t lines = 1;
bool jsflg = false;
for (uint32_t i = 0; i <= sizeof(kModuleNiceList); i++) {
if (i > 0) { midx = pgm_read_byte(kModuleNiceList + i -1); }
if (!jsflg) {
Response_P(PSTR("{\"" D_CMND_MODULES "%d\":{"), lines);
} else {
ResponseAppend_P(PSTR(","));
}
jsflg = true;
uint32_t j = i ? midx +1 : 0;
if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), j, AnyModuleName(midx).c_str()) > (MAX_LOGSZ - TOPSZ)) || (i == sizeof(kModuleNiceList))) {
ResponseJsonEndEnd();
2021-04-07 14:07:05 +01:00
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, XdrvMailbox.command);
jsflg = false;
lines++;
}
}
2020-10-30 11:29:48 +00:00
ResponseClear();
#endif
}
void CmndGpio(void)
{
2021-06-11 17:14:12 +01:00
if (XdrvMailbox.index < nitems(Settings->my_gp.io)) {
myio template_gp;
TemplateGpios(&template_gp);
if (ValidGPIO(XdrvMailbox.index, template_gp.io[XdrvMailbox.index]) && (XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < AGPIO(GPIO_SENSOR_END))) {
bool present = false;
2021-02-28 11:50:02 +00:00
for (uint32_t i = 0; i < nitems(kGpioNiceList); i++) {
2020-04-30 17:47:34 +01:00
uint32_t midx = pgm_read_word(kGpioNiceList + i);
uint32_t max_midx = ((midx & 0x001F) > 0) ? midx : midx +1;
if ((XdrvMailbox.payload >= (midx & 0xFFE0)) && (XdrvMailbox.payload < max_midx)) {
2020-04-29 16:44:03 +01:00
present = true;
break;
}
}
if (present) {
2021-06-11 17:14:12 +01:00
for (uint32_t i = 0; i < nitems(Settings->my_gp.io); i++) {
if (ValidGPIO(i, template_gp.io[i]) && (Settings->my_gp.io[i] == XdrvMailbox.payload)) {
Settings->my_gp.io[i] = GPIO_NONE;
}
}
2021-06-11 17:14:12 +01:00
Settings->my_gp.io[XdrvMailbox.index] = XdrvMailbox.payload;
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
}
}
bool jsflg = false;
bool jsflg2 = false;
2021-06-11 17:14:12 +01:00
for (uint32_t i = 0; i < nitems(Settings->my_gp.io); i++) {
if (ValidGPIO(i, template_gp.io[i]) || ((255 == XdrvMailbox.payload) && !FlashPin(i))) {
if (!jsflg) {
Response_P(PSTR("{"));
} else {
ResponseAppend_P(PSTR(","));
}
jsflg = true;
2021-06-11 17:14:12 +01:00
uint32_t sensor_type = Settings->my_gp.io[i];
if (!ValidGPIO(i, template_gp.io[i])) {
sensor_type = template_gp.io[i];
2020-04-30 17:47:34 +01:00
if (AGPIO(GPIO_USER) == sensor_type) { // A user GPIO equals a not connected (=GPIO_NONE) GPIO here
sensor_type = GPIO_NONE;
}
}
2020-04-30 17:47:34 +01:00
char sindex[4] = { 0 };
2020-06-24 14:58:56 +01:00
uint32_t sensor_name_idx = BGPIO(sensor_type);
2020-04-30 17:47:34 +01:00
uint32_t nice_list_search = sensor_type & 0xFFE0;
2021-02-28 11:50:02 +00:00
for (uint32_t j = 0; j < nitems(kGpioNiceList); j++) {
2020-04-30 17:47:34 +01:00
uint32_t nls_idx = pgm_read_word(kGpioNiceList + j);
if (((nls_idx & 0xFFE0) == nice_list_search) && ((nls_idx & 0x001F) > 0)) {
snprintf_P(sindex, sizeof(sindex), PSTR("%d"), (sensor_type & 0x001F) +1);
break;
}
}
const char *sensor_names = kSensorNames;
2020-04-29 16:44:03 +01:00
if (sensor_name_idx > GPIO_FIX_START) {
sensor_name_idx = sensor_name_idx - GPIO_FIX_START -1;
sensor_names = kSensorNamesFixed;
}
char stemp1[TOPSZ];
#ifdef MQTT_DATA_STRING
ResponseAppend_P(PSTR("\"" D_CMND_GPIO "%d\":{\"%d\":\"%s%s\"}"), i, sensor_type, GetTextIndexed(stemp1, sizeof(stemp1), sensor_name_idx, sensor_names), sindex);
jsflg2 = true;
#else
2021-01-24 15:29:56 +00:00
if ((ResponseAppend_P(PSTR("\"" D_CMND_GPIO "%d\":{\"%d\":\"%s%s\"}"), i, sensor_type, GetTextIndexed(stemp1, sizeof(stemp1), sensor_name_idx, sensor_names), sindex) > (MAX_LOGSZ - TOPSZ))) {
2020-12-23 10:49:55 +00:00
ResponseJsonEnd();
2021-04-07 14:07:05 +01:00
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, XdrvMailbox.command);
2021-01-23 17:17:55 +00:00
ResponseClear();
jsflg2 = true;
jsflg = false;
}
#endif
}
}
2021-01-23 17:17:55 +00:00
if (jsflg) {
ResponseJsonEnd();
} else {
2021-01-23 17:17:55 +00:00
if (!jsflg2) {
ResponseCmndChar(PSTR(D_JSON_NOT_SUPPORTED));
}
}
}
}
2020-09-29 17:10:21 +01:00
void ShowGpios(const uint16_t *NiceList, uint32_t size, uint32_t offset, uint32_t &lines) {
uint32_t ridx;
uint32_t midx;
bool jsflg = false;
2020-09-29 17:10:21 +01:00
for (uint32_t i = offset; i < size; i++) { // Skip ADC_NONE
if (NiceList == nullptr) {
ridx = AGPIO(i);
midx = i;
} else {
ridx = pgm_read_word(NiceList + i) & 0xFFE0;
midx = BGPIO(ridx);
}
if (!jsflg) {
Response_P(PSTR("{\"" D_CMND_GPIOS "%d\":{"), lines);
} else {
ResponseAppend_P(PSTR(","));
}
jsflg = true;
char stemp1[TOPSZ];
if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), ridx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)) > (MAX_LOGSZ - TOPSZ)) || (i == size -1)) {
ResponseJsonEndEnd();
2021-04-07 14:07:05 +01:00
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, XdrvMailbox.command);
jsflg = false;
lines++;
}
}
2020-09-29 17:10:21 +01:00
}
void CmndGpios(void)
{
uint32_t lines = 1;
if (XdrvMailbox.payload == 255) {
// DumpConvertTable();
ShowGpios(nullptr, GPIO_SENSOR_END, 0, lines);
} else {
2021-02-28 11:50:02 +00:00
ShowGpios(kGpioNiceList, nitems(kGpioNiceList), 0, lines);
2020-09-29 17:10:21 +01:00
#ifdef ESP8266
#ifndef USE_ADC_VCC
2021-02-28 11:50:02 +00:00
ShowGpios(kAdcNiceList, nitems(kAdcNiceList), 1, lines);
2020-09-29 17:10:21 +01:00
#endif // USE_ADC_VCC
#endif // ESP8266
}
2020-10-30 11:29:48 +00:00
ResponseClear();
}
void CmndTemplate(void)
{
2020-09-29 17:10:21 +01:00
// {"NAME":"Shelly 2.5","GPIO":[320,0,32,0,224,193,0,0,640,192,608,225,3456,4736],"FLAG":0,"BASE":18}
bool error = false;
if (strchr(XdrvMailbox.data, '{') == nullptr) { // If no JSON it must be parameter
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload <= MAXMODULE)) {
XdrvMailbox.payload--;
if (ValidTemplateModule(XdrvMailbox.payload)) {
ModuleDefault(XdrvMailbox.payload); // Copy template module
2021-06-11 17:14:12 +01:00
if (USER_MODULE == Settings->module) { TasmotaGlobal.restart_flag = 2; }
}
}
else if (0 == XdrvMailbox.payload) { // Copy current template to user template
2021-06-11 17:14:12 +01:00
if (Settings->module != USER_MODULE) {
ModuleDefault(Settings->module);
}
}
else if (255 == XdrvMailbox.payload) { // Copy current module with user configured GPIO
2021-06-11 17:14:12 +01:00
if (Settings->module != USER_MODULE) {
ModuleDefault(Settings->module);
}
2021-01-18 20:48:04 +00:00
SettingsUpdateText(SET_TEMPLATE_NAME, PSTR("Merged"));
uint32_t j = 0;
2021-06-11 17:14:12 +01:00
for (uint32_t i = 0; i < nitems(Settings->user_template.gp.io); i++) {
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32C3
#else
if (6 == i) { j = 9; }
if (8 == i) { j = 12; }
#endif
2020-10-30 11:29:48 +00:00
if (TasmotaGlobal.my_module.io[j] > GPIO_NONE) {
2021-06-11 17:14:12 +01:00
Settings->user_template.gp.io[i] = TasmotaGlobal.my_module.io[j];
}
j++;
}
}
}
else {
#ifndef FIRMWARE_MINIMAL // if tasmota-minimal, `Template` is read-only
if (JsonTemplate(XdrvMailbox.data)) {
2021-06-11 17:14:12 +01:00
if (USER_MODULE == Settings->module) { TasmotaGlobal.restart_flag = 2; }
} else {
2020-03-16 17:55:58 +00:00
ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON));
error = true;
}
#endif // FIRMWARE_MINIMAL
}
if (!error) { TemplateJson(); }
}
void CmndButtonDebounce(void)
{
if ((XdrvMailbox.payload > 39) && (XdrvMailbox.payload < 1001)) {
2021-06-11 17:14:12 +01:00
Settings->button_debounce = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->button_debounce);
}
void CmndSwitchDebounce(void)
{
if ((XdrvMailbox.payload > 39) && (XdrvMailbox.payload < 1010)) {
2021-06-11 17:14:12 +01:00
Settings->switch_debounce = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->switch_debounce);
}
void CmndBaudrate(void)
{
if (XdrvMailbox.payload >= 300) {
XdrvMailbox.payload /= 300; // Make it a valid baudrate
2020-10-28 16:32:07 +00:00
TasmotaGlobal.baudrate = (XdrvMailbox.payload & 0xFFFF) * 300;
SetSerialBaudrate(TasmotaGlobal.baudrate);
}
2020-10-28 16:32:07 +00:00
ResponseCmndNumber(TasmotaGlobal.baudrate);
}
void CmndSerialConfig(void)
{
// See TasmotaSerialConfig for possible options
// SerialConfig 0..23 where 3 equals 8N1
// SerialConfig 8N1
if (XdrvMailbox.data_len > 0) {
if (XdrvMailbox.data_len < 3) { // Use 0..23 as serial config option
if ((XdrvMailbox.payload >= TS_SERIAL_5N1) && (XdrvMailbox.payload <= TS_SERIAL_8O2)) {
SetSerialConfig(XdrvMailbox.payload);
}
}
else if ((XdrvMailbox.payload >= 5) && (XdrvMailbox.payload <= 8)) {
2021-11-04 16:14:34 +00:00
int8_t serial_config = ParseSerialConfig(XdrvMailbox.data);
if (serial_config >= 0) {
SetSerialConfig(serial_config);
}
}
}
ResponseCmndChar(GetSerialConfig().c_str());
}
void CmndSerialBuffer(void) {
// Allow non-pesistent serial receive buffer size change
2022-08-28 15:49:52 +01:00
// between MIN_INPUT_BUFFER_SIZE and MAX_INPUT_BUFFER_SIZE characters
size_t size = 0;
if (XdrvMailbox.data_len > 0) {
size = XdrvMailbox.payload;
2022-08-28 15:49:52 +01:00
if (1 == XdrvMailbox.payload) {
size = INPUT_BUFFER_SIZE;
}
2022-08-28 15:49:52 +01:00
else if (XdrvMailbox.payload < MIN_INPUT_BUFFER_SIZE) {
size = MIN_INPUT_BUFFER_SIZE;
}
else if (XdrvMailbox.payload > MAX_INPUT_BUFFER_SIZE) {
size = MAX_INPUT_BUFFER_SIZE;
}
Serial.setRxBufferSize(size);
}
#ifdef ESP8266
ResponseCmndNumber(Serial.getRxBufferSize());
#endif
#ifdef ESP32
if (size) {
ResponseCmndNumber(size);
} else {
ResponseCmndDone();
}
#endif
}
void CmndSerialSend(void) {
if (XdrvMailbox.index > 9) { XdrvMailbox.index -= 10; }
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 6)) {
SetSeriallog(LOG_LEVEL_NONE);
2021-06-11 17:14:12 +01:00
Settings->flag.mqtt_serial = 1; // CMND_SERIALSEND and CMND_SERIALLOG
Settings->flag.mqtt_serial_raw = (XdrvMailbox.index > 3) ? 1 : 0; // CMND_SERIALSEND3
if (XdrvMailbox.data_len > 0) {
if (1 == XdrvMailbox.index) {
Serial.printf("%s\n", XdrvMailbox.data); // "Hello Tiger\n"
}
else if (2 == XdrvMailbox.index || 4 == XdrvMailbox.index) {
for (uint32_t i = 0; i < XdrvMailbox.data_len; i++) {
Serial.write(XdrvMailbox.data[i]); // "Hello Tiger" or "A0"
}
}
else if (3 == XdrvMailbox.index) {
uint32_t dat_len = XdrvMailbox.data_len;
Serial.printf("%s", Unescape(XdrvMailbox.data, &dat_len)); // "Hello\f"
}
else if (5 == XdrvMailbox.index) {
SerialSendRaw(RemoveSpace(XdrvMailbox.data)); // "AA004566" as hex values
}
else if (6 == XdrvMailbox.index) {
SerialSendDecimal(XdrvMailbox.data);
}
2019-08-03 12:01:34 +01:00
ResponseCmndDone();
}
}
}
void CmndSerialDelimiter(void)
{
if ((XdrvMailbox.data_len > 0) && (XdrvMailbox.payload < 256)) {
if (XdrvMailbox.payload > 0) {
2021-06-11 17:14:12 +01:00
Settings->serial_delimiter = XdrvMailbox.payload;
} else {
uint32_t dat_len = XdrvMailbox.data_len;
Unescape(XdrvMailbox.data, &dat_len);
2021-06-11 17:14:12 +01:00
Settings->serial_delimiter = XdrvMailbox.data[0];
}
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->serial_delimiter);
}
void CmndSyslog(void)
{
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_DEBUG_MORE)) {
SetSyslog(XdrvMailbox.payload);
}
2021-06-11 17:14:12 +01:00
Response_P(S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, XdrvMailbox.command, Settings->syslog_level, TasmotaGlobal.syslog_level);
}
void CmndLoghost(void)
{
if (XdrvMailbox.data_len > 0) {
SettingsUpdateText(SET_SYSLOG_HOST, (SC_DEFAULT == Shortcut()) ? SYS_LOG_HOST : XdrvMailbox.data);
}
ResponseCmndChar(SettingsText(SET_SYSLOG_HOST));
}
void CmndLogport(void)
{
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload < 65536)) {
2021-06-11 17:14:12 +01:00
Settings->syslog_port = (1 == XdrvMailbox.payload) ? SYS_LOG_PORT : XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->syslog_port);
}
void CmndIpAddress(void)
{
2021-07-29 15:57:04 +01:00
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) {
2021-01-27 11:03:20 +00:00
char network_address[22];
ext_snprintf_P(network_address, sizeof(network_address), PSTR(" (%_I)"), (uint32_t)WiFi.localIP());
if (!XdrvMailbox.usridx) {
ResponseClear();
2021-07-29 15:57:04 +01:00
for (uint32_t i = 0; i < 5; i++) {
2021-06-11 17:14:12 +01:00
ResponseAppend_P(PSTR("%c\"%s%d\":\"%_I%s\""), (i)?',':'{', XdrvMailbox.command, i +1, Settings->ipv4_address[i], (0 == i)?network_address:"");
}
ResponseJsonEnd();
} else {
2021-01-18 15:32:58 +00:00
uint32_t ipv4_address;
if (ParseIPv4(&ipv4_address, XdrvMailbox.data)) {
2021-06-11 17:14:12 +01:00
Settings->ipv4_address[XdrvMailbox.index -1] = ipv4_address;
}
2021-06-11 17:14:12 +01:00
Response_P(PSTR("{\"%s%d\":\"%_I%s\"}"), XdrvMailbox.command, XdrvMailbox.index, Settings->ipv4_address[XdrvMailbox.index -1], (1 == XdrvMailbox.index)?network_address:"");
}
}
}
void CmndNtpServer(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_NTP_SERVERS)) {
if (!XdrvMailbox.usridx) {
ResponseCmndAll(SET_NTPSERVER1, MAX_NTP_SERVERS);
} else {
uint32_t ntp_server = SET_NTPSERVER1 + XdrvMailbox.index -1;
if (XdrvMailbox.data_len > 0) {
SettingsUpdateText(ntp_server,
2020-05-04 19:00:05 +01:00
(SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? (1 == XdrvMailbox.index) ? PSTR(NTP_SERVER1) : (2 == XdrvMailbox.index) ? PSTR(NTP_SERVER2) : PSTR(NTP_SERVER3) : XdrvMailbox.data);
SettingsUpdateText(ntp_server, ReplaceCommaWithDot(SettingsText(ntp_server)));
2020-10-29 11:21:24 +00:00
// TasmotaGlobal.restart_flag = 2; // Issue #3890
2020-10-30 11:29:48 +00:00
TasmotaGlobal.ntp_force_sync = true;
}
ResponseCmndIdxChar(SettingsText(ntp_server));
}
}
}
void CmndAp(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
switch (XdrvMailbox.payload) {
case 0: // Toggle
2021-06-11 17:14:12 +01:00
Settings->sta_active ^= 1;
break;
case 1: // AP1
case 2: // AP2
2021-06-11 17:14:12 +01:00
Settings->sta_active = XdrvMailbox.payload -1;
}
2021-06-11 17:14:12 +01:00
Settings->wifi_channel = 0; // Disable stored AP
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
}
2021-06-11 17:14:12 +01:00
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, Settings->sta_active +1, EscapeJSONString(SettingsText(SET_STASSID1 + Settings->sta_active)).c_str());
}
void CmndSsid(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SSIDS)) {
if (!XdrvMailbox.usridx) {
ResponseCmndAll(SET_STASSID1, MAX_SSIDS);
} else {
if (XdrvMailbox.data_len > 0) {
SettingsUpdateText(SET_STASSID1 + XdrvMailbox.index -1,
(SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? (1 == XdrvMailbox.index) ? STA_SSID1 : STA_SSID2 : XdrvMailbox.data);
2021-06-11 17:14:12 +01:00
Settings->sta_active = XdrvMailbox.index -1;
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
}
ResponseCmndIdxChar(SettingsText(SET_STASSID1 + XdrvMailbox.index -1));
}
}
}
void CmndPassword(void)
{
2021-04-08 16:57:37 +01:00
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 4)) {
bool show_asterisk = (XdrvMailbox.index > 2);
if (show_asterisk) {
XdrvMailbox.index -= 2;
}
if ((XdrvMailbox.data_len > 4) || (SC_CLEAR == Shortcut()) || (SC_DEFAULT == Shortcut())) {
SettingsUpdateText(SET_STAPWD1 + XdrvMailbox.index -1,
(SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? (1 == XdrvMailbox.index) ? STA_PASS1 : STA_PASS2 : XdrvMailbox.data);
2021-06-11 17:14:12 +01:00
Settings->sta_active = XdrvMailbox.index -1;
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
2021-04-08 16:57:37 +01:00
if (!show_asterisk) {
ResponseCmndIdxChar(SettingsText(SET_STAPWD1 + XdrvMailbox.index -1));
}
} else {
2021-04-08 16:57:37 +01:00
show_asterisk = true;
}
if (show_asterisk) {
Response_P(S_JSON_COMMAND_INDEX_ASTERISK, XdrvMailbox.command, XdrvMailbox.index);
}
}
}
void CmndHostname(void)
{
if (!XdrvMailbox.grpflg && (XdrvMailbox.data_len > 0)) {
SettingsUpdateText(SET_HOSTNAME, (SC_DEFAULT == Shortcut()) ? WIFI_HOSTNAME : XdrvMailbox.data);
if (strchr(SettingsText(SET_HOSTNAME), '%') != nullptr) {
SettingsUpdateText(SET_HOSTNAME, WIFI_HOSTNAME);
}
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
}
ResponseCmndChar(SettingsText(SET_HOSTNAME));
}
void CmndWifiConfig(void)
{
if ((XdrvMailbox.payload >= WIFI_RESTART) && (XdrvMailbox.payload < MAX_WIFI_OPTION)) {
2019-10-22 15:46:07 +01:00
if ((EX_WIFI_SMARTCONFIG == XdrvMailbox.payload) || (EX_WIFI_WPSCONFIG == XdrvMailbox.payload)) {
XdrvMailbox.payload = WIFI_MANAGER;
}
2021-06-11 17:14:12 +01:00
Settings->sta_config = XdrvMailbox.payload;
TasmotaGlobal.wifi_state_flag = Settings->sta_config;
if (WifiState() > WIFI_RESTART) {
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
}
}
2019-10-22 16:29:21 +01:00
char stemp1[TOPSZ];
2021-06-11 17:14:12 +01:00
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, Settings->sta_config, GetTextIndexed(stemp1, sizeof(stemp1), Settings->sta_config, kWifiConfig));
}
void CmndDevicename(void)
{
if (!XdrvMailbox.grpflg && (XdrvMailbox.data_len > 0)) {
2020-05-17 16:38:28 +01:00
SettingsUpdateText(SET_DEVICENAME, ('"' == XdrvMailbox.data[0]) ? "" : (SC_DEFAULT == Shortcut()) ? SettingsText(SET_FRIENDLYNAME1) : XdrvMailbox.data);
}
ResponseCmndChar(SettingsText(SET_DEVICENAME));
}
void CmndFriendlyname(void)
{
2021-04-08 16:57:37 +01:00
snprintf_P(XdrvMailbox.command, CMDSZ, PSTR(D_CMND_FRIENDLYNAME)); // Rename result shortcut command FN to FriendlyName
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_FRIENDLYNAMES)) {
if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) {
ResponseCmndAll(SET_FRIENDLYNAME1, MAX_FRIENDLYNAMES);
} else {
if (XdrvMailbox.data_len > 0) {
char stemp1[TOPSZ];
if (1 == XdrvMailbox.index) {
snprintf_P(stemp1, sizeof(stemp1), PSTR(FRIENDLY_NAME));
} else {
snprintf_P(stemp1, sizeof(stemp1), PSTR(FRIENDLY_NAME "%d"), XdrvMailbox.index);
}
SettingsUpdateText(SET_FRIENDLYNAME1 + XdrvMailbox.index -1, ('"' == XdrvMailbox.data[0]) ? "" : (SC_DEFAULT == Shortcut()) ? stemp1 : XdrvMailbox.data);
}
ResponseCmndIdxChar(SettingsText(SET_FRIENDLYNAME1 + XdrvMailbox.index -1));
}
}
}
void CmndSwitchText(void) {
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SWITCHES_TXT)) {
if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) {
ResponseCmndAll(SET_SWITCH_TXT1, MAX_SWITCHES_TXT);
} else {
if (XdrvMailbox.data_len > 0) {
RemoveSpace(XdrvMailbox.data);
SettingsUpdateText(SET_SWITCH_TXT1 + XdrvMailbox.index -1, ('"' == XdrvMailbox.data[0]) ? "" : XdrvMailbox.data);
}
ResponseCmndIdxChar(SettingsText(SET_SWITCH_TXT1 + XdrvMailbox.index -1));
}
}
}
void CmndSwitchMode(void)
{
2023-02-06 10:45:28 +00:00
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SWITCHES_SET)) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_SWITCH_OPTION)) {
2021-06-11 17:14:12 +01:00
Settings->switchmode[XdrvMailbox.index -1] = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndIdxNumber(Settings->switchmode[XdrvMailbox.index-1]);
}
}
void CmndInterlock(void)
{
// Interlock 0 - Off, Interlock 1 - On, Interlock 1,2 3,4 5,6,7
2020-10-30 11:29:48 +00:00
uint32_t max_relays = TasmotaGlobal.devices_present;
if (TasmotaGlobal.light_type) { max_relays--; }
2021-06-11 17:14:12 +01:00
if (max_relays > sizeof(Settings->interlock[0]) * 8) { max_relays = sizeof(Settings->interlock[0]) * 8; }
if (max_relays > 1) { // Only interlock with more than 1 relay
if (XdrvMailbox.data_len > 0) {
if (strchr(XdrvMailbox.data, ',') != nullptr) { // Interlock entry
2021-06-11 17:14:12 +01:00
for (uint32_t i = 0; i < MAX_INTERLOCKS; i++) { Settings->interlock[i] = 0; } // Reset current interlocks
char *group;
char *q;
uint32_t group_index = 0;
power_t relay_mask = 0;
for (group = strtok_r(XdrvMailbox.data, " ", &q); group && group_index < MAX_INTERLOCKS; group = strtok_r(nullptr, " ", &q)) {
char *str;
char *p;
for (str = strtok_r(group, ",", &p); str; str = strtok_r(nullptr, ",", &p)) {
int pbit = atoi(str);
if ((pbit > 0) && (pbit <= max_relays)) { // Only valid relays
pbit--;
if (!bitRead(relay_mask, pbit)) { // Only relay once
bitSet(relay_mask, pbit);
2021-06-11 17:14:12 +01:00
bitSet(Settings->interlock[group_index], pbit);
}
}
}
group_index++;
}
for (uint32_t i = 0; i < group_index; i++) {
uint32_t minimal_bits = 0;
for (uint32_t j = 0; j < max_relays; j++) {
2021-06-11 17:14:12 +01:00
if (bitRead(Settings->interlock[i], j)) { minimal_bits++; }
}
2021-06-11 17:14:12 +01:00
if (minimal_bits < 2) { Settings->interlock[i] = 0; } // Discard single relay as interlock
}
} else {
2021-06-11 17:14:12 +01:00
Settings->flag.interlock = XdrvMailbox.payload &1; // CMND_INTERLOCK - Enable/disable interlock
if (Settings->flag.interlock) {
2020-10-28 18:03:39 +00:00
SetDevicePower(TasmotaGlobal.power, SRC_IGNORE); // Remove multiple relays if set
}
}
#ifdef USE_SHUTTER
2021-06-11 17:14:12 +01:00
if (Settings->flag3.shutter_mode) { // SetOption80 - Enable shutter support
ShutterInit(); // to update shutter mode
}
#endif // USE_SHUTTER
}
2021-06-11 17:14:12 +01:00
Response_P(PSTR("{\"" D_CMND_INTERLOCK "\":\"%s\",\"" D_JSON_GROUPS "\":\""), GetStateText(Settings->flag.interlock));
uint32_t anygroup = 0;
for (uint32_t i = 0; i < MAX_INTERLOCKS; i++) {
2021-06-11 17:14:12 +01:00
if (Settings->interlock[i]) {
anygroup++;
ResponseAppend_P(PSTR("%s"), (anygroup > 1) ? " " : "");
uint32_t anybit = 0;
power_t mask = 1;
for (uint32_t j = 0; j < max_relays; j++) {
2021-06-11 17:14:12 +01:00
if (Settings->interlock[i] & mask) {
anybit++;
ResponseAppend_P(PSTR("%s%d"), (anybit > 1) ? "," : "", j +1);
}
mask <<= 1;
}
}
}
if (!anygroup) {
for (uint32_t j = 1; j <= max_relays; j++) {
ResponseAppend_P(PSTR("%s%d"), (j > 1) ? "," : "", j);
}
}
ResponseAppend_P(PSTR("\"}"));
} else {
// never ever reset interlock mode inadvertently if we forced it upon compilation
2021-06-11 17:14:12 +01:00
Settings->flag.interlock = APP_INTERLOCK_MODE; // CMND_INTERLOCK - Enable/disable interlock
ResponseCmndStateText(Settings->flag.interlock);
}
}
void CmndTeleperiod(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
2021-06-11 17:14:12 +01:00
Settings->tele_period = (1 == XdrvMailbox.payload) ? TELE_PERIOD : XdrvMailbox.payload;
if ((Settings->tele_period > 0) && (Settings->tele_period < 10)) {
Settings->tele_period = 10; // Do not allow periods < 10 seconds
}
}
2021-06-11 17:14:12 +01:00
TasmotaGlobal.tele_period = (Settings->tele_period) ? Settings->tele_period : 3601; // Show teleperiod data also on empty command
ResponseCmndNumber(Settings->tele_period);
}
2021-05-03 10:00:48 +01:00
void CmndReset(void)
{
switch (XdrvMailbox.payload) {
case 1:
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 211;
2020-05-04 19:00:05 +01:00
ResponseCmndChar(PSTR(D_JSON_RESET_AND_RESTARTING));
break;
case 2 ... 6:
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 210 + XdrvMailbox.payload;
Response_P(PSTR("{\"" D_CMND_RESET "\":\"" D_JSON_ERASE ", " D_JSON_RESET_AND_RESTARTING "\"}"));
break;
case 99:
2021-06-11 17:14:12 +01:00
Settings->bootcount = 0;
Settings->bootcount_reset_time = 0;
ResponseCmndDone();
break;
default:
2020-05-04 19:00:05 +01:00
ResponseCmndChar(PSTR(D_JSON_ONE_TO_RESET));
}
}
void CmndTime(void)
{
// payload 0 = (re-)enable NTP
// payload 1 = Time format {"Time":"2019-09-04T14:31:29"}
// payload 2 = Time format {"Time":"2019-09-04T14:31:29","Epoch":1567600289}
// payload 3 = Time format {"Time":1567600289}
// payload 4 = Time format {"Time":"2019-09-04T14:31:29.123"}
// payload 1451602800 - disable NTP and set time to epoch
2021-06-11 17:14:12 +01:00
uint32_t format = Settings->flag2.time_format;
if (XdrvMailbox.data_len > 0) {
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload < 5)) {
2021-06-11 17:14:12 +01:00
Settings->flag2.time_format = XdrvMailbox.payload -1;
format = Settings->flag2.time_format;
} else {
format = 1; // {"Time":"2019-09-04T14:31:29","Epoch":1567600289}
RtcSetTime(XdrvMailbox.payload);
}
}
2020-10-30 11:29:48 +00:00
ResponseClear();
ResponseAppendTimeFormat(format);
ResponseJsonEnd();
}
void CmndTimezone(void)
{
if ((XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= -13)) {
2021-06-11 17:14:12 +01:00
Settings->timezone = XdrvMailbox.payload;
Settings->timezone_minutes = 0;
if (XdrvMailbox.payload < 15) {
char *p = strtok (XdrvMailbox.data, ":");
if (p) {
p = strtok (nullptr, ":");
if (p) {
2021-06-11 17:14:12 +01:00
Settings->timezone_minutes = strtol(p, nullptr, 10);
if (Settings->timezone_minutes > 59) { Settings->timezone_minutes = 59; }
}
}
} else {
2021-06-11 17:14:12 +01:00
Settings->timezone = 99;
}
2020-10-30 11:29:48 +00:00
TasmotaGlobal.ntp_force_sync = true;
}
2021-06-11 17:14:12 +01:00
if (99 == Settings->timezone) {
ResponseCmndNumber(Settings->timezone);
} else {
char stemp1[TOPSZ];
2021-06-11 17:14:12 +01:00
snprintf_P(stemp1, sizeof(stemp1), PSTR("%+03d:%02d"), Settings->timezone, Settings->timezone_minutes);
2019-08-03 12:01:34 +01:00
ResponseCmndChar(stemp1);
}
}
void CmndTimeStdDst(uint32_t ts)
{
// TimeStd 0/1, 0/1/2/3/4, 1..12, 1..7, 0..23, +/-780
if (XdrvMailbox.data_len > 0) {
if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry
uint32_t tpos = 0; // Parameter index
int value = 0;
char *p = XdrvMailbox.data; // Parameters like "1, 2,3 , 4 ,5, -120" or ",,,,,+240"
char *q = p; // Value entered flag
while (p && (tpos < 7)) {
if (p > q) { // Any value entered
2021-06-11 17:14:12 +01:00
if (1 == tpos) { Settings->tflag[ts].hemis = value &1; }
if (2 == tpos) { Settings->tflag[ts].week = (value < 0) ? 0 : (value > 4) ? 4 : value; }
if (3 == tpos) { Settings->tflag[ts].month = (value < 1) ? 1 : (value > 12) ? 12 : value; }
if (4 == tpos) { Settings->tflag[ts].dow = (value < 1) ? 1 : (value > 7) ? 7 : value; }
if (5 == tpos) { Settings->tflag[ts].hour = (value < 0) ? 0 : (value > 23) ? 23 : value; }
if (6 == tpos) { Settings->toffset[ts] = (value < -900) ? -900 : (value > 900) ? 900 : value; }
}
p = Trim(p); // Skip spaces
if (tpos && (*p == ',')) { p++; } // Skip separator
p = Trim(p); // Skip spaces
q = p; // Reset any value entered flag
value = strtol(p, &p, 10);
tpos++; // Next parameter
}
2020-10-30 11:29:48 +00:00
TasmotaGlobal.ntp_force_sync = true;
} else {
if (0 == XdrvMailbox.payload) {
if (0 == ts) {
SettingsResetStd();
} else {
SettingsResetDst();
}
}
2020-10-30 11:29:48 +00:00
TasmotaGlobal.ntp_force_sync = true;
}
}
Response_P(PSTR("{\"%s\":{\"Hemisphere\":%d,\"Week\":%d,\"Month\":%d,\"Day\":%d,\"Hour\":%d,\"Offset\":%d}}"),
2021-06-11 17:14:12 +01:00
XdrvMailbox.command, Settings->tflag[ts].hemis, Settings->tflag[ts].week, Settings->tflag[ts].month, Settings->tflag[ts].dow, Settings->tflag[ts].hour, Settings->toffset[ts]);
}
void CmndTimeStd(void)
{
CmndTimeStdDst(0);
}
void CmndTimeDst(void)
{
CmndTimeStdDst(1);
}
void CmndAltitude(void)
{
if ((XdrvMailbox.data_len > 0) && ((XdrvMailbox.payload >= -30000) && (XdrvMailbox.payload <= 30000))) {
2021-06-11 17:14:12 +01:00
Settings->altitude = XdrvMailbox.payload;
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->altitude);
}
2020-07-04 14:40:38 +01:00
void CmndLedPower(void) {
2020-10-30 11:29:48 +00:00
// If GPIO_LEDLINK (used for network status) then allow up to 4 GPIO_LEDx control using TasmotaGlobal.led_power
2021-06-11 17:14:12 +01:00
// If no GPIO_LEDLINK then allow legacy single led GPIO_LED1 control using Settings->ledstate
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_LEDS)) {
2020-04-27 16:16:52 +01:00
if (!PinUsed(GPIO_LEDLNK)) { XdrvMailbox.index = 1; }
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
2021-06-11 17:14:12 +01:00
Settings->ledstate &= 8; // Disable power control
uint32_t mask = 1 << (XdrvMailbox.index -1); // Led to control
switch (XdrvMailbox.payload) {
case 0: // Off
2020-10-30 11:29:48 +00:00
TasmotaGlobal.led_power &= (0xFF ^ mask);
2021-06-11 17:14:12 +01:00
Settings->ledstate = 0;
break;
case 1: // On
2020-10-30 11:29:48 +00:00
TasmotaGlobal.led_power |= mask;
2021-06-11 17:14:12 +01:00
Settings->ledstate = 8;
break;
case 2: // Toggle
2020-10-30 11:29:48 +00:00
TasmotaGlobal.led_power ^= mask;
2021-06-11 17:14:12 +01:00
Settings->ledstate ^= 8;
break;
}
2020-10-29 11:39:44 +00:00
TasmotaGlobal.blinks = 0;
2020-04-27 16:16:52 +01:00
if (!PinUsed(GPIO_LEDLNK)) {
2021-06-11 17:14:12 +01:00
SetLedPower(Settings->ledstate &8);
} else {
2020-10-30 11:29:48 +00:00
SetLedPowerIdx(XdrvMailbox.index -1, (TasmotaGlobal.led_power & mask));
}
}
2020-10-30 11:29:48 +00:00
bool state = bitRead(TasmotaGlobal.led_power, XdrvMailbox.index -1);
2020-04-27 16:16:52 +01:00
if (!PinUsed(GPIO_LEDLNK)) {
2021-06-11 17:14:12 +01:00
state = bitRead(Settings->ledstate, 3);
}
2019-08-03 12:01:34 +01:00
ResponseCmndIdxChar(GetStateText(state));
}
}
2020-07-04 14:40:38 +01:00
void CmndLedState(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_LED_OPTION)) {
2021-06-11 17:14:12 +01:00
Settings->ledstate = XdrvMailbox.payload;
if (!Settings->ledstate) {
SetLedPowerAll(0);
SetLedLink(0);
}
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->ledstate);
}
2020-07-04 14:40:38 +01:00
void CmndLedMask(void) {
if (XdrvMailbox.data_len > 0) {
#ifdef USE_PWM_DIMMER
PWMDimmerSetBrightnessLeds(0);
#endif // USE_PWM_DIMMER
2021-06-11 17:14:12 +01:00
Settings->ledmask = XdrvMailbox.payload;
#ifdef USE_PWM_DIMMER
PWMDimmerSetBrightnessLeds(-1);
#endif // USE_PWM_DIMMER
}
char stemp1[TOPSZ];
2021-06-11 17:14:12 +01:00
snprintf_P(stemp1, sizeof(stemp1), PSTR("%d (0x%04X)"), Settings->ledmask, Settings->ledmask);
2019-08-03 12:01:34 +01:00
ResponseCmndChar(stemp1);
}
2020-07-04 14:40:38 +01:00
void CmndLedPwmOff(void) {
if (XdrvMailbox.data_len > 0) {
if (XdrvMailbox.payload < 0) {
2021-06-11 17:14:12 +01:00
Settings->ledpwm_off = 0;
}
else if (XdrvMailbox.payload > 255) {
2021-06-11 17:14:12 +01:00
Settings->ledpwm_off = 255;
} else {
2021-06-11 17:14:12 +01:00
Settings->ledpwm_off = XdrvMailbox.payload;
}
UpdateLedPowerAll();
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->ledpwm_off);
}
2020-07-04 14:40:38 +01:00
void CmndLedPwmOn(void) {
if (XdrvMailbox.data_len > 0) {
if (XdrvMailbox.payload < 0) {
2021-06-11 17:14:12 +01:00
Settings->ledpwm_on = 0;
}
else if (XdrvMailbox.payload > 255) {
2021-06-11 17:14:12 +01:00
Settings->ledpwm_on = 255;
} else {
2021-06-11 17:14:12 +01:00
Settings->ledpwm_on = XdrvMailbox.payload;
}
UpdateLedPowerAll();
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->ledpwm_on);
}
2020-07-04 14:40:38 +01:00
void CmndLedPwmMode(void) {
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_LEDS)) {
if (!PinUsed(GPIO_LEDLNK)) { XdrvMailbox.index = 1; }
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
uint32_t mask = 1 << (XdrvMailbox.index -1); // Led to configure
switch (XdrvMailbox.payload) {
case 0: // digital
2021-06-11 17:14:12 +01:00
Settings->ledpwm_mask &= (0xFF ^ mask);
break;
case 1: // pwm
2021-06-11 17:14:12 +01:00
Settings->ledpwm_mask |= mask;
break;
case 2: // toggle
2021-06-11 17:14:12 +01:00
Settings->ledpwm_mask ^= mask;
break;
}
UpdateLedPowerAll();
}
2021-06-11 17:14:12 +01:00
bool state = bitRead(Settings->ledpwm_mask, XdrvMailbox.index -1);
ResponseCmndIdxChar(GetStateText(state));
}
}
void CmndWifiPower(void)
{
if (XdrvMailbox.data_len > 0) {
2021-06-11 17:14:12 +01:00
Settings->wifi_output_power = (uint8_t)(CharToFloat(XdrvMailbox.data) * 10);
if (Settings->wifi_output_power > 205) {
Settings->wifi_output_power = 205;
}
WifiSetOutputPower();
}
ResponseCmndChar(WifiGetOutputPower().c_str());
}
void CmndWifi(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1)) {
2021-06-11 17:14:12 +01:00
Settings->flag4.network_wifi = XdrvMailbox.payload;
2022-06-13 16:28:13 +01:00
if (Settings->flag4.network_wifi) {
#ifdef ESP32
WifiConnect();
#else
WifiEnable();
#endif
}
} else if ((XdrvMailbox.payload >= 2) && (XdrvMailbox.payload <= 4)) {
2022-10-15 13:33:21 +01:00
// Wifi 2 = B
// Wifi 3 = BG
// Wifi 4 = BGN
#ifdef ESP32
Wifi.phy_mode = XdrvMailbox.payload - 1;
#endif
2022-10-15 13:33:21 +01:00
WiFi.setPhyMode(WiFiPhyMode_t(XdrvMailbox.payload - 1)); // 1-B/2-BG/3-BGN
}
2021-06-11 17:14:12 +01:00
Response_P(PSTR("{\"" D_JSON_WIFI "\":\"%s\",\"" D_JSON_WIFI_MODE "\":\"11%c\"}"), GetStateText(Settings->flag4.network_wifi), pgm_read_byte(&kWifiPhyMode[WiFi.getPhyMode() & 0x3]) );
}
void CmndDnsTimeout(void) {
// Set timeout between 100 and 20000 mSec
if ((XdrvMailbox.payload >= 100) && (XdrvMailbox.payload <= 20000)) {
Settings->dns_timeout = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->dns_timeout);
}
#ifdef USE_I2C
void CmndI2cScan(void)
{
if ((1 == XdrvMailbox.index) && (TasmotaGlobal.i2c_enabled)) {
2021-05-23 15:50:17 +01:00
I2cScan();
}
#ifdef ESP32
if ((2 == XdrvMailbox.index) && (TasmotaGlobal.i2c_enabled_2)) {
2021-05-23 15:50:17 +01:00
I2cScan(1);
}
#endif
}
void CmndI2cDriver(void)
{
if (XdrvMailbox.index < MAX_I2C_DRIVERS) {
if (XdrvMailbox.payload >= 0) {
2021-06-11 17:14:12 +01:00
bitWrite(Settings->i2c_drivers[XdrvMailbox.index / 32], XdrvMailbox.index % 32, XdrvMailbox.payload &1);
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
}
}
Response_P(PSTR("{\"" D_CMND_I2CDRIVER "\":"));
2019-11-04 09:38:05 +00:00
I2cDriverState();
ResponseJsonEnd();
}
#endif // USE_I2C
#ifdef USE_DEVICE_GROUPS
2020-04-06 18:29:50 +01:00
void CmndDevGroupName(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_DEV_GROUP_NAMES)) {
if (XdrvMailbox.data_len > 0) {
if (XdrvMailbox.data_len > TOPSZ)
XdrvMailbox.data[TOPSZ - 1] = 0;
else if (1 == XdrvMailbox.data_len && ('"' == XdrvMailbox.data[0] || '0' == XdrvMailbox.data[0]))
XdrvMailbox.data[0] = 0;
SettingsUpdateText(SET_DEV_GROUP_NAME1 + XdrvMailbox.index - 1, XdrvMailbox.data);
2020-10-29 11:21:24 +00:00
TasmotaGlobal.restart_flag = 2;
2020-04-06 18:29:50 +01:00
}
ResponseCmndAll(SET_DEV_GROUP_NAME1, MAX_DEV_GROUP_NAMES);
}
}
2020-04-07 19:25:58 +01:00
#ifdef USE_DEVICE_GROUPS_SEND
void CmndDevGroupSend(void)
{
uint8_t device_group_index = (XdrvMailbox.usridx ? XdrvMailbox.index - 1 : 0);
if (device_group_index < device_group_count) {
2021-02-09 21:10:32 +00:00
if (!_SendDeviceGroupMessage(-device_group_index, (DevGroupMessageType)(DGR_MSGTYPE_UPDATE_COMMAND + DGR_MSGTYPFLAG_WITH_LOCAL))) {
ResponseCmndChar(XdrvMailbox.data);
}
2020-04-07 19:25:58 +01:00
}
}
#endif // USE_DEVICE_GROUPS_SEND
void CmndDevGroupShare(void)
{
2021-06-11 17:14:12 +01:00
uint32_t parm[2] = { Settings->device_group_share_in, Settings->device_group_share_out };
ParseParameters(2, parm);
2021-06-11 17:14:12 +01:00
Settings->device_group_share_in = parm[0];
Settings->device_group_share_out = parm[1];
Response_P(PSTR("{\"" D_CMND_DEVGROUP_SHARE "\":{\"In\":\"%X\",\"Out\":\"%X\"}}"), Settings->device_group_share_in, Settings->device_group_share_out);
}
void CmndDevGroupStatus(void)
{
DeviceGroupStatus((XdrvMailbox.usridx ? XdrvMailbox.index - 1 : 0));
}
2021-02-10 03:22:43 +00:00
void CmndDevGroupTie(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_DEV_GROUP_NAMES)) {
if (XdrvMailbox.data_len > 0) {
2021-06-11 17:14:12 +01:00
Settings->device_group_tie[XdrvMailbox.index - 1] = XdrvMailbox.payload;
2021-02-10 03:22:43 +00:00
}
2021-05-23 15:50:17 +01:00
Response_P(PSTR("{"));
for (uint32_t i = 0; i < MAX_DEV_GROUP_NAMES; i++) {
2021-06-11 17:14:12 +01:00
ResponseAppend_P(PSTR("%s\"%s%u\":%u"), (i)?",":"", D_CMND_DEVGROUP_TIE, i + 1, Settings->device_group_tie[i]);
}
2021-05-23 15:50:17 +01:00
ResponseJsonEnd();
2021-02-10 03:22:43 +00:00
}
}
#endif // USE_DEVICE_GROUPS
void CmndSetSensor(void)
{
if (XdrvMailbox.index < MAX_XSNS_DRIVERS) {
if (XdrvMailbox.payload >= 0) {
bitWrite(Settings->sensors[0][XdrvMailbox.index / 32], XdrvMailbox.index % 32, XdrvMailbox.payload &1);
if (1 == XdrvMailbox.payload) {
TasmotaGlobal.restart_flag = 2; // To safely re-enable a sensor currently most sensor need to follow complete restart init cycle
}
}
Response_P(PSTR("{\"" D_CMND_SETSENSOR "\":"));
XsnsSensorState(0);
ResponseJsonEnd();
}
}
void CmndSensor(void)
{
XsnsCall(FUNC_COMMAND_SENSOR);
}
void CmndDriver(void)
{
XdrvCall(FUNC_COMMAND_DRIVER);
}
#ifdef ESP32
2020-06-20 16:58:21 +01:00
2020-11-12 16:38:15 +00:00
void CmndInfo(void) {
NvsInfo();
ResponseCmndDone();
}
2020-06-20 16:58:21 +01:00
void CmndCpuFrequency(void) {
if ((80 == XdrvMailbox.payload) || (160 == XdrvMailbox.payload) || (240 == XdrvMailbox.payload)) {
setCpuFrequencyMhz(XdrvMailbox.payload);
}
ResponseCmndNumber(getCpuFrequencyMhz());
}
#if defined(SOC_TOUCH_VERSION_1) || defined(SOC_TOUCH_VERSION_2)
void CmndTouchCal(void) {
if (XdrvMailbox.payload >= 0) {
if (XdrvMailbox.payload == 0) {
2022-09-28 11:17:48 +01:00
TouchButton.calibration = 0;
}
else if (XdrvMailbox.payload < MAX_KEYS + 1) {
2022-09-28 11:17:48 +01:00
TouchButton.calibration = bitSet(TouchButton.calibration, XdrvMailbox.payload);
}
else if (XdrvMailbox.payload == 255) {
2022-09-28 11:17:48 +01:00
TouchButton.calibration = 0x0FFFFFFF; // All MAX_KEYS pins
}
}
2022-09-28 11:17:48 +01:00
ResponseCmndNumber(TouchButton.calibration);
2021-01-23 15:26:23 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("Button Touchvalue Hits,"));
}
void CmndTouchThres(void) {
if (XdrvMailbox.data_len > 0) {
Settings->touch_threshold = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->touch_threshold);
}
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
2020-11-28 11:46:17 +00:00
#endif // ESP32