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

96 lines
4.4 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 libs\db\Db;
/**
* 设备上传状态接口
* Class SetDeviceStatus
* @package app\action
*/
class SetDeviceStatus
{
public function execute($post)
{
// 验证数据
$param = check_valid($post['action'], [
['device_code', 'string', '设备编码'],
['status', 'string', '设备编码'],
], $post['param']);
if($param['status'] == "IDLE" || $param['status'] == "RUN" || $param['status'] == "FINISH" ||
$param['status'] == "TROUBLE" || $param['status'] == "PAUSE" || $param['status'] == "OFFLINE"){
if ($param['status'] == "TROUBLE" && empty($param['errcode'])) {
throw new Exception("请上传异常errcode.", 4001);
}
$errcode = isset($param['errcode'])&&!empty($param['errcode'])?$param['errcode']:null;
$sql = "SELECT id,status,device_category_id FROM hf_mes_device WHERE code='".$param['device_code']."' LIMIT 1;";
$ret_device = Db::fetch($sql);
if(!is_null($ret_device)){
//含有这个device
list($status,$device_category_id) = [$ret_device['status'],$ret_device['device_category_id']];
// 通过这个设备去查找device_log状态
$sql = "SELECT id,status,errcode FROM hf_mes_device_log WHERE device_code='".$param['device_code']."' ORDER BY create_time desc LIMIT 1;";
$ret_device_log = Db::fetch($sql);
if (!is_null($ret_device_log)) { // 如果状态不为空,则有最新状态
if ($status != $param['status'] || ($status == $param['status'] && !empty($param['errcode']) && !empty($ret_device_log['errcode']) && $ret_device_log['errcode'] != $param['errcode'])) { // 如果状态不一致 或者 处于异常状态但是errcode 不一致
$sql = "UPDATE hf_mes_device_log SET release_time=CURRENT_TIMESTAMP(0),duration=EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP(0) - create_time)) WHERE id = {$ret_device_log['id']} ;";
$ret = Db::query($sql);
$this->insert_device_log($param,$ret_device);
}
} else { // 如果device_log为空则插入
$this->insert_device_log($param,$ret_device);
}
}else{
//未发现这个device
throw new Exception("设备编码 ".$param['device_code']." 不存在.", 4001);
}
if(!empty($param['errcode'])){
$sql = "UPDATE hf_mes_device SET msg='".$param['msg']."', errcode='".$errcode."', status='".$param['status']."', last_heartbeat_time=CURRENT_TIMESTAMP(0)
WHERE code='".$param['device_code']."';";
}else{
$sql = "UPDATE hf_mes_device SET status='".$param['status']."', last_heartbeat_time=CURRENT_TIMESTAMP(0)
WHERE code='".$param['device_code']."';";
}
$ret = Db::query($sql);
if(is_null($ret)){
throw new Exception("设备编码 ".$param['device_code']." 更新状态失败.", 4001);
}
}else{
throw new Exception("MES不受理该状态[{$param['status']}]", 4002);
}
return '';
}
/**
* 插入device_log接口
*/
private function insert_device_log($param,$ret_device)
{
if(!empty($param['errcode']) && $param['status'] == "TROUBLE" ){ // 如果状态是故障,则插入故障信息
$val = "('{$param['device_code']}','{$param['status']}','{$param['msg']}','{$param['errcode']}',CURRENT_TIMESTAMP(0),{$ret_device['device_category_id']})";
$sql = "INSERT INTO hf_mes_device_log (device_code, status, msg, errcode,create_time,device_category_id) VALUES " . $val;
}else{
$val = "('{$param['device_code']}', '{$param['status']}',CURRENT_TIMESTAMP(0),{$ret_device['device_category_id']})";
$sql = "INSERT INTO hf_mes_device_log (device_code, status,create_time,device_category_id) VALUES " . $val;
}
$ret_insert_device_log = Db::query($sql);
if(is_null($ret_insert_device_log)){
throw new Exception("设备编码 ".$param['device_code']." 插入设备状态履历失败.", 4001);
}
}
}