You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.9 KiB
PHP

<?php
include('config.inc.php');
$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['database'], $config['dbuser'], $config['password'], [
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'"
]);
$result = array('res' => 0, 'data' => array());
$conditions = array();
$values = array();
if (isset($_GET['towerId']) && intval($_GET['towerId']) > 0)
{
$conditions[] = "t2.`id`=:towerid";
$values['towerid'] = intval($_GET['towerId']);
}
else if (isset($_GET['lineId']) && intval($_GET['lineId']) > 0)
{
$conditions[] = "t3.`id`=:lineid";
$values['lineid'] = intval($_GET['lineId']);
}
if (!empty($_GET['cmdid']))
{
$conditions[] = "t1.`cmdid` LIKE :cmdid";
$values['cmdid'] = '%' . $_GET['cmdid'] . '%';
}
$conditionStr = '';
if (count($conditions) > 0)
{
$conditionStr = ' WHERE ' . implode($conditions, ' AND ');
}
$sql = "SELECT t1.id, t1.cmdid,t1.`display_name`,t1.`protocol`,t2.`name` AS tower_name,t3.`name` AS line_name, t4.last_raw_report AS raw_report,FROM_UNIXTIME(t4.raw_report_time) AS last_heartbeat,t4.in_maintain,t4.quick_hb,t4.mode_time";
$sql .= " FROM terminals AS t1 JOIN towers AS t2 ON t1.tower_id=t2.id JOIN `lines` AS t3 ON t2.line_id=t3.id";
$sql .= " LEFT JOIN mntn_status AS t4 ON t1.id=t4.term_id " . $conditionStr . " ORDER BY t2.line_id,t2.`order`,t1.cmdid";
$stmt = $db->prepare($sql);
$stmt->execute($values);
$result['data'] = $stmt->fetchAll();
$stmt = null;
// "安徽欣影ds2-2x/4x,yw:0829_4.2.502, i1:ahxy_0829_4.2.502, cam:4.2.1, ai:1.1.0_1, i1服务器:47.96.238.157:6891 ,心跳间隔:1 ,
// 电池:6.3V\/0.2V\/87% ,系统重启:1 ,i1重启:13 ,收:10:52 ,拍:48\/44 ,成:44 ,败:0 ,传:44 ,
// 心跳累计:142 ,网络异常:139 ,信号1:4\/99 ,信号2:4\/99 ,卡1:898604F5122380159566,卡2: ,mcu:MTK_MCU_V3.2.5 Aug 9 2023"
$keyMapper = array('i1服务器' => 'cma', '心跳间隔' => 'heartbeatDuration', 'i1' => 'i1Version', 'yw' => 'maintainVersion', '电池' => 'battery', '系统重启' => 'rebootTimes',
'i1重启' => 'i1RebootTimes', '收' => 'recv', '拍' => 'photoTimes', '成' => 'success', '败' => 'failure', '传' => 'uploads', '心跳累计' => 'numberOfHb',
'网络异常' => 'networkError', '信号1' => 'signature1', '信号2' => 'signature2', '卡1' => 'simcard1', '卡2' => 'simcard2', 'mcu' => 'mcu', 'ai' => 'aiVersion', 'cam' => 'cameraService');
$obj = new \stdClass;
foreach($result['data'] as &$row)
{
$row['last_heartbeat_time'] = empty($row['last_heartbeat']) ? '' : substr($row['last_heartbeat'], 11);
if (empty($row['raw_report']))
{
$row['raw_report'] = $obj;
}
else
{
$row['raw_report'] = json_decode($row['raw_report'], true);
if (!empty($row['raw_report']['msg']))
{
$msgs = array();
// "安徽欣影ds2-2x/4x,yw:0829_4.2.502, i1:ahxy_0829_4.2.502, cam:4.2.1, ai:1.1.0_1, i1服务器:47.96.238.157:6891 ,心跳间隔:1 ,
// 电池:6.3V\/0.2V\/87% ,系统重启:1 ,i1重启:13 ,收:10:52 ,拍:48\/44 ,成:44 ,败:0 ,传:44 ,心跳累计:142 ,网络异常:139 ,信号1:4\/99 ,信号2:4\/99 ,卡1:898604F5122380159566,卡2: ,mcu:MTK_MCU_V3.2.5 Aug 9 2023"
$items = explode(',', $row['raw_report']['msg']);
foreach ($items as $item)
{
$pos = strpos($item, ':');
if ($pos === false)
{
continue;
}
$key = trim(substr($item, 0, $pos));
if (isset($keyMapper[$key]))
{
$key = $keyMapper[$key];
}
$msgs[$key] = trim(substr($item, $pos + 1));
}
$row['raw_report']['msgs'] = $msgs;
}
}
}
unset($row);
header ('Content-type: application/json; charset=utf-8');
echo json_encode($result, JSON_UNESCAPED_UNICODE);