#
zwl
2025-06-21 af1e6e2ccc7ecd165ac5f6ee619f0665a4919871
src/main/java/com/zy/core/thread/LedThread.java
@@ -2,11 +2,16 @@
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.CommandInfo;
import com.zy.asrs.service.CommandInfoService;
import com.zy.asrs.service.DeviceErrorService;
import com.zy.common.entity.Parameter;
import com.zy.common.model.MatDto;
import com.zy.core.Slave;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
import com.zy.core.enums.CommandStatusType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.Task;
import com.zy.core.model.command.LedCommand;
@@ -23,6 +28,9 @@
import onbon.bx05.utils.DisplayStyleFactory;
import java.awt.*;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -42,23 +50,15 @@
    private Set<Integer> workNos = new HashSet<>();
    private boolean ledMk = false;
    private boolean resetStatus = false;    // 复位状态
    private StringBuffer errorMsg = new StringBuffer();
    public LedThread(Slave slave) {
        this.slave = slave;
        try {
            Bx5GEnv.initial(3000);
            screen = new Bx5GScreenClient("my");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("led连接构造器错误 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        }
    }
    @Override
    @SuppressWarnings({"InfiniteLoopStatement", "unchecked"})
    public void run() {
        connect();
        close();
        while (true) {
            try {
                Task task = MessageQueue.poll(SlaveType.Led, slave.getId());
@@ -71,6 +71,10 @@
                        // 复位
                        case 2:
                            reset();
//                            this.ledMk = false;
                            break;
                        case 5:
                            error((String) task.getData());
                            break;
                        default:
                            break;
@@ -96,7 +100,7 @@
        TextBxPage page = new TextBxPage();
        for (LedCommand command : list) {
            page.newLine(command.getTitle() +"("+command.getWorkNo()+")");
            page.newLine("源库位:"+command.getSourceLocNo());
            page.newLine("库位:"+ (command.getIoType() < 100 ? command.getLocNo() : command.getSourceLocNo()));
            page.newLine("目标站:"+command.getStaNo());
            if (!command.isEmptyMk()) {
                for (MatDto matDto : command.getMatDtos()) {
@@ -111,6 +115,12 @@
                }
            }
            page.newLine("\n");
            //更新指令状态
            CommandInfoService commandInfoService = SpringUtils.getBean(CommandInfoService.class);
            CommandInfo commandInfo = command.getCommandInfo();
            commandInfo.setCommandStatus(CommandStatusType.COMPLETE.id);
            commandInfoService.updateById(commandInfo);
        }
        // 设置字体
@@ -133,70 +143,47 @@
    }
    private void reset() throws Bx5GException {
        if (!connect()) {
            return;
        }
//        if (resetStatus) {
//            return;
//        }
        pf = new ProgramBxFile( 0, screen.getProfile());
        pf.setFrameShow(false);
        // 分别输入X,Y,width,height
        area = new TextCaptionBxArea( 0,0,96,48, screen.getProfile());
        // 创建一个数据页
        TextBxPage page = new TextBxPage();
//        List<String> list = null;
//        try {
//            String ledDefaultMsg = Parameter.get().getLedDefaultMsg();
//            list = JSON.parseArray(ledDefaultMsg, String.class);
//        } catch (Exception ignore) {
//        }
//        if (!Cools.isEmpty(list)) {
//            for (String str : list) {
//                page.newLine(str);
//            }
//        } else {
//            page.newLine("自动化立体仓库");
//            page.newLine("西格迈股份有限公司");
//        }
        page.newLine("");
        // 设置字体
        page.setFont(new Font("宋体",Font.PLAIN,13));
        // 设置文本颜色
        page.setForeground(Color.red);
        // 设置显示特技为快速打出
        page.setDisplayStyle(styles[6]);
        area.clearPages();
        area.addPage(page);
        pf.addArea(area);
        // 更新节目
        if (pf.validate() != null) {
            log.info("pf out of range");
        } else {
            // 更新节目
            screen.writeProgram(pf);
//            resetStatus = true;
        }
        close();
        errorMsg.delete(0, errorMsg.length());
    }
    @Override
    public boolean connect() {
        boolean connRes = false;
        // ======= 🌐 添加网络可达性检测 =======
        try {
            connRes = screen.connect(slave.getIp(),slave.getPort());
            screen.turnOn();
        } catch (Exception ignore) {
            InetAddress address = InetAddress.getByName(slave.getIp());
            if (!address.isReachable(2000)) {
                log.warn("设备不可达,跳过连接: {}", slave.getIp());
                return false;  // 提前返回,避免无意义连接尝试
            }
        } catch (IOException e) {
            log.error("IP检测异常: {}", e.getMessage());
            return false; // 可视情况决定是否 return
        }
        // ======= 尝试连接设备 =======
        try {
            connRes = screen.connect(slave.getIp(), slave.getPort());
            screen.turnOn(); // 如果 connect 成功再开屏
        } catch (Exception e) {
            log.error("连接异常: {}", e.getMessage(), e); // 避免捕获后无提示
        }
        // ======= 设备错误状态处理 =======
        DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
        if (connRes) {
            log.info("led连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            deviceErrorService.deleteDeviceError("led", slave.getId());
        } else {
            log.error("led连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            deviceErrorService.addDeviceError("led", slave.getId(), "led连接失败");
        }
        return connRes;
    }
    @Override
    public void close() {
@@ -444,8 +431,11 @@
        //
        // 继开与控制器之间的链接
        screen.disconnect();
    }
    private void error(String msg) {
        log.error("错误信息为:{}",msg);
        errorMsg.delete(0, errorMsg.length());
        errorMsg.append(msg);
    }
}