增加端口号参数;对是否允许code在working_subclass间复用设定两种情况
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
function init_db($server_name, $user, $password) {
|
||||
$dbconn = pg_connect(sprintf("host=%s user=%s password=%s", $server_name, $user, $password));
|
||||
function init_db($server_name, $port, $user, $password) {
|
||||
$dbconn = pg_connect(sprintf("host=%s port=%s user=%s password=%s", $server_name, $post, $user, $password));
|
||||
$res = pg_query($dbconn, "select datname from pg_database");
|
||||
|
||||
if (!in_array('scada', pg_fetch_all_columns($res, 0)))
|
||||
pg_query($dbconn, "CREATE DATABASE scada");
|
||||
pg_close($dbconn);
|
||||
|
||||
$dbconn = pg_connect(sprintf("host=%s dbname=scada user=%s password=%s", $server_name, $user, $password));
|
||||
$dbconn = pg_connect(sprintf("host=%s port=%s dbname=scada user=%s password=%s", $server_name, $port, $user, $password));
|
||||
// 全局未启用TSDB的时候在这里对scada数据库启用就好
|
||||
pg_query($dbconn, "CREATE EXTENSION IF NOT EXISTS timescaledb");
|
||||
pg_query($dbconn, "CREATE TABLE IF NOT EXISTS hf_mes_scada_data_capture_node_configure (
|
||||
|
||||
Reference in New Issue
Block a user