[Commands] Adapt all plugins to use parseString for parsing commands

This commit is contained in:
Gijs Noorlander
2019-11-24 04:07:38 +01:00
parent c63f3a28d5
commit 4d7f377788
9 changed files with 104 additions and 147 deletions
+1 -1
View File
@@ -95,7 +95,7 @@ done
mkdir -p ${TMP_DIST}/source
cp -r lib ${TMP_DIST}/source/
cp -r src ${TMP_DIST}/source/
cp platformio.ini ${TMP_DIST}/source/
cp platformio*.ini ${TMP_DIST}/source/
cd ${TMP_DIST}
+18
View File
@@ -1320,6 +1320,15 @@ bool validIntFromString(const String& tBuf, int& result) {
return isvalid;
}
bool validUIntFromString(const String& tBuf, unsigned int& result) {
int tmp;
if (!validIntFromString(tBuf, tmp)) return false;
if (tmp < 0) return false;
result = static_cast<unsigned int>(tmp);
return true;
}
bool validFloatFromString(const String& tBuf, float& result) {
const String numerical = getNumerical(tBuf, false);
const bool isvalid = numerical.length() > 0;
@@ -1329,6 +1338,15 @@ bool validFloatFromString(const String& tBuf, float& result) {
return isvalid;
}
bool validDoubleFromString(const String& tBuf, double& result) {
const String numerical = getNumerical(tBuf, false);
const bool isvalid = numerical.length() > 0;
if (isvalid) {
result = numerical.toDouble();
}
return isvalid;
}
String getNumerical(const String& tBuf, bool mustBeInteger) {
String result = "";
+14 -22
View File
@@ -206,36 +206,28 @@ boolean Plugin_012(byte function, struct EventStruct *event, String& string)
case PLUGIN_WRITE:
{
// FIXME TD-er: This one is not using parseString* function
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (lcd && tmpString.equalsIgnoreCase(F("LCDCMD")))
String cmd = parseString(string, 1);
if (lcd && cmd.equalsIgnoreCase(F("LCDCMD")))
{
success = true;
argIndex = string.lastIndexOf(',');
tmpString = string.substring(argIndex + 1);
if (tmpString.equalsIgnoreCase(F("Off"))){
String arg1 = parseString(string, 2);
if (arg1.equalsIgnoreCase(F("Off"))){
lcd->noBacklight();
}
else if (tmpString.equalsIgnoreCase(F("On"))){
else if (arg1.equalsIgnoreCase(F("On"))){
lcd->backlight();
}
else if (tmpString.equalsIgnoreCase(F("Clear"))){
else if (arg1.equalsIgnoreCase(F("Clear"))){
lcd->clear();
}
}
else if (lcd && tmpString.equalsIgnoreCase(F("LCD")))
else if (lcd && cmd.equalsIgnoreCase(F("LCD")))
{
success = true;
tmpString = P012_parseTemplate(string, Plugin_012_cols);
argIndex = tmpString.lastIndexOf(',');
tmpString = tmpString.substring(argIndex + 1);
int colPos = event->Par2 - 1;
int rowPos = event->Par1 - 1;
String text = parseString(string, 4);
text = P012_parseTemplate(text, Plugin_012_cols);
//clear line before writing new string
if (Plugin_012_mode == 2){
@@ -250,8 +242,8 @@ boolean Plugin_012(byte function, struct EventStruct *event, String& string)
if(Plugin_012_mode == 1 || Plugin_012_mode == 2){
lcd->setCursor(colPos, rowPos);
for (byte i = 0; i < Plugin_012_cols - colPos; i++) {
if(tmpString[i]){
lcd->print(tmpString[i]);
if(text[i]){
lcd->print(text[i]);
}
}
}
@@ -270,15 +262,15 @@ boolean Plugin_012(byte function, struct EventStruct *event, String& string)
//dont print if "lower" than the lcd
if(rowPos < Plugin_012_rows ){
lcd->print(tmpString[charCount - 1]);
lcd->print(text[charCount - 1]);
}
if (!tmpString[charCount]) { // no more chars to process?
if (!text[charCount]) { // no more chars to process?
stillProcessing = 0;
}
charCount += 1;
}
//lcd->print(tmpString.c_str());
//lcd->print(text.c_str());
// end fix
}
+15 -15
View File
@@ -240,6 +240,9 @@ boolean Plugin_023(byte function, struct EventStruct *event, String& string)
String arguments = String(string);
//Fixed bug #1864
// this was to manage multiple instances of the plug-in.
// You can also call it this way:
// [TaskName].OLED, 1,1, Temp. is 19.9
int dotPos = arguments.indexOf('.');
if(dotPos > -1 && arguments.substring(dotPos,dotPos+4).equalsIgnoreCase(F("oled")))
{
@@ -257,29 +260,26 @@ boolean Plugin_023(byte function, struct EventStruct *event, String& string)
}
}
// FIXME TD-er: This one is not using parseString* function
int argIndex = arguments.indexOf(',');
if (argIndex)
arguments = arguments.substring(0, argIndex);
if (arguments.equalsIgnoreCase(F("OLEDCMD")))
// We now continue using 'arguments' and not 'string' as full command line.
// If there was any prefix to address a specific task, it is now removed from 'arguments'
String cmd = parseString(arguments, 1);
if (cmd.equalsIgnoreCase(F("OLEDCMD")))
{
success = true;
argIndex = string.lastIndexOf(',');
arguments = string.substring(argIndex + 1);
if (arguments.equalsIgnoreCase(F("Off")))
String param = parseString(arguments, 2);
if (param.equalsIgnoreCase(F("Off")))
Plugin_023_displayOff(OLED_Settings[index]);
else if (arguments.equalsIgnoreCase(F("On")))
else if (param.equalsIgnoreCase(F("On")))
Plugin_023_displayOn(OLED_Settings[index]);
else if (arguments.equalsIgnoreCase(F("Clear")))
else if (param.equalsIgnoreCase(F("Clear")))
Plugin_023_clear_display(OLED_Settings[index]);
}
else if (arguments.equalsIgnoreCase(F("OLED")))
else if (cmd.equalsIgnoreCase(F("OLED")))
{
success = true;
argIndex = string.lastIndexOf(',');
arguments = string.substring(argIndex + 1);
String newString = P023_parseTemplate(arguments, 16);
Plugin_023_sendStrXY(OLED_Settings[index], newString.c_str(), event->Par1 - 1, event->Par2 - 1);
String text = parseStringToEndKeepCase(arguments, 4);
text = P023_parseTemplate(text, 16);
Plugin_023_sendStrXY(OLED_Settings[index], text.c_str(), event->Par1 - 1, event->Par2 - 1);
}
break;
}
+7 -12
View File
@@ -119,13 +119,8 @@ boolean Plugin_038(byte function, struct EventStruct *event, String& string)
log += string;
}
// FIXME TD-er: This one is not using parseString* function
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase(F("NeoPixel")))
String cmd = parseString(string, 1);
if (cmd.equalsIgnoreCase(F("NeoPixel")))
{
// char Line[80];
// char TmpStr1[80];
@@ -139,7 +134,7 @@ boolean Plugin_038(byte function, struct EventStruct *event, String& string)
}
// extra function to receive HSV values (i.e. homie controler)
if (tmpString.equalsIgnoreCase(F("NeoPixelHSV")))
if (cmd.equalsIgnoreCase(F("NeoPixelHSV")))
{
int rgbw[4];
rgbw[3]=0;
@@ -164,7 +159,7 @@ boolean Plugin_038(byte function, struct EventStruct *event, String& string)
success = true;
}
if (tmpString.equalsIgnoreCase(F("NeoPixelAll")))
if (cmd.equalsIgnoreCase(F("NeoPixelAll")))
{
// char Line[80];
// char TmpStr1[80];
@@ -179,7 +174,7 @@ boolean Plugin_038(byte function, struct EventStruct *event, String& string)
}
if (tmpString.equalsIgnoreCase(F("NeoPixelAllHSV"))) {
if (cmd.equalsIgnoreCase(F("NeoPixelAllHSV"))) {
int rgbw[4];
rgbw[3]=0;
if (PCONFIG(1)==1) { // RGB
@@ -207,7 +202,7 @@ boolean Plugin_038(byte function, struct EventStruct *event, String& string)
success = true;
}
if (tmpString.equalsIgnoreCase(F("NeoPixelLine")))
if (cmd.equalsIgnoreCase(F("NeoPixelLine")))
{
// char Line[80];
// char TmpStr1[80];
@@ -225,7 +220,7 @@ boolean Plugin_038(byte function, struct EventStruct *event, String& string)
success = true;
}
if (tmpString.equalsIgnoreCase(F("NeoPixelLineHSV")))
if (cmd.equalsIgnoreCase(F("NeoPixelLineHSV")))
{
int rgbw[4];
rgbw[3]=0;
+4 -9
View File
@@ -110,13 +110,8 @@ boolean Plugin_041(byte function, struct EventStruct *event, String& string)
case PLUGIN_WRITE:
{
// FIXME TD-er: This one is not using parseString* function
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase(F("NeoClockColor")))
String cmd = parseString(string, 1);
if (cmd.equalsIgnoreCase(F("NeoClockColor")))
{
Plugin_041_red = event->Par1;
Plugin_041_green = event->Par2;
@@ -125,7 +120,7 @@ boolean Plugin_041(byte function, struct EventStruct *event, String& string)
success = true;
}
if (tmpString.equalsIgnoreCase(F("NeoTestAll")))
if (cmd.equalsIgnoreCase(F("NeoTestAll")))
{
for (int i = 0; i < NUM_LEDS; i++)
Plugin_041_pixels->setPixelColor(i, Plugin_041_pixels->Color(event->Par1, event->Par2, event->Par3));
@@ -133,7 +128,7 @@ boolean Plugin_041(byte function, struct EventStruct *event, String& string)
success = true;
}
if (tmpString.equalsIgnoreCase(F("NeoTestLoop")))
if (cmd.equalsIgnoreCase(F("NeoTestLoop")))
{
for (int i = 0; i < NUM_LEDS; i++)
{
+29 -49
View File
@@ -435,38 +435,32 @@ bool p073_plugin_write(struct EventStruct *event, const String& string) {
return false;
}
// FIXME TD-er: This one is not using parseString* function
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex) {
tmpString = tmpString.substring(0, argIndex);
}
tmpString.toLowerCase();
if (tmpString.equals("7dn")) {
return p073_plugin_write_7dn(event, string);
} else if (tmpString.equals("7dt")) {
return p073_plugin_write_7dt(event, string);
} else if (tmpString.equals("7dst")) {
return p073_plugin_write_7dst(event, string);
} else if (tmpString.equals("7dsd")) {
return p073_plugin_write_7dsd(event, string);
} else if (tmpString.equals("7dtext")) {
return p073_plugin_write_7dtext(event, string);
String cmd = parseString(string, 1);
cmd.toLowerCase();
String text = parseStringToEndKeepCase(string, 2);
if (cmd.equals("7dn")) {
return p073_plugin_write_7dn(event, text);
} else if (cmd.equals("7dt")) {
return p073_plugin_write_7dt(event, text);
} else if (cmd.equals("7dst")) {
return p073_plugin_write_7dst(event);
} else if (cmd.equals("7dsd")) {
return p073_plugin_write_7dsd(event);
} else if (cmd.equals("7dtext")) {
return p073_plugin_write_7dtext(event, text);
} else {
bool p073_validcmd = false;
bool p073_displayon;
if (tmpString.equals("7don")) {
if (cmd.equals("7don")) {
addLog(LOG_LEVEL_INFO, F("7DGT : Display ON"));
p073_displayon = true;
p073_validcmd = true;
} else if (tmpString.equals("7doff")) {
} else if (cmd.equals("7doff")) {
addLog(LOG_LEVEL_INFO, F("7DGT : Display OFF"));
p073_displayon = false;
p073_validcmd = true;
} else if (tmpString.equals("7db")) {
} else if (cmd.equals("7db")) {
if ((event->Par1 >= 0) && (event->Par1 < 16)) {
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
String log = F("7DGT : Brightness=");
@@ -502,7 +496,7 @@ bool p073_plugin_write(struct EventStruct *event, const String& string) {
return false;
}
bool p073_plugin_write_7dn(struct EventStruct *event, const String& tmpStr) {
bool p073_plugin_write_7dn(struct EventStruct *event, const String& text) {
P073_data_struct *P073_data =
static_cast<P073_data_struct *>(getPluginTaskData(event->TaskIndex));
@@ -520,8 +514,6 @@ bool p073_plugin_write_7dn(struct EventStruct *event, const String& tmpStr) {
addLog(LOG_LEVEL_INFO, log);
}
int comma1 = tmpStr.indexOf(',');
switch (P073_data->displayModel) {
case P073_TM1637_4DGTCOLON: {
if ((event->Par1 > -1000) && (event->Par1 < 10000)) {
@@ -535,7 +527,7 @@ bool p073_plugin_write_7dn(struct EventStruct *event, const String& tmpStr) {
}
case P073_TM1637_4DGTDOTS: {
if ((event->Par1 > -1000) && (event->Par1 < 10000)) {
P073_data->FillBufferWithNumber(tmpStr.substring(comma1 + 1).c_str());
P073_data->FillBufferWithNumber(text.c_str());
}
else {
P073_data->FillBufferWithDash();
@@ -545,7 +537,7 @@ bool p073_plugin_write_7dn(struct EventStruct *event, const String& tmpStr) {
}
case P073_TM1637_6DGT: {
if ((event->Par1 > -100000) && (event->Par1 < 1000000)) {
P073_data->FillBufferWithNumber(tmpStr.substring(comma1 + 1).c_str());
P073_data->FillBufferWithNumber(text.c_str());
}
else {
P073_data->FillBufferWithDash();
@@ -555,9 +547,9 @@ bool p073_plugin_write_7dn(struct EventStruct *event, const String& tmpStr) {
break;
}
case P073_MAX7219_8DGT: {
if (comma1 > 0) {
if (text.length() > 0) {
if ((event->Par1 > -10000000) && (event->Par1 < 100000000)) {
P073_data->FillBufferWithNumber(tmpStr.substring(comma1 + 1).c_str());
P073_data->FillBufferWithNumber(text.c_str());
} else {
P073_data->FillBufferWithDash();
}
@@ -570,7 +562,7 @@ bool p073_plugin_write_7dn(struct EventStruct *event, const String& tmpStr) {
return true;
}
bool p073_plugin_write_7dt(struct EventStruct *event, const String& tmpStr) {
bool p073_plugin_write_7dt(struct EventStruct *event, const String& text) {
P073_data_struct *P073_data =
static_cast<P073_data_struct *>(getPluginTaskData(event->TaskIndex));
@@ -583,10 +575,8 @@ bool p073_plugin_write_7dt(struct EventStruct *event, const String& tmpStr) {
}
double p073_temptemp = 0;
bool p073_tempflagdot = false;
int comma1 = tmpStr.indexOf(',');
if (comma1 > 0) {
p073_temptemp = atof(tmpStr.substring(comma1 + 1).c_str());
if (text.length() > 0) {
validDoubleFromString(text, p073_temptemp);
}
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
@@ -647,7 +637,7 @@ bool p073_plugin_write_7dt(struct EventStruct *event, const String& tmpStr) {
return true;
}
bool p073_plugin_write_7dst(struct EventStruct *event, const String& string) {
bool p073_plugin_write_7dst(struct EventStruct *event) {
P073_data_struct *P073_data =
static_cast<P073_data_struct *>(getPluginTaskData(event->TaskIndex));
@@ -690,7 +680,7 @@ bool p073_plugin_write_7dst(struct EventStruct *event, const String& string) {
return true;
}
bool p073_plugin_write_7dsd(struct EventStruct *event, const String& string) {
bool p073_plugin_write_7dsd(struct EventStruct *event) {
P073_data_struct *P073_data =
static_cast<P073_data_struct *>(getPluginTaskData(event->TaskIndex));
@@ -731,7 +721,7 @@ bool p073_plugin_write_7dsd(struct EventStruct *event, const String& string) {
return true;
}
bool p073_plugin_write_7dtext(struct EventStruct *event, const String& string) {
bool p073_plugin_write_7dtext(struct EventStruct *event, const String& text) {
P073_data_struct *P073_data =
static_cast<P073_data_struct *>(getPluginTaskData(event->TaskIndex));
@@ -742,22 +732,12 @@ bool p073_plugin_write_7dtext(struct EventStruct *event, const String& string) {
if (P073_data->output != P073_DISP_MANUAL) {
return false;
}
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex) {
tmpString = tmpString.substring(argIndex + 1);
}
else {
tmpString = "";
}
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
String log = F("7DGT : Show Text=");
log += tmpString;
log += text;
addLog(LOG_LEVEL_INFO, log);
}
P073_data->FillBufferWithString(tmpString);
P073_data->FillBufferWithString(text);
switch (P073_data->displayModel) {
case P073_TM1637_4DGTCOLON:
+6 -22
View File
@@ -418,35 +418,19 @@ boolean Plugin_076(byte function, struct EventStruct *event, String &string) {
case PLUGIN_WRITE:
if (Plugin_076_hlw) {
// FIXME TD-er: This one is not using parseString* function
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex){
tmpString = tmpString.substring(0, argIndex);
}
if (tmpString.equalsIgnoreCase(F("hlwreset"))) {
String command = parseString(string, 1);
if (command.equalsIgnoreCase(F("hlwreset"))) {
Plugin076_ResetMultipliers();
success = true;
}
if (tmpString.equalsIgnoreCase(F("hlwcalibrate"))) {
String tmpStr = string;
if (command.equalsIgnoreCase(F("hlwcalibrate"))) {
unsigned int CalibVolt = 0;
double CalibCurr = 0;
unsigned int CalibAcPwr = 0;
int comma1 = tmpStr.indexOf(',');
int comma2 = tmpStr.indexOf(',', comma1 + 1);
int comma3 = tmpStr.indexOf(',', comma2 + 1);
if (comma1 != 0) {
if (comma2 == 0) {
CalibVolt = tmpStr.substring(comma1 + 1).toInt();
} else if (comma3 == 0) {
CalibVolt = tmpStr.substring(comma1 + 1, comma2).toInt();
CalibCurr = atof(tmpStr.substring(comma2 + 1).c_str());
} else {
CalibVolt = tmpStr.substring(comma1 + 1, comma2).toInt();
CalibCurr = atof(tmpStr.substring(comma2 + 1, comma3).c_str());
CalibAcPwr = tmpStr.substring(comma3 + 1).toInt();
if (validUIntFromString(parseString(string, 2), CalibVolt)) {
if (validDoubleFromString(parseString(string, 3), CalibCurr)) {
validUIntFromString(parseString(string, 4), CalibAcPwr);
}
}
if (PLUGIN_076_DEBUG) {
+10 -17
View File
@@ -185,25 +185,18 @@ boolean Plugin_088(byte function, struct EventStruct *event, String& string)
unsigned int temperature = 22;
unsigned int vDir = VDIR_UP;
unsigned int hDir = HDIR_AUTO;
char command[80];
command[0] = 0;
String TmpStr1 = "";
string.toCharArray(command, 80);
// FIXME TD-er: This one is not using parseString* function
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex) tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase(F("HEATPUMPIR")) && Plugin_088_irSender != NULL)
String cmd = parseString(string, 1);
if (cmd.equalsIgnoreCase(F("HEATPUMPIR")) && Plugin_088_irSender != NULL)
{
if (GetArgv(command, TmpStr1, 2)) heatpumpModel = TmpStr1;
if (GetArgv(command, TmpStr1, 3)) powerMode = str2int(TmpStr1.c_str());
if (GetArgv(command, TmpStr1, 4)) operatingMode = str2int(TmpStr1.c_str());
if (GetArgv(command, TmpStr1, 5)) fanSpeed = str2int(TmpStr1.c_str());
if (GetArgv(command, TmpStr1, 6)) temperature = str2int(TmpStr1.c_str());
if (GetArgv(command, TmpStr1, 7)) vDir = str2int(TmpStr1.c_str());
if (GetArgv(command, TmpStr1, 8)) hDir = str2int(TmpStr1.c_str());
String TmpStr1;
if (GetArgv(string.c_str(), TmpStr1, 2)) heatpumpModel = TmpStr1;
if (GetArgv(string.c_str(), TmpStr1, 3)) powerMode = str2int(TmpStr1.c_str());
if (GetArgv(string.c_str(), TmpStr1, 4)) operatingMode = str2int(TmpStr1.c_str());
if (GetArgv(string.c_str(), TmpStr1, 5)) fanSpeed = str2int(TmpStr1.c_str());
if (GetArgv(string.c_str(), TmpStr1, 6)) temperature = str2int(TmpStr1.c_str());
if (GetArgv(string.c_str(), TmpStr1, 7)) vDir = str2int(TmpStr1.c_str());
if (GetArgv(string.c_str(), TmpStr1, 8)) hDir = str2int(TmpStr1.c_str());
#ifdef IR_SEND_TIME
sendHour = hour();
sendMinute = minute();