基本完成前后端代码

This commit is contained in:
Yu Sun
2022-07-10 03:36:18 +08:00
parent 9fb0906b09
commit dcc4780d46
7 changed files with 287 additions and 52 deletions

View File

@@ -24,11 +24,13 @@ $worker -> onWorkerStart = function(Worker $worker) {
$worker -> onMessage = function(TcpConnection $connection, Request $request) {
global $options, $dbconn;
// 仅供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);
$enode_configure = new ENodeConfigure($dbconn, post: $post);
$res = $enode_configure -> $action();
if ($res === true)
$connection -> send(json_encode(array(
@@ -46,11 +48,20 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
'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();
}
} else {
$post = json_decode($request -> rawBody());
if (json_last_error() !== JSON_ERROR_NONE)
dump('fuck');
$connection -> send(json_encode(array(
'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',
@@ -64,16 +75,23 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
'errmsg' => 'ROLLBACKed: Bad data received (structure and/or values)'
)));
}
} else {
$connection -> send(json_encode(array(
'action' => $post -> action,
'errcode' => 4002,
'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();

View File

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

View File

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

View File

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

View File

@@ -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,

View File

@@ -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

View File

@@ -0,0 +1,115 @@
<template>
<d2-container>
<el-form :inline="true" :model="formInline" class="demo-form-inline">
<el-form-item label="工序单元">
<el-select
v-model="formInline.workingSubclass"
filterable
clearable
placeholder="工序单元"
@change="getCodesByWorkingSubclass(formInline.workingSubclass)">
<el-option
v-for="workingSubclass in workingSubclasses"
:key="workingSubclass"
:value="workingSubclass">
</el-option>
</el-select>
</el-form-item>
<el-form-item label="节点编码">
<el-select
v-model="formInline.code"
filterable
clearable
placeholder="节点编码">
<el-option
v-for="code in codes"
:key="code"
:value="code">
</el-option>
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="getData">查询</el-button>
</el-form-item>
</el-form>
<c-grid
:data="data">
<c-grid-column
field="id"
width="5%">
序号
</c-grid-column>
<c-grid-column
field="name"
width="16.25%">
节点名称
</c-grid-column>
<c-grid-column
field="value"
width="16.25%"
sort="true">
</c-grid-column>
<c-grid-column
field="device_code"
width="16.25%"
sort="true">
设备
</c-grid-column>
<c-grid-column
field="batch"
width="16.25%"
sort="true">
批次
</c-grid-column>
<c-grid-column
field="capture_time"
width="30%"
sort="true">
采集时间
</c-grid-column>
</c-grid>
</d2-container>
</template>
<script>
export default {
data () {
return {
formInline: {
workingSubclass: '',
code: ''
},
workingSubclasses: [],
codes: [],
data: []
}
},
methods: {
async getworkingSubclasses () {
try {
this.workingSubclasses = await this.$api.QUERY_WORKING_SUBCLASSES()
} catch (e) {
console.log(e)
}
},
async getCodesByWorkingSubclass (workingSubclass) {
try {
this.codes = await this.$api.QUERY_CODES(workingSubclass)
} catch (e) {
console.log(e)
}
},
async getData () {
try {
this.data = await this.$api.QUERY_NODE_DATA(this.formInline.workingSubclass, this.formInline.code)
} catch (e) {
console.log(e)
}
}
},
mounted () {
this.getworkingSubclasses()
}
}
</script>