package com.zy.core.thread;
|
|
import com.zy.common.model.MatDto;
|
import com.zy.common.utils.News;
|
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.Bx5GException;
|
import onbon.bx05.Bx5GScreenClient;
|
import onbon.bx05.area.TextCaptionBxArea;
|
import onbon.bx05.area.page.TextBxPage;
|
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
|
*/
|
@Data
|
@Slf4j
|
public class LedThread implements Runnable, ThreadHandler {
|
|
private Bx5GScreenClient screen;
|
private ProgramBxFile pf;
|
private TextCaptionBxArea area;
|
DisplayStyleFactory.DisplayStyle[] styles = DisplayStyleFactory.getStyles().toArray(new DisplayStyleFactory.DisplayStyle[0]);
|
private Slave slave;
|
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;
|
}
|
|
@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();
|
}
|
}
|
}
|
|
private void write(List<LedCommand> list) throws Bx5GException {
|
if (!connect()) {
|
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();
|
for (LedCommand command : list) {
|
page.newLine(command.getTitle() +"("+command.getWorkNo()+")");
|
page.newLine("库位:"+ (command.getIoType() < 100 ? command.getLocNo() : command.getSourceLocNo()));
|
page.newLine("目标站:"+command.getStaNo());
|
if (!command.isEmptyMk()) {
|
for (MatDto matDto : command.getMatDtos()) {
|
//去掉小数点
|
String strQty = matDto.getCount().toString();
|
int idx = strQty.lastIndexOf(".");
|
if(idx >= 0){
|
strQty.substring(0,idx);
|
}
|
page.newLine(matDto.getMaknx() + "[数量" + strQty +"]");
|
// page.newLine(matDto.getMaknx() + "【数量" + matDto.getCount() +"】");
|
}
|
}
|
page.newLine("\n");
|
}
|
|
// 设置字体
|
page.setFont(new Font("宋体",Font.PLAIN,12));
|
// 设置文本颜色
|
page.setForeground(Color.red);
|
// 设置显示特技为快速打出
|
page.setDisplayStyle(styles[6]);
|
area.clearPages();
|
area.addPage(page);
|
pf.addArea(area);
|
if (pf.validate() != null) {
|
News.info("Led"+" - 2"+" - pf out of range");
|
} else {
|
// 更新节目
|
screen.writeProgram(pf);
|
// resetStatus = false;
|
}
|
close();
|
}
|
|
|
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() {
|
boolean connRes = false;
|
try {
|
connRes = screen.connect(slave.getIp(),slave.getPort());
|
screen.turnOn();
|
} catch (Exception ignore) {
|
}
|
if (connRes) {
|
News.info("Led"+" - 4"+" - led连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
|
} else {
|
News.error("Led"+" - 5"+" - led连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
|
}
|
return connRes;
|
}
|
|
@Override
|
public void close() {
|
}
|
|
}
|