| | |
| | | package com.jcdm.main.da.opcuaconfig.init; |
| | | |
| | | import com.jcdm.main.da.opcuaconfig.controller.DaOpcuaConfigController; |
| | | import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig; |
| | | import com.jcdm.main.da.opcuaconfig.service.IDaOpcuaConfigService; |
| | | import com.kangaroohy.milo.runner.subscription.SubscriptionCallback; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.lang.reflect.Method; |
| | | import java.util.List; |
| | | |
| | | @Component |
| | | public class TestCallback implements SubscriptionCallback { |
| | | |
| | | // public List<DaOpcuaConfig> b = null; |
| | | // @Autowired |
| | | // private DaOpcuaConfigController daOpcuaConfigController; |
| | | // public void onSubscribe(String identifier, Object value) { |
| | | // if (b == null || 0 > b.size()) { |
| | | // DaOpcuaConfig daOpcuaConfig=new DaOpcuaConfig(); |
| | | // daOpcuaConfig.setSubscribe(1L); |
| | | // b=daOpcuaConfigController.getconfig(daOpcuaConfig); |
| | | // } |
| | | // //使用Stream API在List<T>中查找元素 |
| | | // DaOpcuaConfig opcuaConf = b.stream() |
| | | // .filter(customer ->identifier.equals(customer.getNode())) |
| | | // .findAny() |
| | | // .orElse(null); |
| | | // try { |
| | | // Class<?> clazz = Class.forName(opcuaConf.getrModule()); |
| | | // Method method = clazz.getMethod(opcuaConf.getrFunction(), new Class[] { String.class, |
| | | // String.class }); |
| | | // method.invoke(clazz.newInstance(),new Object[] { |
| | | // new String(identifier), new String(value.toString()) }); |
| | | // } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | |
| | | // InvocationTargetException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | @Autowired |
| | | protected IDaOpcuaConfigService daOpcuaConfigService; |
| | | private static TestCallback testCallback; |
| | | private List<DaOpcuaConfig> b=null; |
| | | //在自定义类中调用service层等Spring其他层 |
| | | @PostConstruct |
| | | public void init() { |
| | | testCallback = this; |
| | | testCallback.daOpcuaConfigService = this.daOpcuaConfigService; |
| | | // 初使化时将已静态化的testService实例化 |
| | | } |
| | | @Override |
| | | public void onSubscribe(String identifier, Object value) { |
| | | if(identifier.equals("nbu01.OP1010.PalletID")){ |
| | | switch(value.toString()) { |
| | | case "Q": |
| | | System.out.println("订阅回调成功QQQ"); |
| | | break; |
| | | case "T": |
| | | System.out.println("订阅回调成功TTT"); |
| | | break; |
| | | case "F": |
| | | System.out.println("订阅回调成功"); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | if (b == null || 0 > b.size()) { |
| | | DaOpcuaConfig daOpcuaConfig=new DaOpcuaConfig(); |
| | | daOpcuaConfig.setSubscribe(1L); |
| | | b=testCallback.daOpcuaConfigService.selectDaOpcuaConfigList(daOpcuaConfig); |
| | | } |
| | | //使用Stream API在List<T>中查找元素 |
| | | DaOpcuaConfig opcuaConf = b.stream() |
| | | .filter(customer ->identifier.equals(customer.getNode())) |
| | | .findAny() |
| | | .orElse(null); |
| | | try { |
| | | Class<?> clazz = Class.forName(opcuaConf.getrModule()); |
| | | Method method = clazz.getMethod(opcuaConf.getrFunction(), new Class[] { String.class, String.class }); |
| | | method.invoke(clazz.newInstance(),new Object[] { new String(identifier), new String(value.toString()) }); |
| | | } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { |
| | | System.out.println("节点"+identifier+"订阅回调失败"); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | public void getSn(String Node, String value) { |
| | | System.out.println("订阅回调成功"); |
| | | } |
| | | } |