-
Notifications
You must be signed in to change notification settings - Fork 515
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,7 +252,7 @@ function message($message) | |
$topic = str_replace(":","",$topic); | ||
|
||
//Check and see if the input is a valid JSON and when decoded is an array. A single number is valid JSON. | ||
$jsondata = json_decode($value,true,2); | ||
$jsondata = json_decode($value,true,3); | ||
if ((json_last_error() === JSON_ERROR_NONE) && is_array($jsondata)) { | ||
// JSON is valid - is it an array | ||
$jsoninput = true; | ||
|
@@ -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)); | ||
$time = time(); | ||
} | ||
} else { | ||
|
@@ -291,7 +291,7 @@ function message($message) | |
$time = time(); | ||
} | ||
|
||
$log->info($topic." ".$value); | ||
$log->info($topic." ".($jsoninput ? json_encode($jsondata) : $value)); | ||
$count ++; | ||
|
||
$inputs = array(); | ||
|
@@ -334,7 +334,19 @@ function message($message) | |
$input_name = implode("_",$input_name_parts)."_"; | ||
} | ||
foreach ($jsondata as $key=>$value) { | ||
$inputs[] = array("userid"=>$userid, "time"=>$time, "nodeid"=>$nodeid, "name"=>$input_name.$key, "value"=>$value); | ||
// Unbox { name: xxx, value: xxx } | ||
if (is_array($value) && array_key_exists("value", $value)) { | ||
if (array_key_exists("name", $value) && $value["name"]) | ||
$key .= '_'.$value["name"]; | ||
$value = $value["value"]; | ||
} | ||
|
||
if (is_scalar($value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...? |
||
$inputs[] = array("userid"=>$userid, "time"=>$time, "nodeid"=>$nodeid, "name"=>$input_name.$key, "value"=>$value); | ||
} else { | ||
$log->warn("Unable to unpack JSON, not recording ".$key." : ".json_encode($value)); | ||
continue; | ||
} | ||
} | ||
} else if ($route_len>=$min_route_len) { | ||
// Input name is all the remaining parts connected together | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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. :)