src.dualinventive.com/mtinfo/dist/webroot/main/scripts/other/vitelec.php

150 lines
4.8 KiB
PHP

<?php
/**
* \file scripts\other\vitelec.php
* \brief Vitelec SenseConnect (CRT-M) device
* \author Jack Weeland, Core|Vision B.V.
* \version $Revision: 1.8 $
* \date $Date: 2013/03/14 22:50:27 $
*/
header("Content-type: application/json");
vitelec_run();
function vitelec_run()
{
// Get JSON object from the POSTed data; Vitelec's input does not conform
// to the RFC's so we must get the raw post data
$json_str = file_get_contents("php://input");
$json = json_parse($json_str);
// sanity check
if( !$json ) return vitelec_error("No data");
// Locate the gateway in the database
$gateway = json_get_object($json, "gateway");
if( !($db_gateway = db_fetch_imei(json_get_object($gateway, "uid"), NULL)) ) {
return vitelec_error("Invalid device ID: " . json_get_object($gateway, "uid"));
}
else $db_gateway = $db_gateway[0];
// build status string
$status = array(
"changes" => STATUPDATE_ANY,
"mcu_state" => 0x0000, // batteries are undefined
"mcu_local_state" => MCULOC_INIT_DONE, //
"mcu_persistent" => 0xBB00, // batteries, 3.6V LiIon
"t" => json_get_object($gateway, "date_time")
);
// Get fixed GPS position
$gps = db_fetch_cache($db_gateway['id'], "gps");
if( $gps ) {
$status['gps_mode'] = 'init';
$status['speed'] = 0.0;
$status = array_merge($status, $gps);
}
// Update time stamp in "log_realtime"
db_store_log_rt($db_gateway['id'], $status);
// Handle the sensors (if any)
$sensors = json_get_object($json, "sensors");
if( $sensors ) {
foreach( $sensors as $sensor ) {
// Get sensor from the database
if( !($db_sensor = db_fetch_imei(json_get_object($sensor, "address"), NULL)) ) {
return vitelec_error("Unknown sensor, address " . json_get_object($sensor, "address"));
}
$db_sensor = $db_sensor[0];
$device = db_fetch_system_devices(NULL, $db_sensor['device']);
$db_sensor['threshold_t_ext1'] = $device[0]['threshold_t_ext1'];
$db_sensor['threshold_t_ext2'] = $device[0]['threshold_t_ext2'];
// global information about the sensor
$batt_lvl = json_get_object($sensor, "battery_level");
foreach( json_get_object($sensor, "messages") as $message ) {
$t = json_get_object($message, "date");
$status = array(
"changes" => STATUPDATE_ANY,
"mcu_state" => 0x7000, // battery #2 not supported
"mcu_local_state" => MCULOC_INIT_DONE,//
"mcu_persistent" => 0xBB00, // batteries, 3.6V LiIon
"wcpu_state" => 0, //
"t" => $t, //
"gsm_rssi" => json_get_object($message, "message_rssi"),
"gsm_berr" => 0
);
// Get fixed GPS position
$gps = db_fetch_cache($db_sensor['id'], "gps");
if( $gps ) {
$status['gps_mode'] = 'init';
$status['speed'] = 0.0;
$status = array_merge($status, $gps);
}
// battery level
$status['batt_sel'] = 0;
$status['batt1_niveau'] = $batt_lvl / 1000.0;
if( $batt_lvl < 3200 ) $status['mcu_state'] |= MCUSTAT_BATTERY1_LOW;
if( $batt_lvl < 3000 ) $stauts['mcu_state'] |= MCUSTAT_BATTERY1_CRITICAL;
$sensor_data = json_get_object($message, "data");
foreach( $sensor_data as $key => $item ) {
switch( $key ) {
case "0":
$T = json_get_object($item, "value") / 10.0;
$status["temp_onboard"] = $T;
$major = MAJ_TEMP;
$minor = MIN_TEMP_ONBOARD;
if( $T >= $db_sensor['threshold_t_ext2'] ) $status['wcpu_state'] |= WCPUERR_TEMP_EXT2;
break;
case "1":
$T = json_get_object($item, "value") / 10.0;
$status["temp_ntc"] = $T;
$major = MAJ_TEMP;
$minor = MIN_TEMP_EXT1;
if( $T >= $db_sensor['threshold_t_ext1'] ) $status['wcpu_state'] |= WCPUERR_TEMP_EXT1;
break;
default:
// not supported
continue;
}
// Add to "log_zkl"
db_store_log_zkl($db_sensor['id'], array(0,0), $t, $major, $minor, $T);
}
// Add to "log_realtime"
db_store_log_rt($db_sensor['id'], $status);
db_store_cache($db_sensor['id'], "status", $status);
}
}
}
// Success
return vitelec_success();
}
function vitelec_success()
{
echo "{\n";
echo " \"Result\": {\n";
echo " \"Status\": \"OK\"\n";
echo " }\n";
echo "}\n";
return TRUE;
}
function vitelec_error($msg)
{
echo "{\n";
echo " \"Result\": {\n";
echo " \"Status\": \"ERROR\",\n";
echo " \"Message\": \"" . $msg . "\"\n";
echo " }\n";
echo "}\n";
DBG("Vitelec SenseConnect: ERROR: " . $msg);
return FALSE;
}
?>