This commit is contained in:
wu
2022-08-20 00:11:33 +08:00
4 changed files with 42 additions and 57 deletions

View File

@@ -323,12 +323,6 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
'code' => 0,
'data' => $devices
)));
} else if ($get['query'] === 'all_devices') {
$all_devices = EConfigure::get_all_devices($dbconn);
$connection -> send(json_encode(array(
'code' => 0,
'data' => $all_devices
)));
} else {
$connection -> send(json_encode(array(
'code' => 1,

View File

@@ -11,44 +11,58 @@ class ECommand {
$command2API = [
'server_restart' => '/Admin/ServerCloseAndRestart',
'server_close' => '/Admin/ServerClose',
'device_stop' => '/Edge/DeviceStopRequest',
'device_continue' => '/Edge/DeviceContinueRequest'
];
$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);
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
} else if (str_starts_with($this -> post -> command, 'device')) {
$ch = curl_init(
$server_info[0]
. ":"
. $this -> post -> password
. $server_info[1]
. $command2API[$this -> post -> command]
. '?data='
. $this -> post -> device_name
);
} else {
return;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 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);
if ($return) {
pg_update(
$this -> dbconn,
"hf_mes_scada_edgeserver_controller_server",
['updated' => false],
['id' => $this -> post -> server_id]
);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$return = curl_exec($ch);
curl_close($ch);
if ($return) {
pg_update(
$this -> dbconn,
"hf_mes_scada_edgeserver_controller_server",
['updated' => false],
['id' => $this -> post -> server_id]
);
}
}
pg_insert(
@@ -56,6 +70,7 @@ class ECommand {
'hf_mes_scada_edgeserver_controller_command',
[
'server_id' => $this -> post -> server_id,
'device_name' => $this -> post -> server_id ?? NULL,
'command' => $this -> post -> command,
'success' => $return
],

View File

@@ -162,28 +162,4 @@ class EConfigure {
));
return pg_fetch_all($res);
}
static function get_all_devices() {
$res = pg_query(
"SELECT server_id,
server.name AS server_name,
url, port,
device.name AS device_name,
conf -> '@DeviceType' AS device_type
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;"
);
$server_devices = pg_fetch_all($res);
$all_devices = [];
foreach ($server_devices as $device) {
if (!isset($all_devices[$device['server_id']]))
$all_devices[$device['server_id']] = array_slice($device, 0, 4);
$all_devices[$device['server_id']]['devices'][] = array_slice($device, -2);
}
return $all_devices;
}
}

View File

@@ -44,7 +44,7 @@ function init_db($server_name, $port, $user, $password) {
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,
device_name text,
command text,
create_date timestamp NOT NULL DEFAULT NOW(),
success bool