cl
2024-01-19 e0fac38b26845f25de479783e0c76cf12a5311e0
guns-vip-main/src/main/java/cn/stylefeng/guns/plcserver/server/DOP020/impl/DOP020ServerInterfaceImpl.java
@@ -8,6 +8,14 @@
import cn.stylefeng.guns.modular.cm.paramCollection.service.ParamCollectionService;
import cn.stylefeng.guns.modular.cm.passingStationCollection.model.params.PassingStationCollectionParam;
import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
import cn.stylefeng.guns.modular.dq.materialTraceability.model.params.MaterialTraceabilityParam;
import cn.stylefeng.guns.modular.dq.materialTraceability.service.MaterialTraceabilityService;
import cn.stylefeng.guns.modular.gm.greaseManage.model.params.GreaseManageParam;
import cn.stylefeng.guns.modular.gm.greaseManage.model.result.GreaseManageResult;
import cn.stylefeng.guns.modular.gm.greaseManage.service.GreaseManageService;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
import cn.stylefeng.guns.plcserver.opc.OPCComm;
import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
@@ -19,6 +27,10 @@
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
@@ -44,10 +56,33 @@
    private static String GROUP_NAME  = "DOP020_P";//组名
    private static List list = null;
    private static Group group = null;
    private int count = 0;
    /**
     * 设置心跳标志位,保存到全局变量
     */
    public void setHeartbeat(String value) {
        if ("false".equals(value)) {
            OPCUnit.write(opc.getDOP020_F_HEART_BEAT(), "true");
            GlobalVariable.DOP020_F_HEART_BEAT = true;
            count = 0;
        } else {
            count++;
        }
        // 循环读取频率是:500ms一次,如果超过6次,也就是3s认定超时
        if (count > GlobalVariable.HEART_MAX_COUNT) {
            GlobalVariable.DOP020_F_HEART_BEAT = false;
        }
    }
    @Override
    public synchronized void monitor() {
        String ecpStr = "";//异常记录标记
        try{
           /* String isHeartBeat = OPCUnit.read(opc.getDOP020_F_HEART_BEAT());
            setHeartbeat(isHeartBeat);*/
            if(GlobalVariable.DOP020_F_HEART_BEAT) {
                //PC读工单
                String isPLCStart = OPCUnit.read(opc.getDOP020_J_PLC_START());//PLC开始
@@ -64,11 +99,12 @@
                if(isPLCFinish.equals("true")){
                    String isFinish = OPCUnit.read(opc.getDOP020_J_PC_FINISH());//PC操作完成
                    if(isFinish.equals("false")){
                        handleInfo();
                        handleFinsh();
                        handleInfo();
                        handleMaterial();
                    }
                }
                handleOilState();
            }else{
                logger.info("DOP020工位与PLC断开,请检查PLC或网络是否异常!");
            }
@@ -80,6 +116,33 @@
                logger.info(ecpStr+"\r\n");
            }
        }
    }
    public void handleOilState(){
        GreaseManageParam param = new GreaseManageParam();
        GreaseManageService greaseManageService = opc.greaseManageService;
        param.setLocationCode(LOCATION_CODE);
        List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
        if(list.size()>0) {
            String duaDate = list.get(0).getDuaDate();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            String currentDate = format.format(new Date());
            long  daysBetween = getRemainder(currentDate,duaDate);
            if(daysBetween>0){
                OPCUnit.write(opc.getDOP020_OIL_STATE(), "false"); //写油脂状态
            }else{
                OPCUnit.write(opc.getDOP020_OIL_STATE(), "true"); //写油脂状态
            }
            //logger.info("到期日期:"+duaDate+"当前日期:"+currentDate+"剩余天数::"+daysBetween);
        }
    }
    public static long getRemainder(String currentDate,String tableDate){
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDate1 = LocalDate.parse(currentDate, dateFormatter);
        LocalDate localDate2 = LocalDate.parse(tableDate, dateFormatter);
        long days = ChronoUnit.DAYS.between(localDate1, localDate2);
        return days;
    }
    //PC读工单
@@ -146,6 +209,42 @@
        logger.info("DOP020工位,handleInfo END!");
    }
    public void handleMaterial() {
        //查询批次上料信息
        ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
        productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
        productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
        ProductionOrderBatchInfoService productionOrderBatchInfoService = opc.productionOrderBatchInfoService;
        List<ProductionOrderBatchInfoResult> list1 = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
        for(int i=0;i<list1.size();i++){
            ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list1.get(i);
            int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
            if(remainingQuantity>0 ){
                //新增物料追溯
                MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
                materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
                materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
                materialTraceabilityParam.setProductNo(S_SFC_CODE);
                materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
                materialTraceabilityParam.setLocationCode(LOCATION_CODE);
                materialTraceabilityParam.setAssemblyQty(1);
                materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
                materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
                materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
                MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
                materialTraceabilityService.add(materialTraceabilityParam);
                //更新剩余数量
                productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
                productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
                productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
            }
        }
    }
    //PC完成
    public void handleFinsh() {
        if("".equals(S_ORDER_CODE)){