From dcc4780d467624ff86aecf929c286f4ab4b6be72 Mon Sep 17 00:00:00 2001 From: Yu Sun Date: Sun, 10 Jul 2022 03:36:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E5=89=8D?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EdgeManager.php | 131 +++++++++++++++++------ app/EDataCapture/EDataCapture.php | 51 ++++++--- app/EDataCapture/ENodeConfigure.php | 15 +++ src/api/modules/scada.configure.api.js | 12 +++ src/main.js | 3 + src/views/scada/scadaConfigure/index.vue | 12 +-- src/views/scada/scadaQuery/index.vue | 115 ++++++++++++++++++++ 7 files changed, 287 insertions(+), 52 deletions(-) diff --git a/EdgeManager.php b/EdgeManager.php index 0a92e8a..9e4201f 100644 --- a/EdgeManager.php +++ b/EdgeManager.php @@ -24,56 +24,74 @@ $worker -> onWorkerStart = function(Worker $worker) { $worker -> onMessage = function(TcpConnection $connection, Request $request) { global $options, $dbconn; - $post = $request -> post(); - if (isset($post['action']) and str_contains($post['action'], 'node')) { - $action = $post['action']; - unset($post['action']); - $enode_configure = new ENodeConfigure($dbconn, $post = $post); - $res = $enode_configure -> $action(); - 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' => '服务器内部逻辑错误,请联系开发者!' - ))); + // 仅供Axios使用 + if ($request->header('content-type') === 'application/json') { + $post = $request -> post(); + if (isset($post['action']) and str_contains($post['action'], 'node')) { + $action = $post['action']; + unset($post['action']); + $enode_configure = new ENodeConfigure($dbconn, post: $post); + $res = $enode_configure -> $action(); + 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' => '服务器内部逻辑错误,请联系开发者!' + ))); + } } - } else if (isset($post['action']) and str_contains($post['action'], 'data')) { - $action = $post['action']; - unset($post['action']); - $data_capture = new EDataCapture($dbconn, $post = $post); - $res = $data_capture -> $action(); - if ($res === true) { + } else { + $post = json_decode($request -> rawBody()); + if (json_last_error() !== JSON_ERROR_NONE) + dump('fuck'); $connection -> send(json_encode(array( - 'action' => 'result_set_data', - 'errcode' => 0, - 'errmsg' => '' + 'action' => '错得太离谱以至于我也不知道这是什么动作', + 'errcode' => 4001, + 'errmsg' => 'POST的内容不是JSON,也并非可按照JSON解析的字符串!' ))); + dump($post); + if ($post -> action === 'set_node_data') { + $data_capture = new EDataCapture($dbconn, post: $post); + $res = $data_capture -> set_node_data(); + if ($res === true) { + $connection -> send(json_encode(array( + 'action' => 'result_set_data', + 'errcode' => 0, + 'errmsg' => '' + ))); + } else { + $connection -> send(json_encode(array( + 'action' => 'result_set_data', + 'errcode' => 4002, + 'errmsg' => 'ROLLBACKed: Bad data received (structure and/or values)' + ))); + } } else { $connection -> send(json_encode(array( - 'action' => 'result_set_data', + 'action' => $post -> action, 'errcode' => 4002, - 'errmsg' => 'ROLLBACKed: Bad data received (structure and/or values)' + 'errmsg' => '请检查传入的字段值!' ))); } } $get = $request -> get(); if (isset($get['query']) and $get['query'] == 'nodes') { - $enode_configure = new ENodeConfigure($dbconn, $get = $get); + $enode_configure = new ENodeConfigure($dbconn, get: $get); $nodes = $enode_configure -> get_nodes(); if (is_null($nodes)) $connection -> send(json_encode(array( 'code' => 1, - 'msg' => 'no node data' + 'msg' => 'no node data yet' ))); else $connection -> send(json_encode(array( @@ -81,6 +99,51 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { 'data' => $nodes ))); } + + if (isset($get['query']) and $get['query'] == 'working_subclasses') { + $enode_configure = new ENodeConfigure($dbconn, get: $get); + $working_subclasses = $enode_configure -> get_working_subclasses(); + if (is_null($working_subclasses)) + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '还没有工序单元!' + ))); + else + $connection -> send(json_encode(array( + 'code' => 0, + 'data' => $working_subclasses + ))); + } + + if (isset($get['query']) and $get['query'] == 'codes') { + $enode_configure = new ENodeConfigure($dbconn, get: $get); + $codes = $enode_configure -> get_codes_by_working_subclasses(); + if (is_null($codes)) + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '服务器内部逻辑错误,请联系开发者!' + ))); + else + $connection -> send(json_encode(array( + 'code' => 0, + 'data' => $codes + ))); + } + + if (isset($get['query']) and $get['query'] == 'node_data') { + $data_capture = new EDataCapture($dbconn, get: $get); + $data = $data_capture -> get_node_data(); + if (is_null($data)) + $connection -> send(json_encode(array( + 'code' => 1, + 'msg' => '服务器内部逻辑错误,请联系开发者!' + ))); + else + $connection -> send(json_encode(array( + 'code' => 0, + 'data' => $data + ))); + } }; Worker::runAll(); diff --git a/app/EDataCapture/EDataCapture.php b/app/EDataCapture/EDataCapture.php index e16f52b..b4bc288 100644 --- a/app/EDataCapture/EDataCapture.php +++ b/app/EDataCapture/EDataCapture.php @@ -10,23 +10,50 @@ class EDataCapture { protected $get = NULL ) {} - private function set_data() { - foreach (array_chunk($this -> post -> param, 6507524, true) as $chunk) { - $sql_cmd[] = [sprintf( - "INSERT INTO hf_mes_scada_data_capture_node_data_%s", - $chunk[0] -> working_subclass - )]; + function set_node_data() { + foreach (array_chunk($this -> post -> param -> data, 6507524, true) as $chunk) { + $sql_head = sprintf( + "INSERT INTO hf_mes_scada_data_capture_node_data_%s (code, v_%s, device_code, batch) + VALUES", + $this -> post -> param -> working_subclass, + $this -> post -> param -> type + ); foreach ($chunk as $row) { - $sql_cmd[] = sprintf( - "(code, v_%s, device_code, batch) VALUES('%s', %s, %s, %s)", - $row -> type, + $sql_values[] = sprintf( + "('%s', %s, %s, %s)", $row -> code, $row -> value, - $row -> device_code ?? NULL, - $row -> batch ?? NULL + $row -> device_code ?? 'DEFAULT', + $row -> batch ?? 'DEFAULT' ); } - return pg_query($this -> dbconn, implode(' ', $sql_cmd)); + return pg_query($this -> dbconn, $sql_head . implode(',', $sql_values)); } } + + function get_node_data() { + $name_type = pg_fetch_assoc(pg_query($this -> dbconn, sprintf( + "SELECT name, type + FROM hf_mes_scada_data_capture_node_configure + WHERE working_subclass = '%s' + AND code = '%s'", + $this -> get['working_subclass'], + $this -> get['code'] + ))); + + $res = pg_fetch_all(pg_query($this -> dbconn, sprintf( + "SELECT id, v_%s AS value, device_code, batch, capture_time + FROM hf_mes_scada_data_capture_node_data_%s + WHERE code = '%s'", + $name_type['type'], + $this -> get['working_subclass'], + $this -> get['code'] + ))); + + array_walk($res, function(&$v, $k) use ($name_type) { + $v['name'] = $name_type['name']; + }); + + return $res; + } } diff --git a/app/EDataCapture/ENodeConfigure.php b/app/EDataCapture/ENodeConfigure.php index 7bf157d..8ae4b47 100644 --- a/app/EDataCapture/ENodeConfigure.php +++ b/app/EDataCapture/ENodeConfigure.php @@ -105,4 +105,19 @@ class ENodeConfigure { $res = pg_query($this -> dbconn, "SELECT * FROM hf_mes_scada_data_capture_node_configure"); return pg_fetch_all($res); } + + function get_working_subclasses() { + $res = pg_query($this -> dbconn, "SELECT DISTINCT ON (working_subclass) working_subclass FROM hf_mes_scada_data_capture_node_configure"); + return pg_fetch_all_columns($res, 0); + } + + function get_codes_by_working_subclasses() { + $res = pg_query($this -> dbconn, sprintf( + "SELECT code + FROM hf_mes_scada_data_capture_node_configure + WHERE working_subclass = '%s'", + $this -> get['working_subclass'] + )); + return pg_fetch_all_columns($res, 0); + } } diff --git a/src/api/modules/scada.configure.api.js b/src/api/modules/scada.configure.api.js index d73bf78..a3e44c1 100644 --- a/src/api/modules/scada.configure.api.js +++ b/src/api/modules/scada.configure.api.js @@ -16,5 +16,17 @@ export default ({ service, request, serviceForMock, requestForMock, mock, faker, QUERY_NODE () { return request({ url: '?query=nodes' }) + }, + + QUERY_WORKING_SUBCLASSES () { + return request({ url: '?query=working_subclasses' }) + }, + + QUERY_CODES (workingSubclass) { + return request({ url: '?query=codes&working_subclass=' + workingSubclass }) + }, + + QUERY_NODE_DATA (workingSubclass, code) { + return request({ url: '?query=node_data&working_subclass=' + workingSubclass + '&code=' + code }) } }) diff --git a/src/main.js b/src/main.js index d622f24..034a1b7 100644 --- a/src/main.js +++ b/src/main.js @@ -10,6 +10,8 @@ import store from '@/store/index' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import D2Crud from '@d2-projects/d2-crud' +// Cheetah-Grid +import vueCheetahGrid from 'vue-cheetah-grid' // 菜单和路由设置 import router from './router' @@ -20,6 +22,7 @@ import { frameInRoutes } from '@/router/routes' Vue.use(d2Admin) Vue.use(ElementUI) Vue.use(D2Crud) +Vue.use(vueCheetahGrid) new Vue({ router, diff --git a/src/views/scada/scadaConfigure/index.vue b/src/views/scada/scadaConfigure/index.vue index 644289a..f5bfd6a 100644 --- a/src/views/scada/scadaConfigure/index.vue +++ b/src/views/scada/scadaConfigure/index.vue @@ -105,20 +105,20 @@ export default { name: 'el-select', options: [ { - value: 'text', - label: '字符串' + value: 'string', + label: 'string (字符串)' }, { value: 'int', - label: '整数' + label: 'int (整数)' }, { - value: 'float8', - label: '浮点数' + value: 'float', + label: 'float (浮点数)' }, { value: 'bool', - label: '逻辑值' + label: 'bool (逻辑值)' } ], span: 12 diff --git a/src/views/scada/scadaQuery/index.vue b/src/views/scada/scadaQuery/index.vue index e69de29..43d6242 100644 --- a/src/views/scada/scadaQuery/index.vue +++ b/src/views/scada/scadaQuery/index.vue @@ -0,0 +1,115 @@ + + +