| | |
| | | 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; |
| | |
| | | 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()); |
| | |
| | | // 复位 |
| | | case 2: |
| | | reset(); |
| | | // this.ledMk = false; |
| | | break; |
| | | case 5: |
| | | error((String) task.getData()); |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | } |
| | | |
| | | 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()); |
| | |
| | | log.error("led连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | deviceErrorService.addDeviceError("led", slave.getId(), "led连接失败"); |
| | | } |
| | | |
| | | return connRes; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void close() { |
| | |
| | | // |
| | | // 继开与控制器之间的链接 |
| | | screen.disconnect(); |
| | | |
| | | |
| | | } |
| | | private void error(String msg) { |
| | | log.error("错误信息为:{}",msg); |
| | | errorMsg.delete(0, errorMsg.length()); |
| | | errorMsg.append(msg); |
| | | } |
| | | |
| | | } |