diff --git a/conf/config.inc.php b/conf/config.inc.php index c50dbee..a3094b4 100644 --- a/conf/config.inc.php +++ b/conf/config.inc.php @@ -13,3 +13,7 @@ $config['password'] = 'Xymp@2023'; $config['user'] = 'xymp'; $config['group'] = 'xymp'; $config['ffmpeg'] = '/usr/local/bin/ffmpeg'; +$config['xympadmn_path'] = 'D:/Workspace/Github/xymp/utils/admin/bin/xympadmn.exe'; +$config['cma_server'] = '180.166.218.222'; +$config['cma_port'] = 40032; +$config['clientId'] = 11; diff --git a/maintain/index.php b/maintain/index.php index b58aa6f..afb94c7 100644 --- a/maintain/index.php +++ b/maintain/index.php @@ -8,6 +8,26 @@ function OutputResult($res) exit(); } +function getIPAddress() +{ + //whether ip is from the share internet + if(!empty($_SERVER['HTTP_CLIENT_IP'])) + { + $ip = $_SERVER['HTTP_CLIENT_IP']; + } + //whether ip is from the proxy + elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) + { + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + } + //whether ip is from the remote address + else + { + $ip = $_SERVER['REMOTE_ADDR']; + } + return $ip; +} + $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'" @@ -56,7 +76,7 @@ $stmt->execute(array('termid' => $term['id'])); $termStatus = $stmt->fetch(); $stmt = null; -if (empty($termStatus['raw_report_time'])) +if (!empty($termStatus['raw_report_time'])) { $mntnHb = time() - intval($termStatus['raw_report_time']); apcu_store('mntn_hb.' . $term['id'], $mntnHb); @@ -65,8 +85,8 @@ if (empty($termStatus['raw_report_time'])) $result['yw'] = (isset($termStatus['in_maintain']) && ($termStatus['in_maintain'] != 0)) ? 1 : 0; $result['kxt'] = (isset($termStatus['quick_hb']) && ($termStatus['quick_hb'] != 0)) ? 1 : 0; -$stmt = $db->prepare("INSERT INTO mntn_raw_reports(term_id, content) VALUES(:termid,:content)"); -$stmt->execute(array('termid' => $term['id'], 'content' => $body)); +$stmt = $db->prepare("INSERT INTO mntn_raw_reports(term_id, ip,content) VALUES(:termid,:ip,:content)"); +$stmt->execute(array('termid' => $term['id'], 'content' => $body, 'ip' => getIPAddress())); $sql = "SELECT * FROM mntn_cmds WHERE term_id=:termid ORDER BY id LIMIT 1"; $stmt = $db->prepare($sql); @@ -93,6 +113,7 @@ foreach ($rows as $row) { // kxt: quick heartbeat $cmdData = empty($row['cmd']) ? array() : json_decode($row['cmd'], true); + $result['cid'] = $row['id']; if ($row['name'] == 'upgrade') { @@ -122,7 +143,6 @@ foreach ($rows as $row) } $result['cmd'] = $row['name']; - $result['cid'] = $row['id']; if (!empty($row['cmd'])) { $result = array_merge($result, $cmdData); diff --git a/maintain/status/index.php b/maintain/status/index.php new file mode 100644 index 0000000..ddd9163 --- /dev/null +++ b/maintain/status/index.php @@ -0,0 +1,46 @@ + 0); + +function OutputResult($res) +{ + echo json_encode($res, JSON_UNESCAPED_UNICODE); + exit(); +} + +function getIPAddress() +{ + //whether ip is from the share internet + if(!empty($_SERVER['HTTP_CLIENT_IP'])) + { + $ip = $_SERVER['HTTP_CLIENT_IP']; + } + //whether ip is from the proxy + elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) + { + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + } + //whether ip is from the remote address + else + { + $ip = $_SERVER['REMOTE_ADDR']; + } + return $ip; +} + + +$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'" + ]); + +header ('Content-type: application/json; charset=utf-8'); +$request = ($_SERVER['REQUEST_METHOD'] == 'POST') ? $_POST : $_GET; + +$sql = 'INSERT INTO mntn_cmd_results(`cid`,`result`,`content`) VALUES(:cid,:result,:content)'; +$stmt = $db->prepare($sql); +$stmt->execute(array('cid' => intval($request['cid']), 'result' => intval($request['res']), 'content' => empty($request['content']) ? '' : $request['content'])); + +OutputResult($result); diff --git a/maintain/web/api/cmd.inc.php b/maintain/web/api/cmd.inc.php index 8070dc6..06c9497 100644 --- a/maintain/web/api/cmd.inc.php +++ b/maintain/web/api/cmd.inc.php @@ -3,7 +3,7 @@ $keyMapper = array('i1服务器' => 'cma', '心跳间隔' => 'heartbeatDuration', 'i1' => 'i1Version', 'yw' => 'maintainVersion', '电池' => 'battery', '系统重启' => 'rebootTimes', '重启' => 'rebootTimes', 'i1重启' => 'i1RebootTimes', 'i1启' => 'i1RebootTimes', '收' => 'recv', '拍' => 'photoTimes', '成' => 'success', '败' => 'failure', '传' => 'uploads', '心跳累计' => 'numberOfHb', '心跳' => 'numberOfHb', '网络异常' => 'networkError', '网络' => 'networkError', '信号1' => 'signature1', '信号2' => 'signature2', - '卡1' => 'simcard1', '卡2' => 'simcard2', 'mcu' => 'mcu', 'ai' => 'aiVersion', 'cam' => 'cameraService', '主板温度' => 'mainBoardTmp', '电池温度' => 'batteryTmp'); + '卡1' => 'simcard1', '卡2' => 'simcard2', 'mcu' => 'mcu', 'ai' => 'aiVersion', 'cam' => 'cameraService', '主板温度' => 'mainBoardTmp', '电池温度' => 'batteryTmp', '剩余存储空间' => 'freeROM'); function parseCmd($content) { diff --git a/maintain/web/api/queryCfg.php b/maintain/web/api/queryCfg.php new file mode 100644 index 0000000..88c30f6 --- /dev/null +++ b/maintain/web/api/queryCfg.php @@ -0,0 +1,170 @@ + \PDO::FETCH_ASSOC, + \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'" + ]); + +$action = isset($_GET['act']) ? $_GET['act'] : ''; + +if ($action == 'q') +{ + $cmdid = isset($_GET['cmdid']) ? $_GET['cmdid'] : ''; + + $result = array('code' => 0, 'data' => array()); + + if (isset($_GET['mntn']) && $_GET['mntn'] == 1) + { + $values['cmdName'] = 'upd_cfg'; + + $sql = "SELECT `id` FROM terminals WHERE cmdid=:cmdid LIMIT 1"; + $stmt = $db->prepare($sql); + $stmt->execute(array('cmdid' => $cmdid)); + $terminal = $stmt->fetch(); + + if ($terminal) + { + $termId = $terminal['id']; + $sql = "DELETE FROM mntn_cmds WHERE term_id=:termid AND `name`=:cmdName LIMIT 1"; + $stmt1 = $db->prepare($sql); + + $sql = "INSERT INTO mntn_cmds(term_id,`name`,`cmd`,`desc`) VALUES(:termId,:cmdName,:cmd,:cmdDesc)"; + $stmt2 = $db->prepare($sql); + + $path_parts = pathinfo($_GET['filename']); + + $idx = 0; + $cmd = array(); + $cmd['path'] = $path_parts['dirname']; + $cmd['fileName'] = $path_parts['basename']; + + $configs = array(); + foreach ($_GET['cfgName'] as $cfgName) + { + if (empty($cfgName)) + { + $idx++; + continue; + } + + $cfg = array(); + + $cfg['name'] = $cfgName; + $cfg['type'] = intval($_GET['cfgType'][$idx]); + if (strlen($_GET['cfgValue'][$idx]) == 0) + { + $cfg['value'] = null; + } + else + { + if ($cfg['type'] == 0) + { + $cfg['value'] = intval($_GET['cfgValue'][$idx]); + } + else if ($cfg['type'] == 1) + { + $cfg['value'] = strval($_GET['cfgValue'][$idx]); + } + else if ($cfg['type'] == 2) + { + $cfg['value'] = floatval($_GET['cfgValue'][$idx]); + } + } + + $configs[] = $cfg; + $idx++; + } + $cmd['configs'] = $configs; + if (!empty($_GET['packageName'])) + { + $cmd['packageName'] = $_GET['packageName']; + } + + $values = array('termId' => $termId, 'cmd' => json_encode($cmd), 'cmdDesc' => '修改配置', 'cmdName' => 'upd_cfg'); + + if (!empty($action)) + { + $res = $stmt1->execute(array('termid' => $termId, 'cmdName' => $values['cmdName'])); + if (!$res) + { + // print_r($stmt1->errorInfo()); + } + + $res = $stmt2->execute($values); + + if (!$res) + { + // print_r($stmt2->errorInfo()); + } + } + + if ($res) + { + $result['data'][] = $termId; + } + } + else + { + echo "false"; + } + } + else + { + // http://dev.xinyingpower.com:40101/web/api/queryCfg.php?act=q&filename=data%2Fchannels%2F1.json + $path = str_replace('com.xypower.mpapp/', '', $_GET['filename']); + $cmd = $config['xympadmn_path'] . ' --act=cfg --server=' . $config['cma_server'] . ' --port=' . $config['cma_port'] .' --cmdid="' . $cmdid . '" --pathType=1 --updateType=0 --path="' . $path . '"'; + // --name1=osd.leftTop --type1=1 --value1="OSD for LeftTop %%CH%%" --name2=osd.rightTop --type2=255 --value2="OSD for rIGHTTop %%CH%%" --name3=usbCamera --type3=0 --value3=1 --clientid=5 --reqid=TS'; + + if (!apcu_exists('xympadmn.requestid')) + { + apcu_store('xympadmn.requestid', time()); + } + + $requestId = apcu_inc('xympadmn.requestid'); + + $cmd .= ' --clientid=' . $config['clientId'] . ' --reqid=' . $requestId; + header('Cmd:' . $cmd); + + $result_code = 1; + $res = exec($cmd, $output, $result_code); + if ($res !== false && $result_code === 0) + { + $result['clientId'] = $config['clientId']; + $result['requestId'] = $requestId; + } + else + { + $result['code'] = 1; + } + } +} +else +{ + $clientId = intval($_GET['clientId']); + $requestId = intval($_GET['reqId']); + + $sql = 'SELECT * FROM request_results WHERE client_id=:clientId AND request_id=:reqId'; + + $stmt = $db->prepare($sql); + $stmt->execute(array('clientId' => $clientId, 'reqId' => $requestId)); + $row = $stmt->fetch(); + + if (isset($row['data'])) + { + $row['data'] = json_decode($row['data'], true); + + if (isset($row['data']['content'])) + { + $row['data']['content'] = json_decode(base64_decode($row['data']['content']), true); + } + } + $result['data'] = $row; +} + + +header ('Access-Control-Allow-Origin: *'); +header ('Content-type: application/json; charset=utf-8'); +// $result['code'] = $res ? 0 : 1; +echo json_encode($result, JSON_UNESCAPED_UNICODE); diff --git a/maintain/web/api/queryCmds.php b/maintain/web/api/queryCmds.php index 229fa68..d872969 100644 --- a/maintain/web/api/queryCmds.php +++ b/maintain/web/api/queryCmds.php @@ -38,7 +38,7 @@ $result['page'] = array('page' => 1, 'pageSize' => $pageSize, "totalPages" => $n if ($queryType == 'pending') { - $sql = 'SELECT t1.cmdid,t1.`display_name` AS displayName,t2.`name` AS cmdName,t2.`cmd`,t2.`create_time` AS createTime,t2.term_id AS termId,t2.`id` FROM ' . $tableName . ' AS t2 JOIN terminals AS t1 ON t2.term_id=t1.`id` ORDER BY t2.`id` DESC LIMIT ' . (($page - 1) * $pageSize) . ',' . $pageSize; + $sql = 'SELECT t1.cmdid,t1.`display_name` AS displayName,t2.`name` AS cmdName,t2.`cmd`,t2.`desc` AS cmdDesc,t2.`create_time` AS createTime,t2.term_id AS termId,t2.`id`,t3.raw_report_time AS prevReportTime FROM ' . $tableName . ' AS t2 JOIN terminals AS t1 ON t2.term_id=t1.`id` JOIN mntn_status AS t3 ON t2.term_id=t3.term_id ORDER BY t2.`id` DESC LIMIT ' . (($page - 1) * $pageSize) . ',' . $pageSize; } else { @@ -67,6 +67,19 @@ foreach ($result['data'] as &$item) { $item['cmd'] = $obj; } + + if (isset($item['prevReportTime'])) + { + $hb = apcu_fetch('mntn_hb.' . $item['termId']); + if ($hb !== false && $hb !== 0) + { + $item['estimatedPublishTime'] = $item['prevReportTime'] + $hb; + } + else + { + $item['estimatedPublishTime'] = 0; + } + } } unset($item); diff --git a/maintain/web/api/queryTerms.php b/maintain/web/api/queryTerms.php index 2f91ef7..15c9186 100644 --- a/maintain/web/api/queryTerms.php +++ b/maintain/web/api/queryTerms.php @@ -84,7 +84,7 @@ if (count($conditions) > 0) $conditionStr = ' WHERE ' . implode(' AND ', $conditions); } -$sql = "SELECT t1.id, t1.cmdid,t1.`display_name`,t1.`protocol`,t1.bs_identifier AS `oid`,t2.`name` AS tower_name,t3.`name` AS line_name,t4.last_raw_report AS raw_report,t4.raw_report_time AS last_heartbeat_ts,FROM_UNIXTIME(t4.raw_report_time) AS last_heartbeat,t4.in_maintain,t4.quick_hb,t4.mode_time"; +$sql = "SELECT t1.id, t1.cmdid,t1.`display_name`,t1.`protocol`,t1.bs_identifier AS `oid`,t2.`name` AS tower_name,t3.`name` AS line_name,t4.last_raw_report AS raw_report,t4.raw_report_time AS last_heartbeat_ts,FROM_UNIXTIME(t4.raw_report_time) AS last_heartbeat,t4.in_maintain,t4.quick_hb,t4.mode_time,t4.comment"; $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; if (empty($orderField)) diff --git a/maintain/web/api/updCfg.php b/maintain/web/api/updCfg.php new file mode 100644 index 0000000..7d9ec12 --- /dev/null +++ b/maintain/web/api/updCfg.php @@ -0,0 +1,178 @@ + \PDO::FETCH_ASSOC, + \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'" + ]); + + +$action = isset($_POST['act']) ? $_POST['act'] : ''; +$cmdid = isset($_POST['cmdid']) ? $_POST['cmdid'] : ''; + +$result = array('code' => 0, 'data' => array()); + +if (isset($_POST['mntn']) && $_POST['mntn'] == 1) +{ + $values['cmdName'] = 'upd_cfg'; + + $sql = "SELECT `id` FROM terminals WHERE cmdid=:cmdid LIMIT 1"; + $stmt = $db->prepare($sql); + $stmt->execute(array('cmdid' => $cmdid)); + $terminal = $stmt->fetch(); + + if ($terminal) + { + $termId = $terminal['id']; + $sql = "DELETE FROM mntn_cmds WHERE term_id=:termid AND `name`=:cmdName LIMIT 1"; + $stmt1 = $db->prepare($sql); + + $sql = "INSERT INTO mntn_cmds(term_id,`name`,`cmd`,`desc`) VALUES(:termId,:cmdName,:cmd,:cmdDesc)"; + $stmt2 = $db->prepare($sql); + + $path_parts = pathinfo($_POST['filename']); + + $idx = 0; + $cmd = array(); + $cmd['path'] = $path_parts['dirname']; + $cmd['fileName'] = $path_parts['basename']; + + $configs = array(); + foreach ($_POST['cfgName'] as $cfgName) + { + if (empty($cfgName)) + { + $idx++; + continue; + } + + $cfg = array(); + + $cfg['name'] = $cfgName; + $cfg['type'] = intval($_POST['cfgType'][$idx]); + if (strlen($_POST['cfgValue'][$idx]) == 0) + { + $cfg['value'] = null; + } + else + { + if ($cfg['type'] == 0) + { + $cfg['value'] = intval($_POST['cfgValue'][$idx]); + } + else if ($cfg['type'] == 1) + { + $cfg['value'] = strval($_POST['cfgValue'][$idx]); + } + else if ($cfg['type'] == 2) + { + $cfg['value'] = floatval($_POST['cfgValue'][$idx]); + } + } + + $configs[] = $cfg; + $idx++; + } + $cmd['configs'] = $configs; + if (!empty($_POST['packageName'])) + { + $cmd['packageName'] = $_POST['packageName']; + } + + $values = array('termId' => $termId, 'cmd' => json_encode($cmd), 'cmdDesc' => '修改配置', 'cmdName' => 'upd_cfg'); + + if (!empty($action)) + { + $res = $stmt1->execute(array('termid' => $termId, 'cmdName' => $values['cmdName'])); + if (!$res) + { + // print_r($stmt1->errorInfo()); + } + + $res = $stmt2->execute($values); + + if (!$res) + { + // print_r($stmt2->errorInfo()); + } + } + + if ($res) + { + $result['data'][] = $termId; + } + } + else + { + echo "false"; + } +} +else +{ + $path = str_replace('com.xypower.mpapp/', '', $_POST['filename']); + $cmd = $config['xympadmn_path'] . ' --act=cfg --server=' . $config['cma_server'] . ' --port=' . $config['cma_port'] .' --cmdid="' . $cmdid . '" --pathType=1 --updateType=0 --path="' . $path . '"'; + // --name1=osd.leftTop --type1=1 --value1="OSD for LeftTop %%CH%%" --name2=osd.rightTop --type2=255 --value2="OSD for rIGHTTop %%CH%%" --name3=usbCamera --type3=0 --value3=1 --clientid=5 --reqid=TS'; + + if (!empty($_POST['packageName'])) + { + $cmd .= ' --reboot=1'; + } + + if (!apcu_exists('xympadmn.requestid')) + { + apcu_store('xympadmn.requestid', time()); + } + + + $requestId = apcu_inc('xympadmn.requestid'); + + $idx = 0; + $configs = ''; + foreach ($_POST['cfgName'] as $cfgName) + { + if (empty($cfgName)) + { + $idx++; + continue; + } + + $cfgType = intval($_POST['cfgType'][$idx]); + + $configs .= ' --name' . ($idx + 1) . '="' . $cfgName . '" --type' . ($idx + 1) . '=' . intval($_POST['cfgType'][$idx]) . ' --value' . ($idx + 1) . '='; + + if ($cfgType == 0) + { + $configs .= intval($_POST['cfgValue'][$idx]); + } + else if ($cfgType == 1) + { + $configs .= '"' . strval($_POST['cfgValue'][$idx]) . '"'; + } + else if ($cfgType == 2) + { + $configs .= floatval($_POST['cfgValue'][$idx]); + } + $idx++; + } + + $cmd .= ' --configs=' . $idx . $configs . ' --clientid=' . $config['clientId'] . ' --reqid=' . $requestId; + header('Cmd:' . $cmd); + + $result_code = 1; + $res = exec($cmd, $output, $result_code); + if ($res !== false && $result_code === 0) + { + $result['clientId'] = 11; + $result['requestId'] = $requestId; + } + else + { + $result['code'] = 1; + } +} + +header ('Access-Control-Allow-Origin: *'); +header ('Content-type: application/json; charset=utf-8'); +// $result['code'] = $res ? 0 : 1; +echo json_encode($result, JSON_UNESCAPED_UNICODE); diff --git a/maintain/web/api/updComment.php b/maintain/web/api/updComment.php new file mode 100644 index 0000000..12dccbd --- /dev/null +++ b/maintain/web/api/updComment.php @@ -0,0 +1,24 @@ + \PDO::FETCH_ASSOC, + \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'" + ]); + +$values['term_id'] = empty($_GET['id']) ? 0 : intval($_GET['id']); +$values['comment'] = empty($_GET['comment']) ? '' : $_GET['comment']; + +$result = array('res' => 0, 'data' => array('id' => $values['term_id'])); + +$sql = "UPDATE mntn_status SET `comment`=:comment WHERE `term_id`=:term_id"; +$stmt = $db->prepare($sql); +if (!$stmt->execute($values)) +{ + $result['res'] = 1; +} + +header ('Access-Control-Allow-Origin: *'); +header ('Content-type: application/json; charset=utf-8'); +echo json_encode($result, JSON_UNESCAPED_UNICODE); diff --git a/maintain/web/api/updUpload.php b/maintain/web/api/updUpload.php new file mode 100644 index 0000000..abc3483 --- /dev/null +++ b/maintain/web/api/updUpload.php @@ -0,0 +1,42 @@ + \PDO::FETCH_ASSOC, + \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'" + ]); + +$result = array('res' => 0, 'data' => array()); + +$id = empty($_GET['id']) ? 0 : intval($_GET['id']); +$action = empty($_GET['act']) ? '' : $_GET['act']; + +if ($action == 'del') +{ + $values = array('id' => $id); + $sql = "SELECT `id`,`path` FROM mntn_uploads WHERE `id`=:id"; + $stmt = $db->prepare($sql); + $stmt->execute($values); + + $rows = $stmt->fetchAll(); + $stmt = null; + + $sql = "DELETE FROM mntn_uploads WHERE `id`=:id LIMIT 1"; + $stmt = $db->prepare($sql); + + foreach ($rows as $row) + { + $dest = $config['term_logs_root'] . $row['path']; + if (unlink($dest)) + { + $stmt->execute(array('id' => $row['id'])); + $result['data'][] = $row['id']; + } + } + unset($item); +} + +header ('Access-Control-Allow-Origin: *'); +header ('Content-type: application/json; charset=utf-8'); +echo json_encode($result, JSON_UNESCAPED_UNICODE); diff --git a/maintain/web/cfg/config-app.html b/maintain/web/cfg/config-app.html new file mode 100644 index 0000000..5f72059 --- /dev/null +++ b/maintain/web/cfg/config-app.html @@ -0,0 +1,77 @@ +
+ +