Version 2 of sending the correct data to influx. Also catching the exception that would stop updates working.

This commit is contained in:
David Bell 2016-04-12 10:01:20 +01:00
parent 77002d972d
commit a988e83df7
3 changed files with 32 additions and 6 deletions

View File

@ -1252,10 +1252,28 @@ function function_check($function) {
return function_exists($function);
}
function force_influx_data($type,$data) {
if ($type == 'f' || $type == 'float') {
return(sprintf("%.1f",$data));
function force_influx_data($data) {
/*
* It is not trivial to detect if something is a float or an integer, and
* therefore may cause breakages on inserts.
* Just setting every number to a float gets around this, but may introduce
* inefficiencies.
* I've left the detection statement in there for a possible change in future,
* but currently everything just gets set to a float.
*/
if (is_numeric($data)) {
// If it is an Integer
if (ctype_digit($data)) {
return floatval($data);
// Else it is a float
} else {
return floatval($data);
}
} else {
return $data;
}
}// end force_influx_data
/**

View File

@ -58,7 +58,10 @@ function influx_update($device,$measurement,$tags=array(),$fields) {
$tmp_tags[$k] = $v;
}
foreach ($fields as $k => $v) {
$tmp_fields[$k] = force_influx_data('f',$v);
$tmp_fields[$k] = force_influx_data($v);
if( $tmp_fields[$k] === null) {
unset($tmp_fields[$k]);
}
}
d_echo("\nInfluxDB data:\n");
@ -76,7 +79,12 @@ function influx_update($device,$measurement,$tags=array(),$fields) {
$tmp_fields // optional additional fields
)
);
$result = $influxdb->writePoints($points);
try {
$result = $influxdb->writePoints($points);
} catch (Exception $e) {
d_echo("Caught exception: ", $e->getMessage(), "\n");
d_echo($e->getTrace());
}
}
else {
print $console_color->convert('[%gInfluxDB Disabled%n] ', false);

View File

@ -54,7 +54,7 @@ if ($device['os'] != 'Snom') {
$value = $data_array[0][$oid];
}
else {
$value = 'U';
$value = null;
}
$fields[$oid] = $value;
}