懒羊羊
2023-12-25 634648a7b68dbe1294064495762e13d7fc7eab01
Merge remote-tracking branch 'origin/master'
已修改2个文件
104 ■■■■ 文件已修改
jcdm-main/src/main/java/com/jcdm/main/da/opcuaconfig/init/CustomRunner.java 28 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jcdm-main/src/main/java/com/jcdm/main/da/opcuaconfig/init/TestCallback.java 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jcdm-main/src/main/java/com/jcdm/main/da/opcuaconfig/init/CustomRunner.java
@@ -1,32 +1,44 @@
package com.jcdm.main.da.opcuaconfig.init;
import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig;
import com.jcdm.main.da.opcuaconfig.service.IDaOpcuaConfigService;
import com.kangaroohy.milo.service.MiloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;
@Component
@Slf4j
public class CustomRunner implements ApplicationRunner {
    @Autowired
    private MiloService miloService;
    @Autowired
    protected IDaOpcuaConfigService daOpcuaConfigService;
    private static CustomRunner customRunner;
    private List<DaOpcuaConfig> b=null;
    //在自定义类中调用service层等Spring其他层
    @PostConstruct
    public void init() {
        customRunner = this;
        customRunner.daOpcuaConfigService = this.daOpcuaConfigService;
        // 初使化时将已静态化的testService实例化
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
        sub();
    }
    private void sub() throws Exception {
        List<String> ids = new ArrayList<>();
        ids.add("nbu01.OP1010.PalletID");
//        miloService.subscriptionFromOpcUa(ids, (id, value) -> log.info("subscription 点位:{} 订阅到消息:{}", id, value));
        DaOpcuaConfig daOpcuaConfig=new DaOpcuaConfig();
        daOpcuaConfig.setSubscribe(1L);
        b=customRunner.daOpcuaConfigService.selectDaOpcuaConfigList(daOpcuaConfig);
        List<String> ids = b.stream().map(e -> e.getNode()).collect(Collectors.toList());
        TestCallback testCallback = new TestCallback();
        miloService.subscriptionFromOpcUa(ids,testCallback);
    }
jcdm-main/src/main/java/com/jcdm/main/da/opcuaconfig/init/TestCallback.java
@@ -1,58 +1,50 @@
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("订阅回调成功");
    }
}