diff --git a/EdgeManager.php b/EdgeManager.php index 2d41f32..465a932 100644 --- a/EdgeManager.php +++ b/EdgeManager.php @@ -7,8 +7,7 @@ use Workerman\Protocols\Http\Response; require_once __DIR__ . '/vendor/autoload.php'; use EdgeManager\EDataCapture\{ EDataCapture, ENodeConfigure }; -use EdgeManager\EController\EConfigure; -use EdgeManager\EMonitor\EStatusCapture; +use EdgeManager\EController\{ EConfigure, ECommand }; $options = getopt('h::', ['no_dup_code', 'server_name:', 'port::', 'user:', 'password:', 'help::']); @@ -191,6 +190,19 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { 'errmsg' => 'ROLLBACKed: Bad data received (structure and/or values)' ))); } + } else if ($post -> action === 'exec') { + $task_connection = new AsyncTcpConnection('Text://127.0.0.1:1888'); + $task_data = array( + 'action' => 'exec', + 'data' => $post, + ); + $task_connection -> send(json_encode($task_data)); + $task_connection -> onMessage = function(AsyncTcpConnection $task_connection, $task_result) use ($connection) { + $task_connection -> close(); + $connection -> send($task_result); + }; + // 执行异步连接 + $task_connection->connect(); } else { // 有action,但是不知道是什么鬼动作 $response = new Response(200, [ @@ -311,20 +323,6 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { 'code' => 0, 'data' => $devices ))); - } else if ($get['query'] === 'device_status') { - $e_status_capture = new EStatusCapture($dbconn, get: $get); - $device_status = $e_status_capture -> get_device_status(); - - $connection -> send(json_encode(array( - 'code' => 0, - 'data' => $device_status - ))); - } else if ($get['query'] === 'all_status') { - $all_status = EStatusCapture::get_all_status($dbconn); - $connection -> send(json_encode(array( - 'code' => 0, - 'data' => $all_status - ))); } else { $connection -> send(json_encode(array( 'code' => 1, @@ -391,6 +389,14 @@ $consumer -> onMessage = function(TcpConnection $connection, $task_data) { 'msg' => '服务器内部逻辑错误,请联系开发者!' ))); } + } else if ($task_data -> action === 'exec') { + $e_command = new ECommand($consumer_dbconn, $task_data -> data); + $e_command -> exec(); + + $connection -> send(json_encode(array( + 'code' => 0, + 'msg' => 'Success' + ))); } }; diff --git a/EdgeManager/EController/ECommand.php b/EdgeManager/EController/ECommand.php new file mode 100644 index 0000000..26f2be5 --- /dev/null +++ b/EdgeManager/EController/ECommand.php @@ -0,0 +1,56 @@ + '/Admin/ServerCloseAndRestart', + 'server_close' => '/Admin/ServerClose', + ]; + + if (str_starts_with($this -> post -> command, 'server')) { + $res = pg_query($this -> dbconn, sprintf( + "SELECT url, port + FROM hf_mes_scada_edgeserver_controller_server + WHERE id = '%s'", + $this -> post -> server_id + )); + $server_info = pg_fetch_row($res); + + $ch = curl_init( + $server_info[0] + . ":" + . $server_info[1] + . $command2API[$this -> post -> command] + ); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($ch, CURLOPT_HEADER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt( + $ch, + CURLOPT_USERPWD, + $this -> post -> username + . ":" + . $this -> post -> password + ); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + $return = curl_exec($ch); + curl_close($ch); + } + + pg_insert( + $this -> dbconn, + 'hf_mes_scada_edgeserver_controller_command', + [ + 'server_id' => $this -> post -> server_id, + 'command' => $this -> post -> command, + 'success' => $return + ], + ); + } +} \ No newline at end of file diff --git a/EdgeManager/EController/EConfigure.php b/EdgeManager/EController/EConfigure.php index d65c1ea..cb90dc4 100644 --- a/EdgeManager/EController/EConfigure.php +++ b/EdgeManager/EController/EConfigure.php @@ -128,7 +128,7 @@ class EConfigure { function get_device_list() { $res = pg_query($this -> dbconn, sprintf( - "SELECT id, name, status FROM hf_mes_scada_edgeserver_controller_device + "SELECT id, name FROM hf_mes_scada_edgeserver_controller_device WHERE server_id = '%s'", $this -> get['id'] )); diff --git a/EdgeManager/EMonitor/EStatusCapture.php b/EdgeManager/EMonitor/EStatusCapture.php deleted file mode 100644 index 322493e..0000000 --- a/EdgeManager/EMonitor/EStatusCapture.php +++ /dev/null @@ -1,61 +0,0 @@ - dbconn, sprintf( - "SELECT server.status AS server_status, - device.id, - device.status as device_status - FROM hf_mes_scada_edgeserver_controller_server server, - hf_mes_scada_edgeserver_controller_device device - WHERE server.id = '%s' - AND server.id = server_id", $this -> get['id'] - )); - $res = pg_fetch_all($res); - - $device_status = []; - array_walk($res, function($v, $k) use (&$device_status) { - $device_status[$v['id']] = $v['device_status']; - }); - - return [ - 'server_status' => $res[0]['server_status'], - 'device_status' => $device_status - ]; - } - - static function get_all_status($dbconn) { - $res = pg_query( - "SELECT server_id, - server.name AS server_name, - url, port, - server.status AS server_status, - device.name AS device_name, - conf -> '@DeviceType' AS device_type, - device.status AS device_status, - success_count, failed_count - FROM hf_mes_scada_edgeserver_controller_server server, - hf_mes_scada_edgeserver_controller_device device - WHERE (conf -> '@DeviceType') IS NOT NULL - AND server.id = server_id;" - ); - $all_status = pg_fetch_all($res); - - $server_devices = []; - foreach ($all_status as $device) { - if (!isset($server_devices[$device['server_id']])) - $server_devices[$device['server_id']] = array_slice($device, 0, 5); - $server_devices[$device['server_id']]['devices'][] = array_slice($device, -5); - } - - return $server_devices; - } -} \ No newline at end of file diff --git a/EdgeManager/Init.php b/EdgeManager/Init.php index 8a814d9..330780a 100644 --- a/EdgeManager/Init.php +++ b/EdgeManager/Init.php @@ -28,7 +28,6 @@ function init_db($server_name, $port, $user, $password) { url text NOT NULL, port int2 NOT NULL, address text NOT NULL, - status bool, create_date timestamp NOT NULL DEFAULT NOW(), note text )"); @@ -39,20 +38,15 @@ function init_db($server_name, $port, $user, $password) { server_id serial2 references hf_mes_scada_edgeserver_controller_server(id), conf json, create_date timestamp NOT NULL DEFAULT NOW(), - note text, - status bool, - success_count int8, - failed_count int8, - duration int8 + note text )"); pg_query($dbconn, "CREATE TABLE IF NOT EXISTS hf_mes_scada_edgeserver_controller_command ( id serial8 primary key, server_id serial2 references hf_mes_scada_edgeserver_controller_server(id), device_id text, - node text, command text, create_date timestamp NOT NULL DEFAULT NOW(), - update_status bool + success bool )"); pg_close($dbconn); }