Skip to content

[P043] [JSON] Add feature to output times in JSON#5524

Open
chromoxdor wants to merge 4 commits intoletscontrolit:megafrom
chromoxdor:feature-P043_CLK_Times_JSON
Open

[P043] [JSON] Add feature to output times in JSON#5524
chromoxdor wants to merge 4 commits intoletscontrolit:megafrom
chromoxdor:feature-P043_CLK_Times_JSON

Conversation

@chromoxdor
Copy link
Copy Markdown
Contributor

No description provided.

Comment thread src/src/WebServer/JSON.cpp Outdated
Comment thread src/src/WebServer/JSON.cpp Outdated
@TD-er
Copy link
Copy Markdown
Member

TD-er commented Apr 7, 2026

Apart from my comments, how do you like the new JSON/Webform writer classes? :)

@chromoxdor
Copy link
Copy Markdown
Contributor Author

Apart from my comments, how do you like the new JSON/Webform writer classes? :)

The fact that I was able to write working code with it in minutes speaks for itself :)

@TD-er
Copy link
Copy Markdown
Member

TD-er commented Apr 7, 2026

Apart from my comments, how do you like the new JSON/Webform writer classes? :)

The fact that I was able to write working code with it in minutes speaks for itself :)

The nice thing is, the same code can also be used to add stuff to a webpage, just hand over a different writer type.

Comment thread src/src/CustomBuild/define_plugin_sets.h
@chromoxdor
Copy link
Copy Markdown
Contributor Author

Maybe better to add some PLUGIN_TASK_JSON or something more descriptive :) and let it be handled in the plugin code.

That was not so intuitive for me anymore. 😳
I needed some help to figure out how to make the call.

What do you think about the defines? We have a plugin-specific one (FEATURE_P043_CLK_TIMES_JSON) and one for handling extra JSON in general (FEATURE_ADDITIONAL_JSON_FROM_PLUGIN)

Comment thread src/Custom-sample.h Outdated
Comment thread src/src/CustomBuild/define_plugin_sets.h Outdated
Comment thread src/src/CustomBuild/define_plugin_sets.h Outdated
@chromoxdor
Copy link
Copy Markdown
Contributor Author

Never thought that trying to wrap my head around these definitions is such a challenge for me.
@tonhuisman can you take a look?

In the #ifdef PLUGIN_BUILD_MAX_ESP32 section I added:

     #ifndef FEATURE_P043_CLK_TIMES_JSON && defined(USES_P043)
      #define FEATURE_P043_CLK_TIMES_JSON 1
      #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 1
     #endif

and at the very end I added:

#if !defined(USES_P043)
  #define FEATURE_P043_CLK_TIMES_JSON 0
  #ifndef FEATURE_ADDITIONAL_JSON_FROM_PLUGIN
    #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 0
  #endif
#elif FEATURE_P043_CLK_TIMES_JSON
  #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 1
#endif

@tonhuisman
Copy link
Copy Markdown
Contributor

tonhuisman commented Apr 9, 2026

#if !defined(USES_P043)
  #define FEATURE_P043_CLK_TIMES_JSON 0
  #ifndef FEATURE_ADDITIONAL_JSON_FROM_PLUGIN
    #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 0
  #endif
#elif FEATURE_P043_CLK_TIMES_JSON
  #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 1
#endif

Well, you're effectively doing the same check twice, that last part I'd simplify to:

#ifndef FEATURE_ADDITIONAL_JSON_FROM_PLUGIN
  #define FEATURE_ADDITIONAL_JSON_FROM_PLUGIN 0
#endif

and also add this similar check:

#ifndef FEATURE_P043_CLK_TIMES_JSON
  #define FEATURE_P043_CLK_TIMES_JSON 0
#endif

Oh, and this won't work:

#ifndef FEATURE_P043_CLK_TIMES_JSON && defined(USES_P043)

Should be like:
#if !defined(FEATURE_P043_CLK_TIMES_JSON) && defined(USES_P043)

@TD-er
Copy link
Copy Markdown
Member

TD-er commented Apr 9, 2026

yep, shorthand notation like #ifdef or #ifndef cannot be combined with logic operators.

@chromoxdor
Copy link
Copy Markdown
Contributor Author

chromoxdor commented Apr 10, 2026

Last try. I don't know why this is so hard for me.
Maybe one you can give me the right example if this is still not right.

#ifdef PLUGIN_BUILD_MAX_ESP32 section:

#ifdef USES_P043
      #define FEATURE_P043_CLK_TIMES_JSON 1
#endif

and at the very end:

#ifndef FEATURE_P043_CLK_TIMES_JSON
  #define FEATURE_P043_CLK_TIMES_JSON 0
#endif

#if FEATURE_P043_CLK_TIMES_JSON && !defined(USES_P043)
  #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

@tonhuisman
Copy link
Copy Markdown
Contributor

#if FEATURE_P043_CLK_TIMES_JSON && !defined(USES_P043)
  #define FEATURE_P043_CLK_TIMES_JSON 0
#endif

Well, there's 1 small issue here 🤣
As FEATURE_P043_CLK_TIMES_JSON is already defined as value 1, the preprocessor will complain about you setting it to 0 here. It should be #undef'ed first:

#if FEATURE_P043_CLK_TIMES_JSON && !defined(USES_P043)
  #undef FEATURE_P043_CLK_TIMES_JSON
  #define FEATURE_P043_CLK_TIMES_JSON 0
#endif

The rest looks OK.

@chromoxdor
Copy link
Copy Markdown
Contributor Author

chromoxdor commented Apr 10, 2026

The rest looks OK.

Puhhh… I am relieved.
I made an observation while testing.
When changing longitude and latitude for proper sunrise and sunset times, it seems the recalculation only takes place after a reboot. It probably should happen when hitting save, right?

@TD-er
Copy link
Copy Markdown
Member

TD-er commented Apr 10, 2026

It is being done when the time is set and on a new date.
But I don't see why we shouldn't recalculate this when saving the settings.

# if FEATURE_ADDITIONAL_JSON_FROM_PLUGIN

EventStruct TempEvent(TaskIndex);
TempEvent.Par1 = reinterpret_cast<intptr_t>(taskWriter.get());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't do nasty things like this. There is a proper pointer in the EventStruct for this.

KeyValueWriter* kvWriter = nullptr;

Comment thread src/_P043_ClkOutput.ino
case PLUGIN_TASK_JSON:
{
addLog(LOG_LEVEL_INFO, F("P043 JSON CALLED"));
auto taskWriter = reinterpret_cast<KeyValueWriter *>(event->Par1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, please use the already present KeyValueWriter* kvWriter in the EventStruct

Comment thread src/_P043_ClkOutput.ino
auto timesWriter = taskWriter->createChildArray(F("Times"));
if (!timesWriter) break;

LoadTaskSettings(event->TaskIndex);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure this is needed as the Cache also does quite a lot of checking.
This is potentially quite a resource intensive operation and should only be done when absolutely needed.

@chromoxdor
Copy link
Copy Markdown
Contributor Author

@TD-er : If you soon plan to make a new release and find this PR worth merging could you take over for the necessary changes? I am at the hospital right know waiting for a new life to be born and most definitely will not be available for some time.

@TD-er
Copy link
Copy Markdown
Member

TD-er commented Apr 14, 2026

@TD-er : If you soon plan to make a new release and find this PR worth merging could you take over for the necessary changes? I am at the hospital right know waiting for a new life to be born and most definitely will not be available for some time.

Sure, will do.
And I totally understand the rollercoaster you will experience right now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants