232 lines
9.2 KiB
PHP
232 lines
9.2 KiB
PHP
<?php
|
||
//Выдираю все объекты на которых установлен терминал и отправляю клиенту
|
||
|
||
@session_start();
|
||
|
||
if(!isset($HTTP_RAW_POST_DATA))
|
||
{ $HTTP_RAW_POST_DATA = file_get_contents("php://input");
|
||
}
|
||
|
||
$object = json_decode($HTTP_RAW_POST_DATA);
|
||
//if($object->active=='') $object->active='null';
|
||
//if($object->cnumber=='') $object->cnumber='null'; else $object->cnumber='"%'.$object->cnumber.'%"'; //Гос. номер
|
||
//if($object->tnumber=='') $object->tnumber='null'; else $object->tnumber='"%'.$object->tnumber.'%"'; //Номер ТД
|
||
//if($object->country_seal_begin=='') $object->country_seal_begin='null'; //Страна пломб.
|
||
//if($object->country_seal_end=='') $object->country_seal_end='null'; //Страна распломб.
|
||
|
||
require_once("../../monitoring/config.php");
|
||
require_once("../../resources/metadata/include/tools.php");
|
||
|
||
$db=connectToDB();
|
||
$db->exec("SET TIME ZONE 'UTC';"); //Ниже в коде есть смещение временной зоны под настройки каждого пользователя
|
||
|
||
$fn=0;
|
||
if(isset($_GET['fn'])) { $fn=$_GET['fn']; }
|
||
if($fn=='0') //Вернуть список объектов в соответствии с фильтром
|
||
{
|
||
if(!property_exists($object,'company_id') or $object->company_id=='') $object->company_id='null'; //id компании
|
||
if(!property_exists($object,'cnumber') or $object->cnumber=='') $object->cnumber='null'; else $object->cnumber='\'%'.$object->cnumber.'%\''; //Гос. номер
|
||
|
||
$sql = 'SELECT
|
||
o.id,
|
||
0 as seq,
|
||
o.name,
|
||
sl.lat,
|
||
sl.lon,
|
||
round(cast(sl.speed as numeric),1) speed,
|
||
to_char(sl.date+(select timezone from main._users where id='.$_SESSION['USER_ID'].'), \'yyyy.mm.dd HH24:MI:SS\') as date,
|
||
\'\' as declaration,
|
||
i.file_name as icon_name,
|
||
\'\' as active,
|
||
t.serial || \' (\' || t.imei || \')\' AS terminal_name,
|
||
coalesce(g.geofences,\'\') as geofences
|
||
from
|
||
main.objects o
|
||
LEFT JOIN main.terminals_locations sl ON sl.id=o.terminal_location_id
|
||
LEFT JOIN main.terminals t ON t.id = o.terminal_id
|
||
LEFT JOIN main.icons i on i.id=o.icon_id
|
||
LEFT JOIN (SELECT og.object_id, CAST(array_to_string(array_agg(gg.name), \', \') AS character varying ) AS geofences FROM main.objects_geofences og, main.geofences gg WHERE og.del = FALSE and gg.del=false AND gg.id = og.geofence_id GROUP BY og.object_id) g ON o.id = g.object_id
|
||
where
|
||
o.del=false
|
||
and ('.$object->company_id.' is null or '.$object->company_id.' in (select company_id from main.companies_objects where del=false and object_id=o.id))
|
||
and ('.$object->cnumber.' is null or o.name like '.$object->cnumber.')
|
||
order by o.name,o.id';
|
||
|
||
try
|
||
{
|
||
$res = $db->query($sql);
|
||
}catch (Exception $e)
|
||
{ echo $e->getMessage();
|
||
$res = null;
|
||
}
|
||
|
||
//Массив объектов
|
||
$json ='[';
|
||
//Перебираю и последовательно отправляю не отправленные пломбы
|
||
while ($row = $res->fetch(PDO::FETCH_ASSOC))
|
||
{
|
||
$json .="\n";
|
||
$json .='{';
|
||
$json .='"id":"'.$row['id']."\",\n";
|
||
$json .='"seq":"'.$row['seq']."\",\n";
|
||
$json .='"active":"'.$row['active']."\",\n";
|
||
$json .='"name":"'.$row['name']."\",\n";
|
||
$json .='"terminal_name":"'.$row['terminal_name']."\",\n";
|
||
$json .='"declaration":"'.$row['declaration']."\",\n";
|
||
$json .='"icon_name":"'.afterLast($row['icon_name'],'_')."\",\n";
|
||
|
||
//выбираю показания 3х датчиков для данного объекта (установки)
|
||
//$json .="\"bat\":\"0\",\n";
|
||
//$json .="\"bat_date\":\"0\",\n";
|
||
//$json .="\"tros\":\"0\",\n";
|
||
//$json .="\"tros_date\":\"0\",\n";
|
||
//$json .="\"box\":\"0\",\n";
|
||
//$json .="\"box_date\":\"0\",\n";
|
||
|
||
$json .='"lat":'.json_encode($row['lat']).",\n";
|
||
$json .='"lon":'.json_encode($row['lon']).",\n";
|
||
$json .='"speed":'.json_encode($row['speed']).",\n";
|
||
|
||
$json .='"date":"'.$row['date']."\",\n";
|
||
|
||
$json .="\"country\":\"KZ\",\n";
|
||
$json .='"geofences":"'.$row['geofences']."\",\n";
|
||
|
||
//Выбираю показания всех датчиков (которые видны)
|
||
$json .='"sensors":[';
|
||
$sql="
|
||
select
|
||
obs.id,
|
||
obs.name,
|
||
round(obr.value::numeric,2) as value,
|
||
to_char(obr.date+(select timezone from main._users where id=".$_SESSION['USER_ID']."), 'yyyy.mm.dd HH24:MI:SS') as date,
|
||
obs.sensor_type_id,
|
||
trst.measurement as terminalsensortype_name
|
||
from
|
||
main.objects_sensors obs
|
||
left join main.sensors_values obr on obr.id=obs.sensor_value_id
|
||
left join main.terminals_sensors trs on trs.id=obs.terminal_sensor_id
|
||
left join main.sensors_types trst on trst.id=trs.sensor_type_id
|
||
where
|
||
obs.del=false
|
||
and obs.object_id=".$row['id']."
|
||
order by obs.name";
|
||
try
|
||
{
|
||
$res2 = $db->query($sql);
|
||
}catch (Exception $e)
|
||
{ echo $e->getMessage();
|
||
$res2 = null;
|
||
}
|
||
if ($res2->rowCount() > 0) {
|
||
while ($row2 = $res2->fetch(PDO::FETCH_ASSOC))
|
||
{
|
||
$json .='{';
|
||
$json .='"id":"'.$row2['id']."\",\n";
|
||
$json .='"name":"'.$row2['name']."\",\n";
|
||
$json .='"value":"'.$row2['value']."\",\n";
|
||
$json .='"type_name":"'.$row2['terminalsensortype_name']."\",\n";
|
||
$json .='"date":"'.$row2['date']."\"\n";
|
||
$json .='},';
|
||
}
|
||
$json=substr($json, 0, -1);
|
||
}
|
||
$json .=']';
|
||
|
||
$json .='},';
|
||
}
|
||
if($json[strlen($json) - 1]==','){
|
||
$json=substr ( $json , 0, strlen($json)-1 );
|
||
}
|
||
$json .=']';
|
||
|
||
header('Content-Type: application/json');
|
||
echo $json;
|
||
exit;
|
||
}else
|
||
if($fn=='1') //Вернуть список объектов для обновления полей по переданным ID
|
||
{
|
||
|
||
$sql = 'SELECT
|
||
o.id,
|
||
0 as seq,
|
||
o.name,
|
||
sl.lat,
|
||
sl.lon,
|
||
to_char(sl.date+(select timezone from main._users where id='.$_SESSION['USER_ID'].'), \'yyyy.mm.dd HH24:MI:SS\') as date,
|
||
\'\' as declaration,
|
||
i.file_name as icon_name,
|
||
\'\' as active
|
||
from
|
||
main.objects o
|
||
LEFT JOIN main.terminals_locations sl ON sl.id=o.terminal_location_id
|
||
LEFT JOIN main.icons i on i.id=o.icon_id
|
||
where
|
||
o.del=false
|
||
and o.id in '.str_replace("]", ")", str_replace("[", "(", $HTTP_RAW_POST_DATA)).'
|
||
order by o.name,o.id';
|
||
|
||
try
|
||
{
|
||
$res = $db->query($sql);
|
||
}catch (Exception $e)
|
||
{ echo $e->getMessage();
|
||
$res = null;
|
||
}
|
||
|
||
//Массив объектов
|
||
$json ='[';
|
||
//Перебираю и последовательно отправляю не отправленные пломбы
|
||
while ($row = $res->fetch(PDO::FETCH_ASSOC))
|
||
{
|
||
$json .="\n";
|
||
$json .='{';
|
||
$json .='"id":"'.$row['id']."\",\n";
|
||
$json .='"lat":"'.$row['lat']."\",\n";
|
||
$json .='"lon":"'.$row['lon']."\",\n";
|
||
$json .='"date":"'.$row['date']."\",\n";
|
||
//Выбираю показания всех датчиков (которые видны)
|
||
$json .='"sensors":[';
|
||
$sql="
|
||
select
|
||
obs.id,
|
||
round(obr.value::numeric,2) as value,
|
||
to_char(obr.date+(select timezone from main._users where id=".$_SESSION['USER_ID']."), 'yyyy.mm.dd HH24:MI:SS') as date
|
||
from
|
||
main.objects_sensors obs
|
||
left join main.sensors_values obr on obr.id=obs.sensor_value_id
|
||
left join main.terminals_sensors trs on trs.id=obs.terminal_sensor_id
|
||
where
|
||
obs.del=false
|
||
and obs.object_id=".$row['id']."
|
||
order by obs.name";
|
||
try
|
||
{
|
||
$res2 = $db->query($sql);
|
||
}catch (Exception $e)
|
||
{ echo $e->getMessage();
|
||
$res2 = null;
|
||
}
|
||
if ($res2->rowCount() > 0) {
|
||
while ($row2 = $res2->fetch(PDO::FETCH_ASSOC))
|
||
{
|
||
$json .='{';
|
||
$json .='"id":"'.$row2['id']."\",\n";
|
||
$json .='"value":"'.$row2['value']."\",\n";
|
||
$json .='"date":"'.$row2['date']."\"\n";
|
||
$json .='},';
|
||
}
|
||
$json=substr($json, 0, -1);
|
||
}
|
||
$json .=']';
|
||
$json .='},';
|
||
}
|
||
if($json[strlen($json) - 1]==','){
|
||
$json=substr ( $json , 0, strlen($json)-1 );
|
||
}
|
||
$json .=']';
|
||
|
||
header('Content-Type: application/json');
|
||
echo $json;
|
||
exit;
|
||
} |