cl
2024-09-27 ba3b8825b92d4e1b58b73cb29be252935adce23c
jcdm-main/src/main/java/com/jcdm/main/plcserver/sub/OPCUaSubscription.java
@@ -9,6 +9,7 @@
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jcdm.common.core.domain.AjaxResult;
import com.jcdm.framework.websocket.WebSocketUsers;
import com.jcdm.main.constant.Constants;
@@ -20,6 +21,8 @@
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.jcdm.main.da.psConf.domain.DaPsConf;
import com.jcdm.main.da.psConf.service.IDaPsConfService;
import com.jcdm.main.da.testDeviceInterfaceTemp.domain.DaTestDeviceInterfaceTemp;
import com.jcdm.main.da.testDeviceInterfaceTemp.service.IDaTestDeviceInterfaceTempService;
import com.jcdm.main.om.productionOrde.domain.OmProductionOrdeInfo;
@@ -32,12 +35,14 @@
import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
import com.kangaroohy.milo.service.MiloService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import javax.websocket.Session;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@@ -65,7 +70,7 @@
    public static IDaTestDeviceInterfaceTempService daTestDeviceInterfaceTempService;
    public static IDaCellDataService daCellDataService;
    public static IDaPsConfService daPsConfService;
/*    @Value("${orderLineUrl}")
    private static String orderLineUrl;*/
@@ -75,7 +80,8 @@
                             IDaParamCollectionService daParamCollectionService,
                             IOmProductionOrdeInfoService omProductionOrdeInfoService,
                             IDaTestDeviceInterfaceTempService daTestDeviceInterfaceTempService,
                             IDaCellDataService daCellDataService) {
                             IDaCellDataService daCellDataService,
                             IDaPsConfService daPsConfService) {
        OPCUaSubscription.miloService = miloService;
        OPCUaSubscription.daPassingStationCollectionService = daPassingStationCollectionService;
        OPCUaSubscription.collectionParamConfService = collectionParamConfService;
@@ -83,6 +89,7 @@
        OPCUaSubscription.omProductionOrdeInfoService = omProductionOrdeInfoService;
        OPCUaSubscription.daTestDeviceInterfaceTempService = daTestDeviceInterfaceTempService;
        OPCUaSubscription.daCellDataService = daCellDataService;
        OPCUaSubscription.daPsConfService = daPsConfService;
    }
@@ -154,14 +161,15 @@
                    //OP020 电芯挡位校验
                    if (Constants.OP020.equals(device)){
                        Integer result = 11;
                        Object cellGearObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".CellGear").getValue();
                        /*Object cellGearObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".CellGear").getValue();
                        if (ObjectUtil.isNull(cellGearObjcet)){
                            result = 16;//电芯挡位为空
                            result = 11;//如果电芯挡位为空,不进行挡位校验
                        }else{
                            String cellGear = cellGearObjcet.toString();
                            /*List<String> cellCodeList = readCellCodeList(thoroughfare, device);
                            result = checkCellGear(thoroughfare, device,cellCodeList,cellGear);//校验电芯挡位和组别*/
                        }
                            List<String> cellCodeList = readCellCodeList(thoroughfare, device);
                            result = checkCellGear(thoroughfare, device,cellCodeList,cellGear);//校验电芯挡位和组别
                            Constants.OLD_CELL_GEAR = cellGear;//挡位赋值
                        }*/
                        miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(result).build());
                    }else if (Constants.OP030.equals(device)){
@@ -238,7 +246,7 @@
                                return;
                            }
                            result = savePassingStation(thoroughfare, device,cellCodeList);//保存过站
                            //result = savePassingStation(thoroughfare, device,cellCodeList);//保存过站
                            if(result == 21) {
                                if(Constants.OP020.contains(device)){
                                    if(!cellCode1.isEmpty()){
@@ -287,7 +295,7 @@
                            Object stationStatusObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus").getValue();//站状态地址
                            if (ObjectUtil.isNotNull(stationStatusObjcet)){
                                String stationStatus = stationStatusObjcet.toString();
                                result = savePassingStation(thoroughfare, device,moduleCode,stationStatus);//保存过站
                                //result = savePassingStation(thoroughfare, device,moduleCode,stationStatus);//保存过站
                                if(result == 21) {
                                    result = saveParamCollection(device,moduleCode,stationStatus);//保存参数,发送工厂MES
                                }
@@ -857,7 +865,7 @@
     */
    private Integer checkCellGear(String thoroughfare, String device,List<String> cellCodeList,String cellGear) throws Exception {
        Integer result = 11;
        String cellSerial = "";
        for(int i = 0; i < cellCodeList.size(); i ++){
            Integer cellStatus = 1;
            String cellCode = cellCodeList.get(i);
@@ -867,20 +875,31 @@
                if(CollUtil.isNotEmpty(list)){
                    DaCellData daCellData = list.get(0);
                    String cellValue = daCellData.getCellValue();//数据库中电芯挡位
                    String cellSerial = daCellData.getCellSerial();//数据库中电芯组别
                    cellSerial = daCellData.getCellSerial();//数据库中电芯组别
                    if(!cellValue.isEmpty() && cellValue.equals(cellGear)){
                        cellStatus = 1;
                    }else {
                        cellStatus = 2;
                        result = 17;//挡位校验不合格
                        result = 16;//挡位校验不合格
                        log.info("电芯挡位校验不合格:电芯码为{},PLC设置挡位为{},工厂MES下发挡位为:{}",cellCode,cellGear,cellValue);
                    }
                }else {
                    cellStatus = 2;
                    result = 17;//查不到要校验的挡位
                    result = 16;//查不到要校验的挡位
                    log.info("电芯挡位校验不合格:电芯码为{},PLC设置挡位为{},工厂MES下发挡位为空,查询不到该数据",cellCode,cellGear);
                }
                miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_"+(i+1)).value(cellStatus).build());
                miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".Cell_Status_"+(i+1)).value(cellStatus).build());
            }
        }
        //当电芯挡位发生变化时,并且老的挡位不为空,删除当前电芯批次
        if(!Constants.OLD_CELL_GEAR.isEmpty() && !cellGear.equals(Constants.OLD_CELL_GEAR)){
            if(!cellSerial.isEmpty()){
                daCellDataService.deleteDaCellDataByCellSerial(cellSerial);
            }
            log.info("电芯挡位发生改变,老的挡位为:{},新的挡位为:{},批次号为{}",Constants.OLD_CELL_GEAR,cellGear,cellSerial);
        }
        return result;
    }
@@ -1040,14 +1059,21 @@
    public static String getWorkReportResultFeedback(String productNum,String stationCode,String confirmTime)
    {
        String result = "";
        String url = Constants.FACTORY_EMS_UAT_GET_RUL + "workReportResultFeedback?siteCode="+Constants.FACTORY_EMS_SITE_CODE+"&stationCode="+stationCode+"&productNum="+productNum+"&confirmTime="+confirmTime;
        try {
            String url = Constants.FACTORY_EMS_UAT_GET_RUL + "workReportResultFeedback?siteCode="+Constants.FACTORY_EMS_SITE_CODE+"&stationCode="+stationCode+"&productNum="+productNum+"&confirmTime="+confirmTime;
            log.info("执行工厂MES报工方法start,序列号{}工位号{}url{}",productNum,stationCode ,url);
            HttpResponse response = HttpRequest.get(url).execute();
            HttpRequest httpRequest = HttpRequest.get(url);
            //HttpRequest httpRequest = HttpRequest.get(url);
            result =  response.body();
            log.info("执行工厂MES报工方法第一次end,序列号{}工位号{}返回数据{}",productNum,stationCode ,result);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if(result.isEmpty()){
                HttpResponse response = HttpRequest.get(url).execute();
                result =  response.body();
                log.info("执行工厂MES报工方法第二次end,序列号{}工位号{}返回数据{}",productNum,stationCode ,result);
            }
            return result;
        }
    }
@@ -1070,6 +1096,11 @@
            );
            if (CollUtil.isNotEmpty(list)){
                for(DaCollectionParamConf conf:list){
                    //1P1S生成
                    if("1P1S".equals(conf.getCollectParameterId())){
                        String result = get1P1S(sfcCode);
                        conf.setParamCentral(result);
                    }
                    ChildVO childVO = new ChildVO();
                    childVO.setItemCode(conf.getCollectParameterId());//参数
                    childVO.setItemType(conf.getItemType());
@@ -1096,7 +1127,57 @@
            throw new RuntimeException(e);
        }
    }
    /**
     * 生成1P1S码
     * @param sfcCode
     * @return list
     */
    public static String get1P1S(String sfcCode) {
        String result = "";
        LocalDate now = LocalDate.now();
        String supplierCode  = sfcCode.substring(0,3);
        try {
            List<DaPsConf> list = daPsConfService.list(new LambdaQueryWrapper<DaPsConf>()
                    .eq(DaPsConf::getSpareField1, supplierCode)//供应商识别码
                    .eq(DaPsConf::getState,"1"));//状态
            if(!list.isEmpty()){
                String mfCode = list.get(0).getMfCode();//厂商代码
                String proTypeCode = list.get(0).getProTypeCode();//产品类型代码
                String batteryTypeCode = list.get(0).getBatteryTypeCode();//电池类型代码
                String specificationsCode = list.get(0).getSpecificationsCode();//规格代码
                String traceInfoCode = list.get(0).getTraceInfoCode();//追溯信息代码
                String oldDateCode = list.get(0).getProDateCode();
                String proDateCode = Constants.YEARSMAP.get(now.getYear())
                        + Constants.MONTHSMAP.get(now.getMonthValue())
                        + Constants.DAYMAP.get(now.getDayOfMonth());//生产日期
                String code = "";
                if(oldDateCode.equals(proDateCode)){
                    code = list.get(0).getSfcCode();//序列号
                }else {
                    code = "0";
                }
                code = StringUtils.leftPad(String.valueOf(Integer.valueOf(code)+1),7, "0");;//序列号
                result = mfCode+proTypeCode+batteryTypeCode+specificationsCode+traceInfoCode+proDateCode+code;
                log.info("读取到电芯码为:{},1P1S码为:{}",sfcCode,result);
                //更新日期和序列号
                LambdaUpdateWrapper<DaPsConf> lambdaUpdateWrapper = new LambdaUpdateWrapper<DaPsConf>();
                lambdaUpdateWrapper.set(DaPsConf::getProDateCode,proDateCode);//生产日期
                lambdaUpdateWrapper.set(DaPsConf::getSfcCode,code);//序列号
                lambdaUpdateWrapper.eq(DaPsConf::getSpareField1,supplierCode);//供应商识别码
                lambdaUpdateWrapper.eq(DaPsConf::getState,"1");//状态
                daPsConfService.update(lambdaUpdateWrapper);
            }else{
                log.info("请先去1P1S配置页面配置规则!");
            }
            return result;
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}