From 59f5a646c6db1d371e51a4c21e2285d5e21aa7de Mon Sep 17 00:00:00 2001 From: Yu Sun Date: Fri, 12 Aug 2022 13:55:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=9A=84=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E3=80=81=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EdgeManager.php | 79 ++++- EdgeManager/EController/EConfigure.php | 100 +++++++ EdgeManager/Init.php | 17 +- src/api/modules/edgeServer.api.js | 38 +++ src/api/modules/scada.api.js | 8 +- src/api/tools.js | 6 + src/menu/index.js | 7 + src/router/routes.js | 18 ++ .../edgeServer/edgeServerConfigure/index.vue | 283 ++++++++++++++++++ src/views/scada/scadaConfigure/index.vue | 2 +- 10 files changed, 547 insertions(+), 11 deletions(-) create mode 100644 EdgeManager/EController/EConfigure.php create mode 100644 src/api/modules/edgeServer.api.js create mode 100644 src/views/edgeServer/edgeServerConfigure/index.vue diff --git a/EdgeManager.php b/EdgeManager.php index b901a37..3998f27 100644 --- a/EdgeManager.php +++ b/EdgeManager.php @@ -7,6 +7,7 @@ use Workerman\Protocols\Http\Response; require_once __DIR__ . '/vendor/autoload.php'; use EdgeManager\EDataCapture\{ EDataCapture, ENodeConfigure }; +use EdgeManager\EController\EConfigure; $options = getopt('h::', ['no_dup_code', 'server_name:', 'port::', 'user:', 'password:', 'help::']); @@ -108,7 +109,48 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { 'msg' => '服务器内部逻辑错误,请联系开发者!' ))); } - } + } + } else if ( + str_ends_with($post -> action, 'server') + or str_ends_with($post -> action, 'device') + ) { + $action = $post -> action; + unset($post -> action); + + if ($action === 'add_server') { + $task_connection = new AsyncTcpConnection('Text://127.0.0.1:1888'); + $task_data = array( + 'action' => 'add_server', + '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 { + $e_configure = new EConfigure($dbconn, post: $post); + $res = $e_configure -> $action(); + + if ($res === true) + $connection -> send(json_encode(array( + 'code' => 0, + 'msg' => 'Success' + ))); + else if ($res === "REMAINING") { + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '该服务中仍有关联的设备,不允许删除!' + ))); + } else if ($res === false) { + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '服务器内部逻辑错误,请联系开发者!' + ))); + } + } } else if ($post -> action === 'set_node_data') { $data_capture = new EDataCapture($dbconn, no_dup_code: $options['no_dup_code'] ?? true, post: $post); if ($data_capture -> check_res === 'WRONG_WORKING_SUBCLASS') { @@ -176,7 +218,7 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { if (is_null($nodes)) $connection -> send(json_encode(array( 'code' => 1, - 'msg' => 'no node data yet' + 'msg' => '未添加过节点!' ))); else $connection -> send(json_encode(array( @@ -227,6 +269,19 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { 'code' => 0, 'data' => $data ))); + } else if ($get['query'] == 'servers') { + $e_configure = new EConfigure($dbconn, get: $get); + $nodes = $e_configure -> get_servers(); + if (is_null($nodes)) + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '未添加过服务!' + ))); + else + $connection -> send(json_encode(array( + 'code' => 0, + 'data' => $nodes + ))); } else { $connection -> send(json_encode(array( 'code' => 1, @@ -273,6 +328,26 @@ $consumer -> onMessage = function(TcpConnection $connection, $task_data) { 'msg' => '服务器内部逻辑错误,请联系开发者!' ))); } + } else if ($task_data -> action === 'add_server') { + $e_configure = new EConfigure($consumer_dbconn, post: $task_data -> data); + $res = $e_configure -> add_server(); + + if ($res === true) + $connection -> send(json_encode(array( + 'code' => 0, + 'msg' => 'Success' + ))); + else if ($res === "REPLICATED") + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '该地址或名称已注册过服务!' + ))); + else if ($res === false) { + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '服务器内部逻辑错误,请联系开发者!' + ))); + } } }; diff --git a/EdgeManager/EController/EConfigure.php b/EdgeManager/EController/EConfigure.php new file mode 100644 index 0000000..67a3c6e --- /dev/null +++ b/EdgeManager/EController/EConfigure.php @@ -0,0 +1,100 @@ + dbconn, "BEGIN"); + $addr_exists = pg_query($this -> dbconn, sprintf( + "SELECT EXISTS ( + SELECT 1 FROM hf_mes_scada_edgeserver_controller_server + WHERE url = '%s' + AND port = '%s' + )", + $this -> post -> url, + $this -> post -> port + )); + $name_exists = pg_query($this -> dbconn, sprintf( + "SELECT EXISTS ( + SELECT 1 FROM hf_mes_scada_edgeserver_controller_server + WHERE name = '%s' + )", + $this -> post -> name + )); + + if ( + pg_fetch_assoc($addr_exists)['exists'] === 't' + or pg_fetch_assoc($name_exists)['exists'] === 't' + ) { + pg_query($this -> dbconn, "ROLLBACK"); + return "REPLICATED"; + } else { + $res = pg_insert( + $this -> dbconn, + 'hf_mes_scada_edgeserver_controller_server', + (array) $this -> post + ); + if ($res) { + pg_query($this -> dbconn, "COMMIT"); + return true; + } else { + pg_query($this -> dbconn, "ROLLBACK"); + return false; + } + } + } + + function remove_server() { + pg_query($this -> dbconn, "BEGIN"); + $exists = pg_query($this -> dbconn, sprintf( + "SELECT EXISTS( + SELECT 1 FROM hf_mes_scada_edgeserver_controller_device + WHERE server_id = '%s' + )", $this -> post -> id + )); + if (pg_fetch_assoc($exists)['exists'] === 't') { + pg_query($this -> dbconn, "ROLLBACK"); + return "REMAINING"; + } else { + $res = pg_delete( + $this -> dbconn, + 'hf_mes_scada_edgeserver_controller_server', + (array) $this -> post, + ); + if ($res) { + pg_query($this -> dbconn, "COMMIT"); + return true; + } else { + pg_query($this -> dbconn, "ROLLBACK"); + return false; + } + } + } + + function update_server() { + return pg_update( + $this -> dbconn, + 'hf_mes_scada_edgeserver_controller_server', + (array) $this -> post, + ['id' => $this -> post -> id] + ); + } + + function get_servers() { + $res = pg_query($this -> dbconn, "SELECT * FROM hf_mes_scada_edgeserver_controller_server"); + return pg_fetch_all($res); + } + + function add_device() { + return pg_insert( + $this -> dbconn, + 'hf_mes_scada_edgeserver_controller_server', + (array) $this -> post + ); + } +} diff --git a/EdgeManager/Init.php b/EdgeManager/Init.php index 612e6c4..f950c56 100644 --- a/EdgeManager/Init.php +++ b/EdgeManager/Init.php @@ -25,12 +25,25 @@ function init_db($server_name, $port, $user, $password) { id serial2 primary key, code text, name text NOT NULL, - ip text NOT NULL, + url text NOT NULL, port int2 NOT NULL, - sync_addr text NOT NULL, + address text NOT NULL, create_date timestamp NOT NULL DEFAULT NOW(), note text )"); + pg_query($dbconn, "CREATE TABLE IF NOT EXISTS hf_mes_scada_edgeserver_controller_device ( + id serial2 primary key, + code text, + name text NOT NULL, + 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 + )"); 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), diff --git a/src/api/modules/edgeServer.api.js b/src/api/modules/edgeServer.api.js new file mode 100644 index 0000000..4b0626b --- /dev/null +++ b/src/api/modules/edgeServer.api.js @@ -0,0 +1,38 @@ +import { handlePost } from '../tools' + +export default ({ service, request, serviceForMock, requestForMock, mock, faker, tools }) => ({ + /** + * @description 方法名称 + * @param {Object} data 请求携带的信息 + */ + VERIFY_SERVER (url, username, password) { + return request({ + auth: { + username: username, + password: password + }, + method: 'post', + url: url + '/Admin/ServerSettingsRequest' + }) + }, + + MODIFY_SERVER (url, username, password, data) { + return request({ + auth: { + username: username, + password: password + }, + data: data, + method: 'post', + url: url + '/Admin/ServerSettingsModify' + }) + }, + + ADD_SERVER: (data) => handlePost(request, data), + UPDATE_SERVER: (data) => handlePost(request, data), + REMOVE_SERVER: (data) => handlePost(request, data), + + QUERY_SERVERS () { + return request({ url: '?query=servers' }) + } +}) diff --git a/src/api/modules/scada.api.js b/src/api/modules/scada.api.js index 6fc3cc3..e908027 100644 --- a/src/api/modules/scada.api.js +++ b/src/api/modules/scada.api.js @@ -1,8 +1,4 @@ -const handlePost = (request, data) => (request({ - url: '', - method: 'post', - data -})) +import { handlePost } from '../tools' export default ({ service, request, serviceForMock, requestForMock, mock, faker, tools }) => ({ /** @@ -14,7 +10,7 @@ export default ({ service, request, serviceForMock, requestForMock, mock, faker, UPDATE_NODE: (data) => handlePost(request, data), REMOVE_NODE: (data) => handlePost(request, data), - QUERY_NODE () { + QUERY_NODES () { return request({ url: '?query=nodes' }) }, diff --git a/src/api/tools.js b/src/api/tools.js index 990e840..4e39616 100644 --- a/src/api/tools.js +++ b/src/api/tools.js @@ -84,3 +84,9 @@ export function errorCreate (msg) { errorLog(error) throw error } + +export const handlePost = (request, data) => (request({ + url: '', + method: 'post', + data +})) diff --git a/src/menu/index.js b/src/menu/index.js index aa40f68..a009e10 100644 --- a/src/menu/index.js +++ b/src/menu/index.js @@ -23,5 +23,12 @@ export const menuAside = supplementPath([ { path: '/scada_configure', title: 'SCADA节点配置' }, { path: '/scada_query', title: 'SCADA数据查询' } ] + }, + { + title: '采集服务管理', + children: [ + { path: '/edge_server_configure', title: '服务配置' }, + { path: '/edge_server_monitor', title: '服务监控' } + ] } ]) diff --git a/src/router/routes.js b/src/router/routes.js index e60ab13..1f48169 100644 --- a/src/router/routes.js +++ b/src/router/routes.js @@ -39,6 +39,24 @@ const frameIn = [ }, component: _import('scada/scadaQuery') }, + { + path: 'edge_server_configure', + name: 'edge_server_configure', + meta: { + title: '服务配置', + auth: true + }, + component: _import('edgeServer/edgeServerConfigure') + }, + { + path: 'edge_server_monitor', + name: 'edge_server_monitor', + meta: { + title: '服务监控', + auth: true + }, + component: _import('edgeServer/edgeServerMonitor') + }, // 系统 前端日志 { path: 'log', diff --git a/src/views/edgeServer/edgeServerConfigure/index.vue b/src/views/edgeServer/edgeServerConfigure/index.vue new file mode 100644 index 0000000..2a00c17 --- /dev/null +++ b/src/views/edgeServer/edgeServerConfigure/index.vue @@ -0,0 +1,283 @@ + + + diff --git a/src/views/scada/scadaConfigure/index.vue b/src/views/scada/scadaConfigure/index.vue index e16ebab..fab3746 100644 --- a/src/views/scada/scadaConfigure/index.vue +++ b/src/views/scada/scadaConfigure/index.vue @@ -193,7 +193,7 @@ export default { methods: { async getNodes () { try { - const res = await this.$api.QUERY_NODE() + const res = await this.$api.QUERY_NODES() this.data = each(res, (o) => ( assign(o, { showEditButton: true,