From 54eae084729ccdba52473c48fba97c61897d025d Mon Sep 17 00:00:00 2001 From: Yu Sun Date: Wed, 10 Aug 2022 14:56:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=AB=AF=E5=8F=A3=E5=8F=B7?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=9B=E5=AF=B9=E6=98=AF=E5=90=A6=E5=85=81?= =?UTF-8?q?=E8=AE=B8code=E5=9C=A8working=5Fsubclass=E9=97=B4=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E8=AE=BE=E5=AE=9A=E4=B8=A4=E7=A7=8D=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EdgeManager.php | 33 +++++++++++++++------ EdgeManager/EDataCapture/EDataCapture.php | 22 +++++++++----- EdgeManager/EDataCapture/ENodeConfigure.php | 27 ++++++++++++----- EdgeManager/Init.php | 6 ++-- 4 files changed, 61 insertions(+), 27 deletions(-) diff --git a/EdgeManager.php b/EdgeManager.php index 8e90634..1c4b4b9 100644 --- a/EdgeManager.php +++ b/EdgeManager.php @@ -8,9 +8,24 @@ require_once __DIR__ . '/vendor/autoload.php'; use EdgeManager\EDataCapture\{ EDataCapture, ENodeConfigure }; -$options = getopt('h::', ['server_name:', 'user:', 'password:', 'help::']); +$options = getopt('h::', ['no_dup_code', 'server_name:', 'port::', 'user:', 'password:', 'help::']); -init_db($options['server_name'], $options['user'], $options['password']); +if (array_key_exists('h', $options) or array_key_exists('help', $options)) { + print_r( + "EdgeManager使用说明: + + --no_dup_code 禁止code在不同的working subclass间复用 + --server_name pg实例的FQDN + --user pg实例的用户名 + --port pg实例的端口号 + --password pg实例的密码 + -h, --help 显示此帮助信息 +" + ); + exit; +} + +init_db($options['server_name'], $options['port'] ?? 5432, $options['user'], $options['password']); $worker = new Worker('http://0.0.0.0:8888'); $worker -> name = 'EntryPoint'; @@ -20,8 +35,8 @@ $worker -> onWorkerStart = function(Worker $worker) { global $options, $dbconn; $dbconn = pg_connect(sprintf( - "host=%s dbname=scada user=%s password=%s", - $options['server_name'], $options['user'], $options['password'] + "host=%s port=%s dbname=scada user=%s password=%s", + $options['server_name'], $options['port'] ?? 5432, $options['user'], $options['password'] )); }; @@ -95,7 +110,7 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { } } } else if ($post -> action === 'set_node_data') { - $data_capture = new EDataCapture($dbconn, post: $post); + $data_capture = new EDataCapture($dbconn, post: $post, no_dup_code: $options['no_dup_code'] ?? true); if ($data_capture -> check_res === 'WRONG_WORKING_SUBCLASS') { $response = new Response(200, [ 'Content-Type' => 'application/json;charset=utf-8', @@ -228,8 +243,8 @@ $consumer -> onWorkerStart = function(Worker $consumer) { global $options, $consumer_dbconn; $consumer_dbconn = pg_connect(sprintf( - "host=%s dbname=scada user=%s password=%s", - $options['server_name'], $options['user'], $options['password'] + "host=%s port=%s dbname=scada user=%s password=%s", + $options['server_name'], $options['port'] ?? 5432, $options['user'], $options['password'] )); }; @@ -239,7 +254,7 @@ $consumer -> onMessage = function(TcpConnection $connection, $task_data) { $task_data = json_decode($task_data); if ($task_data -> action === 'add_node') { - $enode_configure = new ENodeConfigure($consumer_dbconn, post: $task_data -> data); + $enode_configure = new ENodeConfigure($consumer_dbconn, post: $task_data -> data, no_dup_code: $options['no_dup_code'] ?? true); $res = $enode_configure -> add_node(); if ($res === true) @@ -250,7 +265,7 @@ $consumer -> onMessage = function(TcpConnection $connection, $task_data) { else if ($res === "REPLICATED") $connection -> send(json_encode(array( 'code' => 1, - 'msg' => '同一工序单元内的节点编码不可重复!' + 'msg' => isset($options['no_dup_code']) ? '节点编码不可重复!' : '同一工序单元内的节点编码不可重复!' ))); else if ($res === false) { $connection -> send(json_encode(array( diff --git a/EdgeManager/EDataCapture/EDataCapture.php b/EdgeManager/EDataCapture/EDataCapture.php index b9b86d8..3916b47 100644 --- a/EdgeManager/EDataCapture/EDataCapture.php +++ b/EdgeManager/EDataCapture/EDataCapture.php @@ -9,7 +9,8 @@ class EDataCapture { public $check_res = NULL, protected $working_subclass = NULL, protected $code_type = [], - protected $data = [] + protected $data = [], + protected $no_dup_code ) { if (!is_null($this -> post)) { if (!in_array( @@ -22,12 +23,19 @@ class EDataCapture { $this -> working_subclass = $this -> post -> param -> working_subclass; } - $res = pg_fetch_all(pg_query($this -> dbconn, sprintf( - "SELECT code, type - FROM hf_mes_scada_data_capture_node_configure - WHERE working_subclass = '%s'", - $this -> post -> param -> working_subclass - ))); + if ($this -> no_dup_code) { + $res = pg_fetch_all(pg_query($this -> dbconn, + "SELECT code, type + FROM hf_mes_scada_data_capture_node_configure" + )); + } else { + $res = pg_fetch_all(pg_query($this -> dbconn, sprintf( + "SELECT code, type + FROM hf_mes_scada_data_capture_node_configure + WHERE working_subclass = '%s'", + $this -> post -> param -> working_subclass + ))); + } $code_type = &$this -> code_type; array_walk($res, function(&$v, $k) use (&$code_type) { diff --git a/EdgeManager/EDataCapture/ENodeConfigure.php b/EdgeManager/EDataCapture/ENodeConfigure.php index 687d7d6..ce3459a 100644 --- a/EdgeManager/EDataCapture/ENodeConfigure.php +++ b/EdgeManager/EDataCapture/ENodeConfigure.php @@ -5,18 +5,29 @@ class ENodeConfigure { function __construct( protected $dbconn, protected $post = NULL, - protected $get = NULL + protected $get = NULL, + protected $no_dup_code ) {} function add_node() { pg_query($this -> dbconn, "BEGIN"); - $exists = pg_query($this -> dbconn, sprintf( - "SELECT EXISTS( - SELECT 1 FROM hf_mes_scada_data_capture_node_configure - WHERE code = '%s' - AND working_subclass = '%s' - )", $this -> post -> code, $this -> post -> working_subclass - )); + if ($this -> no_dup_code) { + $exists = pg_query($this -> dbconn, sprintf( + "SELECT EXISTS( + SELECT 1 FROM hf_mes_scada_data_capture_node_configure + WHERE code = '%s' + )", $this -> post -> code + )); + } else { + $exists = pg_query($this -> dbconn, sprintf( + "SELECT EXISTS( + SELECT 1 FROM hf_mes_scada_data_capture_node_configure + WHERE code = '%s' + AND working_subclass = '%s' + )", $this -> post -> code, $this -> post -> working_subclass + )); + } + if (pg_fetch_assoc($exists)['exists'] === 't') { pg_query($this -> dbconn, "ROLLBACK"); return "REPLICATED"; diff --git a/EdgeManager/Init.php b/EdgeManager/Init.php index baeb86d..728aa71 100644 --- a/EdgeManager/Init.php +++ b/EdgeManager/Init.php @@ -1,13 +1,13 @@