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 list = stationCollectionService.list(new QueryWrapper() .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); } }