2022-07-07 01:27:45 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
use Workerman\Worker;
|
2022-08-04 15:27:43 +08:00
|
|
|
|
use \Workerman\Connection\AsyncTcpConnection;
|
2022-07-07 01:27:45 +08:00
|
|
|
|
use Workerman\Connection\TcpConnection;
|
|
|
|
|
|
use Workerman\Protocols\Http\Request;
|
|
|
|
|
|
use Workerman\Protocols\Http\Response;
|
|
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
|
|
use EdgeManager\EDataCapture\{ EDataCapture, ENodeConfigure };
|
|
|
|
|
|
|
2022-08-10 14:56:42 +08:00
|
|
|
|
$options = getopt('h::', ['no_dup_code', 'server_name:', 'port::', 'user:', 'password:', 'help::']);
|
2022-07-07 01:27:45 +08:00
|
|
|
|
|
2022-08-10 14:56:42 +08:00
|
|
|
|
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']);
|
2022-07-07 01:27:45 +08:00
|
|
|
|
|
2022-07-09 14:30:23 +08:00
|
|
|
|
$worker = new Worker('http://0.0.0.0:8888');
|
2022-08-04 15:27:43 +08:00
|
|
|
|
$worker -> name = 'EntryPoint';
|
2022-07-14 00:13:42 +08:00
|
|
|
|
$worker -> count = 20;
|
2022-07-07 01:27:45 +08:00
|
|
|
|
|
|
|
|
|
|
$worker -> onWorkerStart = function(Worker $worker) {
|
|
|
|
|
|
global $options, $dbconn;
|
|
|
|
|
|
|
2022-08-04 15:27:43 +08:00
|
|
|
|
$dbconn = pg_connect(sprintf(
|
2022-08-10 14:56:42 +08:00
|
|
|
|
"host=%s port=%s dbname=scada user=%s password=%s",
|
|
|
|
|
|
$options['server_name'], $options['port'] ?? 5432, $options['user'], $options['password']
|
2022-08-04 15:27:43 +08:00
|
|
|
|
));
|
2022-07-07 01:27:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$worker -> onMessage = function(TcpConnection $connection, Request $request) {
|
|
|
|
|
|
global $options, $dbconn;
|
|
|
|
|
|
|
2022-07-11 23:58:53 +08:00
|
|
|
|
// 先预处理POST内容
|
2022-07-10 03:36:18 +08:00
|
|
|
|
if ($request->header('content-type') === 'application/json') {
|
|
|
|
|
|
$post = $request -> post();
|
2022-07-12 22:36:14 +08:00
|
|
|
|
if (isset($post['action'])) {
|
|
|
|
|
|
$post = json_decode(json_encode($post, JSON_PRESERVE_ZERO_FRACTION));
|
|
|
|
|
|
}
|
2022-07-11 23:58:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
$body = $request -> rawBody();
|
|
|
|
|
|
if ($body === "") {
|
|
|
|
|
|
$response = new Response(200, [
|
|
|
|
|
|
'Content-Type' => 'application/json;charset=utf-8',
|
|
|
|
|
|
], "空请求!");
|
|
|
|
|
|
$connection -> send($response);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$post = json_decode($request -> rawBody());
|
|
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
|
|
$response = new Response(200, [
|
|
|
|
|
|
'Content-Type' => 'application/json;charset=utf-8',
|
|
|
|
|
|
], json_encode(array(
|
2022-08-04 15:27:43 +08:00
|
|
|
|
'action' => '错得太离谱以至于我也不知道这是什么动作',
|
|
|
|
|
|
'errcode' => 4001,
|
|
|
|
|
|
'errmsg' => 'POST的内容不是JSON,也并非可按照JSON解析的字符串!或者检查一下你的JSON是不是有注释?'
|
2022-07-10 03:36:18 +08:00
|
|
|
|
)));
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$connection -> send($response);
|
2022-07-10 03:36:18 +08:00
|
|
|
|
}
|
2022-07-09 14:30:23 +08:00
|
|
|
|
}
|
2022-07-11 23:58:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-12 23:26:06 +08:00
|
|
|
|
if (isset($post) and !(is_array($post) and count($post) === 0)) {
|
|
|
|
|
|
// Axios:发送的POST是array
|
|
|
|
|
|
if (isset($post -> action)) {
|
|
|
|
|
|
if (str_ends_with($post -> action, 'node')) {
|
|
|
|
|
|
// 用这种方式自动匹配调用和action相对应的方法
|
|
|
|
|
|
// 因为pg_insert()不需要action字段
|
|
|
|
|
|
$action = $post -> action;
|
|
|
|
|
|
unset($post -> action);
|
2022-08-04 15:27:43 +08:00
|
|
|
|
|
|
|
|
|
|
if ($action === 'add_node') {
|
|
|
|
|
|
$task_connection = new AsyncTcpConnection('Text://127.0.0.1:1888');
|
|
|
|
|
|
$task_data = array(
|
|
|
|
|
|
'action' => 'add_node',
|
|
|
|
|
|
'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 {
|
2022-08-10 16:04:35 +08:00
|
|
|
|
$enode_configure = new ENodeConfigure($dbconn, no_dup_code: $options['no_dup_code'] ?? true, post: $post);
|
2022-08-04 15:27:43 +08:00
|
|
|
|
$res = $enode_configure -> $action();
|
|
|
|
|
|
|
|
|
|
|
|
if ($res === true)
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 0,
|
|
|
|
|
|
'msg' => 'Success'
|
|
|
|
|
|
)));
|
|
|
|
|
|
else if ($res === false) {
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
|
|
|
|
|
'msg' => '服务器内部逻辑错误,请联系开发者!'
|
|
|
|
|
|
)));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-12 23:26:06 +08:00
|
|
|
|
} else if ($post -> action === 'set_node_data') {
|
2022-08-10 16:04:35 +08:00
|
|
|
|
$data_capture = new EDataCapture($dbconn, no_dup_code: $options['no_dup_code'] ?? true, post: $post);
|
2022-07-12 23:26:06 +08:00
|
|
|
|
if ($data_capture -> check_res === 'WRONG_WORKING_SUBCLASS') {
|
|
|
|
|
|
$response = new Response(200, [
|
|
|
|
|
|
'Content-Type' => 'application/json;charset=utf-8',
|
|
|
|
|
|
], json_encode(array(
|
|
|
|
|
|
'action' => 'result_set_node_data',
|
|
|
|
|
|
'errcode' => 4002,
|
|
|
|
|
|
'errmsg' => '未登记过的工序单元!'
|
|
|
|
|
|
)));
|
|
|
|
|
|
$connection -> send($response);
|
|
|
|
|
|
} else if ($data_capture -> check_res === 'MISMATCH_TYPE') {
|
|
|
|
|
|
$response = new Response(200, [
|
|
|
|
|
|
'Content-Type' => 'application/json;charset=utf-8',
|
|
|
|
|
|
], json_encode(array(
|
|
|
|
|
|
'action' => 'result_set_node_data',
|
|
|
|
|
|
'errcode' => 4002,
|
|
|
|
|
|
'errmsg' => '节点编码和数值类型不匹配!'
|
|
|
|
|
|
)));
|
|
|
|
|
|
$connection -> send($response);
|
|
|
|
|
|
}
|
|
|
|
|
|
$res = $data_capture -> set_node_data();
|
|
|
|
|
|
if ($res === true) {
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'action' => 'result_set_node_data',
|
|
|
|
|
|
'errcode' => 0,
|
|
|
|
|
|
'errmsg' => ''
|
|
|
|
|
|
)));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'action' => 'result_set_node_data',
|
|
|
|
|
|
'errcode' => 4002,
|
|
|
|
|
|
'errmsg' => 'ROLLBACKed: Bad data received (structure and/or values)'
|
|
|
|
|
|
)));
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 有action,但是不知道是什么鬼动作
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$response = new Response(200, [
|
|
|
|
|
|
'Content-Type' => 'application/json;charset=utf-8',
|
|
|
|
|
|
], json_encode(array(
|
2022-07-12 23:26:06 +08:00
|
|
|
|
'action' => $post -> action,
|
2022-07-11 23:58:53 +08:00
|
|
|
|
'errcode' => 4002,
|
2022-07-12 23:26:06 +08:00
|
|
|
|
'errmsg' => '乜Q action吖?同朕check check佢'
|
2022-07-11 23:58:53 +08:00
|
|
|
|
)));
|
|
|
|
|
|
$connection -> send($response);
|
|
|
|
|
|
}
|
2022-07-09 14:30:23 +08:00
|
|
|
|
} else {
|
2022-07-12 23:26:06 +08:00
|
|
|
|
// action都无
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$response = new Response(200, [
|
|
|
|
|
|
'Content-Type' => 'application/json;charset=utf-8',
|
|
|
|
|
|
], json_encode(array(
|
2022-07-12 23:26:06 +08:00
|
|
|
|
'action' => '缺少action字段',
|
|
|
|
|
|
'errcode' => 4001,
|
|
|
|
|
|
'errmsg' => '你请求紧乜撚动作吖?'
|
2022-07-09 14:30:23 +08:00
|
|
|
|
)));
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$connection -> send($response);
|
2022-07-09 14:30:23 +08:00
|
|
|
|
}
|
2022-07-07 01:27:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$get = $request -> get();
|
2022-07-11 23:58:53 +08:00
|
|
|
|
if (isset($get['query'])) {
|
|
|
|
|
|
if ($get['query'] == 'nodes') {
|
2022-08-10 16:04:35 +08:00
|
|
|
|
$enode_configure = new ENodeConfigure($dbconn, no_dup_code: $options['no_dup_code'] ?? true, get: $get);
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$nodes = $enode_configure -> get_nodes();
|
|
|
|
|
|
if (is_null($nodes))
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
|
|
|
|
|
'msg' => 'no node data yet'
|
|
|
|
|
|
)));
|
|
|
|
|
|
else
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 0,
|
|
|
|
|
|
'data' => $nodes
|
|
|
|
|
|
)));
|
|
|
|
|
|
} else if ($get['query'] == 'working_subclasses') {
|
2022-08-10 16:04:35 +08:00
|
|
|
|
$enode_configure = new ENodeConfigure($dbconn, no_dup_code: $options['no_dup_code'] ?? true, get: $get);
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$working_subclasses = $enode_configure -> get_working_subclasses($dbconn);
|
|
|
|
|
|
if (is_null($working_subclasses))
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
|
|
|
|
|
'msg' => '还没有工序单元!'
|
|
|
|
|
|
)));
|
|
|
|
|
|
else
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 0,
|
|
|
|
|
|
'data' => $working_subclasses
|
|
|
|
|
|
)));
|
|
|
|
|
|
} else if ($get['query'] == 'codes') {
|
2022-08-10 16:04:35 +08:00
|
|
|
|
$enode_configure = new ENodeConfigure($dbconn, no_dup_code: $options['no_dup_code'] ?? true, get: $get);
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$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
|
|
|
|
|
|
)));
|
|
|
|
|
|
} else if ($get['query'] == 'node_data') {
|
2022-08-10 16:04:35 +08:00
|
|
|
|
$data_capture = new EDataCapture($dbconn, no_dup_code: $options['no_dup_code'] ?? true, get: $get);
|
2022-07-11 23:58:53 +08:00
|
|
|
|
$data = $data_capture -> get_node_data();
|
|
|
|
|
|
if (is_null($data))
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
|
|
|
|
|
'msg' => '服务器内部逻辑错误,请联系开发者!'
|
|
|
|
|
|
)));
|
2022-07-12 22:36:14 +08:00
|
|
|
|
else if ($data === "TOO_MANY")
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
|
|
|
|
|
'msg' => '结果太多(超过二百万条),请收紧查询条件!'
|
|
|
|
|
|
)));
|
2022-07-11 23:58:53 +08:00
|
|
|
|
else
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 0,
|
|
|
|
|
|
'data' => $data
|
|
|
|
|
|
)));
|
|
|
|
|
|
} else {
|
2022-07-10 03:36:18 +08:00
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
2022-07-11 23:58:53 +08:00
|
|
|
|
'msg' => '你请求紧乜撚嘢啊?'
|
2022-07-10 03:36:18 +08:00
|
|
|
|
)));
|
2022-07-11 23:58:53 +08:00
|
|
|
|
}
|
2022-07-12 23:26:06 +08:00
|
|
|
|
}
|
2022-07-07 01:27:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-08-04 15:27:43 +08:00
|
|
|
|
$consumer = new Worker('Text://0.0.0.0:1888');
|
|
|
|
|
|
$consumer -> name = 'Consumer';
|
|
|
|
|
|
|
|
|
|
|
|
$consumer -> onWorkerStart = function(Worker $consumer) {
|
|
|
|
|
|
global $options, $consumer_dbconn;
|
|
|
|
|
|
|
|
|
|
|
|
$consumer_dbconn = pg_connect(sprintf(
|
2022-08-10 14:56:42 +08:00
|
|
|
|
"host=%s port=%s dbname=scada user=%s password=%s",
|
|
|
|
|
|
$options['server_name'], $options['port'] ?? 5432, $options['user'], $options['password']
|
2022-08-04 15:27:43 +08:00
|
|
|
|
));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$consumer -> onMessage = function(TcpConnection $connection, $task_data) {
|
|
|
|
|
|
global $consumer_dbconn;
|
|
|
|
|
|
|
|
|
|
|
|
$task_data = json_decode($task_data);
|
|
|
|
|
|
|
|
|
|
|
|
if ($task_data -> action === 'add_node') {
|
2022-08-10 16:04:35 +08:00
|
|
|
|
$enode_configure = new ENodeConfigure($consumer_dbconn, no_dup_code: $options['no_dup_code'] ?? true, post: $task_data -> data);
|
2022-08-04 15:27:43 +08:00
|
|
|
|
$res = $enode_configure -> add_node();
|
|
|
|
|
|
|
|
|
|
|
|
if ($res === true)
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 0,
|
|
|
|
|
|
'msg' => 'Success'
|
|
|
|
|
|
)));
|
|
|
|
|
|
else if ($res === "REPLICATED")
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
2022-08-10 14:56:42 +08:00
|
|
|
|
'msg' => isset($options['no_dup_code']) ? '节点编码不可重复!' : '同一工序单元内的节点编码不可重复!'
|
2022-08-04 15:27:43 +08:00
|
|
|
|
)));
|
|
|
|
|
|
else if ($res === false) {
|
|
|
|
|
|
$connection -> send(json_encode(array(
|
|
|
|
|
|
'code' => 1,
|
|
|
|
|
|
'msg' => '服务器内部逻辑错误,请联系开发者!'
|
|
|
|
|
|
)));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-07-07 01:27:45 +08:00
|
|
|
|
Worker::runAll();
|