package com.jcdm.main.da.opcuaconfig.init; import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf; import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService; import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig; import com.jcdm.main.da.opcuaconfig.service.IDaOpcuaConfigService; import com.jcdm.main.da.paramCollection.domain.DaParamCollection; import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService; import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection; import com.jcdm.main.da.passingStationCollection.service.IDaPassingStationCollectionService; import com.kangaroohy.milo.model.ReadWriteEntity; import com.kangaroohy.milo.runner.subscription.SubscriptionCallback; import com.kangaroohy.milo.service.MiloService; 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.ArrayList; import java.util.List; import java.util.stream.Collectors; @Component public class TestCallback implements SubscriptionCallback { private IDaParamCollectionService daParamCollectionService = BeanUtils.getBean(IDaParamCollectionService.class); private IDaOpcuaConfigService daOpcuaConfigService = BeanUtils.getBean(IDaOpcuaConfigService.class); private IDaCollectionParamConfService collectionParamConfService = BeanUtils.getBean(IDaCollectionParamConfService.class); private MiloService miloService = BeanUtils.getBean(MiloService.class); private List b=null; // private CustomRunner customRunner; @Override public void onSubscribe(String identifier, Object value) { if (b == null || 0 > b.size()) { DaOpcuaConfig daOpcuaConfig=new DaOpcuaConfig(); daOpcuaConfig.setSubscribe(1L); b=daOpcuaConfigService.selectDaOpcuaConfigList(daOpcuaConfig); } //使用Stream API在List中查找元素 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 SiteCollection(String Node, String value) throws Exception { String[] parts = Node.split("[.]"); switch (value) { case "0": //初始 miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(parts[0]+"."+parts[1]+".RecordDataDone").value(0).build()); break; case "1": //请求下发进站状态 miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(parts[0]+"."+parts[1]+".RecordDataDone").value(11).build()); break; case "2": //请求记录工位数据 List b; DaCollectionParamConf daCollectionParamConf=new DaCollectionParamConf(); daCollectionParamConf.setCollectParameterId(parts[0]+"."+parts[1]); b=collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf); List ids = b.stream().map(e -> e.getCollectParameterId()).collect(Collectors.toList()); List s=miloService.readFromOpcUa(ids); s.forEach(element->{ DaParamCollection Config=new DaParamCollection(); String[] q = element.getIdentifier().split("[.]"); Config.setParamCode(q[2]); Config.setLocationCode(q[1]); Config.setParamValue(element.getValue().toString()); daParamCollectionService.insertDaParamCollection(Config); }); miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(parts[0]+"."+parts[1]+".RecordDataDone").value(21).build()); break; default: break; } } }