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 input json #801

Merged
merged 16 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ emoncms.log
.idea
.vagrant/
.env
.vscode/*

# Ignore all modules except core ones
Modules/*
Expand Down
51 changes: 44 additions & 7 deletions scripts/phpmqtt_input.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,47 @@ function disconnect() {
function message($message)
{
try {
$jsoninput = false;
$topic = $message->topic;
$value = $message->payload;

$time = time();

global $mqtt_server, $user, $input, $process, $feed, $device, $log, $count;

global $mqtt_server, $user, $input, $process, $device, $log, $count;

//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);
if ((json_last_error() === JSON_ERROR_NONE) && is_array($jsondata)) {
// JSON is valid - is it an array
$jsoninput = true;
$log->info("MQTT Valid JSON found ");
//Create temporary array and change all keys to lower case to look for a 'time' key
$jsondataLC = array_change_key_case($jsondata);

#If JSON check to see if there is a time value else set to time now.
if (array_key_exists('time',$jsondataLC)){
$time = $jsondataLC['time'];
if (is_string($time)){
if (($timestamp = strtotime($time)) === false) {
//If time string is not valid, use system time.
$time = time();
$log->warn("Time string not valid ".$time);
} else {
$log->info("Valid time string used ".$time);
$time = $timestamp;
}
} else {
$log->info("Valid time in seconds used ".$time);
//Do nothings as it has been assigned to $time as a value
}
} else {
$log->info("No time element found in JSON - System time used");
$time = time();
}
} else {
$jsoninput = false;
$log->info("No JSON found - System time used");
$time = time();
}

$log->info($topic." ".$value);
$count ++;

Expand Down Expand Up @@ -218,9 +252,12 @@ function message($message)
{
$nodeid = $route[$st+1];
$dbinputs = $input->get_inputs($userid);

if (isset($route[$st+2]))
{

if ($jsoninput) {
foreach ($jsondata as $key=>$value) {
$inputs[] = array("userid"=>$userid, "time"=>$time, "nodeid"=>$nodeid, "name"=>$key, "value"=>$value);
}
} else if (isset($route[$st+2])) {
$inputs[] = array("userid"=>$userid, "time"=>$time, "nodeid"=>$nodeid, "name"=>$route[$st+2], "value"=>$value);
}
else
Expand Down