finish all except ECommands;

handle GET content ahead
This commit is contained in:
Yu Sun
2022-08-13 21:47:41 +08:00
parent 94d66a7919
commit 2d5e542593
4 changed files with 120 additions and 12 deletions

View File

@@ -8,6 +8,7 @@ require_once __DIR__ . '/vendor/autoload.php';
use EdgeManager\EDataCapture\{ EDataCapture, ENodeConfigure };
use EdgeManager\EController\EConfigure;
use EdgeManager\EMonitor\EStatusCapture;
$options = getopt('h::', ['no_dup_code', 'server_name:', 'port::', 'user:', 'password:', 'help::']);
@@ -51,12 +52,15 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
$post = json_decode(json_encode($post, JSON_PRESERVE_ZERO_FRACTION));
}
} else {
$get = $request -> get();
$body = $request -> rawBody();
if ($body === "") {
$response = new Response(200, [
'Content-Type' => 'application/json;charset=utf-8',
], "空请求!");
$connection -> send($response);
if (count($get) === 0) {
$response = new Response(200, [
'Content-Type' => 'application/json;charset=utf-8',
], "空请求!");
$connection -> send($response);
}
} else {
$post = json_decode($request -> rawBody());
if (json_last_error() !== JSON_ERROR_NONE) {
@@ -210,7 +214,6 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
}
}
$get = $request -> get();
if (isset($get['query'])) {
if ($get['query'] === 'nodes') {
$enode_configure = new ENodeConfigure($dbconn, no_dup_code: $options['no_dup_code'] ?? true, get: $get);
@@ -270,8 +273,7 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
'data' => $data
)));
} else if ($get['query'] === 'servers') {
$e_configure = new EConfigure($dbconn, get: $get);
$servers = $e_configure -> get_servers();
$servers = EConfigure::get_servers($dbconn);
if (is_null($servers))
$connection -> send(json_encode(array(
'code' => 1,
@@ -282,9 +284,22 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
'code' => 0,
'data' => $servers
)));
} else if ($get['query'] === 'devices') {
} else if ($get['query'] === 'device') {
$e_configure = new EConfigure($dbconn, get: $get);
$devices = $e_configure -> get_devices();
$device = $e_configure -> get_device();
if (is_null($device))
$connection -> send(json_encode(array(
'code' => 1,
'msg' => '未添加过设备!'
)));
else
$connection -> send(json_encode(array(
'code' => 0,
'data' => $device
)));
} else if ($get['query'] === 'device_list') {
$e_configure = new EConfigure($dbconn, get: $get);
$devices = $e_configure -> get_device_list();
if (is_null($devices))
$connection -> send(json_encode(array(
'code' => 1,
@@ -295,6 +310,20 @@ $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,

View File

@@ -85,8 +85,8 @@ class EConfigure {
);
}
function get_servers() {
$res = pg_query($this -> dbconn, "SELECT * FROM hf_mes_scada_edgeserver_controller_server");
static function get_servers($dbconn) {
$res = pg_query($dbconn, "SELECT * FROM hf_mes_scada_edgeserver_controller_server");
return pg_fetch_all($res);
}
@@ -108,9 +108,27 @@ class EConfigure {
);
}
function get_devices() {
function update_device () {
return pg_update(
$this -> dbconn,
'hf_mes_scada_edgeserver_controller_device',
(array) $this -> post,
['id' => $this -> post -> id]
);
}
function get_device() {
$res = pg_query($this -> dbconn, sprintf(
"SELECT * FROM hf_mes_scada_edgeserver_controller_device
WHERE id = '%s'",
$this -> get['id']
));
return pg_fetch_all($res);
}
function get_device_list() {
$res = pg_query($this -> dbconn, sprintf(
"SELECT id, name, status FROM hf_mes_scada_edgeserver_controller_device
WHERE server_id = '%s'",
$this -> get['id']
));

View File

@@ -0,0 +1,60 @@
<?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) {
$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,6 +28,7 @@ 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
)");