#
zjj
2 天以前 8f9fa425d667e3d6330a3ed4553834783bb30099
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.zy.core.thread;
 
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Melsec.MelsecMcNet;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.BasExt;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.BasExtService;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.ExtSlave;
import com.zy.core.model.Task;
import com.zy.core.model.protocol.ExtProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.Date;
 
/**
 * 机械臂线程  //Extraman==>机械臂
 * Created by vincent on 2020/8/4
 */
@Data
@Slf4j
public class MelsecExtThread implements Runnable, ThreadHandler {
 
    private MelsecMcNet melsecMcNet;
    private ExtSlave slave;
    private ExtProtocol extProtocol;
    private short heartBeatVal = 1;
    private boolean resetFlag = false;
 
    public boolean isRunning = true;
 
    public MelsecExtThread(ExtSlave slave) {
        this.slave = slave;
    }
 
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        System.out.println("线程启动");
        System.out.println("extProtocol:"+extProtocol);
        this.connect();
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        while (isRunning) {
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Ext, slave.getId());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
                        readStatus();
                        break;
                    default:
                        break;
                }
                // 心跳
//                heartbeat();
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 初始化机械臂状态
     */
    private void initExt() {
        if (null == extProtocol) {
            extProtocol = new ExtProtocol();
        }
        extProtocol.setTake(false);
        extProtocol.setPut(false);
    }
 
    @Override
    public boolean connect() {
        boolean result = false;
        melsecMcNet = new MelsecMcNet(slave.getIp(), slave.getPort());
        OperateResult connect = melsecMcNet.ConnectServer();
        if(connect.IsSuccess){
            result = true;
            OutputQueue.Ext.offer(MessageFormat.format( "【{0}】机械臂plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.info("MelsecExt"+" - 1"+" - 机械臂plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        } else {
            OutputQueue.Ext.offer(MessageFormat.format("【{0}】机械臂plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] ", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("MelsecExt"+" - 2"+" - 机械臂plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            initExt();
        }
//        melsecMcNet.ConnectClose();
        return result;
    }
 
    /**
     * 读取状态
     */
    private void readStatus(){
        try {
            OperateResultExOne<byte[]> result = melsecMcNet.Read("D1035", (short) 56);
            if (result.IsSuccess) {
                if (null == extProtocol) {
                    extProtocol = new ExtProtocol();
                    extProtocol.setExtNo(slave.getId());
                }
                extProtocol.setTake(melsecMcNet.getByteTransform().TransBool(result.Content, 0));
                extProtocol.setPut(melsecMcNet.getByteTransform().TransBool(result.Content, 0));
//                extProtocol.setMode(melsecMcNet.getByteTransform().TransInt16(result.Content, 0));
//                extProtocol.setTaskNo(melsecMcNet.getByteTransform().TransInt16(result.Content, 2));
 
 
// 根据实时信息更新数据库
                BasExtService extService = SpringUtils.getBean(BasExtService.class);
                BasExt basExt = new BasExt();
                basExt.setExtNo(slave.getId());
                basExt.setExtTask(extProtocol.isTake()?"Y":"N");
                basExt.setExtPut(extProtocol.isPut()?"Y":"N");
                if (!extService.updateById(basExt)){
                    log.error("MelsecExt"+" - 4"+" - 机械臂plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                }else {
                    OutputQueue.Ext.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                }
            } else {
                BasExtService extService = SpringUtils.getBean(BasExtService.class);
                BasExt basExt = new BasExt();
                basExt.setExtNo(slave.getId());
                basExt.setExtTask("N");
                basExt.setExtPut("N");
                extService.updateById(basExt);
                OutputQueue.Ext.offer(MessageFormat.format("【{0}】{1}机械臂plc状态信息失败",DateUtils.convert(new Date()), slave.getId()));
                throw new CoolException(MessageFormat.format( "机械臂plc状态信息失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.Ext.offer(MessageFormat.format("【{0}】读取机械臂plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("MelsecExt"+" - 5"+" - 读取机械臂plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            initExt();
        } finally {
//            sign = System.currentTimeMillis();
        }
 
    }
 
    @Override
    public void close() {
        melsecMcNet.ConnectClose();
    }
 
    /**
     * 心跳
     */
    private void heartbeat(){
        if (heartBeatVal >= 30000) {
            heartBeatVal = -30000;
        } else {
            heartBeatVal =(short) (heartBeatVal+1);
        }
        OperateResult write = melsecMcNet.Write("D1011", heartBeatVal);
        if (!write.IsSuccess) {
            log.error("MelsecExt"+" - 9"+" - 机械臂plc心跳通讯失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        }
    }
 
 
    /******************************************************************************************/
    /**************************************** 测试专用 *****************************************/
    /*****************************************************************************************/
    public static void main(String[] args) throws InterruptedException {
 
    }
 
    // 提供一个方法来停止线程
    public void requestStop() {
        isRunning = false;
    }
 
    // 提供一个方法来重启线程
    public Thread restartThread() {
        isRunning = true;
        Thread newThread = new Thread(this);
        newThread.start();
        return newThread;
    }
 
}