2019-07-24 10:55:58 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
support_command.ino - command support for Tasmota
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 Theo Arends
|
2019-07-24 10:55:58 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-11 17:12:18 +01:00
|
|
|
const char kTasmotaCommands[] PROGMEM = "|" // No prefix
|
2019-07-27 10:13:41 +01:00
|
|
|
D_CMND_BACKLOG "|" D_CMND_DELAY "|" D_CMND_POWER "|" D_CMND_STATUS "|" D_CMND_STATE "|" D_CMND_SLEEP "|" D_CMND_UPGRADE "|" D_CMND_UPLOAD "|" D_CMND_OTAURL "|"
|
|
|
|
D_CMND_SERIALLOG "|" D_CMND_RESTART "|" D_CMND_POWERONSTATE "|" D_CMND_PULSETIME "|" D_CMND_BLINKTIME "|" D_CMND_BLINKCOUNT "|" D_CMND_SAVEDATA "|"
|
2020-04-21 08:48:27 +01:00
|
|
|
D_CMND_SO "|" D_CMND_SETOPTION "|" D_CMND_TEMPERATURE_RESOLUTION "|" D_CMND_HUMIDITY_RESOLUTION "|" D_CMND_PRESSURE_RESOLUTION "|" D_CMND_POWER_RESOLUTION "|"
|
2019-07-27 10:13:41 +01:00
|
|
|
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 "|"
|
2019-12-06 14:02:05 +00:00
|
|
|
D_CMND_BUTTONDEBOUNCE "|" D_CMND_SWITCHDEBOUNCE "|" D_CMND_SYSLOG "|" D_CMND_LOGHOST "|" D_CMND_LOGPORT "|" D_CMND_SERIALSEND "|" D_CMND_BAUDRATE "|" D_CMND_SERIALCONFIG "|"
|
2019-07-27 10:13:41 +01:00
|
|
|
D_CMND_SERIALDELIMITER "|" D_CMND_IPADDRESS "|" D_CMND_NTPSERVER "|" D_CMND_AP "|" D_CMND_SSID "|" D_CMND_PASSWORD "|" D_CMND_HOSTNAME "|" D_CMND_WIFICONFIG "|"
|
2020-05-17 16:10:17 +01:00
|
|
|
D_CMND_DEVICENAME "|" D_CMND_FRIENDLYNAME "|" D_CMND_SWITCHMODE "|" D_CMND_INTERLOCK "|" D_CMND_TELEPERIOD "|" D_CMND_RESET "|" D_CMND_TIME "|" D_CMND_TIMEZONE "|" D_CMND_TIMESTD "|"
|
2020-05-23 14:17:13 +01:00
|
|
|
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 "|"
|
|
|
|
D_CMND_WIFIPOWER "|" D_CMND_TEMPOFFSET "|" D_CMND_HUMOFFSET "|" D_CMND_SPEEDUNIT "|" D_CMND_GLOBAL_TEMP "|" D_CMND_GLOBAL_HUM "|"
|
2019-07-27 10:13:41 +01:00
|
|
|
#ifdef USE_I2C
|
2019-11-03 16:54:39 +00:00
|
|
|
D_CMND_I2CSCAN "|" D_CMND_I2CDRIVER "|"
|
2019-12-07 09:41:29 +00:00
|
|
|
#endif
|
2020-02-21 15:09:21 +00:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-04-07 19:25:58 +01:00
|
|
|
D_CMND_DEVGROUP_NAME "|"
|
|
|
|
#ifdef USE_DEVICE_GROUPS_SEND
|
|
|
|
D_CMND_DEVGROUP_SEND "|"
|
|
|
|
#endif // USE_DEVICE_GROUPS_SEND
|
2020-04-15 14:40:18 +01:00
|
|
|
D_CMND_DEVGROUP_SHARE "|" D_CMND_DEVGROUPSTATUS "|"
|
2020-02-21 15:09:21 +00:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-05-30 12:50:22 +01:00
|
|
|
D_CMND_SENSOR "|" D_CMND_DRIVER
|
|
|
|
#ifdef ESP32
|
|
|
|
"|" D_CMND_TOUCH_CAL "|" D_CMND_TOUCH_THRES "|" D_CMND_TOUCH_NUM
|
2020-05-30 13:59:52 +01:00
|
|
|
#endif //ESP32
|
2020-05-30 12:50:22 +01:00
|
|
|
;
|
2019-08-01 14:46:12 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void (* const TasmotaCommand[])(void) PROGMEM = {
|
|
|
|
&CmndBacklog, &CmndDelay, &CmndPower, &CmndStatus, &CmndState, &CmndSleep, &CmndUpgrade, &CmndUpgrade, &CmndOtaUrl,
|
|
|
|
&CmndSeriallog, &CmndRestart, &CmndPowerOnState, &CmndPulsetime, &CmndBlinktime, &CmndBlinkcount, &CmndSavedata,
|
2020-04-21 08:48:27 +01:00
|
|
|
&CmndSetoption, &CmndSetoption, &CmndTemperatureResolution, &CmndHumidityResolution, &CmndPressureResolution, &CmndPowerResolution,
|
2019-07-27 10:13:41 +01:00
|
|
|
&CmndVoltageResolution, &CmndFrequencyResolution, &CmndCurrentResolution, &CmndEnergyResolution, &CmndWeightResolution,
|
|
|
|
&CmndModule, &CmndModules, &CmndGpio, &CmndGpios, &CmndTemplate, &CmndPwm, &CmndPwmfrequency, &CmndPwmrange,
|
2019-12-06 14:02:05 +00:00
|
|
|
&CmndButtonDebounce, &CmndSwitchDebounce, &CmndSyslog, &CmndLoghost, &CmndLogport, &CmndSerialSend, &CmndBaudrate, &CmndSerialConfig,
|
2019-07-27 10:13:41 +01:00
|
|
|
&CmndSerialDelimiter, &CmndIpAddress, &CmndNtpServer, &CmndAp, &CmndSsid, &CmndPassword, &CmndHostname, &CmndWifiConfig,
|
2020-05-17 16:10:17 +01:00
|
|
|
&CmndDevicename, &CmndFriendlyname, &CmndSwitchMode, &CmndInterlock, &CmndTeleperiod, &CmndReset, &CmndTime, &CmndTimezone, &CmndTimeStd,
|
2020-05-23 14:17:13 +01:00
|
|
|
&CmndTimeDst, &CmndAltitude, &CmndLedPower, &CmndLedState, &CmndLedMask, &CmndLedPwmOn, &CmndLedPwmOff, &CmndLedPwmMode,
|
|
|
|
&CmndWifiPower, &CmndTempOffset, &CmndHumOffset, &CmndSpeedUnit, &CmndGlobalTemp, &CmndGlobalHum,
|
2019-07-27 10:13:41 +01:00
|
|
|
#ifdef USE_I2C
|
2019-11-03 16:54:39 +00:00
|
|
|
&CmndI2cScan, CmndI2cDriver,
|
2019-07-27 10:13:41 +01:00
|
|
|
#endif
|
2020-02-21 15:09:21 +00:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-04-07 19:25:58 +01:00
|
|
|
&CmndDevGroupName,
|
|
|
|
#ifdef USE_DEVICE_GROUPS_SEND
|
|
|
|
&CmndDevGroupSend,
|
|
|
|
#endif // USE_DEVICE_GROUPS_SEND
|
2020-04-15 14:40:18 +01:00
|
|
|
&CmndDevGroupShare, &CmndDevGroupStatus,
|
2020-02-21 15:09:21 +00:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-05-30 13:59:52 +01:00
|
|
|
&CmndSensor, &CmndDriver
|
2020-05-30 12:50:22 +01:00
|
|
|
#ifdef ESP32
|
|
|
|
,&CmndTouchCal, &CmndTouchThres, &CmndTouchNum
|
2020-05-30 13:59:52 +01:00
|
|
|
#endif //ESP32
|
2020-05-30 12:50:22 +01:00
|
|
|
};
|
2019-07-24 10:55:58 +01:00
|
|
|
|
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;
|
|
|
|
|
2019-07-24 10:55:58 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-08-03 12:01:34 +01:00
|
|
|
void ResponseCmndNumber(int value)
|
|
|
|
{
|
|
|
|
Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, value);
|
|
|
|
}
|
|
|
|
|
2019-11-18 11:15:49 +00:00
|
|
|
void ResponseCmndFloat(float value, uint32_t decimals)
|
|
|
|
{
|
|
|
|
char stemp1[TOPSZ];
|
|
|
|
dtostrfd(value, decimals, stemp1);
|
|
|
|
Response_P(S_JSON_COMMAND_XVALUE, XdrvMailbox.command, stemp1); // Return float value without quotes
|
|
|
|
}
|
|
|
|
|
2019-08-03 12:01:34 +01:00
|
|
|
void ResponseCmndIdxNumber(int value)
|
|
|
|
{
|
|
|
|
Response_P(S_JSON_COMMAND_INDEX_NVALUE, XdrvMailbox.command, XdrvMailbox.index, value);
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:55:58 +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
|
|
|
}
|
|
|
|
|
2019-08-03 12:01:34 +01:00
|
|
|
void ResponseCmndChar(const char* value)
|
|
|
|
{
|
|
|
|
Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResponseCmndStateText(uint32_t value)
|
|
|
|
{
|
|
|
|
ResponseCmndChar(GetStateText(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResponseCmndDone(void)
|
|
|
|
{
|
|
|
|
ResponseCmndChar(D_JSON_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResponseCmndIdxChar(const char* value)
|
|
|
|
{
|
|
|
|
Response_P(S_JSON_COMMAND_INDEX_SVALUE, XdrvMailbox.command, XdrvMailbox.index, value);
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:10:21 +00:00
|
|
|
void ResponseCmndAll(uint32_t text_index, uint32_t count)
|
|
|
|
{
|
2020-03-28 15:48:36 +00:00
|
|
|
uint32_t real_index = text_index;
|
2020-01-12 12:10:21 +00:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2020-03-28 15:48:36 +00:00
|
|
|
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, SettingsText(real_index +i));
|
2020-01-12 12:10:21 +00:00
|
|
|
}
|
|
|
|
ResponseJsonEnd();
|
|
|
|
}
|
|
|
|
|
2019-08-03 12:01:34 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
void ExecuteCommand(const char *cmnd, uint32_t source)
|
2019-07-24 10:55:58 +01:00
|
|
|
{
|
2019-10-02 15:45:53 +01:00
|
|
|
// cmnd: "status 0" = stopic "status" and svalue " 0"
|
|
|
|
// cmnd: "var1 =1" = stopic "var1" and svalue " =1"
|
|
|
|
// cmnd: "var1=1" = stopic "var1" and svalue "=1"
|
2019-07-24 10:55:58 +01:00
|
|
|
#ifdef USE_DEBUG_DRIVER
|
|
|
|
ShowFreeMem(PSTR("ExecuteCommand"));
|
|
|
|
#endif
|
|
|
|
ShowSource(source);
|
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
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
|
2019-10-02 15:45:53 +01:00
|
|
|
start = pos + 1;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if ('\0' == *start || pos <= start) {
|
|
|
|
return; // Did not find any command to execute
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
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));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
// topicBuf: /power1 dataBuf: toggle = Console command
|
2020-01-16 13:22:39 +00:00
|
|
|
// topicBuf: cmnd/tasmota/power1 dataBuf: toggle = Mqtt command using topic
|
|
|
|
// topicBuf: cmnd/tasmotas/power1 dataBuf: toggle = Mqtt command using a group topic
|
2019-10-02 15:45:53 +01:00
|
|
|
// topicBuf: cmnd/DVES_83BB10_fb/power1 dataBuf: toggle = Mqtt command using fallback topic
|
2019-07-24 12:09:42 +01:00
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len)
|
2019-07-24 10:55:58 +01:00
|
|
|
{
|
|
|
|
#ifdef USE_DEBUG_DRIVER
|
|
|
|
ShowFreeMem(PSTR("CommandHandler"));
|
|
|
|
#endif
|
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
while (*dataBuf && isspace(*dataBuf)) {
|
|
|
|
dataBuf++; // Skip leading spaces in data
|
|
|
|
data_len--;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 10:51:39 +01:00
|
|
|
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;
|
2020-04-10 09:38:39 +01:00
|
|
|
}
|
2020-04-09 10:51:39 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-26 08:52:14 +01:00
|
|
|
char stemp1[TOPSZ];
|
2019-10-31 16:24:06 +00:00
|
|
|
GetFallbackTopic_P(stemp1, ""); // Full Fallback topic = cmnd/DVES_xxxxxxxx_fb/
|
2019-07-24 10:55:58 +01:00
|
|
|
fallback_topic_flag = (!strncmp(topicBuf, stemp1, strlen(stemp1)));
|
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
char *type = strrchr(topicBuf, '/'); // Last part of received topic is always the command (type)
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-26 08:52:14 +01:00
|
|
|
uint32_t index = 1;
|
|
|
|
bool user_index = false;
|
2019-07-24 10:55:58 +01:00
|
|
|
if (type != nullptr) {
|
|
|
|
type++;
|
2019-10-30 13:08:43 +00:00
|
|
|
uint32_t i;
|
2020-04-24 23:55:39 +01:00
|
|
|
int nLen; // strlen(type)
|
|
|
|
char *s = type;
|
|
|
|
for (nLen = 0; *s; s++, nLen++) {
|
|
|
|
*s=toupper(*s);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2020-04-24 23:55:39 +01:00
|
|
|
i = nLen;
|
|
|
|
if (i > 0) { // may be 0
|
|
|
|
while (isdigit(type[i-1])) {
|
|
|
|
i--;
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2020-04-24 23:55:39 +01:00
|
|
|
if (i < nLen) {
|
|
|
|
index = atoi(type + i);
|
2019-07-24 10:55:58 +01:00
|
|
|
user_index = true;
|
|
|
|
}
|
|
|
|
type[i] = '\0';
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:45:53 +01:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("CMD: " D_GROUP " %d, " D_INDEX " %d, " D_COMMAND " \"%s\", " D_DATA " \"%s\""), grpflg, index, type, dataBuf);
|
2019-07-24 10:55:58 +01:00
|
|
|
|
|
|
|
if (type != nullptr) {
|
|
|
|
Response_P(PSTR("{\"" D_JSON_COMMAND "\":\"" D_JSON_ERROR "\"}"));
|
2019-07-27 10:13:41 +01:00
|
|
|
|
2019-07-24 10:55:58 +01:00
|
|
|
if (Settings.ledstate &0x02) { blinks++; }
|
|
|
|
|
|
|
|
if (!strcmp(dataBuf,"?")) { data_len = 0; }
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
char *p;
|
|
|
|
int32_t payload = strtol(dataBuf, &p, 0); // decimal, octal (0) or hex (0x)
|
|
|
|
if (p == dataBuf) { payload = -99; }
|
2019-07-24 10:55:58 +01:00
|
|
|
int temp_payload = GetStateNumber(dataBuf);
|
|
|
|
if (temp_payload > -1) { payload = temp_payload; }
|
|
|
|
|
2019-08-08 16:51:49 +01:00
|
|
|
DEBUG_CORE_LOG(PSTR("CMD: Payload %d"), payload);
|
2019-07-27 10:13:41 +01:00
|
|
|
|
2019-10-09 16:52:52 +01:00
|
|
|
// backlog_delay = millis() + (100 * MIN_BACKLOG_DELAY);
|
|
|
|
backlog_delay = millis() + Settings.param[P_BACKLOG_DELAY];
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-12-27 10:13:22 +00:00
|
|
|
char command[CMDSZ] = { 0 };
|
2019-07-28 16:14:20 +01:00
|
|
|
XdrvMailbox.command = command;
|
2019-07-27 10:13:41 +01:00
|
|
|
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()) {
|
2019-09-24 07:51:09 +01:00
|
|
|
if (!DecodeCommand(kTasmotaCommands, TasmotaCommand)) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 09:25:02 +01:00
|
|
|
#else //USE_SCRIPT_SUB_COMMAND
|
2019-08-01 14:46:12 +01:00
|
|
|
if (!DecodeCommand(kTasmotaCommands, TasmotaCommand)) {
|
2019-07-24 10:55:58 +01:00
|
|
|
if (!XdrvCall(FUNC_COMMAND)) {
|
|
|
|
if (!XsnsCall(FUNC_COMMAND)) {
|
|
|
|
type = nullptr; // Unknown command
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 09:25:02 +01:00
|
|
|
#endif //USE_SCRIPT_SUB_COMMAND
|
2019-09-24 07:51:09 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == nullptr) {
|
|
|
|
blinks = 201;
|
2020-01-14 15:57:55 +00:00
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR(D_JSON_COMMAND));
|
2019-07-27 10:13:41 +01:00
|
|
|
Response_P(PSTR("{\"" D_JSON_COMMAND "\":\"" D_JSON_UNKNOWN "\"}"));
|
2020-01-14 15:57:55 +00:00
|
|
|
type = (char*)stemp1;
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mqtt_data[0] != '\0') {
|
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, type);
|
|
|
|
XdrvRulesProcess();
|
|
|
|
}
|
|
|
|
fallback_topic_flag = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void CmndBacklog(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len) {
|
2019-09-09 16:24:27 +01:00
|
|
|
|
|
|
|
#ifdef SUPPORT_IF_STATEMENT
|
|
|
|
char *blcommand = strtok(XdrvMailbox.data, ";");
|
|
|
|
while ((blcommand != nullptr) && (backlog.size() < MAX_BACKLOG))
|
|
|
|
#else
|
2019-07-27 10:13:41 +01:00
|
|
|
uint32_t bl_pointer = (!backlog_pointer) ? MAX_BACKLOG -1 : backlog_pointer;
|
|
|
|
bl_pointer--;
|
|
|
|
char *blcommand = strtok(XdrvMailbox.data, ";");
|
2019-09-09 16:24:27 +01:00
|
|
|
while ((blcommand != nullptr) && (backlog_index != bl_pointer))
|
|
|
|
#endif
|
|
|
|
{
|
2019-07-27 10:13:41 +01:00
|
|
|
while(true) {
|
|
|
|
blcommand = Trim(blcommand);
|
|
|
|
if (!strncasecmp_P(blcommand, PSTR(D_CMND_BACKLOG), strlen(D_CMND_BACKLOG))) {
|
|
|
|
blcommand += strlen(D_CMND_BACKLOG); // Skip unnecessary command Backlog
|
|
|
|
} else {
|
|
|
|
break;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
if (*blcommand != '\0') {
|
2019-09-09 16:24:27 +01:00
|
|
|
#ifdef SUPPORT_IF_STATEMENT
|
|
|
|
if (backlog.size() < MAX_BACKLOG) {
|
|
|
|
backlog.add(blcommand);
|
|
|
|
}
|
|
|
|
#else
|
2019-07-27 10:13:41 +01:00
|
|
|
backlog[backlog_index] = String(blcommand);
|
|
|
|
backlog_index++;
|
|
|
|
if (backlog_index >= MAX_BACKLOG) backlog_index = 0;
|
2019-09-09 16:24:27 +01:00
|
|
|
#endif
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
blcommand = strtok(nullptr, ";");
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
// ResponseCmndChar(D_JSON_APPENDED);
|
2019-07-27 10:13:41 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
} else {
|
2019-09-09 16:24:27 +01:00
|
|
|
bool blflag = BACKLOG_EMPTY;
|
|
|
|
#ifdef SUPPORT_IF_STATEMENT
|
|
|
|
backlog.clear();
|
|
|
|
#else
|
2019-07-27 10:13:41 +01:00
|
|
|
backlog_pointer = backlog_index;
|
2019-09-09 16:24:27 +01:00
|
|
|
#endif
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndChar(blflag ? D_JSON_EMPTY : D_JSON_ABORTED);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndDelay(void)
|
|
|
|
{
|
2019-10-09 16:52:52 +01:00
|
|
|
if ((XdrvMailbox.payload >= (MIN_BACKLOG_DELAY / 100)) && (XdrvMailbox.payload <= 3600)) {
|
2019-07-27 10:13:41 +01:00
|
|
|
backlog_delay = millis() + (100 * XdrvMailbox.payload);
|
|
|
|
}
|
|
|
|
uint32_t bl_delay = 0;
|
|
|
|
long bl_delta = TimePassedSince(backlog_delay);
|
|
|
|
if (bl_delta < 0) { bl_delay = (bl_delta *-1) / 100; }
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(bl_delay);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPower(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= devices_present)) {
|
2019-09-04 11:20:04 +01:00
|
|
|
if ((XdrvMailbox.payload < POWER_OFF) || (XdrvMailbox.payload > POWER_BLINK_STOP)) {
|
|
|
|
XdrvMailbox.payload = POWER_SHOW_STATE;
|
|
|
|
}
|
2019-11-03 11:33:36 +00:00
|
|
|
// Settings.flag.device_index_enable = XdrvMailbox.usridx; // SetOption26 - Switch between POWER or POWER1
|
2019-07-27 10:13:41 +01:00
|
|
|
ExecuteCommandPower(XdrvMailbox.index, XdrvMailbox.payload, SRC_IGNORE);
|
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
}
|
2019-09-03 22:04:49 +01:00
|
|
|
else if (0 == XdrvMailbox.index) {
|
2019-09-04 11:20:04 +01:00
|
|
|
if ((XdrvMailbox.payload < POWER_OFF) || (XdrvMailbox.payload > POWER_TOGGLE)) {
|
|
|
|
XdrvMailbox.payload = POWER_SHOW_STATE;
|
2019-09-03 22:04:49 +01:00
|
|
|
}
|
2019-09-04 11:20:04 +01:00
|
|
|
SetAllPower(XdrvMailbox.payload, SRC_IGNORE);
|
|
|
|
mqtt_data[0] = '\0';
|
2019-09-03 22:04:49 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndStatus(void)
|
|
|
|
{
|
|
|
|
uint32_t payload = ((XdrvMailbox.payload < 0) || (XdrvMailbox.payload > MAX_STATUS)) ? 99 : XdrvMailbox.payload;
|
|
|
|
|
|
|
|
uint32_t option = STAT;
|
2019-12-16 14:13:57 +00:00
|
|
|
char stemp[200];
|
2019-12-22 14:23:52 +00:00
|
|
|
char stemp2[TOPSZ];
|
2019-07-27 10:13:41 +01:00
|
|
|
|
|
|
|
// Workaround MQTT - TCP/IP stack queueing when SUB_PREFIX = PUB_PREFIX
|
2020-01-18 15:57:48 +00:00
|
|
|
// Commented on 20200118 as it seems to be no longer needed
|
|
|
|
// if (!strcmp(SettingsText(SET_MQTTPREFIX1), SettingsText(SET_MQTTPREFIX2)) && (!payload)) { option++; } // TELE
|
2019-07-27 10:13:41 +01:00
|
|
|
|
2019-11-03 11:33:36 +00:00
|
|
|
if ((!Settings.flag.mqtt_enabled) && (6 == payload)) { payload = 99; } // SetOption3 - Enable MQTT
|
2019-07-27 10:13:41 +01:00
|
|
|
if (!energy_flg && (9 == payload)) { payload = 99; }
|
2019-12-28 13:54:26 +00:00
|
|
|
if (!CrashFlag() && (12 == payload)) { payload = 99; }
|
2019-12-08 12:18:15 +00:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
if ((0 == payload) || (99 == payload)) {
|
|
|
|
uint32_t maxfn = (devices_present > MAX_FRIENDLYNAMES) ? MAX_FRIENDLYNAMES : (!devices_present) ? 1 : 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++) {
|
2019-12-16 14:13:57 +00:00
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("%s%s\"%s\"" ), stemp, (i > 0 ? "," : ""), SettingsText(SET_FRIENDLYNAME1 +i));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
stemp2[0] = '\0';
|
|
|
|
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
|
|
|
snprintf_P(stemp2, sizeof(stemp2), PSTR("%s%s%d" ), stemp2, (i > 0 ? "," : ""), Settings.switchmode[i]);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2020-05-17 16:10:17 +01:00
|
|
|
Response_P(PSTR("{\"" D_CMND_STATUS "\":{\"" D_CMND_MODULE "\":%d,\"" D_CMND_DEVICENAME "\":\"%s\",\"" D_CMND_FRIENDLYNAME "\":[%s],\"" D_CMND_TOPIC "\":\"%s\",\""
|
2019-08-10 16:34:59 +01:00
|
|
|
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}}"),
|
2020-05-17 16:10:17 +01:00
|
|
|
ModuleNr(), SettingsText(SET_DEVICENAME), stemp, mqtt_topic,
|
2019-12-16 14:13:57 +00:00
|
|
|
SettingsText(SET_MQTT_BUTTON_TOPIC), power, Settings.poweronstate, Settings.ledstate,
|
2019-11-03 11:33:36 +00:00
|
|
|
Settings.ledmask, Settings.save_data,
|
|
|
|
Settings.flag.save_state, // SetOption0 - Save power state and use after restart
|
2019-12-16 14:13:57 +00:00
|
|
|
SettingsText(SET_MQTT_SWITCH_TOPIC),
|
2019-11-03 11:33:36 +00:00
|
|
|
stemp2,
|
|
|
|
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
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((0 == payload) || (1 == payload)) {
|
2019-12-27 10:13:22 +00:00
|
|
|
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\",\""
|
2019-08-10 16:34:59 +01:00
|
|
|
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
|
|
|
|
"}}"),
|
2019-12-29 12:27:48 +00:00
|
|
|
Settings.baudrate * 300, GetSerialConfig().c_str(), SettingsText(SET_MQTT_GRP_TOPIC), SettingsText(SET_OTAURL),
|
2019-12-08 12:18:15 +00:00
|
|
|
GetResetReason().c_str(), GetUptime().c_str(), GetDateAndTime(DT_RESTART).c_str(), Settings.sleep,
|
2020-04-19 15:58:13 +01:00
|
|
|
Settings.cfg_holder, Settings.bootcount, GetDateAndTime(DT_BOOTCOUNT).c_str(), Settings.save_flag
|
|
|
|
#ifdef ESP8266
|
|
|
|
, GetSettingsAddress()
|
|
|
|
#endif
|
|
|
|
);
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "1"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((0 == payload) || (2 == payload)) {
|
2020-04-19 15:58:13 +01:00
|
|
|
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS2_FIRMWARE "\":{\"" D_JSON_VERSION "\":\"%s%s\",\"" D_JSON_BUILDDATETIME "\":\"%s\""
|
|
|
|
#ifdef ESP8266
|
|
|
|
",\"" D_JSON_BOOTVERSION "\":%d"
|
|
|
|
#endif
|
|
|
|
",\"" D_JSON_COREVERSION "\":\"" ARDUINO_CORE_RELEASE "\",\"" D_JSON_SDKVERSION "\":\"%s\","
|
2020-05-30 13:59:52 +01:00
|
|
|
"\"CpuFrequency\":%d,\"Hardware\":\"%s\""
|
2019-12-09 15:16:21 +00:00
|
|
|
"%s}}"),
|
2020-04-19 15:58:13 +01:00
|
|
|
my_version, my_image, GetBuildDateAndTime().c_str()
|
|
|
|
#ifdef ESP8266
|
|
|
|
, ESP.getBootVersion()
|
|
|
|
#endif
|
|
|
|
, ESP.getSdkVersion(),
|
2020-05-30 13:59:52 +01:00
|
|
|
ESP.getCpuFreqMHz(), GetDeviceHardware().c_str(),
|
2019-12-11 09:49:57 +00:00
|
|
|
GetStatistics().c_str());
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((0 == payload) || (3 == payload)) {
|
2019-10-04 09:24:21 +01:00
|
|
|
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,\""
|
2019-08-10 16:34:59 +01:00
|
|
|
D_CMND_LOGHOST "\":\"%s\",\"" D_CMND_LOGPORT "\":%d,\"" D_CMND_SSID "\":[\"%s\",\"%s\"],\"" D_CMND_TELEPERIOD "\":%d,\""
|
2019-10-31 11:12:48 +00:00
|
|
|
D_JSON_RESOLUTION "\":\"%08X\",\"" D_CMND_SETOPTION "\":[\"%08X\",\"%s\",\"%08X\",\"%08X\"]}}"),
|
2019-10-04 09:24:21 +01:00
|
|
|
Settings.seriallog_level, Settings.weblog_level, Settings.mqttlog_level, Settings.syslog_level,
|
2019-12-16 14:13:57 +00:00
|
|
|
SettingsText(SET_SYSLOG_HOST), Settings.syslog_port, SettingsText(SET_STASSID1), SettingsText(SET_STASSID2), Settings.tele_period,
|
2019-10-31 11:12:48 +00:00
|
|
|
Settings.flag2.data, Settings.flag.data, ToHex_P((unsigned char*)Settings.param, PARAM8_SIZE, stemp2, sizeof(stemp2)),
|
|
|
|
Settings.flag3.data, Settings.flag4.data);
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "3"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((0 == payload) || (4 == payload)) {
|
2019-08-10 16:34:59 +01: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_PSRMAXMEMORY "\":%d,\"" D_JSON_PSRFREEMEMORY "\":%d,"
|
|
|
|
#endif
|
2020-04-19 15:58:13 +01:00
|
|
|
D_JSON_PROGRAMFLASHSIZE "\":%d,\"" D_JSON_FLASHSIZE "\":%d"
|
|
|
|
#ifdef ESP8266
|
|
|
|
",\"" D_JSON_FLASHCHIPID "\":\"%06X\""
|
|
|
|
#endif
|
2020-05-30 13:59:52 +01:00
|
|
|
",\"FlashFrequency\":%d,\"" D_JSON_FLASHMODE "\":%d,\""
|
2020-02-10 14:18:11 +00:00
|
|
|
D_JSON_FEATURES "\":[\"%08X\",\"%08X\",\"%08X\",\"%08X\",\"%08X\",\"%08X\",\"%08X\"]"),
|
2020-04-22 15:07:52 +01:00
|
|
|
ESP_getSketchSize()/1024, ESP.getFreeSketchSpace()/1024, ESP_getFreeHeap()/1024,
|
2020-05-12 14:42:16 +01:00
|
|
|
#ifdef ESP32
|
|
|
|
ESP.getPsramSize()/1024, ESP.getFreePsram()/1024,
|
|
|
|
#endif
|
2020-04-19 15:58:13 +01:00
|
|
|
ESP.getFlashChipSize()/1024, ESP.getFlashChipRealSize()/1024
|
|
|
|
#ifdef ESP8266
|
|
|
|
, ESP.getFlashChipId()
|
|
|
|
#endif
|
2020-05-30 13:59:52 +01:00
|
|
|
, ESP.getFlashChipSpeed()/1000000, ESP.getFlashChipMode(),
|
2020-02-10 14:18:11 +00:00
|
|
|
LANGUAGE_LCID, feature_drv1, feature_drv2, feature_sns1, feature_sns2, feature5, feature6);
|
2019-08-18 12:23:43 +01:00
|
|
|
XsnsDriverState();
|
|
|
|
ResponseAppend_P(PSTR(",\"Sensors\":"));
|
|
|
|
XsnsSensorState();
|
2019-09-12 13:19:44 +01:00
|
|
|
ResponseJsonEndEnd();
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "4"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((0 == payload) || (5 == payload)) {
|
2019-08-10 16:34:59 +01:00
|
|
|
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS5_NETWORK "\":{\"" D_CMND_HOSTNAME "\":\"%s\",\"" D_CMND_IPADDRESS "\":\"%s\",\"" D_JSON_GATEWAY "\":\"%s\",\""
|
|
|
|
D_JSON_SUBNETMASK "\":\"%s\",\"" D_JSON_DNSSERVER "\":\"%s\",\"" D_JSON_MAC "\":\"%s\",\""
|
2019-12-27 10:13:22 +00:00
|
|
|
D_CMND_WEBSERVER "\":%d,\"" D_CMND_WIFICONFIG "\":%d,\"" D_CMND_WIFIPOWER "\":%s}}"),
|
2019-08-10 16:34:59 +01:00
|
|
|
my_hostname, WiFi.localIP().toString().c_str(), IPAddress(Settings.ip_address[1]).toString().c_str(),
|
|
|
|
IPAddress(Settings.ip_address[2]).toString().c_str(), IPAddress(Settings.ip_address[3]).toString().c_str(), WiFi.macAddress().c_str(),
|
2019-12-27 10:13:22 +00:00
|
|
|
Settings.webserver, Settings.sta_config, WifiGetOutputPower().c_str());
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "5"));
|
|
|
|
}
|
|
|
|
|
2019-11-03 11:33:36 +00:00
|
|
|
if (((0 == payload) || (6 == payload)) && Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT
|
2019-08-10 16:34:59 +01:00
|
|
|
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}}"),
|
2019-12-16 14:13:57 +00:00
|
|
|
SettingsText(SET_MQTT_HOST), Settings.mqtt_port, SettingsText(SET_MQTT_CLIENT),
|
|
|
|
mqtt_client, SettingsText(SET_MQTT_USER), MqttConnectCount(), MQTT_MAX_PACKET_SIZE, MQTT_KEEPALIVE);
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "6"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((0 == payload) || (7 == payload)) {
|
|
|
|
if (99 == Settings.timezone) {
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("%d" ), Settings.timezone);
|
|
|
|
} else {
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("\"%s\"" ), GetTimeZone().c_str());
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
#if defined(USE_TIMERS) && defined(USE_SUNRISE)
|
2019-08-10 16:34:59 +01:00
|
|
|
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\"}}"),
|
2020-02-19 14:23:59 +00:00
|
|
|
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());
|
2019-07-27 10:13:41 +01:00
|
|
|
#else
|
2019-08-10 16:34:59 +01:00
|
|
|
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}}"),
|
2020-02-19 14:23:59 +00:00
|
|
|
GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_LOCALNOTZ).c_str(), GetDateAndTime(DT_DST).c_str(),
|
|
|
|
GetDateAndTime(DT_STD).c_str(), stemp);
|
2019-07-27 10:13:41 +01:00
|
|
|
#endif // USE_TIMERS and USE_SUNRISE
|
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "7"));
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:46:12 +01:00
|
|
|
#if defined(USE_ENERGY_SENSOR) && defined(USE_ENERGY_MARGIN_DETECTION)
|
2019-07-27 10:13:41 +01:00
|
|
|
if (energy_flg) {
|
|
|
|
if ((0 == payload) || (9 == payload)) {
|
2019-08-10 16:34:59 +01:00
|
|
|
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS9_MARGIN "\":{\"" D_CMND_POWERDELTA "\":%d,\"" D_CMND_POWERLOW "\":%d,\"" D_CMND_POWERHIGH "\":%d,\""
|
|
|
|
D_CMND_VOLTAGELOW "\":%d,\"" D_CMND_VOLTAGEHIGH "\":%d,\"" D_CMND_CURRENTLOW "\":%d,\"" D_CMND_CURRENTHIGH "\":%d}}"),
|
|
|
|
Settings.energy_power_delta, Settings.energy_min_power, Settings.energy_max_power,
|
|
|
|
Settings.energy_min_voltage, Settings.energy_max_voltage, Settings.energy_min_current, Settings.energy_max_current);
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "9"));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-08-01 14:46:12 +01:00
|
|
|
#endif // USE_ENERGY_MARGIN_DETECTION
|
2019-07-27 10:13:41 +01:00
|
|
|
|
|
|
|
if ((0 == payload) || (8 == payload) || (10 == payload)) {
|
|
|
|
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS10_SENSOR "\":"));
|
|
|
|
MqttShowSensor();
|
|
|
|
ResponseJsonEnd();
|
|
|
|
if (8 == payload) {
|
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "8"));
|
|
|
|
} else {
|
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "10"));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((0 == payload) || (11 == payload)) {
|
|
|
|
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS11_STATUS "\":"));
|
|
|
|
MqttShowState();
|
|
|
|
ResponseJsonEnd();
|
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "11"));
|
|
|
|
}
|
2019-10-10 12:39:52 +01:00
|
|
|
|
2019-12-28 13:54:26 +00:00
|
|
|
if (CrashFlag()) {
|
2019-12-08 12:18:15 +00:00
|
|
|
if ((0 == payload) || (12 == payload)) {
|
|
|
|
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS12_STATUS "\":"));
|
|
|
|
CrashDump();
|
|
|
|
ResponseJsonEnd();
|
|
|
|
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "12"));
|
|
|
|
}
|
2019-12-07 17:32:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 12:39:52 +01:00
|
|
|
#ifdef USE_SCRIPT_STATUS
|
|
|
|
if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">U",2,mqtt_data);
|
|
|
|
#endif
|
2020-05-10 10:10:55 +01:00
|
|
|
|
|
|
|
if (payload) {
|
|
|
|
XdrvRulesProcess(); // Allow rule processing on single Status command only
|
|
|
|
}
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndState(void)
|
|
|
|
{
|
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
MqttShowState();
|
2019-11-03 12:51:22 +00:00
|
|
|
if (Settings.flag3.hass_tele_on_power) { // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT
|
2019-07-27 10:13:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_STATE), MQTT_TELE_RETAIN);
|
|
|
|
}
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2019-11-03 12:51:22 +00:00
|
|
|
if (Settings.flag.hass_discovery) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59)
|
2019-07-27 10:13:41 +01:00
|
|
|
HAssPublishStatus();
|
|
|
|
}
|
|
|
|
#endif // USE_HOME_ASSISTANT
|
|
|
|
}
|
|
|
|
|
2019-11-18 08:18:21 +00:00
|
|
|
void CmndTempOffset(void)
|
|
|
|
{
|
2019-11-18 11:15:49 +00:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
int value = (int)(CharToFloat(XdrvMailbox.data) * 10);
|
|
|
|
if ((value > -127) && (value < 127)) {
|
|
|
|
Settings.temp_comp = value;
|
|
|
|
}
|
2019-11-18 08:18:21 +00:00
|
|
|
}
|
2019-11-18 11:15:49 +00:00
|
|
|
ResponseCmndFloat((float)(Settings.temp_comp) / 10, 1);
|
2019-11-18 08:18:21 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
Settings.hum_comp = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResponseCmndFloat((float)(Settings.hum_comp) / 10, 1);
|
|
|
|
}
|
|
|
|
|
2020-04-13 12:26:49 +01:00
|
|
|
void CmndGlobalTemp(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2020-04-14 10:58:24 +01:00
|
|
|
float temperature = CharToFloat(XdrvMailbox.data);
|
|
|
|
if (!isnan(temperature) && Settings.flag.temperature_conversion) { // SetOption8 - Switch between Celsius or Fahrenheit
|
|
|
|
temperature = (temperature - 32) / 1.8; // Celsius
|
|
|
|
}
|
|
|
|
if ((temperature >= -50.0) && (temperature <= 100.0)) {
|
|
|
|
ConvertTemp(temperature);
|
|
|
|
global_update = 1; // Keep global values just entered valid
|
2020-04-13 12:26:49 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 10:58:24 +01:00
|
|
|
ResponseCmndFloat(global_temperature, 1);
|
2020-04-13 12:26:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndGlobalHum(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2020-04-14 10:58:24 +01:00
|
|
|
float humidity = CharToFloat(XdrvMailbox.data);
|
|
|
|
if ((humidity >= 0.0) && (humidity <= 100.0)) {
|
|
|
|
ConvertHumidity(humidity);
|
|
|
|
global_update = 1; // Keep global values just entered valid
|
2020-04-13 12:26:49 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 10:58:24 +01:00
|
|
|
ResponseCmndFloat(global_humidity, 1);
|
2020-04-13 12:26:49 +01:00
|
|
|
}
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndSleep(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 251)) {
|
|
|
|
Settings.sleep = XdrvMailbox.payload;
|
2020-04-10 17:24:08 +01:00
|
|
|
ssleep = XdrvMailbox.payload;
|
2019-07-27 10:13:41 +01:00
|
|
|
WiFiSetSleepMode();
|
|
|
|
}
|
2020-04-10 17:24:08 +01:00
|
|
|
Response_P(S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, XdrvMailbox.command, Settings.sleep, ssleep);
|
2019-10-22 17:34:41 +01:00
|
|
|
|
2019-07-27 10:13: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))) {
|
|
|
|
ota_state_flag = 3;
|
|
|
|
char stemp1[TOPSZ];
|
|
|
|
Response_P(PSTR("{\"%s\":\"" D_JSON_VERSION " %s " D_JSON_FROM " %s\"}"), XdrvMailbox.command, my_version, GetOtaUrl(stemp1, sizeof(stemp1)));
|
|
|
|
} else {
|
|
|
|
Response_P(PSTR("{\"%s\":\"" D_JSON_ONE_OR_GT "\"}"), XdrvMailbox.command, my_version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndOtaUrl(void)
|
|
|
|
{
|
2019-12-16 14:13:57 +00:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2020-05-04 19:00:05 +01:00
|
|
|
SettingsUpdateText(SET_OTAURL, (SC_DEFAULT == Shortcut()) ? PSTR(OTA_URL) : XdrvMailbox.data);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-12-16 14:13:57 +00:00
|
|
|
ResponseCmndChar(SettingsText(SET_OTAURL));
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSeriallog(void)
|
|
|
|
{
|
2019-10-24 07:55:00 +01:00
|
|
|
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_DEBUG_MORE)) {
|
2019-11-03 11:33:36 +00:00
|
|
|
Settings.flag.mqtt_serial = 0; // CMND_SERIALSEND and CMND_SERIALLOG
|
2019-07-27 10:13:41 +01:00
|
|
|
SetSeriallog(XdrvMailbox.payload);
|
|
|
|
}
|
|
|
|
Response_P(S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, XdrvMailbox.command, Settings.seriallog_level, seriallog_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndRestart(void)
|
|
|
|
{
|
|
|
|
switch (XdrvMailbox.payload) {
|
|
|
|
case 1:
|
|
|
|
restart_flag = 2;
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndChar(D_JSON_RESTARTING);
|
2019-07-27 10:13:41 +01:00
|
|
|
break;
|
2019-12-07 17:32:39 +00:00
|
|
|
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;
|
2019-07-27 10:13:41 +01:00
|
|
|
case 99:
|
|
|
|
AddLog_P(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));
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPowerOnState(void)
|
|
|
|
{
|
2020-04-10 17:24:08 +01:00
|
|
|
#ifdef ESP8266
|
|
|
|
if (my_module_type != MOTOR)
|
|
|
|
#endif // ESP8266
|
|
|
|
{
|
2019-07-27 10:13:41 +01:00
|
|
|
/* 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)) {
|
|
|
|
Settings.poweronstate = XdrvMailbox.payload;
|
|
|
|
if (POWER_ALL_ALWAYS_ON == Settings.poweronstate) {
|
|
|
|
for (uint32_t i = 1; i <= devices_present; i++) {
|
|
|
|
ExecuteCommandPower(i, POWER_ON, SRC_IGNORE);
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.poweronstate);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPulsetime(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_PULSETIMERS)) {
|
2019-10-03 09:02:57 +01:00
|
|
|
uint32_t items = 1;
|
2019-10-03 12:11:43 +01:00
|
|
|
if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) {
|
2019-10-03 09:02:57 +01:00
|
|
|
items = MAX_PULSETIMERS;
|
|
|
|
} else {
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 65536)) {
|
|
|
|
Settings.pulse_timer[XdrvMailbox.index -1] = XdrvMailbox.payload; // 0 - 65535
|
|
|
|
SetPulseTimer(XdrvMailbox.index -1, XdrvMailbox.payload);
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-10-03 09:02:57 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
for (uint32_t i = 0; i < items; i++) {
|
2019-10-03 12:11:43 +01:00
|
|
|
uint32_t index = (1 == items) ? XdrvMailbox.index : i +1;
|
2019-10-03 09:02:57 +01:00
|
|
|
ResponseAppend_P(PSTR("%c\"%s%d\":{\"" D_JSON_SET "\":%d,\"" D_JSON_REMAINING "\":%d}"),
|
|
|
|
(i) ? ',' : '{',
|
2019-10-03 12:11:43 +01:00
|
|
|
XdrvMailbox.command, index,
|
|
|
|
Settings.pulse_timer[index -1], GetPulseTimer(index -1));
|
2019-10-03 09:02:57 +01:00
|
|
|
}
|
|
|
|
ResponseJsonEnd();
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndBlinktime(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload > 1) && (XdrvMailbox.payload <= 3600)) {
|
|
|
|
Settings.blinktime = XdrvMailbox.payload;
|
|
|
|
if (blink_timer > 0) { blink_timer = millis() + (100 * XdrvMailbox.payload); }
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.blinktime);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndBlinkcount(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 65536)) {
|
|
|
|
Settings.blinkcount = XdrvMailbox.payload; // 0 - 65535
|
|
|
|
if (blink_counter) { blink_counter = Settings.blinkcount *2; }
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.blinkcount);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSavedata(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3600)) {
|
|
|
|
Settings.save_data = XdrvMailbox.payload;
|
|
|
|
save_data_counter = Settings.save_data;
|
|
|
|
}
|
|
|
|
SettingsSaveAll();
|
|
|
|
char stemp1[TOPSZ];
|
|
|
|
if (Settings.save_data > 1) {
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR(D_JSON_EVERY " %d " D_UNIT_SECOND), Settings.save_data);
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndChar((Settings.save_data > 1) ? stemp1 : GetStateText(Settings.save_data));
|
2019-07-27 10:13:41 +01: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
|
|
|
|
|
2019-10-31 11:12:48 +00:00
|
|
|
if (XdrvMailbox.index < 114) {
|
2019-07-27 10:13:41 +01:00
|
|
|
uint32_t ptype;
|
|
|
|
uint32_t pindex;
|
|
|
|
if (XdrvMailbox.index <= 31) { // SetOption0 .. 31 = Settings.flag
|
2019-10-31 12:18:00 +00:00
|
|
|
ptype = 2;
|
2019-07-27 10:13:41 +01:00
|
|
|
pindex = XdrvMailbox.index; // 0 .. 31
|
|
|
|
}
|
|
|
|
else if (XdrvMailbox.index <= 49) { // SetOption32 .. 49 = Settings.param
|
2019-10-31 12:18:00 +00:00
|
|
|
ptype = 1;
|
2019-07-27 10:13:41 +01:00
|
|
|
pindex = XdrvMailbox.index -32; // 0 .. 17 (= PARAM8_SIZE -1)
|
|
|
|
}
|
2019-10-31 11:12:48 +00:00
|
|
|
else if (XdrvMailbox.index <= 81) { // SetOption50 .. 81 = Settings.flag3
|
2019-10-31 12:18:00 +00:00
|
|
|
ptype = 3;
|
2019-07-27 10:13:41 +01:00
|
|
|
pindex = XdrvMailbox.index -50; // 0 .. 31
|
|
|
|
}
|
2019-10-31 11:12:48 +00:00
|
|
|
else { // SetOption82 .. 113 = Settings.flag4
|
2019-10-31 12:18:00 +00:00
|
|
|
ptype = 4;
|
2019-10-31 11:12:48 +00:00
|
|
|
pindex = XdrvMailbox.index -82; // 0 .. 31
|
|
|
|
}
|
2019-10-31 12:18:00 +00:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
if (XdrvMailbox.payload >= 0) {
|
2019-10-31 12:18:00 +00:00
|
|
|
if (1 == ptype) { // SetOption32 .. 49
|
2019-07-27 10:13:41 +01:00
|
|
|
uint32_t param_low = 0;
|
|
|
|
uint32_t param_high = 255;
|
|
|
|
switch (pindex) {
|
|
|
|
case P_HOLD_TIME:
|
|
|
|
case P_MAX_POWER_RETRY:
|
|
|
|
param_low = 1;
|
|
|
|
param_high = 250;
|
|
|
|
break;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
if ((XdrvMailbox.payload >= param_low) && (XdrvMailbox.payload <= param_high)) {
|
|
|
|
Settings.param[pindex] = XdrvMailbox.payload;
|
2019-07-24 10:55:58 +01:00
|
|
|
#ifdef USE_LIGHT
|
2019-10-11 10:23:53 +01:00
|
|
|
if (P_RGB_REMAP == pindex) {
|
|
|
|
LightUpdateColorMapping();
|
2019-10-31 12:18:00 +00:00
|
|
|
restart_flag = 2; // SetOption37 needs a reboot in most cases
|
2019-10-11 10:23:53 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
#endif
|
2019-08-28 21:01:01 +01:00
|
|
|
#if (defined(USE_IR_REMOTE) && defined(USE_IR_RECEIVE)) || defined(USE_IR_REMOTE_FULL)
|
2019-10-11 10:23:53 +01:00
|
|
|
if (P_IR_UNKNOW_THRESHOLD == pindex) {
|
|
|
|
IrReceiveUpdateThreshold();
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-10-11 10:23:53 +01:00
|
|
|
#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
|
|
|
|
restart_flag = 2;
|
|
|
|
default:
|
|
|
|
bitWrite(Settings.flag.data, pindex, XdrvMailbox.payload);
|
|
|
|
}
|
|
|
|
if (12 == pindex) { // stop_flash_rotate
|
|
|
|
stop_flash_rotate = XdrvMailbox.payload;
|
|
|
|
SettingsSave(2);
|
|
|
|
}
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
if ((19 == pindex) || (30 == pindex)) {
|
|
|
|
HAssDiscover(); // Delayed execution to provide enough resources during hass_discovery or hass_light
|
|
|
|
}
|
|
|
|
#endif // USE_HOME_ASSISTANT
|
|
|
|
}
|
|
|
|
else if (3 == ptype) { // SetOption50 .. 81
|
|
|
|
bitWrite(Settings.flag3.data, pindex, XdrvMailbox.payload);
|
|
|
|
switch (pindex) {
|
|
|
|
case 5: // SetOption55
|
|
|
|
if (0 == XdrvMailbox.payload) {
|
|
|
|
restart_flag = 2; // Disable mDNS needs restart
|
|
|
|
}
|
|
|
|
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
|
2019-10-31 16:24:06 +00:00
|
|
|
case 25: // SetOption75 grouptopic change
|
2019-10-31 12:18:00 +00:00
|
|
|
restart_flag = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (4 == ptype) { // SetOption82 .. 113
|
|
|
|
bitWrite(Settings.flag4.data, pindex, XdrvMailbox.payload);
|
2020-03-14 23:53:09 +00:00
|
|
|
switch (pindex) {
|
2020-04-24 15:12:43 +01:00
|
|
|
case 3: // SetOption85 - Enable Device Groups
|
2020-03-14 23:53:09 +00:00
|
|
|
case 6: // SetOption88 - PWM Dimmer Buttons control remote devices
|
|
|
|
restart_flag = 2;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-31 12:18:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ptype = 99; // Command Error
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 12:18:00 +00:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
if (ptype < 99) {
|
2019-10-31 12:18:00 +00:00
|
|
|
if (1 == ptype) {
|
2019-10-31 11:12:48 +00:00
|
|
|
ResponseCmndIdxNumber(Settings.param[pindex]);
|
|
|
|
} else {
|
|
|
|
uint32_t flag = Settings.flag.data;
|
2019-10-31 12:18:00 +00:00
|
|
|
if (3 == ptype) {
|
2019-10-31 11:12:48 +00:00
|
|
|
flag = Settings.flag3.data;
|
|
|
|
}
|
2019-10-31 12:18:00 +00:00
|
|
|
else if (4 == ptype) {
|
2019-10-31 11:12:48 +00:00
|
|
|
flag = Settings.flag4.data;
|
|
|
|
}
|
|
|
|
ResponseCmndIdxChar(GetStateText(bitRead(flag, pindex)));
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndTemperatureResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.temperature_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.temperature_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndHumidityResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.humidity_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.humidity_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPressureResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.pressure_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.pressure_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPowerResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.wattage_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.wattage_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndVoltageResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.voltage_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.voltage_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndFrequencyResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.frequency_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.frequency_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndCurrentResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.current_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.current_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndEnergyResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 5)) {
|
|
|
|
Settings.flag2.energy_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.energy_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndWeightResolution(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 3)) {
|
|
|
|
Settings.flag2.weight_resolution = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.flag2.weight_resolution);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-02 14:51:33 +00:00
|
|
|
void CmndSpeedUnit(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6)) {
|
|
|
|
Settings.flag2.speed_conversion = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
ResponseCmndNumber(Settings.flag2.speed_conversion);
|
|
|
|
}
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
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) {
|
|
|
|
Settings.last_module = Settings.module;
|
|
|
|
Settings.module = XdrvMailbox.payload;
|
|
|
|
SetModuleType();
|
|
|
|
if (Settings.last_module != XdrvMailbox.payload) {
|
2020-04-25 10:37:36 +01:00
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.my_gp.io); i++) {
|
2019-07-27 10:13:41 +01:00
|
|
|
Settings.my_gp.io[i] = GPIO_NONE;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
restart_flag = 2;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, ModuleNr(), ModuleName().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndModules(void)
|
|
|
|
{
|
|
|
|
uint32_t midx = USER_MODULE;
|
|
|
|
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) {
|
2019-09-12 13:19:44 +01:00
|
|
|
Response_P(PSTR("{\"" D_CMND_MODULES "%d\":{"), lines);
|
2019-07-27 10:13:41 +01:00
|
|
|
} else {
|
|
|
|
ResponseAppend_P(PSTR(","));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
jsflg = true;
|
|
|
|
uint32_t j = i ? midx +1 : 0;
|
2019-09-12 13:19:44 +01:00
|
|
|
if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), j, AnyModuleName(midx).c_str()) > (LOGSZ - TOPSZ)) || (i == sizeof(kModuleNiceList))) {
|
|
|
|
ResponseJsonEndEnd();
|
2020-05-19 10:15:31 +01:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, XdrvMailbox.command);
|
2019-07-27 10:13:41 +01:00
|
|
|
jsflg = false;
|
|
|
|
lines++;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndGpio(void)
|
|
|
|
{
|
2020-04-25 10:37:36 +01:00
|
|
|
if (XdrvMailbox.index < ARRAY_SIZE(Settings.my_gp.io)) {
|
2019-07-27 10:13:41 +01:00
|
|
|
myio cmodule;
|
|
|
|
ModuleGpios(&cmodule);
|
2020-04-29 16:44:03 +01:00
|
|
|
if (ValidGPIO(XdrvMailbox.index, cmodule.io[XdrvMailbox.index]) && (XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < AGPIO(GPIO_SENSOR_END))) {
|
2019-07-27 10:13:41 +01:00
|
|
|
bool present = false;
|
2020-04-29 16:44:03 +01:00
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) {
|
|
|
|
#ifdef ESP8266
|
|
|
|
uint32_t midx = pgm_read_byte(kGpioNiceList + i);
|
|
|
|
if (midx == XdrvMailbox.payload) {
|
|
|
|
present = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else // ESP32
|
2020-04-30 17:47:34 +01:00
|
|
|
uint32_t midx = pgm_read_word(kGpioNiceList + i);
|
|
|
|
if ((XdrvMailbox.payload >= (midx & 0xFFE0)) && (XdrvMailbox.payload < midx)) {
|
2020-04-29 16:44:03 +01:00
|
|
|
present = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif // ESP8266 - ESP32
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
if (present) {
|
2020-04-25 10:37:36 +01:00
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.my_gp.io); i++) {
|
2019-07-27 10:13:41 +01:00
|
|
|
if (ValidGPIO(i, cmodule.io[i]) && (Settings.my_gp.io[i] == XdrvMailbox.payload)) {
|
|
|
|
Settings.my_gp.io[i] = GPIO_NONE;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
Settings.my_gp.io[XdrvMailbox.index] = XdrvMailbox.payload;
|
|
|
|
restart_flag = 2;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
Response_P(PSTR("{"));
|
|
|
|
bool jsflg = false;
|
2020-04-25 10:37:36 +01:00
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.my_gp.io); i++) {
|
2020-04-30 17:47:34 +01:00
|
|
|
if (ValidGPIO(i, cmodule.io[i]) || ((AGPIO(GPIO_USER) == XdrvMailbox.payload) && !FlashPin(i))) {
|
2019-07-27 10:13:41 +01:00
|
|
|
if (jsflg) { ResponseAppend_P(PSTR(",")); }
|
|
|
|
jsflg = true;
|
2020-04-29 16:44:03 +01:00
|
|
|
uint32_t sensor_type = Settings.my_gp.io[i];
|
2019-10-10 11:26:00 +01:00
|
|
|
if (!ValidGPIO(i, cmodule.io[i])) {
|
|
|
|
sensor_type = cmodule.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
|
2019-10-10 11:26:00 +01:00
|
|
|
sensor_type = GPIO_NONE;
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 17:47:34 +01:00
|
|
|
char sindex[4] = { 0 };
|
2020-04-29 16:44:03 +01:00
|
|
|
#ifdef ESP8266
|
|
|
|
uint32_t sensor_name_idx = sensor_type;
|
|
|
|
#else // ESP32
|
|
|
|
uint32_t sensor_name_idx = sensor_type >> 5;
|
2020-04-30 17:47:34 +01:00
|
|
|
uint32_t nice_list_search = sensor_type & 0xFFE0;
|
|
|
|
for (uint32_t j = 0; j < ARRAY_SIZE(kGpioNiceList); j++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 16:44:03 +01:00
|
|
|
#endif // ESP8266 - ESP32
|
2019-10-10 11:26:00 +01:00
|
|
|
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;
|
2019-10-10 11:26:00 +01:00
|
|
|
sensor_names = kSensorNamesFixed;
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
char stemp1[TOPSZ];
|
2020-04-30 17:47:34 +01:00
|
|
|
ResponseAppend_P(PSTR("\"" D_CMND_GPIO "%d\":{\"%d\":\"%s%s\"}"),
|
|
|
|
i, sensor_type, GetTextIndexed(stemp1, sizeof(stemp1), sensor_name_idx, sensor_names), sindex);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
if (jsflg) {
|
2019-07-24 10:55:58 +01:00
|
|
|
ResponseJsonEnd();
|
2019-07-27 10:13:41 +01:00
|
|
|
} else {
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndChar(D_JSON_NOT_SUPPORTED);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndGpios(void)
|
|
|
|
{
|
|
|
|
myio cmodule;
|
|
|
|
ModuleGpios(&cmodule);
|
|
|
|
uint32_t lines = 1;
|
|
|
|
bool jsflg = false;
|
2020-04-29 16:44:03 +01:00
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) {
|
|
|
|
#ifdef ESP8266
|
|
|
|
uint32_t midx = pgm_read_byte(kGpioNiceList + i);
|
|
|
|
uint32_t ridx = midx;
|
|
|
|
#else // ESP32
|
2020-04-30 17:47:34 +01:00
|
|
|
uint32_t ridx = pgm_read_word(kGpioNiceList + i) & 0xFFE0;
|
|
|
|
uint32_t midx = ridx >> 5;
|
2020-04-29 16:44:03 +01:00
|
|
|
#endif // ESP8266 - ESP32
|
2019-09-12 11:32:33 +01:00
|
|
|
if ((XdrvMailbox.payload != 255) && GetUsedInModule(midx, cmodule.io)) { continue; }
|
|
|
|
if (!jsflg) {
|
2019-09-12 13:19:44 +01:00
|
|
|
Response_P(PSTR("{\"" D_CMND_GPIOS "%d\":{"), lines);
|
2019-09-12 11:32:33 +01:00
|
|
|
} else {
|
|
|
|
ResponseAppend_P(PSTR(","));
|
|
|
|
}
|
|
|
|
jsflg = true;
|
|
|
|
char stemp1[TOPSZ];
|
2020-04-29 16:44:03 +01:00
|
|
|
if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), ridx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)) > (LOGSZ - TOPSZ)) || (i == ARRAY_SIZE(kGpioNiceList) -1)) {
|
2019-09-12 13:19:44 +01:00
|
|
|
ResponseJsonEndEnd();
|
2020-05-19 10:15:31 +01:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, XdrvMailbox.command);
|
2019-09-12 11:32:33 +01:00
|
|
|
jsflg = false;
|
|
|
|
lines++;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndTemplate(void)
|
|
|
|
{
|
|
|
|
// {"NAME":"Generic","GPIO":[17,254,29,254,7,254,254,254,138,254,139,254,254],"FLAG":1,"BASE":255}
|
|
|
|
bool error = false;
|
|
|
|
|
|
|
|
if (strstr(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
|
|
|
|
if (USER_MODULE == Settings.module) { restart_flag = 2; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (0 == XdrvMailbox.payload) { // Copy current template to user template
|
|
|
|
if (Settings.module != USER_MODULE) {
|
|
|
|
ModuleDefault(Settings.module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (255 == XdrvMailbox.payload) { // Copy current module with user configured GPIO
|
|
|
|
if (Settings.module != USER_MODULE) {
|
|
|
|
ModuleDefault(Settings.module);
|
|
|
|
}
|
2020-03-29 16:41:31 +01:00
|
|
|
SettingsUpdateText(SET_TEMPLATE_NAME, "Merged");
|
2019-07-27 10:13:41 +01:00
|
|
|
uint32_t j = 0;
|
2020-04-25 10:37:36 +01:00
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.user_template.gp.io); i++) {
|
2019-07-27 10:13:41 +01:00
|
|
|
if (6 == i) { j = 9; }
|
|
|
|
if (8 == i) { j = 12; }
|
|
|
|
if (my_module.io[j] > GPIO_NONE) {
|
|
|
|
Settings.user_template.gp.io[i] = my_module.io[j];
|
|
|
|
}
|
|
|
|
j++;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (JsonTemplate(XdrvMailbox.data)) { // Free 336 bytes StaticJsonBuffer stack space by moving code to function
|
|
|
|
if (USER_MODULE == Settings.module) { restart_flag = 2; }
|
|
|
|
} else {
|
2020-03-16 17:55:58 +00:00
|
|
|
ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON));
|
2019-07-27 10:13:41 +01:00
|
|
|
error = true;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
if (!error) { TemplateJson(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPwm(void)
|
|
|
|
{
|
|
|
|
if (pwm_present && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_PWMS)) {
|
2020-04-27 11:54:07 +01:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= Settings.pwm_range) && PinUsed(GPIO_PWM1, XdrvMailbox.index -1)) {
|
2019-07-27 10:13:41 +01:00
|
|
|
Settings.pwm_value[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2020-04-26 16:33:27 +01:00
|
|
|
analogWrite(Pin(GPIO_PWM1, XdrvMailbox.index -1), bitRead(pwm_inverted, XdrvMailbox.index -1) ? Settings.pwm_range - XdrvMailbox.payload : XdrvMailbox.payload);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
Response_P(PSTR("{"));
|
|
|
|
MqttShowPWMState(); // Render the PWM status to MQTT
|
|
|
|
ResponseJsonEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPwmfrequency(void)
|
|
|
|
{
|
|
|
|
if ((1 == XdrvMailbox.payload) || ((XdrvMailbox.payload >= PWM_MIN) && (XdrvMailbox.payload <= PWM_MAX))) {
|
|
|
|
Settings.pwm_frequency = (1 == XdrvMailbox.payload) ? PWM_FREQ : XdrvMailbox.payload;
|
2020-05-01 16:38:40 +01:00
|
|
|
#ifdef ESP8266
|
2019-07-27 10:13:41 +01:00
|
|
|
analogWriteFreq(Settings.pwm_frequency); // Default is 1000 (core_esp8266_wiring_pwm.c)
|
2020-05-01 16:38:40 +01:00
|
|
|
#else
|
|
|
|
analogWriteFreqRange(0,Settings.pwm_frequency,Settings.pwm_range);
|
|
|
|
#endif
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.pwm_frequency);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPwmrange(void)
|
|
|
|
{
|
|
|
|
if ((1 == XdrvMailbox.payload) || ((XdrvMailbox.payload > 254) && (XdrvMailbox.payload < 1024))) {
|
|
|
|
Settings.pwm_range = (1 == XdrvMailbox.payload) ? PWM_RANGE : XdrvMailbox.payload;
|
|
|
|
for (uint32_t i = 0; i < MAX_PWMS; i++) {
|
|
|
|
if (Settings.pwm_value[i] > Settings.pwm_range) {
|
|
|
|
Settings.pwm_value[i] = Settings.pwm_range;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 16:38:40 +01:00
|
|
|
#ifdef ESP8266
|
2019-07-27 10:13:41 +01:00
|
|
|
analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h)
|
2020-05-01 16:38:40 +01:00
|
|
|
#else
|
|
|
|
analogWriteFreqRange(0,Settings.pwm_frequency,Settings.pwm_range);
|
|
|
|
#endif
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.pwm_range);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndButtonDebounce(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload > 39) && (XdrvMailbox.payload < 1001)) {
|
|
|
|
Settings.button_debounce = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.button_debounce);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSwitchDebounce(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload > 39) && (XdrvMailbox.payload < 1001)) {
|
|
|
|
Settings.switch_debounce = XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.switch_debounce);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndBaudrate(void)
|
|
|
|
{
|
2019-08-28 11:02:27 +01:00
|
|
|
if (XdrvMailbox.payload >= 300) {
|
|
|
|
XdrvMailbox.payload /= 300; // Make it a valid baudrate
|
2019-12-29 12:27:48 +00:00
|
|
|
uint32_t baudrate = (XdrvMailbox.payload & 0xFFFF) * 300;
|
2019-07-27 10:13:41 +01:00
|
|
|
SetSerialBaudrate(baudrate);
|
|
|
|
}
|
2019-08-28 11:02:27 +01:00
|
|
|
ResponseCmndNumber(Settings.baudrate * 300);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:02:05 +00:00
|
|
|
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)) {
|
|
|
|
uint8_t serial_config = XdrvMailbox.payload -5; // Data bits 5, 6, 7 or 8, No parity and 1 stop bit
|
|
|
|
|
|
|
|
bool valid = true;
|
|
|
|
char parity = (XdrvMailbox.data[1] & 0xdf);
|
|
|
|
if ('E' == parity) {
|
|
|
|
serial_config += 0x08; // Even parity
|
|
|
|
}
|
|
|
|
else if ('O' == parity) {
|
|
|
|
serial_config += 0x10; // Odd parity
|
|
|
|
}
|
|
|
|
else if ('N' != parity) {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('2' == XdrvMailbox.data[2]) {
|
|
|
|
serial_config += 0x04; // Stop bits 2
|
|
|
|
}
|
|
|
|
else if ('1' != XdrvMailbox.data[2]) {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
SetSerialConfig(serial_config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResponseCmndChar(GetSerialConfig().c_str());
|
|
|
|
}
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndSerialSend(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) {
|
|
|
|
SetSeriallog(LOG_LEVEL_NONE);
|
2019-11-03 11:33:36 +00:00
|
|
|
Settings.flag.mqtt_serial = 1; // CMND_SERIALSEND and CMND_SERIALLOG
|
|
|
|
Settings.flag.mqtt_serial_raw = (XdrvMailbox.index > 3) ? 1 : 0; // CMND_SERIALSEND3
|
2019-07-27 10:13:41 +01:00
|
|
|
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"
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
else if (3 == XdrvMailbox.index) {
|
|
|
|
uint32_t dat_len = XdrvMailbox.data_len;
|
|
|
|
Serial.printf("%s", Unescape(XdrvMailbox.data, &dat_len)); // "Hello\f"
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
else if (5 == XdrvMailbox.index) {
|
|
|
|
SerialSendRaw(RemoveSpace(XdrvMailbox.data)); // "AA004566" as hex values
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndDone();
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSerialDelimiter(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.data_len > 0) && (XdrvMailbox.payload < 256)) {
|
|
|
|
if (XdrvMailbox.payload > 0) {
|
|
|
|
Settings.serial_delimiter = XdrvMailbox.payload;
|
|
|
|
} else {
|
|
|
|
uint32_t dat_len = XdrvMailbox.data_len;
|
|
|
|
Unescape(XdrvMailbox.data, &dat_len);
|
|
|
|
Settings.serial_delimiter = XdrvMailbox.data[0];
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.serial_delimiter);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSyslog(void)
|
|
|
|
{
|
2019-10-24 07:55:00 +01:00
|
|
|
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_DEBUG_MORE)) {
|
2019-07-27 10:13:41 +01:00
|
|
|
SetSyslog(XdrvMailbox.payload);
|
|
|
|
}
|
|
|
|
Response_P(S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, XdrvMailbox.command, Settings.syslog_level, syslog_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndLoghost(void)
|
|
|
|
{
|
2019-12-16 14:13:57 +00:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
SettingsUpdateText(SET_SYSLOG_HOST, (SC_DEFAULT == Shortcut()) ? SYS_LOG_HOST : XdrvMailbox.data);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-12-16 14:13:57 +00:00
|
|
|
ResponseCmndChar(SettingsText(SET_SYSLOG_HOST));
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndLogport(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload < 65536)) {
|
|
|
|
Settings.syslog_port = (1 == XdrvMailbox.payload) ? SYS_LOG_PORT : XdrvMailbox.payload;
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.syslog_port);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndIpAddress(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 4)) {
|
|
|
|
uint32_t address;
|
|
|
|
if (ParseIp(&address, XdrvMailbox.data)) {
|
|
|
|
Settings.ip_address[XdrvMailbox.index -1] = address;
|
2019-07-24 10:55:58 +01:00
|
|
|
// restart_flag = 2;
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
char stemp1[TOPSZ];
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR(" (%s)"), WiFi.localIP().toString().c_str());
|
|
|
|
Response_P(S_JSON_COMMAND_INDEX_SVALUE_SVALUE, XdrvMailbox.command, XdrvMailbox.index, IPAddress(Settings.ip_address[XdrvMailbox.index -1]).toString().c_str(), (1 == XdrvMailbox.index) ? stemp1:"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndNtpServer(void)
|
|
|
|
{
|
2020-01-12 12:10:21 +00:00
|
|
|
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);
|
2020-01-12 12:10:21 +00:00
|
|
|
SettingsUpdateText(ntp_server, ReplaceCommaWithDot(SettingsText(ntp_server)));
|
|
|
|
// restart_flag = 2; // Issue #3890
|
|
|
|
ntp_force_sync = true;
|
|
|
|
}
|
|
|
|
ResponseCmndIdxChar(SettingsText(ntp_server));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndAp(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
|
|
|
|
switch (XdrvMailbox.payload) {
|
|
|
|
case 0: // Toggle
|
|
|
|
Settings.sta_active ^= 1;
|
|
|
|
break;
|
|
|
|
case 1: // AP1
|
|
|
|
case 2: // AP2
|
|
|
|
Settings.sta_active = XdrvMailbox.payload -1;
|
|
|
|
}
|
2020-06-09 10:25:43 +01:00
|
|
|
Settings.wifi_channel = 0; // Disable stored AP
|
2019-07-27 10:13:41 +01:00
|
|
|
restart_flag = 2;
|
|
|
|
}
|
2019-12-16 14:13:57 +00:00
|
|
|
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, Settings.sta_active +1, SettingsText(SET_STASSID1 + Settings.sta_active));
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSsid(void)
|
|
|
|
{
|
2020-01-12 12:10:21 +00:00
|
|
|
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);
|
|
|
|
Settings.sta_active = XdrvMailbox.index -1;
|
|
|
|
restart_flag = 2;
|
|
|
|
}
|
|
|
|
ResponseCmndIdxChar(SettingsText(SET_STASSID1 + XdrvMailbox.index -1));
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndPassword(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 2)) {
|
2019-12-16 14:13:57 +00:00
|
|
|
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);
|
2019-07-27 10:13:41 +01:00
|
|
|
Settings.sta_active = XdrvMailbox.index -1;
|
|
|
|
restart_flag = 2;
|
2019-12-16 14:13:57 +00:00
|
|
|
ResponseCmndIdxChar(SettingsText(SET_STAPWD1 + XdrvMailbox.index -1));
|
2019-07-27 10:13:41 +01:00
|
|
|
} else {
|
|
|
|
Response_P(S_JSON_COMMAND_INDEX_ASTERISK, XdrvMailbox.command, XdrvMailbox.index);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndHostname(void)
|
|
|
|
{
|
2019-12-16 14:13:57 +00:00
|
|
|
if (!XdrvMailbox.grpflg && (XdrvMailbox.data_len > 0)) {
|
|
|
|
SettingsUpdateText(SET_HOSTNAME, (SC_DEFAULT == Shortcut()) ? WIFI_HOSTNAME : XdrvMailbox.data);
|
|
|
|
if (strstr(SettingsText(SET_HOSTNAME), "%") != nullptr) {
|
|
|
|
SettingsUpdateText(SET_HOSTNAME, WIFI_HOSTNAME);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
restart_flag = 2;
|
|
|
|
}
|
2019-12-16 14:13:57 +00:00
|
|
|
ResponseCmndChar(SettingsText(SET_HOSTNAME));
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
Settings.sta_config = XdrvMailbox.payload;
|
|
|
|
wifi_state_flag = Settings.sta_config;
|
|
|
|
if (WifiState() > WIFI_RESTART) {
|
|
|
|
restart_flag = 2;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-10-22 16:29:21 +01:00
|
|
|
char stemp1[TOPSZ];
|
|
|
|
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, Settings.sta_config, GetTextIndexed(stemp1, sizeof(stemp1), Settings.sta_config, kWifiConfig));
|
2020-05-17 16:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-05-17 16:10:17 +01:00
|
|
|
}
|
|
|
|
ResponseCmndChar(SettingsText(SET_DEVICENAME));
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmndFriendlyname(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_FRIENDLYNAMES)) {
|
2020-03-13 11:50:27 +00:00
|
|
|
if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) {
|
2020-01-12 12:10:21 +00:00
|
|
|
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);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2020-01-12 12:10:21 +00:00
|
|
|
ResponseCmndIdxChar(SettingsText(SET_FRIENDLYNAME1 + XdrvMailbox.index -1));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndSwitchMode(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SWITCHES)) {
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_SWITCH_OPTION)) {
|
|
|
|
Settings.switchmode[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndIdxNumber(Settings.switchmode[XdrvMailbox.index-1]);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndInterlock(void)
|
|
|
|
{
|
|
|
|
// Interlock 0 - Off, Interlock 1 - On, Interlock 1,2 3,4 5,6,7
|
|
|
|
uint32_t max_relays = devices_present;
|
|
|
|
if (light_type) { max_relays--; }
|
|
|
|
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 (strstr(XdrvMailbox.data, ",") != nullptr) { // Interlock entry
|
|
|
|
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);
|
|
|
|
bitSet(Settings.interlock[group_index], pbit);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
group_index++;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
for (uint32_t i = 0; i < group_index; i++) {
|
|
|
|
uint32_t minimal_bits = 0;
|
|
|
|
for (uint32_t j = 0; j < max_relays; j++) {
|
|
|
|
if (bitRead(Settings.interlock[i], j)) { minimal_bits++; }
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
if (minimal_bits < 2) { Settings.interlock[i] = 0; } // Discard single relay as interlock
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-03 11:33:36 +00:00
|
|
|
Settings.flag.interlock = XdrvMailbox.payload &1; // CMND_INTERLOCK - Enable/disable interlock
|
2019-07-27 10:13:41 +01:00
|
|
|
if (Settings.flag.interlock) {
|
|
|
|
SetDevicePower(power, SRC_IGNORE); // Remove multiple relays if set
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-12 14:40:31 +00:00
|
|
|
#ifdef USE_SHUTTER
|
|
|
|
if (Settings.flag3.shutter_mode) { // SetOption80 - Enable shutter support
|
|
|
|
ShutterInit(); // to update shutter mode
|
|
|
|
}
|
|
|
|
#endif // USE_SHUTTER
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +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++) {
|
|
|
|
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++) {
|
|
|
|
if (Settings.interlock[i] & mask) {
|
|
|
|
anybit++;
|
|
|
|
ResponseAppend_P(PSTR("%s%d"), (anybit > 1) ? "," : "", j +1);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
mask <<= 1;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
if (!anygroup) {
|
|
|
|
for (uint32_t j = 1; j <= max_relays; j++) {
|
|
|
|
ResponseAppend_P(PSTR("%s%d"), (j > 1) ? "," : "", j);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
ResponseAppend_P(PSTR("\"}"));
|
|
|
|
} else {
|
2019-11-03 11:33:36 +00:00
|
|
|
Settings.flag.interlock = 0; // CMND_INTERLOCK - Enable/disable interlock
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndStateText(Settings.flag.interlock);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndTeleperiod(void)
|
2019-07-24 10:55:58 +01:00
|
|
|
{
|
2019-07-27 10:13:41 +01:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
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
|
2020-05-16 16:44:29 +01:00
|
|
|
// tele_period = Settings.tele_period;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2020-05-16 16:44:29 +01:00
|
|
|
tele_period = Settings.tele_period; // Show teleperiod data also on empty command
|
2019-10-22 17:34:41 +01:00
|
|
|
ResponseCmndNumber(Settings.tele_period);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndReset(void)
|
|
|
|
{
|
|
|
|
switch (XdrvMailbox.payload) {
|
|
|
|
case 1:
|
|
|
|
restart_flag = 211;
|
2020-05-04 19:00:05 +01:00
|
|
|
ResponseCmndChar(PSTR(D_JSON_RESET_AND_RESTARTING));
|
2019-07-27 10:13:41 +01:00
|
|
|
break;
|
|
|
|
case 2 ... 6:
|
|
|
|
restart_flag = 210 + XdrvMailbox.payload;
|
|
|
|
Response_P(PSTR("{\"" D_CMND_RESET "\":\"" D_JSON_ERASE ", " D_JSON_RESET_AND_RESTARTING "\"}"));
|
|
|
|
break;
|
2019-09-05 11:32:28 +01:00
|
|
|
case 99:
|
|
|
|
Settings.bootcount = 0;
|
2020-02-07 11:38:10 +00:00
|
|
|
Settings.bootcount_reset_time = 0;
|
2019-09-05 11:32:28 +01:00
|
|
|
ResponseCmndDone();
|
|
|
|
break;
|
2019-07-27 10:13:41 +01:00
|
|
|
default:
|
2020-05-04 19:00:05 +01:00
|
|
|
ResponseCmndChar(PSTR(D_JSON_ONE_TO_RESET));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndTime(void)
|
|
|
|
{
|
2019-09-04 17:06:34 +01:00
|
|
|
// payload 0 = (re-)enable NTP
|
2019-09-06 14:46:40 +01:00
|
|
|
// payload 1 = Time format {"Time":"2019-09-04T14:31:29"}
|
|
|
|
// payload 2 = Time format {"Time":"2019-09-04T14:31:29","Epoch":1567600289}
|
2019-09-04 17:06:34 +01:00
|
|
|
// payload 3 = Time format {"Time":1567600289}
|
2020-05-25 10:44:17 +01:00
|
|
|
// payload 4 = Time format {"Time":"2019-09-04T14:31:29.123"}
|
2019-09-04 17:06:34 +01:00
|
|
|
// payload 1451602800 - disable NTP and set time to epoch
|
|
|
|
|
|
|
|
uint32_t format = Settings.flag2.time_format;
|
2019-07-27 10:13:41 +01:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2020-05-25 10:44:17 +01:00
|
|
|
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload < 5)) {
|
2019-09-04 17:06:34 +01:00
|
|
|
Settings.flag2.time_format = XdrvMailbox.payload -1;
|
|
|
|
format = Settings.flag2.time_format;
|
|
|
|
} else {
|
2019-09-06 14:46:40 +01:00
|
|
|
format = 1; // {"Time":"2019-09-04T14:31:29","Epoch":1567600289}
|
2019-09-04 17:06:34 +01:00
|
|
|
RtcSetTime(XdrvMailbox.payload);
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-09-04 17:06:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
|
|
|
ResponseAppendTimeFormat(format);
|
2019-07-27 10:13:41 +01:00
|
|
|
ResponseJsonEnd();
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndTimezone(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= -13)) {
|
|
|
|
Settings.timezone = XdrvMailbox.payload;
|
|
|
|
Settings.timezone_minutes = 0;
|
|
|
|
if (XdrvMailbox.payload < 15) {
|
|
|
|
char *p = strtok (XdrvMailbox.data, ":");
|
|
|
|
if (p) {
|
|
|
|
p = strtok (nullptr, ":");
|
|
|
|
if (p) {
|
|
|
|
Settings.timezone_minutes = strtol(p, nullptr, 10);
|
|
|
|
if (Settings.timezone_minutes > 59) { Settings.timezone_minutes = 59; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Settings.timezone = 99;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
ntp_force_sync = true;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
if (99 == Settings.timezone) {
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.timezone);
|
2019-07-27 10:13:41 +01:00
|
|
|
} else {
|
|
|
|
char stemp1[TOPSZ];
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR("%+03d:%02d"), Settings.timezone, Settings.timezone_minutes);
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndChar(stemp1);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-08-01 14:46:12 +01:00
|
|
|
void CmndTimeStdDst(uint32_t ts)
|
2019-07-27 10:13:41 +01:00
|
|
|
{
|
|
|
|
// TimeStd 0/1, 0/1/2/3/4, 1..12, 1..7, 0..23, +/-780
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (strstr(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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
ntp_force_sync = true;
|
|
|
|
} else {
|
|
|
|
if (0 == XdrvMailbox.payload) {
|
|
|
|
if (0 == ts) {
|
|
|
|
SettingsResetStd();
|
|
|
|
} else {
|
|
|
|
SettingsResetDst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ntp_force_sync = true;
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
Response_P(PSTR("{\"%s\":{\"Hemisphere\":%d,\"Week\":%d,\"Month\":%d,\"Day\":%d,\"Hour\":%d,\"Offset\":%d}}"),
|
|
|
|
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]);
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-08-01 14:46:12 +01:00
|
|
|
void CmndTimeStd(void)
|
|
|
|
{
|
|
|
|
CmndTimeStdDst(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndTimeDst(void)
|
|
|
|
{
|
|
|
|
CmndTimeStdDst(1);
|
|
|
|
}
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndAltitude(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.data_len > 0) && ((XdrvMailbox.payload >= -30000) && (XdrvMailbox.payload <= 30000))) {
|
|
|
|
Settings.altitude = XdrvMailbox.payload;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.altitude);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndLedPower(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_LEDS)) {
|
2020-04-27 16:16:52 +01:00
|
|
|
if (!PinUsed(GPIO_LEDLNK)) { XdrvMailbox.index = 1; }
|
2019-07-27 10:13:41 +01:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
|
|
|
|
Settings.ledstate &= 8; // Disable power control
|
|
|
|
uint32_t mask = 1 << (XdrvMailbox.index -1); // Led to control
|
|
|
|
switch (XdrvMailbox.payload) {
|
|
|
|
case 0: // Off
|
|
|
|
led_power &= (0xFF ^ mask);
|
|
|
|
Settings.ledstate = 0;
|
|
|
|
break;
|
|
|
|
case 1: // On
|
|
|
|
led_power |= mask;
|
|
|
|
Settings.ledstate = 8;
|
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
|
|
|
led_power ^= mask;
|
|
|
|
Settings.ledstate ^= 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
blinks = 0;
|
2020-04-27 16:16:52 +01:00
|
|
|
if (!PinUsed(GPIO_LEDLNK)) {
|
2019-07-27 10:13:41 +01:00
|
|
|
SetLedPower(Settings.ledstate &8);
|
|
|
|
} else {
|
|
|
|
SetLedPowerIdx(XdrvMailbox.index -1, (led_power & mask));
|
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
bool state = bitRead(led_power, XdrvMailbox.index -1);
|
2020-04-27 16:16:52 +01:00
|
|
|
if (!PinUsed(GPIO_LEDLNK)) {
|
2019-07-27 10:13:41 +01:00
|
|
|
state = bitRead(Settings.ledstate, 3);
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndIdxChar(GetStateText(state));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndLedState(void)
|
|
|
|
{
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_LED_OPTION)) {
|
|
|
|
Settings.ledstate = XdrvMailbox.payload;
|
|
|
|
if (!Settings.ledstate) {
|
|
|
|
SetLedPowerAll(0);
|
|
|
|
SetLedLink(0);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndNumber(Settings.ledstate);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndLedMask(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
Settings.ledmask = XdrvMailbox.payload;
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
char stemp1[TOPSZ];
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR("%d (0x%04X)"), Settings.ledmask, Settings.ledmask);
|
2019-08-03 12:01:34 +01:00
|
|
|
ResponseCmndChar(stemp1);
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2020-05-23 14:17:13 +01:00
|
|
|
void CmndLedPwmOff(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (XdrvMailbox.payload < 0) {
|
|
|
|
Settings.ledpwm_off = 0;
|
|
|
|
}
|
|
|
|
else if (XdrvMailbox.payload > 255) {
|
|
|
|
Settings.ledpwm_off = 255;
|
|
|
|
} else {
|
|
|
|
Settings.ledpwm_off = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
UpdateLedPowerAll();
|
|
|
|
}
|
|
|
|
ResponseCmndNumber(Settings.ledpwm_off);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndLedPwmOn(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (XdrvMailbox.payload < 0) {
|
|
|
|
Settings.ledpwm_on = 0;
|
|
|
|
}
|
|
|
|
else if (XdrvMailbox.payload > 255) {
|
|
|
|
Settings.ledpwm_on = 255;
|
|
|
|
} else {
|
|
|
|
Settings.ledpwm_on = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
UpdateLedPowerAll();
|
|
|
|
}
|
|
|
|
ResponseCmndNumber(Settings.ledpwm_on);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
Settings.ledpwm_mask &= (0xFF ^ mask);
|
|
|
|
break;
|
|
|
|
case 1: // pwm
|
|
|
|
Settings.ledpwm_mask |= mask;
|
|
|
|
break;
|
|
|
|
case 2: // toggle
|
|
|
|
Settings.ledpwm_mask ^= mask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UpdateLedPowerAll();
|
|
|
|
}
|
|
|
|
bool state = bitRead(Settings.ledpwm_mask, XdrvMailbox.index -1);
|
|
|
|
ResponseCmndIdxChar(GetStateText(state));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:00:32 +00:00
|
|
|
void CmndWifiPower(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
Settings.wifi_output_power = (uint8_t)(CharToFloat(XdrvMailbox.data) * 10);
|
|
|
|
if (Settings.wifi_output_power > 205) {
|
|
|
|
Settings.wifi_output_power = 205;
|
|
|
|
}
|
|
|
|
WifiSetOutputPower();
|
|
|
|
}
|
2019-12-27 10:13:22 +00:00
|
|
|
ResponseCmndChar(WifiGetOutputPower().c_str());
|
2019-11-08 12:00:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
#ifdef USE_I2C
|
|
|
|
void CmndI2cScan(void)
|
|
|
|
{
|
|
|
|
if (i2c_flg) {
|
|
|
|
I2cScan(mqtt_data, sizeof(mqtt_data));
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
}
|
2019-11-03 16:54:39 +00:00
|
|
|
|
|
|
|
void CmndI2cDriver(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.index < MAX_I2C_DRIVERS) {
|
|
|
|
if (XdrvMailbox.payload >= 0) {
|
|
|
|
bitWrite(Settings.i2c_drivers[XdrvMailbox.index / 32], XdrvMailbox.index % 32, XdrvMailbox.payload &1);
|
2019-11-03 16:57:22 +00:00
|
|
|
restart_flag = 2;
|
2019-11-03 16:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Response_P(PSTR("{\"" D_CMND_I2CDRIVER "\":"));
|
2019-11-04 09:38:05 +00:00
|
|
|
I2cDriverState();
|
2019-11-03 16:54:39 +00:00
|
|
|
ResponseJsonEnd();
|
|
|
|
}
|
2019-07-27 10:13:41 +01:00
|
|
|
#endif // USE_I2C
|
2019-07-24 10:55:58 +01:00
|
|
|
|
2020-02-21 15:09:21 +00:00
|
|
|
#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);
|
|
|
|
restart_flag = 2;
|
|
|
|
}
|
|
|
|
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) {
|
2020-04-24 15:12:43 +01:00
|
|
|
if (!_SendDeviceGroupMessage(device_group_index, DGR_MSGTYPE_UPDATE_COMMAND)) {
|
|
|
|
ResponseCmndChar(XdrvMailbox.data);
|
|
|
|
}
|
2020-04-07 19:25:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_DEVICE_GROUPS_SEND
|
|
|
|
|
2020-02-21 15:09:21 +00:00
|
|
|
void CmndDevGroupShare(void)
|
|
|
|
{
|
|
|
|
uint32_t parm[2] = { Settings.device_group_share_in, Settings.device_group_share_out };
|
|
|
|
ParseParameters(2, parm);
|
|
|
|
Settings.device_group_share_in = parm[0];
|
|
|
|
Settings.device_group_share_out = parm[1];
|
2020-02-25 11:13:49 +00:00
|
|
|
Response_P(PSTR("{\"" D_CMND_DEVGROUP_SHARE "\":{\"In\":\"%X\",\"Out\":\"%X\"}}"), Settings.device_group_share_in, Settings.device_group_share_out);
|
2020-02-21 15:09:21 +00:00
|
|
|
}
|
2020-04-15 14:40:18 +01:00
|
|
|
|
|
|
|
void CmndDevGroupStatus(void)
|
|
|
|
{
|
|
|
|
DeviceGroupStatus((XdrvMailbox.usridx ? XdrvMailbox.index - 1 : 0));
|
|
|
|
}
|
2020-02-21 15:09:21 +00:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
|
2019-07-27 10:13:41 +01:00
|
|
|
void CmndSensor(void)
|
|
|
|
{
|
|
|
|
XsnsCall(FUNC_COMMAND_SENSOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndDriver(void)
|
|
|
|
{
|
|
|
|
XdrvCall(FUNC_COMMAND_DRIVER);
|
2019-07-24 10:55:58 +01:00
|
|
|
}
|
2020-05-30 12:50:22 +01:00
|
|
|
|
|
|
|
#ifdef ESP32
|
|
|
|
void CmndTouchCal(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.payload >= 0) {
|
|
|
|
if (XdrvMailbox.payload < MAX_KEYS + 1) TOUCH_BUTTON.calibration = bitSet(TOUCH_BUTTON.calibration, XdrvMailbox.payload);
|
|
|
|
if (XdrvMailbox.payload == 0) TOUCH_BUTTON.calibration = 0;
|
|
|
|
if (XdrvMailbox.payload == 255) TOUCH_BUTTON.calibration = 255; // all pinss
|
|
|
|
}
|
|
|
|
Response_P(PSTR("{\"" D_CMND_TOUCH_CAL "\": %u"), TOUCH_BUTTON.calibration);
|
|
|
|
ResponseJsonEnd();
|
|
|
|
AddLog_P2(LOG_LEVEL_INFO, PSTR("Button Touchvalue Hits,"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndTouchThres(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.payload >= 0) {
|
|
|
|
if (XdrvMailbox.payload<256){
|
|
|
|
TOUCH_BUTTON.pin_threshold = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Response_P(PSTR("{\"" D_CMND_TOUCH_THRES "\": %u"), TOUCH_BUTTON.pin_threshold);
|
|
|
|
ResponseJsonEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndTouchNum(void)
|
2020-05-30 13:59:52 +01:00
|
|
|
{
|
2020-05-30 12:50:22 +01:00
|
|
|
if (XdrvMailbox.payload >= 0) {
|
|
|
|
if (XdrvMailbox.payload<32){
|
|
|
|
TOUCH_BUTTON.hit_threshold = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Response_P(PSTR("{\"" D_CMND_TOUCH_NUM "\": %u"), TOUCH_BUTTON.hit_threshold);
|
|
|
|
ResponseJsonEnd();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //ESP32
|