自动化立体仓库 - WCS系统
野心家
2023-11-04 98d2aecd3899b1b8419aca65a4f899b9dc9c5b3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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() {
    }
 
}