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
package com.mes.plcTaskThread;
 
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.mes.common.S7objectMachine;
import com.mes.device.PlcParameterInfo;
import com.mes.device.PlcParameterObject;
import com.mes.md.entity.Machine;
import com.mes.md.entity.Tasking;
import com.mes.md.mapper.MachineMapper;
import com.mes.md.service.TaskingService;
import lombok.extern.slf4j.Slf4j;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
@Slf4j
public class MachineLamination extends Thread {
 
 
    private MachineMapper machineMapper;
    private TaskingService taskingService;
    private S7objectMachine s7objectMachine;
    public Machine thisMachine;
 
    public MachineLamination(Machine machine, MachineMapper machineMapper, TaskingService taskingService) {
        this.thisMachine = machine;
        this.machineMapper = machineMapper;
        this.taskingService = taskingService;
    }
 
    public void plcStart()throws Exception {
        Machine machine = machineMapper.selectById(thisMachine.getId());
 
        S7objectMachine s7objectMachine = new S7objectMachine(machine.getIp(), machine.getPort(), machine.getFileName(), EPlcType.S1200);
        s7objectMachine.readData();//按照json文件读取内容
        PlcParameterObject plcParameterObject = s7objectMachine.PlcMesObject;
        if (plcParameterObject == null) {
            s7objectMachine.plccontrol.closeS7client();
            return;
        }
        try {
            PlcParameterInfo plcRequest = plcParameterObject.getPlcParameter("plcRequest");//请求字
            PlcParameterInfo mesSend = plcParameterObject.getPlcParameter("mesSend");//发送字
            PlcParameterInfo lengthOutOfService = plcParameterObject.getPlcParameter("length");//长
            PlcParameterInfo widthOutOfService = plcParameterObject.getPlcParameter("width");//宽
            PlcParameterInfo markingMode = plcParameterObject.getPlcParameter("thickness");//厚度
 
            PlcParameterInfo plcReport = plcParameterObject.getPlcParameter("plcReport");//汇报字
            PlcParameterInfo mesConfirm = plcParameterObject.getPlcParameter("mesConfirm");//确认字
            PlcParameterInfo mesConfirmID = plcParameterObject.getPlcParameter("mesConfirmID");//确认ID
            if (machine.getIsLog() > 0) {
                s7objectMachine.consoleLogInfo();
            }
            if ("0".equals(plcRequest.getValue()) && "1".equals(mesSend.getValue())) {
                //发送字置0
                log.info("1.发送字置零,清除上次发送的数据{}", mesSend.getValue());
                s7objectMachine.plccontrol.writeWord(mesSend.getAddress(), 0);
                return;
            }
            if ("0".equals(plcReport.getValue()) && "1".equals(mesConfirm.getValue())) {
                log.info("2.确认字置零");
                //发送字置0
                s7objectMachine.plccontrol.writeWord(mesConfirm.getAddress(), 0);
                return;
            }
            if ("1".equals(plcReport.getValue()) && "0".equals(mesConfirm.getValue())) {
                int finishCount = taskingService.finishMachineTask(machine);
                log.info("3、任务完成");
                if (finishCount > 0) {//有任务
                    s7objectMachine.plccontrol.writeWord(mesConfirm.getAddress(), 1);
                    return;
                }
                return;
            }
            List<Tasking> taskingCount = taskingService.findMachineWorkStateTask(machine, "正在工作");
            if ("1".equals(plcRequest.getValue()) && "0".equals(mesSend.getValue())
                    && "开工".equals(machine.getState())
                    && taskingCount.size() < machine.getMaxTaskCount()) {
 
                Tasking tasking = taskingService.startMachineTask(machine);
                if (tasking != null) {
                    log.info("4、发送数据:{},{},{}", tasking.getLength().intValue(), tasking.getWidth().intValue(), 1);
                    s7objectMachine.plccontrol.writeWord(lengthOutOfService.getAddress(), tasking.getLength().intValue());//x
                    s7objectMachine.plccontrol.writeWord(widthOutOfService.getAddress(), tasking.getWidth().intValue());//y
 
                    s7objectMachine.plccontrol.writeWord(mesSend.getAddress(), 1);
                    return;
                }
            }
        } catch (Exception e) {
            s7objectMachine.plccontrol.closeS7client();
            log.info("逻辑处理异常:设备id【{}】", machine.getId());
        }
 
    }
 
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                plcStart();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                log.info("{}", e.getMessage());
            }
        }
    }
}