src.dualinventive.com/mtinfo/dist/webroot/support/mobile-status.php

466 lines
17 KiB
PHP

<?php
/*
** mobile-status.php
*/
define('CONFIG_DIR', '/etc/di');
define('DBCONFIG_DIR', CONFIG_DIR . '/database');
$_HOSTNAME = array('di-tcp.core-vision.com');
require_once("../include/i18n.php");
require_once("../include/cp3000-tcpclient.php");
if( isset($_GET['zkl']) )
$zkl_array = $_GET['zkl'];
else if( isset($_POST['zkl']) )
$zkl_array = $_POST['zkl'];
else
unset($zkl_array);
if( isset($_GET['debug']) )
$debug = $_GET['debug'];
else if( isset($_POST['debug']) )
$debug = $_POST['debug'];
else
$debug = FALSE;
if( isset($_GET['versions']) )
$versions = $_GET['versions'];
else if( isset($_POST['versions']) )
$versions = $_POST['versions'];
else
$versions = FALSE;
if( isset($_GET['timeout']) )
$tcp_timeout = $_GET['timeout'];
else if( isset($_POST['timeout']) )
$tcp_timeout = $_POST['timeout'];
else
$tcp_timeout = 3.0;
//
// Check inputs for SQL statement injection and such.
// Items not tested below are not vulnerable for SQL statement injection, e.g.
// a time is always converted using 'strtotime()' and other items are only
// used in the PHP but never in the composition of an SQL statement
// [security audit 2013-10-23]
//
if(
(isset($zkl_array) && !is_numeric($zkl_array)) ||
(isset($_GET['db']) && dirname($_GET['db']) != ".")
) {
// don't do anything fancy or interruptive as fiddling with the GET parameters
// is intended by the author
die("Parameter error");
}
// read the database information
if( isset($_GET['db']) ) $db_info = read_database(DBCONFIG_DIR, $_GET['db']);
else $db_info = array();
if( !isset($db_info['host']) ) $db_info['host'] = "localhost";
if( !isset($db_info['user']) ) $db_info['user'] = "root";
if( !isset($db_info['passwd']) ) $db_info['passwd'] = "";
if( !isset($db_info['db']) ) $db_info['db'] = "di_zkl";
// open the database
$db_data_handle = mysql_connect($db_info['host'], $db_info['user'], $db_info['passwd']);
if( $db_data_handle === FALSE ) {
echo mysql_error();
exit(1);
}
mysql_select_db($db_info['database'], $db_data_handle);
if( $db_info['main'] ) {
$db_main_info = read_database(DBCONFIG_DIR, $db_info['main']);
$db_main_handle = mysql_connect($db_main_info['host'], $db_main_info['user'], $db_main_info['passwd'], true);
if( $db_main_handle === FALSE ) {
echo mysql_error();
exit(1);
}
mysql_select_db($db_main_info['database'], $db_main_handle);
}
else {
$db_main_handle = $db_data_handle;
$db_main_info = $db_info;
}
function read_database($dir, $file)
{
$path = $dir . "/" . $file;
if( is_dir($path) ) return false;
if( ($fp = fopen($path, "r")) === false ) return false;
// read the file
$database = array();
$database['file'] = $file;
while( ($line = fgets($fp, 256)) !== false ) {
// delete comments
$line = strtok($line, "#");
if( ($n = strpos($line, "=")) !== false ) {
$key = trim(substr($line, 0, $n));
$value = trim(substr($line, $n + 1));
if( $key == "type" && ($n = strpos($value, ",")) !== false ) {
switch( ($database[$key] = substr($value, 0, $n)) ) {
case 'archive':
$database['main'] = substr($value, $n + 1);
break;
}
}
else $database[$key] = $value;
}
}
// done
fclose($fp);
if( !$database['name'] ) {
$database['name'] = "";
if( $database['host'] ) {
$database['name'] .= $database['host'];
if( $database['port'] ) $database['name'] .= ":" . $database['port'];
$database['name'] .= ":";
}
$database['name'] .= $database['database'];
}
return $database;
}
function mysql_run($query, $db_handle) {
$result = mysql_query($query, $db_handle);
if( !$result ) {
error_log($_PHP_SELF . "db-error: " . mysql_error($db_handle) . ", query " . $query);
}
return $result;
}
if( !isset($zkl_array) ) {
echo "<form id=\"zkl_select\" method=\"get\">\n";
echo "<input type=\"hidden\" id=\"db\" name=\"db\" value=\"" . $db_main_info['file'] . "\">\n";
// page - list ZKLs
$query = "SELECT id,idcode,serienr FROM zkl ORDER BY idcode";
$result = mysql_run($query, $db_main_handle);
echo "<p align=\"center\">";
echo "<select id=\"zkl\" name=\"zkl[]\" size=\"10\" multiple>\n";
while( $row = mysql_fetch_assoc($result) ) {
echo "<option value=\"" . $row['id'] . "\">";
echo $row['idcode'];
echo "</option>\n";
}
echo "</select>\n";
echo "<br>\n";
echo "<input type=\"checkbox\" name=\"debug\"> Extra debug informatie.<br>\n";
echo "<input type=\"checkbox\" name=\"versions\"> Versie-informatie.<br>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\">";
echo "</p>\n";
echo "</form>\n";
}
else {
header("refresh: 5; url=?db=" . urlencode($db_main_info['file']) . "&zkl[]=" . implode("&zkl[]=", $zkl_array) . ($debug ? "&debug=1" : "") . "&timeout=" . sprintf("%.1f", $tcp_timeout));
$tcp = array();
foreach( $zkl_array as $zkl ) {
// info about the device
$result = mysql_run("SELECT serienr,idcode FROM zkl WHERE id=" . $zkl, $db_main_handle);
$zkl_info = mysql_fetch_assoc($result);
$result = mysql_run("SELECT capabilities,nr_batterijen FROM device WHERE id=(SELECT device FROM zkl WHERE id=" . $zkl . ")", $db_main_handle);
$row = mysql_fetch_assoc($result);
$zkl_info['capabilities'] = explode(",", $row['capabilities']);
$zkl_info['nr_batterijen'] = $row['nr_batterijen'];
$result = mysql_run("SELECT COUNT(*) FROM zkl_geofence WHERE zkl=" . $zkl, $db_main_handle);
$row = mysql_fetch_array($result);
$zkl_info['geofence'] = ($row[0] != 0);
echo "<p>";
echo "<b>" . $zkl_info['idcode'] . "</b>:<br>";
// login in with the TCP server
$result = mysql_run("SELECT adres FROM server WHERE id = (SELECT tcp_server FROM zkl WHERE id=" . $zkl . ")", $db_main_handle);
$row = mysql_fetch_assoc($result);
$tcp_server = $row['adres'];
// not using shared memory, because we (may) need to retrieve the Switch status
$tcp_channel = zkl_tcplogin($zkl, $tcp_server, NULL, NULL, ZKL_NORMAL);
if( $tcp_channel === FALSE ) {
echo "Error: " . $zkl_error . "<br>\n";
}
else {
// get (cached) status
$status = zkl_get_cached_status($tcp_channel, $tcp_timeout);
// detection and measurements
if( in_array("meting", $zkl_info['capabilities']) ) {
echo "Detectie: ";
if( $status['detection']['active'] ) {
if( $status['detection']['ok'] )
echo "<font color=\"green\">ok</font>";
else
echo "<font color=\"red\">alarm</font>";
echo ", b/a=" . sprintf("%.3f", $status['detection']['b/a']);
if( $debug ) echo " (grens: " . sprintf("%.3f", $status['detection']['b/a limit']) . ")";
echo ", %Vrms=" . sprintf("%.1f", sqrt($status['detection']['rms']));
}
else {
echo "uitgeschakeld";
}
echo "<br>\n";
}
// Switch3000
if( in_array("kortsluiting schakelen", $zkl_info['capabilities']) && $debug ) {
zkl_store($tcp_channel, "SWITCH", "F0");
usleep(500000);
$swstatus = zkl_retrieve($tcp_channel, "SWITCH", $tcp_timeout);
if( $swstatus !== FALSE ) {
$status_str = $swstatus;
$swstatus = intval(substr($status_str, 2), 16);
$swerror = intval(substr($status_str, 0, 2), 16);
}
}
else $swstatus = FALSE;
if( in_array("kortsluiting schakelen", $zkl_info['capabilities']) ) {
echo "Switch3000: ";
if( $status['switch3000']['on'] )
echo "<font color=\"green\">ingeschakeld</font>";
else
echo "<font color=\"red\">uitgeschakeld</font>";
if( $debug ) {
echo ", status: ";
if( $swstatus !== FALSE ) {
if( ($swerror & 0x01) == 0 ) {
echo "<font color=\"red\"><i>oude gegevens</i></font> ";
}
if( ($swerror & 0x80) != 0 ) {
if( ($swstatus & 0x00100000) != 0 )
echo "<font color=\"red\">Switch3000 time-out</font>";
else if( ($swstatus & 0x00200000) != 0 )
echo "<font color=\"red\">Switch3000 fout op 1-Wire verbinding</font>";
else
echo "<font color=\"red\">onbekende Switch3000 fout</font>";
}
else {
if( ($swstatus & 0x000F0000) == 0x00000000 )
echo "<font color=\"green\">alle secties uitgeschakeld</font>";
else if( ($swstatus & 0x000F0000) == 0x000F0000 )
echo "alle secties <font color=\"green\">ingeschakeld</font>";
else for( $i = 0; $i < 4; $i++ ) {
if( $i > 0 ) echo ", ";
echo "sectie " . ($i + 1) . " ";
if( $status['sw3000']['on'] ) {
if( ($swstatus & (1 << ($i + 16))) != 0 )
echo "<font color=\"green\">ingeschakeld</font>";
else
echo "<font color=\"red\">uitgeschakeld</font>";
}
else {
if( ($swstatus & (1 << ($i + 16))) != 0 )
echo "<font color=\"red\">ingeschakeld</font>";
else
echo "<font color=\"green\">uitgeschakeld</font>";
}
}
}
}
else echo "<font color=\"red\">geen antwoord binnen " . sprintf("%.1f", $tcp_timeout) . "&nbsp;s</font>";
}
echo "<br>\n";
}
if( in_array("sleutelschakelaar", $zkl_info['capabilities']) ) {
echo "Sleutelschakelaar: ";
if( $status['switch3000']['key']['on'] ) echo "<q>aan</q>";
else if( $status['switch3000']['key']['off'] ) echo "<q>uit</q>";
else echo "<q>operationeel</q>";
echo "<br>\n";
}
// Batteries
if( $swstatus !== FALSE && ($swerror & 0x80) == 0 && in_array("kortsluiting schakelen", $zkl_info['capabilities']) ) {
echo "Batterijen Switch3000: ";
if( ($swstatus & 0x00008F00) == 0x00008F00 )
echo "<font color=\"green\">ok</font>";
else {
for( $i = 0; $i < 4; $i++ ) {
if( $i > 0 ) echo ", ";
echo "sectie " . ($i + 1) . " ";
if( ($swstatus & (1 << ($i + 8))) == 0 )
echo "<font color=\"red\">niet ok</font>";
else
echo "<font color=\"green\">ok</font>";
}
echo ", noodschakelaar ";
if( ($swstatus & 0x00008000) == 0 )
echo "<font color=\"red\">niet ok</font>";
else
echo "<font color=\"green\">ok</font>";
}
echo "<br>\n";
}
for( $batt = 0; $batt < $zkl_info['nr_batterijen']; $batt++ ) {
echo "Accu " . ($batt + 1) . ": ";
echo sprintf("%.3fV, ", $status['batt'][$batt]['V']);
if( $status['batt'][$batt]['status'] == "ok" )
echo "<font color=\"green\">ok</font>";
else if( $status['batt'][$batt]['status'] == "removed" )
echo "verwijderd";
else if( $status['batt'][$batt]['status'] == "alarm" )
echo "<font color=\"red\">alarm</font>";
else
echo "<font color=\"red\">leeg</font>";
if( $status['batt']['select'] == $batt || $status['batt']['select'] == -1 ) echo ", ingeschakeld";
echo "<br>\n";
}
// GSM (debug only)
if( $debug && in_array("gsm", $zkl_info['capabilities']) && $status['gsm']['enabled'] ) {
echo "GSM: ";
if( !$status['gsm']['connected'] ) {
echo "<font color=\"red\">";
echo "netwerk";
echo "</font>";
echo ", ";
}
if( $status['gsm']['sim initialized'] )
echo "<font color=\"green\">";
echo "SIM";
echo "</font>";
echo ", ";
if( $status['gsm']['gprs connected'] )
echo "<font color=\"green\">";
echo "GPRS";
echo "</font>";
echo ", ";
if( $status['gsm']['tcp/ip connected'] )
echo "<font color=\"green\">";
echo "TCP";
echo "</font>";
if( $status['gsm']['received signal strength'] != -999 ) {
echo ", signaal=";
if( $status['gsm']['received signal strength'] <= -97.0 )
echo "<font color=\"red\">";
else if( $status['gsm']['received signal strength'] <= -81.0 )
echo "<font color=\"orange\">";
else
echo "<font color=\"green\">";
echo sprintf("%d</font> dBm", $status['gsm']['received signal strength']);
echo ", bit error rate=" . $status['gsm']['bit error rate'];
}
echo "<br>\n";
}
// Temperature
if( in_array("temperatuursensor on-board", $zkl_info['capabilities']) ) {
echo "T<sub>on-board</sub>: " . sprintf("%.1f&deg;C", $status['temp']['on-board']['C']) . "<br>\n";
}
if( in_array("temperatuursensor extern", $zkl_info['capabilities']) ) {
if( $status['temp']['ext1']['C'] !== FALSE && $status['temp']['ext1']['C'] > -128 )
echo "T<sub>ext1</sub>: " . sprintf("%.1f&deg;C", $status['temp']['ext1']['C']) . "<br>\n";
if( $status['temp']['ext2']['C'] !== FALSE && $status['temp']['ext2']['C'] > -128 )
echo "T<sub>ext2</sub>: " . sprintf("%.1f&deg;C", $status['temp']['ext2']['C']) . "<br>\n";
}
if( in_array("kortsluiting schakelen", $zkl_info['capabilities']) ) {
if( $status['temp']['switch3000']['C'] !== FALSE && $status['temp']['switch3000']['C'] > -128 )
echo "T<sub>switch3000</sub>: " . sprintf("%.1f&deg;C", $status['temp']['switch3000']['C']) . "<br>\n";
}
if( $swstatus !== FALSE && ($swerror & 0x80) == 0 ) {
echo "T<sub>switch</sub>: " . sprintf("%.1f&deg;C", $swstatus & 0xFF) . "<br>\n";
}
// GPS
if( in_array("gps", $zkl_info['capabilities']) ) {
echo "GPS: ";
echo sprintf("%.6f,%.6f", $status['gps']['latitude'], $status['gps']['longitude']);
// OBSOLETE: age is affected too much by whether someone is viewing the RT status
//if( $status['gps']['tijd'] != 0 ) {
// $age = time() - $status['gps']['tijd'];
// if( $age < 0 ) $age = 0; // clocks may differ...
// echo sprintf(", ouderdom=%u&prime;%02u&Prime;", $age / 60, $age % 60);
//}
if( $status['gps']['speed'] != 0.0 ) {
echo sprintf(", tc=%.1f, speed=%.1f km/h", $status['gps']['heading'], $status['gps']['speed']);
}
echo sprintf(", hdop=%.1f", $status['gps']['hdop']);
if( $debug ) {
if( isset($status['gps']['nsv']) ) {
echo ", nsv=" . $status['gps']['nsv'];
}
switch( $status['gps']['mode'] ) {
case 'A':
echo ", fix=<font color=\"green\">normaal</font>";
break;
case 'D':
echo ", fix=<font color=\"green\">differentieel</font>";
break;
case 'E':
echo ", fix=<font color=\"orange\">schatting</font>";
break;
case 'I':
echo ", <font color=\"orange\">oude data van mcu</font>";
break;
case 'U':
echo ", <font color=\"red\">initialiseert</font>";
break;
case 'N':
echo ", <font color=\"red\">geen fix</font>";
break;
default:
if( !$status['gps']['fix'] ) echo ", <font color=\"red\">geen fix</font>";
break;
}
}
else if( !$status['gps']['fix'] ) {
echo ", <font color=\"red\">geen fix</font>";
}
echo "<br>\n";
// Geofence
if( $zkl_info['geofence'] ) {
echo "Geofence: ";
if( $status['geofence']['inside'] ) {
echo "<font color=\"green\">binnen</font>";
$geofences = zkl_command($tcp_channel, $tcp_timeout, "\$GEOFENCE");
if( $geofences === FALSE ) {
echo ", <font color=\"red\">geen antwoord binnen " . sprintf("%.1f", $tcp_timeout) . "&nbsp;s</font>";
}
else {
$geofences = explode(",", $geofences);
$result = mysql_run("SELECT naam FROM geofence WHERE id IN (" . implode(",", $geofences) . ")", $db_main_handle);
$sep = " ";
while( $row = mysql_fetch_assoc($result) ) {
echo $sep;
echo $row['naam'];
$sep = ", ";
}
}
echo "<br>\n";
}
else {
echo "<font color=\"red\">buiten</font>";
echo "<br>\n";
}
}
}
// Firmware
if( $versions ) {
zkl_queue_retrieval($tcp_channel, "VERSION[FW-MCU]");
zkl_queue_retrieval($tcp_channel, "VERSION[FW-WCPU]");
zkl_queue_retrieval($tcp_channel, "VERSION[FW-WAVECOM]");
zkl_read_result($tcp_channel, $mcu_version, $tcp_timeout);
zkl_read_result($tcp_channel, $wcpu_version, $tcp_timeout);
zkl_read_result($tcp_channel, $q2686_version, $tcp_timeout);
echo "Firmware: " . $mcu_version . ", " . $wcpu_version . ", " . $q2686_version . "<br>\n";
}
// Done
zkl_tcplogout($tcp_channel);
}
echo "</p>\n";
}
}
// clean-up
if( $db_main_info['file'] != $db_info['file'] ) mysql_close($db_main_handle);
mysql_close($db_data_handle);
?>