diff --git a/EdgeManager.php b/EdgeManager.php index ccd34a8..d7b174a 100644 --- a/EdgeManager.php +++ b/EdgeManager.php @@ -53,92 +53,89 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { } } - if (isset($post) and is_array($post)) { - if (count($post) !== 0) { - // Axios:发送的POST是array - if (is_array($post) and isset($post['action'])) { - if (str_ends_with($post['action'], 'node')) { - // 用这种方式自动匹配调用和action相对应的方法 - $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' => '服务器内部逻辑错误,请联系开发者!' - ))); - } + 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); + $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 if ($post -> action === 'set_node_data') { + $data_capture = new EDataCapture($dbconn, post: $post); + 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 if (isset($post -> action)) { - if ($post -> action === 'set_node_data') { - $data_capture = new EDataCapture($dbconn, post: $post); - 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', + // 有action,但是不知道是什么鬼动作 + $response = new Response(200, [ + 'Content-Type' => 'application/json;charset=utf-8', + ], json_encode(array( + 'action' => $post -> action, 'errcode' => 4002, - 'errmsg' => 'ROLLBACKed: Bad data received (structure and/or values)' + 'errmsg' => '乜Q action吖?同朕check check佢' ))); + $connection -> send($response); } } else { - // 有action,但是不知道是什么鬼动作 + // action都无 $response = new Response(200, [ 'Content-Type' => 'application/json;charset=utf-8', ], json_encode(array( - 'action' => $post -> action, - 'errcode' => 4002, - 'errmsg' => '同朕check check佢' + 'action' => '缺少action字段', + 'errcode' => 4001, + 'errmsg' => '你请求紧乜撚动作吖?' ))); $connection -> send($response); } - } else { - // action都无 - $response = new Response(200, [ - 'Content-Type' => 'application/json;charset=utf-8', - ], json_encode(array( - 'action' => '缺少action字段', - 'errcode' => 4001, - 'errmsg' => '你请求紧乜撚动作吖?' - ))); - $connection -> send($response); } $get = $request -> get(); @@ -206,7 +203,7 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) { 'msg' => '你请求紧乜撚嘢啊?' ))); } - } + } }; Worker::runAll(); diff --git a/app/EDataCapture/ENodeConfigure.php b/app/EDataCapture/ENodeConfigure.php index a8108e7..5af7b5f 100644 --- a/app/EDataCapture/ENodeConfigure.php +++ b/app/EDataCapture/ENodeConfigure.php @@ -12,13 +12,13 @@ class ENodeConfigure { $exists = pg_query(sprintf( "SELECT EXISTS( SELECT 1 FROM hf_mes_scada_data_capture_node_configure WHERE code='%s' - )", $this -> post['code'] + )", $this -> post -> code )); if (pg_fetch_assoc($exists)['exists'] == 't') { return "REPLICATED"; } - $table_name = "hf_mes_scada_data_capture_node_data_" . $this -> post['working_subclass']; + $table_name = "hf_mes_scada_data_capture_node_data_" . $this -> post -> working_subclass; pg_query($this -> dbconn, "BEGIN"); $res[] = pg_query($this -> dbconn, sprintf( @@ -37,7 +37,7 @@ class ENodeConfigure { $res[] = pg_insert( $this -> dbconn, 'hf_mes_scada_data_capture_node_configure', - $this -> post + (array) $this -> post ); // 检查一下,如果超表已经有了就不要尝试重复创建了 $table_exists = pg_query(sprintf( @@ -69,17 +69,17 @@ class ENodeConfigure { $res[] = pg_delete( $this -> dbconn, 'hf_mes_scada_data_capture_node_configure', - $this -> post, + (array) $this -> post, ); $exists = pg_query(sprintf( "SELECT EXISTS( SELECT 1 FROM hf_mes_scada_data_capture_node_configure WHERE code='%s' - )", $this -> post['working_subclass'] + )", $this -> post -> working_subclass )); if (pg_fetch_assoc($exists)['exists'] == 'f') { $res[] = pg_query(sprintf( "DROP TABLE hf_mes_scada_data_capture_node_data_%s", - $this -> post['working_subclass'] + $this -> post -> working_subclass )); } @@ -96,8 +96,8 @@ class ENodeConfigure { return pg_update( $this -> dbconn, 'hf_mes_scada_data_capture_node_configure', - $this -> post, - ['code' => $this -> post['code']] + (array) $this -> post, + ['code' => $this -> post -> code] ); }