remove monitors;

add command for restart/close server
This commit is contained in:
Yu Sun
2022-08-16 00:41:31 +08:00
parent 6f28d923d2
commit 0ed5eddd9a
5 changed files with 81 additions and 86 deletions

View File

@@ -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'
)));
}
};

View File

@@ -0,0 +1,56 @@
<?php
namespace EdgeManager\EController;
class ECommand {
function __construct(
protected $dbconn,
protected $post = NULL,
) {}
function exec() {
$command2API = [
'server_restart' => '/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
],
);
}
}

View File

@@ -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']
));

View File

@@ -1,61 +0,0 @@
<?php
namespace EdgeManager\EMonitor;
Class EStatusCapture {
function __construct(
protected $dbconn,
protected $post = NULL,
protected $get = NULL,
) {}
function get_device_status() {
$res = pg_query($this -> 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;
}
}

View File

@@ -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);
}