|  |  |  | 
|---|
|  |  |  | package com.zy.core.thread; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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.SlaveType; | 
|---|
|  |  |  | import com.zy.core.model.Task; | 
|---|
|  |  |  | import com.zy.core.model.command.LedCommand; | 
|---|
|  |  |  | import lombok.Data; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import onbon.bx05.Bx5GEnv; | 
|---|
|  |  |  | import onbon.bx05.Bx5GScreenClient; | 
|---|
|  |  |  | import onbon.bx05.area.TextCaptionBxArea; | 
|---|
|  |  |  | import onbon.bx05.area.page.TextBxPage; | 
|---|
|  |  |  | import onbon.bx05.cmd.dyn7.DynamicBxAreaRule; | 
|---|
|  |  |  | import onbon.bx05.file.ProgramBxFile; | 
|---|
|  |  |  | import onbon.bx05.utils.DisplayStyleFactory; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.awt.*; | 
|---|
|  |  |  | import java.util.HashSet; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 2020/9/1 | 
|---|
|  |  |  | 
|---|
|  |  |  | public class LedThread implements Runnable, ThreadHandler { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Slave slave; | 
|---|
|  |  |  | private Bx5GScreenClient screen; | 
|---|
|  |  |  | private Set<Integer> workNos = new HashSet<>(); | 
|---|
|  |  |  | private boolean ledMk = false; | 
|---|
|  |  |  | private boolean resetStatus = false;    // 复位状态 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 显示器 | 
|---|
|  |  |  | private StringBuffer stringBuffer = new StringBuffer(); | 
|---|
|  |  |  | private List<LedCommand> commandList; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private StringBuffer errorMsg = new StringBuffer(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public LedThread(Slave slave) { | 
|---|
|  |  |  | this.slave = slave; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Bx5GEnv.initial(3000); | 
|---|
|  |  |  | screen = new Bx5GScreenClient("MyScreen"); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | log.info("led连接构造器错误 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @SuppressWarnings("InfiniteLoopStatement") | 
|---|
|  |  |  | @SuppressWarnings({"InfiniteLoopStatement", "unchecked"}) | 
|---|
|  |  |  | public void run() { | 
|---|
|  |  |  | connect(); | 
|---|
|  |  |  | //        connect(); | 
|---|
|  |  |  | while (true) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Thread.sleep(500); | 
|---|
|  |  |  | Task task = MessageQueue.poll(SlaveType.Led, slave.getId()); | 
|---|
|  |  |  | if (task != null) { | 
|---|
|  |  |  | switch (task.getStep()) { | 
|---|
|  |  |  | // 写数据 | 
|---|
|  |  |  | case 1: | 
|---|
|  |  |  | write((List<LedCommand>)task.getData()); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | // 复位 | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | reset(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 3: | 
|---|
|  |  |  | error((String) task.getData()); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 4: | 
|---|
|  |  |  | errorReset(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Thread.sleep(1000); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void write(List<LedCommand> list) { | 
|---|
|  |  |  | commandList = list; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | StringBuilder sb = new StringBuilder(); | 
|---|
|  |  |  | for (LedCommand command : list) { | 
|---|
|  |  |  | sb.append(command.getTitle()).append("(").append(command.getWorkNo()).append(")").append("\n"); | 
|---|
|  |  |  | sb.append("源库位:").append(command.getSourceLocNo()).append("\n"); | 
|---|
|  |  |  | sb.append("目标站:").append(command.getStaNo()).append("\n"); | 
|---|
|  |  |  | if (!command.isEmptyMk()) { | 
|---|
|  |  |  | for (MatDto matDto : command.getMatDtos()) { | 
|---|
|  |  |  | sb.append("物料编码:").append(matDto.getMatNo()).append("\n"); | 
|---|
|  |  |  | sb.append("名称:").append(matDto.getMaknx()).append("\n"); | 
|---|
|  |  |  | sb.append("数量:").append(matDto.getCount()).append("\n"); | 
|---|
|  |  |  | sb.append("规格:").append(matDto.getSpecs()).append("\n"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | sb.append("\n"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | stringBuffer.delete(0, stringBuffer.length()); | 
|---|
|  |  |  | stringBuffer.append(sb.toString()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | errorReset(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void reset() { | 
|---|
|  |  |  | commandList = null; | 
|---|
|  |  |  | stringBuffer.delete(0, stringBuffer.length()); | 
|---|
|  |  |  | errorMsg.delete(0, errorMsg.length()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void error(String msg) { | 
|---|
|  |  |  | errorMsg.delete(0, errorMsg.length()); | 
|---|
|  |  |  | errorMsg.append(msg); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void errorReset() { | 
|---|
|  |  |  | this.errorMsg.delete(0, errorMsg.length()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean connect() { | 
|---|
|  |  |  | boolean connRes = false; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | connRes = screen.connect(slave.getIp(),slave.getPort()); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (connRes) { | 
|---|
|  |  |  | log.info("led连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | log.error("led连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return connRes; | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void close() { | 
|---|
|  |  |  | screen.disconnect(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) throws Exception { | 
|---|
|  |  |  | Bx5GEnv.initial(3000); | 
|---|
|  |  |  | // 创建screen对象,用于对控制器进行访问,客户端模式 | 
|---|
|  |  |  | Bx5GScreenClient screen = new Bx5GScreenClient("MyScreen"); | 
|---|
|  |  |  | // 创建screen对象,用于对控制器进行访问,串口模式 | 
|---|
|  |  |  | //        Bx5GScreenRS screen = new Bx5GScreenRS("MyScreen"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 在对控制器交互之前,需要先与控制器建立连接 | 
|---|
|  |  |  | screen.connect("192.168.10.61",5005); | 
|---|
|  |  |  | // 与控制器交互完成后,需断开与控制器之间的连接 | 
|---|
|  |  |  | screen.disconnect(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 以下为一些简单控制命令的使用方法 | 
|---|
|  |  |  | // 开关机命令 | 
|---|
|  |  |  | //        screen.turnOff();// 关机 | 
|---|
|  |  |  | //        screen.turnOn();// 开机 | 
|---|
|  |  |  | //        screen.syncTime();// 校时 | 
|---|
|  |  |  | //        screen.ping();// ping命令 | 
|---|
|  |  |  | //        // 查询控制器状态 | 
|---|
|  |  |  | //        screen.checkControllerStatus(); | 
|---|
|  |  |  | //        // 查询控制器当前固件版本 | 
|---|
|  |  |  | //        screen.checkFirmware(); | 
|---|
|  |  |  | //        // 查询控制器内存 | 
|---|
|  |  |  | //        screen.checkMemVolumes(); | 
|---|
|  |  |  | //        // 锁定屏幕当前画面 | 
|---|
|  |  |  | //        screen.lock(); | 
|---|
|  |  |  | //        // 解除锁定屏幕当前画面 | 
|---|
|  |  |  | //        screen.unlock(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // 以下是动态区部分 Demo | 
|---|
|  |  |  | // 动态区的特点 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // DynamicBxAreaRule(id, runMode, immediatePlay, timeout) | 
|---|
|  |  |  | // runMode 运行模式: | 
|---|
|  |  |  | //   0:循环显示。 | 
|---|
|  |  |  | //   1:显示完成后静止显示最后一页数据。 | 
|---|
|  |  |  | //   2:循环显示,超过设定时间后数据仍未更新时不再显示。 | 
|---|
|  |  |  | //   3:循环显示,超过设定时间后数据仍未更新时显示 Logo 信息。 | 
|---|
|  |  |  | //   4:循环显示,显示完最后一页后就不再显示。 | 
|---|
|  |  |  | // immediatePlay 是否立即播放: | 
|---|
|  |  |  | //   0:与异步节目一起播放。 | 
|---|
|  |  |  | //   1:异步节目停止播放,仅播放动态区域。 | 
|---|
|  |  |  | //   2:当播放完节目编号最高的异步节目后播放该动态区域。 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // 定义一个动态区 | 
|---|
|  |  |  | // 可以通过ID来更新不同的动态区内容, 此处 ID 为 0 | 
|---|
|  |  |  | DynamicBxAreaRule dynRule = new DynamicBxAreaRule(0, (byte) 0, (byte) 1, 0); | 
|---|
|  |  |  | //dArea.addProgram("P000"); | 
|---|
|  |  |  | //dArea.addProgram("P001"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int posX = 440; | 
|---|
|  |  |  | int posY = 4; | 
|---|
|  |  |  | TextCaptionBxArea dAreaContent = new TextCaptionBxArea(posX, posY, 64, 16, screen.getProfile()); | 
|---|
|  |  |  | TextBxPage page = new TextBxPage("动态第一次尝试"); | 
|---|
|  |  |  | page.setDisplayStyle(DisplayStyleFactory.getStyle(4)); | 
|---|
|  |  |  | dAreaContent.addPage(page); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 发送动态区之前,如果需要删除之前的动态区,可以调用以下接口 | 
|---|
|  |  |  | // 通常如果动态区的位置或大小没有发生改变,不用删除 | 
|---|
|  |  |  | screen.deleteAllDynamic(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 更新动态区 | 
|---|
|  |  |  | screen.writeDynamic(dynRule, dAreaContent); | 
|---|
|  |  |  | Thread.sleep(15000); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // 下面模拟再次更新动态区 | 
|---|
|  |  |  | page = new TextBxPage("再次尝试"); | 
|---|
|  |  |  | TextBxPage page2 = new TextBxPage("成功"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | dAreaContent.clearPages(); | 
|---|
|  |  |  | dAreaContent.addPage(page); | 
|---|
|  |  |  | dAreaContent.addPage(page2); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 更新动态区 | 
|---|
|  |  |  | screen.writeDynamic(dynRule, dAreaContent); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // 继开与控制器之间的链接 | 
|---|
|  |  |  | screen.disconnect(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|