SCTMES_V5/mes_in_sct/app/action/SetDeviceProcess.php
2025-06-14 18:55:09 +08:00

109 lines
3.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\action;
use Exception;
use Throwable;
use stdClass;
use libs\db\Db;
use Workerman\Connection\AsyncTcpConnection;
class SetDeviceProcess
{
/*
{
"action": "set_node_data",
"param": {
"working_subclass":"ROUGHING",
"data": [
{
"code": "temperature",
"value": 1250.4,
"device_code": "",
"batch": ""
},
{
"code": "width",
"value": 29.45,
"device_code": "",
"batch": ""
}
]
}
}
*/
public function execute($post)
{
try{
// 验证数据
$param = $post['param'];
$batch = $param['batch'];
$data = $param['data'];
$workingsubclass = $param['workingsubclass'];
// 判断批次是否存在
$sql = "SELECT b.subbatch FROM hf_mes_production_planning_management_batch a
INNER JOIN hf_mes_production_planning_management_subbatch b ON a.id = b.batch_id
WHERE batch='".$batch."' ORDER BY b.id DESC LIMIT 1";
$ret = Db::fetch($sql);
if(empty($ret)){
throw new Exception("批次[".$batch."]不存在子批次信息,请检查批次是否已创建");
}
foreach($data as $key=>$val){
$device_code = $val["DEVICE_CODE"];
unset($val["DEVICE_CODE"]);
foreach($val as $k=>$v){
$post_data[] = [
'code' => $k,
"value" =>$v,
"parent_device_code"=> $device_code,
"device_code"=>$device_code,
"batch"=> $batch
];
}
}
// 直接拼接转发数据
$post_action_data = json_encode([
"action"=>"set_node_data",
"param"=>[
"working_subclass"=>$workingsubclass,
"data"=>$post_data
]
]);
//检查env的转发ip是否已经配置
if(!envs("SCADA_IP") || !envs("SCADA_IP")){
echo 'ENV文件配置缺少SetDeviceProcess需要使用的SCADA_IP和SCADA_PORT配置请先配置';
return '';
}
$ip = envs("SCADA_IP");
$port = envs("SCADA_PORT");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ip.':'.$port);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_action_data );
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$return = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
throw new Exception('数据请求写入失败!');
}
curl_close($ch);
$resutl = json_decode($return,true);
if($resutl['errcode'] > 0 ){
throw new Exception($resutl['errmsg']);
}
}catch(Throwable $t){
//捕获Exception和Throwable的异常防止命令行报错导致接口无任何返回
throw new Exception($t->getMessage());
}
}
}