| | |
| | | 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 java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * Created by vincent on 2020/9/1 |
| | | */ |
| | | @Data |
| | | @Slf4j |
| | | public class LedThread implements Runnable, ThreadHandler { |
| | | public interface LedThread extends ThreadHandler { |
| | | |
| | | private Slave slave; |
| | | private Set<Integer> workNos = new HashSet<>(); |
| | | void write(List<LedCommand> commands); |
| | | |
| | | // 显示器 |
| | | private StringBuffer stringBuffer = new StringBuffer(); |
| | | private List<LedCommand> commandList; |
| | | void reset(); |
| | | |
| | | private StringBuffer errorMsg = new StringBuffer(); |
| | | void error(String error); |
| | | |
| | | public LedThread(Slave slave) { |
| | | this.slave = slave; |
| | | } |
| | | void errorReset(); |
| | | |
| | | @Override |
| | | @SuppressWarnings({"InfiniteLoopStatement", "unchecked"}) |
| | | public void run() { |
| | | // connect(); |
| | | while (true) { |
| | | try { |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | void setLedMk(boolean mk); |
| | | |
| | | 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(matDto.getMatnr()).append("-").append(matDto.getBatch()).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()); |
| | | } |
| | | |
| | | |
| | | 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() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | } |
| | | boolean isLedMk(); |
| | | |
| | | } |