diff --git a/src/Custom-sample.h b/src/Custom-sample.h index 11bf1af843..06147dee7f 100644 --- a/src/Custom-sample.h +++ b/src/Custom-sample.h @@ -30,6 +30,7 @@ #define FEATURE_JSON_EVENT 0 // Generates an event with the values of a JSON repsonse of an HTTP call. Keys are stored in json.keys one key per line (e.g.: Body.Data.DAY_ENERGY.Values.1) // #define FEATURE_SD 1 // Enable SD card support // #define FEATURE_DOWNLOAD 1 // Enable downloading a file from an url +//#define FEATURE_P043_CLK_TIMES_JSON 1 // When defined, Plugin 043 will add the time settings to the JSON output #ifdef BUILD_GIT # undef BUILD_GIT diff --git a/src/_P043_ClkOutput.ino b/src/_P043_ClkOutput.ino index c9c60b330f..6070fbb961 100644 --- a/src/_P043_ClkOutput.ino +++ b/src/_P043_ClkOutput.ino @@ -366,6 +366,41 @@ boolean Plugin_043(uint8_t function, struct EventStruct *event, String& string) # undef P043_GETVALUE_LENGTH break; } + + #if FEATURE_P043_CLK_TIMES_JSON + case PLUGIN_TASK_JSON: + { + addLog(LOG_LEVEL_INFO, F("P043 JSON CALLED")); + + if (!event->kvWriter) break; + event->kvWriter->write({ F("OnOff"), PCONFIG(6) }); + + auto timesWriter = event->kvWriter->createChildArray(F("Times")); + if (!timesWriter) break; + + if (ExtraTaskSettings.TaskIndex != event->TaskIndex) { + LoadTaskSettings(event->TaskIndex); + } + + const int offset = + (validGpio(CONFIG_PIN1) || (PCONFIG(7) == 1)) ? 1 : 0; + + for (int x = 0; x < PCONFIG(7); x++) { + + auto timeWriter = timesWriter->createChild(); + if (!timeWriter) continue; + + long value = Cache.getTaskDevicePluginConfigLong(event->TaskIndex, x); + int val = ExtraTaskSettings.TaskDevicePluginConfig[x] - offset; + + timeWriter->write({ F("Time"), timeLong2String(value) }); + timeWriter->write({ F("Value"), val }); + } + + success = true; + break; + } + #endif // FEATURE_P043_CLK_TIMES_JSON } return success; } diff --git a/src/src/CustomBuild/define_plugin_sets.h b/src/src/CustomBuild/define_plugin_sets.h index 2328cd9f1c..aaac81e562 100644 --- a/src/src/CustomBuild/define_plugin_sets.h +++ b/src/src/CustomBuild/define_plugin_sets.h @@ -1300,6 +1300,9 @@ To create/register a plugin, you have to : // FIXME TD-er: Should this be enabled on non-Custom builds??? #define FEATURE_CUSTOM_PROVISIONING 1 + #ifdef USES_P043 + #define FEATURE_P043_CLK_TIMES_JSON 1 + #endif // See also PLUGIN_SET_MAX section at end, to include any disabled plugins from other definitions // See also PLUGIN_SET_COLLECTION_ESP32 section at end, @@ -4559,4 +4562,20 @@ To create/register a plugin, you have to : #endif #endif // if !FEATURE_SPI && !FEATURE_I2C && !FEATURE_MODBUS && !FEATURE_CAN && !FEATURE_WRMBUS && !FEATURE_WIMBUS +//-------------------Additional JSON per Plugin Section---------------- +#ifndef FEATURE_P043_CLK_TIMES_JSON + #define FEATURE_P043_CLK_TIMES_JSON 0 +#endif +#if FEATURE_P043_CLK_TIMES_JSON && !defined(USES_P043) + #undef FEATURE_P043_CLK_TIMES_JSON + #define FEATURE_P043_CLK_TIMES_JSON 0 +#endif +#if FEATURE_P043_CLK_TIMES_JSON + #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 1 +#endif +#ifndef FEATURE_ADDITIONAL_JSON_FROM_PLUGIN + #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 0 +#endif +//---------------End of Additional JSON per Plugin Section------------- + #endif // CUSTOMBUILD_DEFINE_PLUGIN_SETS_H diff --git a/src/src/DataTypes/ESPEasy_plugin_functions.h b/src/src/DataTypes/ESPEasy_plugin_functions.h index 7090c90ecb..fbd1419eef 100644 --- a/src/src/DataTypes/ESPEasy_plugin_functions.h +++ b/src/src/DataTypes/ESPEasy_plugin_functions.h @@ -38,6 +38,9 @@ enum PluginFunctions_e { PLUGIN_GET_DEVICEGPIONAMES, // Allow for specific formatting of the label for standard pin configuration (e.g. "GPIO <- TX") PLUGIN_EXIT, // Called when a task no longer is enabled (or deleted) PLUGIN_GET_CONFIG_VALUE, // Similar to PLUGIN_WRITE, but meant to fetch some information. Must return success = true when it + #if FEATURE_ADDITIONAL_JSON_FROM_PLUGIN + PLUGIN_TASK_JSON, // Called when generating the JSON for a task, allowing the plugin to add some custom values to the JSON output for that task. + #endif //FEATURE_ADDITIONAL_JSON_FROM_PLUGIN // can handle the command. Can also be used to access extra unused task values. // PLUGIN_UNCONDITIONAL_POLL , // Used to be called 10x per sec, but no longer used as GPIO related plugins now use a different // technique. diff --git a/src/src/Globals/Plugins.cpp b/src/src/Globals/Plugins.cpp index dc6e7e542d..c22f09ad25 100644 --- a/src/src/Globals/Plugins.cpp +++ b/src/src/Globals/Plugins.cpp @@ -992,6 +992,9 @@ bool PluginCall(uint8_t Function, struct EventStruct *event, String& str) // Call to specific task not interacting with hardware case PLUGIN_GET_CONFIG_VALUE: +#if FEATURE_ADDITIONAL_JSON_FROM_PLUGIN + case PLUGIN_TASK_JSON: +#endif //FEATURE_ADDITIONAL_JSON_FROM_PLUGIN case PLUGIN_GET_DEVICEVALUENAMES: case PLUGIN_GET_DEVICEGPIONAMES: case PLUGIN_WEBFORM_SAVE: diff --git a/src/src/WebServer/JSON.cpp b/src/src/WebServer/JSON.cpp index 90eb4ed797..7771f0a743 100644 --- a/src/src/WebServer/JSON.cpp +++ b/src/src/WebServer/JSON.cpp @@ -575,6 +575,15 @@ void handle_json() } } +# if FEATURE_ADDITIONAL_JSON_FROM_PLUGIN + + EventStruct TempEvent(TaskIndex); + TempEvent.kvWriter = taskWriter.get(); + + String dummy; + PluginCall(PLUGIN_TASK_JSON, &TempEvent, dummy); +# endif // FEATURE_ADDITIONAL_JSON_FROM_PLUGIN + #if FEATURE_PLUGIN_STATS && FEATURE_CHART_JS if (showPluginStats && Device[DeviceIndex].PluginStats) {