134 lines
3.9 KiB
PHP
134 lines
3.9 KiB
PHP
<?php
|
|
/** \file include\db_cache.php
|
|
* \brief DI webinterface cache functionality
|
|
* \author Rob Schalken, Core|Vision
|
|
* \version $Revision: 26247 $
|
|
* \date $Date: 2016-02-29 10:40:22 +0100 (Mon, 29 Feb 2016) $
|
|
*
|
|
* This file contains the device cache functions. This file is always included.
|
|
*/
|
|
|
|
/**
|
|
* Store data in the database cache
|
|
*
|
|
* inputs
|
|
* - dbid Equipment database id
|
|
* - item Key for the item ('gps', 'temp' are the only ones supported)
|
|
* - data Data for the itme
|
|
*
|
|
* Returns: (nothing)
|
|
*/
|
|
function db_store_cache($dbid, $item, $value, $t = 0) {
|
|
unset($dbvalue);
|
|
if( !is_numeric($dbid) ) {
|
|
trigger_error("dbid must be numerical", E_USER_NOTICE);
|
|
return FALSE;
|
|
}
|
|
if( !$item ) {
|
|
trigger_error("item is required", E_USER_NOTICE);
|
|
return FALSE;
|
|
}
|
|
|
|
switch( $item ) {
|
|
case "gps":
|
|
if( is_string($value) ) $value = explode(",", $value);
|
|
$lat = $value[0];
|
|
$lon = $value[1];
|
|
$dbvalue = sprintf("%.7F,%.7F", $lat, $lon);
|
|
break;
|
|
case "status":
|
|
$dbvalue = build_log_rt($value);
|
|
break;
|
|
case "msgnr":
|
|
// Vitelec SenseConnect message number
|
|
$dbvalue = $value;
|
|
break;
|
|
default:
|
|
if( preg_match("/temp\[([0-9]+)\]/", $item, $index) ) {
|
|
// re-format key
|
|
$item = sprintf("temp[%u]", $index[1]);
|
|
$dbvalue = sprintf("%.1F", $value);
|
|
}
|
|
break;
|
|
}
|
|
|
|
if( isset($dbvalue) ) {
|
|
if( !$t ) $t = "now";
|
|
db_store_data(
|
|
"REPLACE INTO zkl_cache (`zkl`,`key`,`value`,`t`) " .
|
|
"VALUES (" .
|
|
$dbid . "," .
|
|
"'" . $item . "'," .
|
|
"'" . $dbvalue . "'," .
|
|
strtotime($t) . ")"
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fetch last detection ok from cache
|
|
*
|
|
* inputs:
|
|
* - dbid Equipment database id
|
|
* - item Which item from cache ('det ok','gps');
|
|
*
|
|
* Return: log_realtime/zkl_cache entry
|
|
*/
|
|
function db_fetch_cache($dbid, $item) {
|
|
// Initial return value
|
|
$result = "";
|
|
if( !is_numeric($dbid) ) {
|
|
trigger_error("dbid must be numerical", E_USER_NOTICE);
|
|
return FALSE;
|
|
}
|
|
if( !$item ) {
|
|
trigger_error("item is required", E_USER_NOTICE);
|
|
return FALSE;
|
|
}
|
|
|
|
// Check cache
|
|
$cache_info = db_fetch_data("SELECT * FROM zkl_cache WHERE zkl=" . $dbid . " AND `key`='" . $item . "'");
|
|
|
|
// Parse data, where needed
|
|
if (is_array($cache_info)) {
|
|
switch($item) {
|
|
case "det ok":
|
|
$temp = explode(",", $cache_info[0]['value']);
|
|
$result['b_a'] = $temp[0];
|
|
$result['b_a_autocal'] = $temp[1];
|
|
$result['t'] = $cache_info[0]['t'];
|
|
break;
|
|
case "gps":
|
|
$temp = explode(",", $cache_info[0]['value']);
|
|
$result['latitude'] = $temp[0];
|
|
$result['longitude'] = $temp[1];
|
|
$result['t_gps'] = $cache_info[0]['t'];
|
|
break;
|
|
case "msgnr":
|
|
// Vitelec SenseConnect message number
|
|
$result = $cache_info[0]['value'];
|
|
break;
|
|
case "status":
|
|
$result = zkl_interpret_status($cache_info[0]['value']);
|
|
$result['t'] = $cache_info[0]['t'];
|
|
break;
|
|
case "temp[0]":
|
|
// sub-items like "log_realtime"
|
|
$result['temp_onboard'] = $cache_info[0]['value'];
|
|
$result['t_temp_onboard'] = $cache_info[0]['t'];
|
|
break;
|
|
case "temp[1]":
|
|
// sub-items like "log_realtime"
|
|
$result['temp_ntc'] = $cache_info[0]['value'];
|
|
$result['t_temp_ntc'] = $cache_info[0]['t'];
|
|
break;
|
|
default:
|
|
// only return the first row (but there won't be more than one row, so that's okay)
|
|
$result = $cache_info[0];
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
?>
|