Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt: support nested JSON values #1779

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

ramcq
Copy link

@ramcq ramcq commented Feb 4, 2022

JSON values from ebusd are published on MQTT with values "boxed" in a
format like: { name: xxx, value: xxx }

Support this structure by allowing deeper-nested JSON structures, and
unpacking the value and adding the name if present.

Ensure non-scalar values (nested JSON, etc) are not recorded, or logged
without being converted back to JSON.

JSON values from ebusd are published on MQTT with values "boxed" in a
format like: { name: xxx, value: xxx }

Support this structure by allowing deeper-nested JSON structures, and
unpacking the value and adding the name if present.

Ensure non-scalar values (nested JSON, etc) are not recorded, or logged
without being converted back to JSON.
@@ -279,7 +279,7 @@ function message($message)
$time = $timestamp;
}
} else {
$log->warn("Time value not valid ".$inputtime);
$log->warn("Time value not valid ".json_encode($inputtime));
Copy link
Contributor

Choose a reason for hiding this comment

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

What difference does the .json_encode make?

Copy link
Author

@ramcq ramcq Feb 4, 2022

Choose a reason for hiding this comment

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

Because $inputtime is derived from json_decoded values it can be a non-scalar type, an array or whatever, and attempting to print it was killing the whole script. Specifically because the source I'm using sometimes emits JSON that happens to contain "time": { "value": xxx } although this is the controllers current time of day, not the event time - discarding it is fine, exploding the whole script less great. :)

$value = $value["value"];
}

if (is_scalar($value)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I quite like this check, but might I suggest it should check for a number instead?

Copy link
Author

Choose a reason for hiding this comment

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

That would be a behavioural change compared to currently, where strings (or whatever) are sent down the line and then presumably dropped by emoncms. :) This check was mostly just to "balance" the consequences of increasing the JSON depth because it could let in other JSON structures that were not { name: xxx, value: xxx } structured.

I don't want to start discarding strings here because some of the values reported by ebusd are truthy values (on / off / true / false / yes / no) which can be safely mapped to 1 and 0, which I'd like to add in another patch. However, here is not the right place to do it, as not all such values follow this JSON path, because it's predicated on JSON and an array - a bare "true" or "yes" value follows the other path. So I think a follow-on patch would need to add that truthy-string test somewhere lower down before they are recorded.

(Separately / additionally, there are also textual enums reported by ebusd - but can't be handled generically here so I don't quite know where I should be thinking about that.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes true and yes sort of dropped later, but often create bad inputs as a result so ensuring only numbers are sent on would be a really good thing (but could be proposed as a separate PR).

Copy link
Author

Choose a reason for hiding this comment

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

Would be nice to know if there's a possible approach for keeping the strings in the pipeline, if we could add a feed function to do matching or mapping somehow...?

@borpin
Copy link
Contributor

borpin commented Feb 4, 2022

I am trying to think where the acceptable JSON structure is documented. It may not be, as it is currently the same for the HTTP interface as well as the MQTT interface.

To be consistent, you may need to update the HTTP interface for full_json as well.

I did some work on this a while back as originally, it did not accept valid JSON.

@borpin
Copy link
Contributor

borpin commented Feb 4, 2022

Can we presume you have tested this with 2 level JSON so it doesn't break anything :)

@borpin
Copy link
Contributor

borpin commented Feb 4, 2022

Yup, input API here

if ($param->exists('fulljson')) {

@ramcq
Copy link
Author

ramcq commented Feb 4, 2022

Can we presume you have tested this with 2 level JSON so it doesn't break anything :)

ahem taptaptaptap Yes that appears to work :)

@borpin
Copy link
Contributor

borpin commented Feb 4, 2022

ahem taptaptaptap Yes that appears to work :)

My life resolves around "Assume nothing"...

@borpin
Copy link
Contributor

borpin commented Feb 4, 2022

I am trying to think where the acceptable JSON structure is documented

Opened an issue #1780

@ramcq
Copy link
Author

ramcq commented Feb 4, 2022

Yup, input API here

if ($param->exists('fulljson')) {

Happy to update this but (and perhaps giving us one place to decode "truthy" string values and rejecting others) these two close-but-no-cigar functions are ideally looking for a couple of utility functions for fetching time and numeric values from submitted strings. Is there somewhere sensible to factor those out to?

@borpin
Copy link
Contributor

borpin commented Feb 6, 2022

Is there somewhere sensible to factor those out to?

@TrystanLea will need to comment on that.

@ramcq
Copy link
Author

ramcq commented Oct 28, 2022

Ping @TrystanLea - looking at this again (trying to remember and make it actually work...!) - is there somewhere I can move the JSON-to-time-and-number functions to so they are not duplicated between the MQTT code and the HTTP input API?

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.

None yet

2 participants