懒羊羊
2023-11-14 8286c62256f23bc2367a6729c0f46f84215e380b
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
package cn.stylefeng.guns.modular.opcua.cert;
 
 
import cn.stylefeng.guns.modular.cm.passingStationCollection.entity.PassingStationCollection;
import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.params.ProductionOrdeInfoParam;
import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.result.ProductionOrdeInfoResult;
import cn.stylefeng.guns.modular.om.productionOrdeInfo.service.ProductionOrdeInfoService;
import cn.stylefeng.guns.modular.opcua.client.ClientHandler;
import cn.stylefeng.guns.modular.opcua.config.BeanUtils;
import cn.stylefeng.guns.modular.opcua.entity.NodeEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import org.apache.poi.ss.formula.functions.T;
 
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
 
public class Heartbeat {
    ClientHandler clientHandler=new ClientHandler();
    Timer timer1 = new Timer();
 
 
    private ProductionOrdeInfoService orderService = BeanUtils.getBean(ProductionOrdeInfoService.class);
 
    private PassingStationCollectionService stationCollectionService = BeanUtils.getBean(PassingStationCollectionService.class);
 
 
    public void snIsItIssued(String Node, String value) throws Exception  {
        timer1.schedule(timerTask,0,5000);
        System.out.println("五秒执行一次定时任务-----------------------");
    }
 
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            ProductionOrdeInfoParam param = new ProductionOrdeInfoParam();
            param.setOrderStatus("3");
            ProductionOrdeInfoResult bySpec = orderService.findBySpec(param);
            if(bySpec!=null){
                List<PassingStationCollection> list = stationCollectionService.list(new QueryWrapper<PassingStationCollection>()
                        .eq("location_code", "OP1010")
                        .eq("work_order_no",bySpec.getWorkOrderNo())
                );
                if(list.size()==bySpec.getPlanQty()){
                    NodeEntity over1= NodeEntity.builder().index(2).identifier("nbu01.OP1010.Spare")
                            .value("2").type("short").build();
                    try {
                        Boolean out1=clientHandler.write(over1);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }else {
                    NodeEntity over2 = NodeEntity.builder().index(2).identifier("nbu01.OP1010.Spare")
                            .value("1").type("short").build();
                    try {
                        Boolean out2 = clientHandler.write(over2);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }else {
                NodeEntity over1= NodeEntity.builder().index(2).identifier("nbu01.OP1010.Spare")
                        .value("2").type("short").build();
                try {
                    Boolean out1=clientHandler.write(over1);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };
 
 
 
 
 
    public void mesPreparationOk(String Node, String value) throws Exception {
        NodeEntity node = NodeEntity.builder().index(2).identifier(Node)
                .value("true").type("boolean").build();
        Boolean out = clientHandler.write(node);
        System.out.println("111111111mes准备好:---------------------"+out);
    }
 
    public void mesPulseWidth(String Node, String value) throws Exception  {
        while (true) {
            NodeEntity node = NodeEntity.builder().index(2).identifier(Node)
                    .value("true").type("boolean").build();
            Boolean out = clientHandler.write(node);
            try {
                Thread.sleep(2000);  // 暂停两秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            NodeEntity node1 = NodeEntity.builder().index(2).identifier(Node)
                    .value("false").type("boolean").build();
            Boolean out1 = clientHandler.write(node1);
            try {
                Thread.sleep(2000);  // 暂停两秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    public void mesPreparationOk1(String Node, String value) throws Exception {
        NodeEntity node = NodeEntity.builder().index(2).identifier(Node)
                .value("true").type("boolean").build();
        Boolean out = clientHandler.write(node);
        System.out.println("mes准备好:---------------------"+out);
    }
 
    public void mesPulseWidth1(String Node, String value) throws Exception  {
        while (true) {
            NodeEntity node = NodeEntity.builder().index(2).identifier(Node)
                    .value("true").type("boolean").build();
            Boolean out = clientHandler.write(node);
            try {
                Thread.sleep(2000);  // 暂停两秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            NodeEntity node1 = NodeEntity.builder().index(2).identifier(Node)
                    .value("false").type("boolean").build();
            Boolean out1 = clientHandler.write(node1);
            try {
                Thread.sleep(2000);  // 暂停两秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    public void plcPreparationOk(String Node, String value) throws Exception {
        while (true) {
            NodeEntity plcNode = NodeEntity.builder().index(2).identifier(Node).build();
            String read = clientHandler.read(plcNode);
            if(read.equals("1")){
                System.out.println("plc系统准备好!!!");
            }
            try {
                Thread.sleep(2000);  // 暂停两秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    public void plcPulseWidth(String Node, String value) throws Exception  {
        timer = new Timer();
        timer.schedule(new HeartbeatTask(), 0, 2000); // 每2秒钟执行一次HeartbeatTask
    }
 
    private Timer timer;
    private static int lastSignal = 3;
 
    class HeartbeatTask extends TimerTask {
        @SneakyThrows
        public void run() {
            int currentSignal = getCurrentSignal(); // 获取当前的信号值,这里假设通过一个函数获取
            if (currentSignal != lastSignal) {
                System.out.println("Signal changed from " + lastSignal + " to " + currentSignal);
                lastSignal = currentSignal;
            }
        }
    }
 
    // 假设这是一个函数,用于获取对方的心跳信号值
    private int getCurrentSignal() throws Exception {
        NodeEntity plcNode = NodeEntity.builder().index(2).identifier("nbu01.system.PLC_Server_lifebeat").build();
        String read = clientHandler.read(plcNode);
        return Integer.valueOf(read); // 这里只是随机返回一个0到9之间的整数作为信号值,仅作示例使用
    }
 
    public ProductionOrdeInfoResult getOrder(){
        ProductionOrdeInfoParam param = new ProductionOrdeInfoParam();
        param.setOrderStatus("3");
        return orderService.findBySpec(param);
    }
 
}