Skip to content

Commit

Permalink
Merge pull request #3730 from tonhuisman/bugfix/task-commands-check-e…
Browse files Browse the repository at this point in the history
…nabled

[Task commands] Check if task enabled before action (bugfix)
  • Loading branch information
TD-er authored Aug 2, 2021
2 parents e6284e8 + 1b022aa commit 68d92bb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/src/Commands/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Simple function to return "Ok", to avoid flash string duplication in the firmware.
const __FlashStringHelper * return_command_success()
{
return F("\nOk");
return F("\nOK");
}

const __FlashStringHelper * return_command_failed()
Expand Down
55 changes: 41 additions & 14 deletions src/src/Commands/Tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../ESPEasyCore/Serial.h"

#include "../Globals/RuntimeData.h"
#include "../Globals/Settings.h"

#include "../Helpers/Misc.h"
#include "../Helpers/Rules_calculate.h"
Expand Down Expand Up @@ -69,12 +70,23 @@ bool validateAndParseTaskValueArguments(struct EventStruct * event, const char *
return true;
}

bool taskValueSet(struct EventStruct *event, const char *Line, taskIndex_t& taskIndex)
const __FlashStringHelper * taskValueSet(struct EventStruct *event, const char *Line, taskIndex_t& taskIndex, bool& success)
{
String TmpStr1;
unsigned int varNr;

if (!(validateAndParseTaskValueArguments(event, Line, taskIndex, varNr) && (getDevicePluginID_from_TaskIndex(taskIndex) == 33))) { return false; } // PluginID 33 = Dummy Device
if (!validateAndParseTaskValueArguments(event, Line, taskIndex, varNr)) {
success = false;
return F("INVALID_PARAMETERS");
}
if (getPluginID_from_TaskIndex(taskIndex) != 33) { // PluginID 33 = Dummy Device
success = false;
return F("NOT_A_DUMMY_TASK");
}
if (!Settings.TaskDeviceEnabled[taskIndex]) {
success = false;
return F("TASK_NOT_ENABLED");
}

unsigned int uservarIndex = (VARS_PER_TASK * taskIndex) + varNr;

Expand All @@ -83,22 +95,26 @@ bool taskValueSet(struct EventStruct *event, const char *Line, taskIndex_t& task
double result = 0;

if (isError(Calculate(TmpStr1, result))) {
return false;
success = false;
return F("CALCULATION_ERROR");
}
UserVar[uservarIndex] = result;
} else {
// TODO: Get Task description and var name
serialPrintln(String(UserVar[uservarIndex]));
}
return true;
success = true;
return return_command_success();
}

const __FlashStringHelper * Command_Task_Clear(struct EventStruct *event, const char *Line)
{
taskIndex_t taskIndex;
unsigned int varNr;

if (!validateAndParseTaskValueArguments(event, Line, taskIndex, varNr)) { return return_command_failed(); }
if (!validateAndParseTaskValueArguments(event, Line, taskIndex, varNr)) {
return F("INVALID_PARAMETERS");
}

taskClear(taskIndex, true);
return return_command_success();
Expand All @@ -124,8 +140,9 @@ const __FlashStringHelper * Command_Task_EnableDisable(struct EventStruct *event
if (setTaskEnableStatus(event, enable)) {
return return_command_success();
}
return return_command_failed();
}
return return_command_failed();
return F("INVALID_PARAMETERS");
}

const __FlashStringHelper * Command_Task_Disable(struct EventStruct *event, const char *Line)
Expand All @@ -141,17 +158,21 @@ const __FlashStringHelper * Command_Task_Enable(struct EventStruct *event, const
const __FlashStringHelper * Command_Task_ValueSet(struct EventStruct *event, const char *Line)
{
taskIndex_t taskIndex;

if (taskValueSet(event, Line, taskIndex)) { return return_command_success(); }
return return_command_failed();
bool success;
return taskValueSet(event, Line, taskIndex, success);
}

const __FlashStringHelper * Command_Task_ValueToggle(struct EventStruct *event, const char *Line)
{
taskIndex_t taskIndex;
unsigned int varNr;

if (!validateAndParseTaskValueArguments(event, Line, taskIndex, varNr)) return return_command_failed();
if (!validateAndParseTaskValueArguments(event, Line, taskIndex, varNr)) {
return F("INVALID_PARAMETERS");
}
if (!Settings.TaskDeviceEnabled[taskIndex]) {
return F("TASK_NOT_ENABLED");
}

unsigned int uservarIndex = (VARS_PER_TASK * taskIndex) + varNr;
const int result = round(UserVar[uservarIndex]);
Expand All @@ -165,21 +186,27 @@ const __FlashStringHelper * Command_Task_ValueToggle(struct EventStruct *event,
const __FlashStringHelper * Command_Task_ValueSetAndRun(struct EventStruct *event, const char *Line)
{
taskIndex_t taskIndex;

if (taskValueSet(event, Line, taskIndex))
bool success;
const __FlashStringHelper * returnvalue = taskValueSet(event, Line, taskIndex, success);
if (success)
{
SensorSendTask(taskIndex);
return return_command_success();
}
return return_command_failed();
return returnvalue;
}

const __FlashStringHelper * Command_Task_Run(struct EventStruct *event, const char *Line)
{
taskIndex_t taskIndex;
unsigned int varNr;

if (!validateAndParseTaskValueArguments(event, Line, taskIndex, varNr)) { return return_command_failed(); }
if (!validateAndParseTaskValueArguments(event, Line, taskIndex, varNr)) {
return F("INVALID_PARAMETERS");
}
if (!Settings.TaskDeviceEnabled[taskIndex]) {
return F("TASK_NOT_ENABLED");
}

SensorSendTask(taskIndex);
return return_command_success();
Expand Down
6 changes: 4 additions & 2 deletions src/src/Globals/Plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ deviceIndex_t getDeviceIndex_from_TaskIndex(taskIndex_t taskIndex) {
/*********************************************************************************************
* get the taskPluginID with required checks, INVALID_PLUGIN_ID when invalid
********************************************************************************************/
pluginID_t getDevicePluginID_from_TaskIndex(taskIndex_t taskIndex) {
pluginID_t getPluginID_from_TaskIndex(taskIndex_t taskIndex) {
if (validTaskIndex(taskIndex)) {
const deviceIndex_t DeviceIndex = getDeviceIndex_from_TaskIndex(taskIndex);

return (validDeviceIndex(DeviceIndex) ? Device[DeviceIndex].Number : INVALID_PLUGIN_ID);
if (validDeviceIndex(DeviceIndex)) {
return DeviceIndex_to_Plugin_id[DeviceIndex];
}
}
return INVALID_PLUGIN_ID;
}
Expand Down
2 changes: 1 addition & 1 deletion src/src/Globals/Plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ deviceIndex_t getDeviceIndex_from_TaskIndex(taskIndex_t taskIndex);
/*********************************************************************************************
* get the taskPluginID with required checks, INVALID_PLUGIN_ID when invalid
********************************************************************************************/
pluginID_t getDevicePluginID_from_TaskIndex(taskIndex_t taskIndex);
pluginID_t getPluginID_from_TaskIndex(taskIndex_t taskIndex);



Expand Down
10 changes: 7 additions & 3 deletions src/src/Helpers/WebServer_commandHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ HandledWebCommand_result handle_command_from_web(EventValueSource::Enum source,
sendOK = true;
} else if (command.equals(F("taskrun")) ||
command.equals(F("taskvalueset")) ||
command.equals(F("taskvaluesetandrun")) ||
command.equals(F("taskvaluetoggle")) ||
command.equals(F("let")) ||
command.equals(F("logPortStatus")) ||
command.equals(F("logportstatus")) ||
command.equals(F("jsonportstatus")) ||
command.equals(F("rules"))) {
printToWeb = true;
handledCmd = ExecuteCommand_internal(source, webrequest.c_str());
sendOK = true;

Expand All @@ -53,15 +55,17 @@ HandledWebCommand_result handle_command_from_web(EventValueSource::Enum source,

if (handledCmd) {
if (sendOK) {
String reply = printWebString.isEmpty() ? F("OK") : printWebString;
reply.replace(F("\n"), EMPTY_STRING); // Don't use newline in JSON.
if (printToWebJSON) {
// Format "OK" to JSON format
printWebString = F("{\"return\": \"");
printWebString += F("OK");
printWebString += reply;
printWebString += F("\",\"command\": \"");
printWebString += webrequest;
printWebString += F("\"}");
} else {
printWebString = F("OK");
printWebString = reply;
}
}
return HandledWebCommand_result::CommandHandled;
Expand Down

0 comments on commit 68d92bb

Please sign in to comment.