| | |
| | | package com.billion.main.plc.sub; |
| | | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.billion.main.da.domain.DaParamCollection; |
| | | import com.billion.main.da.service.IDaParamCollectionService; |
| | | import com.billion.main.da.service.IDaStationCollectionService; |
| | | import com.billion.main.plc.constant.Constants; |
| | | import com.billion.main.sc.domain.ScCollectionParamConf; |
| | | import com.billion.main.sc.service.IScCollectionParamConfService; |
| | | import com.kangaroohy.milo.model.ReadWriteEntity; |
| | | import com.kangaroohy.milo.runner.subscription.SubscriptionCallback; |
| | | import com.kangaroohy.milo.service.MiloService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | | @Slf4j |
| | | public class OPCUaSubscriptionTemp implements SubscriptionCallback { |
| | | |
| | | public static MiloService miloService; |
| | | public static IScCollectionParamConfService collectionParamConfService; |
| | | public static IDaParamCollectionService paramCollectionService; |
| | | public static IDaStationCollectionService stationCollectionService; |
| | | |
| | | public OPCUaSubscriptionTemp(MiloService miloService, IScCollectionParamConfService collectionParamConfService |
| | | , IDaParamCollectionService paramCollectionService, IDaStationCollectionService stationCollectionService) { |
| | | OPCUaSubscriptionTemp.miloService = miloService; |
| | | OPCUaSubscriptionTemp.collectionParamConfService = collectionParamConfService; |
| | | OPCUaSubscriptionTemp.paramCollectionService = paramCollectionService; |
| | | OPCUaSubscriptionTemp.stationCollectionService = stationCollectionService; |
| | | } |
| | | |
| | | @Override |
| | | public void onSubscribe(String identifier, Object value) { |
| | | log.info("地址:"+identifier+"值:"+value); |
| | | try { |
| | | if(null != value && !Constants.ZERO.equals(value.toString())) { |
| | | String[] nodes = identifier.split("[.]"); |
| | | String thoroughfare = nodes[0];//通道 |
| | | String device = nodes[1];//设备 |
| | | String tab = nodes[2];//标记 |
| | | String valueString = value.toString();//地址值 |
| | | |
| | | CompletableFuture<Void> cp1 = CompletableFuture.runAsync(() -> { |
| | | subHandle(thoroughfare,device,tab,valueString); |
| | | }); |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | public void subHandle(String thoroughfare,String device,String tab,String valueString){ |
| | | try{ |
| | | //监听recordData |
| | | if(tab.equals(Constants.RECORD_DATA)){ |
| | | //如果是1做进站处理 |
| | | if(valueString.equals(Constants.ONE)){ |
| | | Object sfcCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".sfcCode").getValue(); |
| | | if(ObjectUtil.isNotNull(sfcCodeObject)){ |
| | | miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.STRING_ONE_ONE).build()); |
| | | }else { |
| | | miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.STRING_ONE_TWO).build()); |
| | | } |
| | | //如果是2做出站处理处理 |
| | | }else if(valueString.equals(Constants.TWO)){ |
| | | String result = Constants.STRING_TWO_ONE; |
| | | //读变速箱总成码 |
| | | Object sfcCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".sfcCode").getValue(); |
| | | //为空时,返回5 |
| | | if (ObjectUtil.isNull(sfcCodeObject)){ |
| | | miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.STRING_TWO_FIVE).build()); |
| | | }else { |
| | | String snCode = sfcCodeObject.toString().trim();//产品SN |
| | | Object stationStatusObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus").getValue();//站状态地址 |
| | | String stationStatus = stationStatusObjcet.toString(); |
| | | if (ObjectUtil.isNotNull(stationStatusObjcet)) { |
| | | //保存参数,发送工厂MES |
| | | result = saveParamCollection(device, snCode, stationStatus); |
| | | //保存过站记录 |
| | | savePassingStation(device,snCode,stationStatus); |
| | | }else { |
| | | result = Constants.STRING_TWO_THREE; |
| | | log.info("读取到工位{}StationStatus数据:{},返回RecordDataDone的值为{}", device, "IS NULL!", result); |
| | | } |
| | | } |
| | | miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(result).build()); |
| | | } |
| | | } |
| | | }catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存过站数据 |
| | | * |
| | | * @param device 工位 |
| | | * @param snCode 产品序列号 |
| | | * @return Integer |
| | | * @throws Exception e |
| | | */ |
| | | private static Integer savePassingStation(String device, String snCode,String stationStatus){ |
| | | Integer result = 21; |
| | | try { |
| | | |
| | | |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 保存参数数据和发送工厂MES |
| | | * @param device 工位 |
| | | * @param snCode 产品SN |
| | | * @param stationStatus 站状态 |
| | | * @return result |
| | | * @throws Exception e |
| | | */ |
| | | private static String saveParamCollection(String device,String snCode,String stationStatus){ |
| | | String result = Constants.STRING_TWO_ONE;//返回结果 |
| | | try { |
| | | //查询参数配置表 |
| | | List<ScCollectionParamConf> list = collectionParamConfService.list(new LambdaQueryWrapper<ScCollectionParamConf>() |
| | | .eq(ScCollectionParamConf::getLocationCode, device)//工位 |
| | | .eq(ScCollectionParamConf::getSubscribe, Constants.ONE)//是否采集 |
| | | ); |
| | | if (CollUtil.isNotEmpty(list)){ |
| | | List<String> collect = list.stream() |
| | | .map(ScCollectionParamConf::getNode).collect(Collectors.toList()); |
| | | List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(collect); |
| | | ArrayList<DaParamCollection> collectionList = new ArrayList<>(); |
| | | for (int i = 0; i < readWriteEntityList.size(); i++) { |
| | | DaParamCollection daParamCollection = new DaParamCollection(); |
| | | daParamCollection.setSfcCode(snCode); |
| | | daParamCollection.setLocationCode(device); |
| | | daParamCollection.setParamCode(list.get(i).getParamCode());//参数编码 |
| | | daParamCollection.setParamName(list.get(i).getParamName());//参数名称 |
| | | daParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString()); |
| | | daParamCollection.setCollectTime(new Date()); |
| | | collectionList.add(daParamCollection); |
| | | } |
| | | paramCollectionService.saveBatch(collectionList); |
| | | } |
| | | |
| | | }catch (Exception e) { |
| | | throw new RuntimeException("保存数据发送工厂MES异常"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | } |
| | | //package com.billion.main.plc.sub; |
| | | // |
| | | // |
| | | //import cn.hutool.core.collection.CollUtil; |
| | | //import cn.hutool.core.util.ObjectUtil; |
| | | //import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | //import com.billion.main.da.domain.DaParamCollection; |
| | | //import com.billion.main.da.service.IDaParamCollectionService; |
| | | //import com.billion.main.da.service.IDaStationCollectionService; |
| | | //import com.billion.main.plc.constant.Constants; |
| | | //import com.billion.main.sc.domain.ScCollectionParamConf; |
| | | //import com.billion.main.sc.service.IScCollectionParamConfService; |
| | | //import com.kangaroohy.milo.model.ReadWriteEntity; |
| | | //import com.kangaroohy.milo.runner.subscription.SubscriptionCallback; |
| | | //import com.kangaroohy.milo.service.MiloService; |
| | | //import lombok.extern.slf4j.Slf4j; |
| | | //import org.springframework.stereotype.Component; |
| | | // |
| | | //import java.util.ArrayList; |
| | | //import java.util.Date; |
| | | //import java.util.List; |
| | | //import java.util.concurrent.CompletableFuture; |
| | | //import java.util.stream.Collectors; |
| | | // |
| | | // |
| | | //@Slf4j |
| | | //public class OPCUaSubscriptionTemp implements SubscriptionCallback { |
| | | // |
| | | // public static MiloService miloService; |
| | | // public static IScCollectionParamConfService collectionParamConfService; |
| | | // public static IDaParamCollectionService paramCollectionService; |
| | | // public static IDaStationCollectionService stationCollectionService; |
| | | // |
| | | // public OPCUaSubscriptionTemp(MiloService miloService, IScCollectionParamConfService collectionParamConfService |
| | | // , IDaParamCollectionService paramCollectionService, IDaStationCollectionService stationCollectionService) { |
| | | // OPCUaSubscriptionTemp.miloService = miloService; |
| | | // OPCUaSubscriptionTemp.collectionParamConfService = collectionParamConfService; |
| | | // OPCUaSubscriptionTemp.paramCollectionService = paramCollectionService; |
| | | // OPCUaSubscriptionTemp.stationCollectionService = stationCollectionService; |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void onSubscribe(String identifier, Object value) { |
| | | // log.info("地址:"+identifier+"值:"+value); |
| | | // try { |
| | | // if(null != value && !Constants.ZERO.equals(value.toString())) { |
| | | // String[] nodes = identifier.split("[.]"); |
| | | // String thoroughfare = nodes[0];//通道 |
| | | // String device = nodes[1];//设备 |
| | | // String tab = nodes[2];//标记 |
| | | // String valueString = value.toString();//地址值 |
| | | // |
| | | // CompletableFuture<Void> cp1 = CompletableFuture.runAsync(() -> { |
| | | // subHandle(thoroughfare,device,tab,valueString); |
| | | // }); |
| | | // |
| | | // } |
| | | // } catch (Exception e) { |
| | | // log.error(e.getMessage()); |
| | | // } |
| | | // } |
| | | // |
| | | // public void subHandle(String thoroughfare,String device,String tab,String valueString){ |
| | | // try{ |
| | | // //监听recordData |
| | | // if(tab.equals(Constants.RECORD_DATA)){ |
| | | // //如果是1做进站处理 |
| | | // if(valueString.equals(Constants.ONE)){ |
| | | // Object sfcCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".sfcCode").getValue(); |
| | | // if(ObjectUtil.isNotNull(sfcCodeObject)){ |
| | | // miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.STRING_ONE_ONE).build()); |
| | | // }else { |
| | | // miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.STRING_ONE_TWO).build()); |
| | | // } |
| | | // //如果是2做出站处理处理 |
| | | // }else if(valueString.equals(Constants.TWO)){ |
| | | // String result = Constants.STRING_TWO_ONE; |
| | | // //读变速箱总成码 |
| | | // Object sfcCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".sfcCode").getValue(); |
| | | // //为空时,返回5 |
| | | // if (ObjectUtil.isNull(sfcCodeObject)){ |
| | | // miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.STRING_TWO_FIVE).build()); |
| | | // }else { |
| | | // String snCode = sfcCodeObject.toString().trim();//产品SN |
| | | // Object stationStatusObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus").getValue();//站状态地址 |
| | | // String stationStatus = stationStatusObjcet.toString(); |
| | | // if (ObjectUtil.isNotNull(stationStatusObjcet)) { |
| | | // //保存参数,发送工厂MES |
| | | // result = saveParamCollection(device, snCode, stationStatus); |
| | | // //保存过站记录 |
| | | // savePassingStation(device,snCode,stationStatus); |
| | | // }else { |
| | | // result = Constants.STRING_TWO_THREE; |
| | | // log.info("读取到工位{}StationStatus数据:{},返回RecordDataDone的值为{}", device, "IS NULL!", result); |
| | | // } |
| | | // } |
| | | // miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(result).build()); |
| | | // } |
| | | // } |
| | | // }catch (Exception e) { |
| | | // log.error(e.getMessage()); |
| | | // } |
| | | // } |
| | | // |
| | | // /** |
| | | // * 保存过站数据 |
| | | // * |
| | | // * @param device 工位 |
| | | // * @param snCode 产品序列号 |
| | | // * @return Integer |
| | | // * @throws Exception e |
| | | // */ |
| | | // private static Integer savePassingStation(String device, String snCode,String stationStatus){ |
| | | // Integer result = 21; |
| | | // try { |
| | | // |
| | | // |
| | | // } catch (Exception e) { |
| | | // throw new RuntimeException(e); |
| | | // } |
| | | // |
| | | // return result; |
| | | // } |
| | | // |
| | | // /** |
| | | // * 保存参数数据和发送工厂MES |
| | | // * @param device 工位 |
| | | // * @param snCode 产品SN |
| | | // * @param stationStatus 站状态 |
| | | // * @return result |
| | | // * @throws Exception e |
| | | // */ |
| | | // private static String saveParamCollection(String device,String snCode,String stationStatus){ |
| | | // String result = Constants.STRING_TWO_ONE;//返回结果 |
| | | // try { |
| | | // //查询参数配置表 |
| | | // List<ScCollectionParamConf> list = collectionParamConfService.list(new LambdaQueryWrapper<ScCollectionParamConf>() |
| | | // .eq(ScCollectionParamConf::getLocationCode, device)//工位 |
| | | // .eq(ScCollectionParamConf::getSubscribe, Constants.ONE)//是否采集 |
| | | // ); |
| | | // if (CollUtil.isNotEmpty(list)){ |
| | | // List<String> collect = list.stream() |
| | | // .map(ScCollectionParamConf::getNode).collect(Collectors.toList()); |
| | | // List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(collect); |
| | | // ArrayList<DaParamCollection> collectionList = new ArrayList<>(); |
| | | // for (int i = 0; i < readWriteEntityList.size(); i++) { |
| | | // DaParamCollection daParamCollection = new DaParamCollection(); |
| | | // daParamCollection.setSfcCode(snCode); |
| | | // daParamCollection.setLocationCode(device); |
| | | // daParamCollection.setParamCode(list.get(i).getParamCode());//参数编码 |
| | | // daParamCollection.setParamName(list.get(i).getParamName());//参数名称 |
| | | // daParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString()); |
| | | // daParamCollection.setCollectTime(new Date()); |
| | | // collectionList.add(daParamCollection); |
| | | // } |
| | | // paramCollectionService.saveBatch(collectionList); |
| | | // } |
| | | // |
| | | // }catch (Exception e) { |
| | | // throw new RuntimeException("保存数据发送工厂MES异常"); |
| | | // } |
| | | // return result; |
| | | // } |
| | | // |
| | | // |
| | | //} |