150 lines
5.4 KiB
PHP
150 lines
5.4 KiB
PHP
<?php
|
||
use Workerman\Worker;
|
||
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 };
|
||
|
||
$options = getopt('h::', ['server_name:', 'user:', 'password:', 'help::']);
|
||
|
||
init_db($options['server_name'], $options['user'], $options['password']);
|
||
|
||
$worker = new Worker('http://0.0.0.0:8888');
|
||
$worker -> name = 'CaptureWorker';
|
||
|
||
$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']));
|
||
};
|
||
|
||
$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);
|
||
$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 {
|
||
$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',
|
||
'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' => $post -> action,
|
||
'errcode' => 4002,
|
||
'errmsg' => '请检查传入的字段值!'
|
||
)));
|
||
}
|
||
}
|
||
|
||
$get = $request -> get();
|
||
if (isset($get['query']) and $get['query'] == 'nodes') {
|
||
$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 yet'
|
||
)));
|
||
else
|
||
$connection -> send(json_encode(array(
|
||
'code' => 0,
|
||
'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();
|