package com.jcdm.main.plcserver.sub; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; 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.framework.websocket.WebSocketUsers; import com.jcdm.main.constant.Constants; import com.jcdm.main.da.cellData.service.IDaCellDataService; import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf; import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService; 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.jcdm.main.da.testDeviceInterfaceTemp.service.IDaTestDeviceInterfaceTempService; import com.jcdm.main.om.productionOrde.domain.OmProductionOrdeInfo; import com.jcdm.main.om.productionOrde.service.IOmProductionOrdeInfoService; import com.jcdm.main.plcserver.util.TimeUtil; import com.jcdm.main.restful.factoryMes.service.RestfulService; import com.jcdm.main.restful.qingYan.doman.ChildVO; import com.jcdm.main.restful.qingYan.doman.ParentVO; import com.kangaroohy.milo.model.ReadWriteEntity; import com.kangaroohy.milo.model.WriteEntity; import com.kangaroohy.milo.runner.subscription.SubscriptionCallback; import com.kangaroohy.milo.service.MiloService; import lombok.extern.slf4j.Slf4j; import org.apache.xmlbeans.GDate; import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime; import org.eclipse.milo.opcua.stack.core.types.builtin.Variant; import org.springframework.stereotype.Component; import javax.websocket.Session; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @Slf4j @Component public class OPCUaSubscription implements SubscriptionCallback { public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Map map = WebSocketUsers.getUsers(); public static MiloService miloService; public static IDaPassingStationCollectionService daPassingStationCollectionService; public static IDaCollectionParamConfService collectionParamConfService; public static IDaParamCollectionService daParamCollectionService; public static IOmProductionOrdeInfoService omProductionOrdeInfoService; public static IDaTestDeviceInterfaceTempService daTestDeviceInterfaceTempService; public static IDaCellDataService daCellDataService; public OPCUaSubscription(MiloService miloService, IDaPassingStationCollectionService daPassingStationCollectionService, IDaCollectionParamConfService collectionParamConfService, IDaParamCollectionService daParamCollectionService, IOmProductionOrdeInfoService omProductionOrdeInfoService, IDaTestDeviceInterfaceTempService daTestDeviceInterfaceTempService, IDaCellDataService daCellDataService) { OPCUaSubscription.miloService = miloService; OPCUaSubscription.daPassingStationCollectionService = daPassingStationCollectionService; OPCUaSubscription.collectionParamConfService = collectionParamConfService; OPCUaSubscription.daParamCollectionService = daParamCollectionService; OPCUaSubscription.omProductionOrdeInfoService = omProductionOrdeInfoService; OPCUaSubscription.daTestDeviceInterfaceTempService = daTestDeviceInterfaceTempService; OPCUaSubscription.daCellDataService = daCellDataService; } @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 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{ if (Constants.RECORD_DATA.equals(tab)) { //出入站 if (Constants.ONE.equals(valueString)) {//入站 if(Constants.OP040_OP160.contains(device)){//人工工位 Object snCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".SNCode").getValue(); if (ObjectUtil.isNull(snCodeObject)){ miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(12).build()); }else{ String productNum = snCodeObject.toString().trim(); if(productNum.length()<10){ miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(12).build()); }else{ //将产品SN发送到前台 productNum = "productNum,"+ productNum; WebSocketUsers.sendMessageToUserByText(map.get(device), productNum); miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(11).build()); } } }else {//自动工位 //所有进站将进站时间写给PLC SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentDate = dateFormat.format(new Date()); miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".StartTime").value(currentDate).build()); Integer recordDataDone = 11; miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(recordDataDone).build()); } } else if(Constants.TWO.equals(valueString)){//出站 if(Constants.OP040_OP160.contains(device)){//人工工位 if(Constants.OP165.equals(device)){ WebSocketUsers.sendMessageToUserByText(map.get(device), "print");//打印 }else { WebSocketUsers.sendMessageToUserByText(map.get(device), "END"); } } else if (Constants.OP0170_OP220.contains(device)) { WebSocketUsers.sendMessageToUserByText(map.get(device), "END"); } else{//自动工位 //先判断是否有产品SN Object snCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".SNCode").getValue(); Object orderObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".WorkOrderNumber").getValue(); if (ObjectUtil.isNull(snCodeObject)){ miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(25).build()); }else { Integer result = 21; String snCode = snCodeObject.toString().trim();//产品SN String orderNumber = orderObjcet.toString().trim();//工单号 if(snCode.length()<10){ result = 22; miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(result).build()); }else { //所有出站将出站时间写给PLC SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentDate = dateFormat.format(new Date()); miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".StopTime").value(currentDate).build()); Object stationStatusObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus").getValue();//站状态地址 if (ObjectUtil.isNotNull(stationStatusObjcet)) { String stationStatus = stationStatusObjcet.toString(); if (Constants.OP010.equals(device)) { Object housingCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".HousingCode").getValue();//壳体条码,存到工单,返修用 if (ObjectUtil.isNull(housingCodeObject)){ result = 25; }else{ String housingCode = housingCodeObject.toString();//壳体条码,存到工单,返修用 if(housingCode.length()<10){ result = 25; }else{ CompletableFuture cp1 = CompletableFuture.runAsync(() -> { updateOrderStatus(snCode, "3",housingCode);//OP010工位更新工单状态为执行中,并更新主物料壳体条码 reportFactoryMes(snCode, device, format.format(new Date()));//工厂MES报工 }); } } } if(Constants.OP230.equals(device)){ CompletableFuture cp1 = CompletableFuture.runAsync(() -> { updateOrderStatus(snCode, "5","");//OP230工位更新工单状态为已完成 reportFactoryMes(snCode, "OP240", format.format(new Date()));//工厂MES报工 //暂时 OP240工位还没有好,在230报240的工 }); } if(result == 21){ result = savePassingStation(thoroughfare, device, snCode, orderNumber, stationStatus);//保存过站 } if (result == 21) { result = saveParamCollection(device, snCode, orderNumber, stationStatus);//保存参数,发送工厂MES } } else { result = 23; log.info("读取到工位{}StationStatus数据:{},返回RecordDataDone的值为{}", device, "IS NULL!", result); } miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(result).build()); } } } } }else if (Constants.RECORD_SN.equals(tab)){//请求产品序列号 if (Constants.ONE.equals(valueString)){ //进站PLC给产品类型,MES读取产品类型 Object productTypeObjcet = miloService.readFromOpcUa(thoroughfare + "." + device + ".ProductType").getValue();//产品类型 if (ObjectUtil.isNotNull(productTypeObjcet)){ String productType = productTypeObjcet.toString();//产品类型 String materialCode = Constants.materialMap.get(productType); //接收工单,保存到数据库,并且将工单传给PLC CompletableFuture cp1 = CompletableFuture.runAsync(() -> { receivingWorkOrders(thoroughfare, device,materialCode); }); }else{ miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordSNDone").value(12).build()); } } }else if(Constants.SCREW.equals(tab)){ List list = new ArrayList<>(); String[] suffixes = {"Torque1", "Angle1", "TorqueResult1", "AngleResult1"}; for (String suffix : suffixes) { String string = thoroughfare + "." + device + "." + suffix; list.add(string); } List list1 = miloService.readFromOpcUa(list); List collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList()); String joinedString = String.join(",", collect.toString()); WebSocketUsers.sendMessageToUserByText(map.get(device), TightenTheConversionOkNg(joinedString)); } }catch (Exception e) { log.error(e.getMessage()); } } /** * 保存过站数据 * @param thoroughfare 通道 * @param device 工位 * @param snCode 产品序列号 * @param stationStatus 站状态 * @return Integer * @throws Exception e */ private static Integer savePassingStation(String thoroughfare, String device,String snCode,String orderNumber,String stationStatus){ Integer result = 21; try { //读进站时间 Date startTime = new Date(); ReadWriteEntity startTimeRead = miloService.readFromOpcUa(thoroughfare + "." + device + ".StartTime");//进站时间 if (ObjectUtil.isNotNull(startTimeRead.getValue())){ startTime = format.parse(startTimeRead.getValue().toString()); }else{ result = 23; log.info("读取到工位{}的StartTime数据:{},返回RecordDataDone的值为{}",device,"IS NULL!",result); return result; } DaPassingStationCollection passingStationCollection = new DaPassingStationCollection(); passingStationCollection.setSfcCode(snCode);//产品序列号 passingStationCollection.setWorkOrderNo(orderNumber);//工单号 passingStationCollection.setLocationCode(device);//工位 passingStationCollection.setInboundTime(startTime);//进站时间 passingStationCollection.setOutboundTime(new Date());//出站时间 passingStationCollection.setOutRsSign(stationStatus);//站状态值 passingStationCollection.setCollectionTime(new Date());//采集时间 daPassingStationCollectionService.save(passingStationCollection); } catch (Exception e) { throw new RuntimeException(e); } return result; } /** * 保存参数数据和发送工厂MES * @param device 工位 * @param snCode 产品SN * @param orderNumber 工单号 * @param stationStatus 站状态 * @return result * @throws Exception e */ private static Integer saveParamCollection(String device,String snCode,String orderNumber,String stationStatus){ Integer result = 21;//返回结果 try { //查询参数配置表 List list = collectionParamConfService.list(new LambdaQueryWrapper() .eq(DaCollectionParamConf::getProcessesCode, device)//工位 .eq(DaCollectionParamConf::getWhetherToCollect, Constants.ONE)//是否采集 ); if (CollUtil.isNotEmpty(list)){ List collect = list.stream() .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList()); List readWriteEntityList = miloService.readFromOpcUa(collect); List collectionList = new ArrayList<>(); List mesList = new ArrayList<>(); for (int i = 0; i < readWriteEntityList.size(); i++) { DaParamCollection daParamCollection = new DaParamCollection(); daParamCollection.setSfcCode(snCode);//产品SN daParamCollection.setWorkOrderNo(orderNumber);//工单号 daParamCollection.setParamCode(list.get(i).getCollectParameterId());//参数编码 daParamCollection.setParamName(list.get(i).getCollectParameterName());//参数名称 String paramValue = ""; if (ObjectUtil.isNotNull(readWriteEntityList.get(i).getValue())){ paramValue = readWriteEntityList.get(i).getValue().toString();//参数值 /*if("DATE".equals(list.get(i).getCollectParameterType()) && !paramValue.isEmpty()){ paramValue = format.parse(TimeUtil.test(TimeUtil.stringProcessing(paramValue))).toString(); }else if("MODEL".equals(list.get(i).getCollectParameterType()) && !paramValue.isEmpty()){ paramValue = Constants.materialMap.get(paramValue); }*/ } daParamCollection.setParamValue(paramValue);//参数值 daParamCollection.setLocationCode(device);//工位 daParamCollection.setCollectionTime(new Date());//采集时间 collectionList.add(daParamCollection);//封装参数采集list //发送给工厂mes参数封装 ChildVO childVO = new ChildVO(); childVO.setItemCode(list.get(i).getCollectParameterId());//参数编码 childVO.setItemType(list.get(i).getItemType()); childVO.setItemValue(paramValue);//参数值 childVO.setItemText(list.get(i).getCollectParameterName()); childVO.setCheckResult("1"); childVO.setCheckTime(format.format(new Date())); mesList.add(childVO); } CompletableFuture cp1 = CompletableFuture.runAsync(() -> { //插入参数采集表 daParamCollectionService.insertBatch(collectionList); //上传到工厂mes ParentVO parentVO = new ParentVO(); parentVO.setStationCode(device);//工位 parentVO.setSiteCode("3983"); parentVO.setRecordId(UUID.randomUUID().toString()); if("2".equals(stationStatus)){//工站状态 parentVO.setTotalResult("0"); }else{ parentVO.setTotalResult("1"); } parentVO.setProductNum(snCode); parentVO.setCheckList(mesList); log.info("执行工厂MES方法start,工位号{} 传入数据:{}",device ,parentVO); HttpResponse execute = HttpRequest.post(Constants.FACTORY_EMS_UAT_GET_RUL+"deviceResultFeedback").body(JSONUtil.toJsonStr(parentVO)).execute(); log.info("执行工厂MES方法end,工位号{} 返回数据:{}",device,execute.body()); }); } }catch (Exception e) { throw new RuntimeException(e); } return result; } /** * 接收工单信息 * @param thoroughfare 通道 * @param device 工位 * @param materialCode 物料号 * @throws Exception e */ public synchronized void receivingWorkOrders(String thoroughfare ,String device ,String materialCode) { try { String productNum = "";//模组码 String orderNum = "";//工单号 //先查询表中是否有剩余工单 List orderList = omProductionOrdeInfoService.list(new LambdaQueryWrapper() .eq(OmProductionOrdeInfo::getOrderStatus, Constants.ONE) .eq(OmProductionOrdeInfo::getStationCode,device)//工位 //.eq(OmProductionOrdeInfo::getProductCode,materialCode) );//产品类型 if (CollUtil.isNotEmpty(orderList)){ Long id = orderList.get(0).getId(); productNum = orderList.get(0).getProductNum();//产品码 orderNum = orderList.get(0).getWorkOrderNo(); }else{ // 查询最新的工单信息 OmProductionOrdeInfo lastOrder = omProductionOrdeInfoService.getLastOrder(); //String facMaterialCode = Constants.facMaterialMap.get(materialCode); log.info("请求工厂MES工单:入参device{},请求工厂物料编码,产品物料编码MaterialCode{}", device,materialCode); String orderJsonString = RestfulService.getProductionWorkOrderRequest(lastOrder.getProductNum(), device,""); log.info("请求工厂MES工单:出参pack:{}", orderJsonString); JSONObject jsonObject = new JSONObject(orderJsonString); // 从JSONObject中获取data对象 JSONObject dataObject = jsonObject.getJSONObject("data"); String code = jsonObject.getStr("code"); // 判断接单是否成功 if("success".equals(code)) { OmProductionOrdeInfo omProductionOrdeInfo = new OmProductionOrdeInfo(); omProductionOrdeInfo.setWorkOrderNo(dataObject.getStr("productionOrderNum")); omProductionOrdeInfo.setProductNum(dataObject.getStr("productNum")); omProductionOrdeInfo.setStationCode(device); omProductionOrdeInfo.setProductCode(materialCode); omProductionOrdeInfo.setMaterialCode(dataObject.getStr("materialCode")); omProductionOrdeInfo.setPlanQty(Long.valueOf(dataObject.getStr("plannedQuantity"))); omProductionOrdeInfo.setOnlineCompletionMark("0"); omProductionOrdeInfo.setSfResult("0"); omProductionOrdeInfo.setProductModel(dataObject.getStr("model")); omProductionOrdeInfo.setCreateTime(new Date()); omProductionOrdeInfo.setCreateUser("工厂MES"); omProductionOrdeInfoService.save(omProductionOrdeInfo); productNum = dataObject.getStr("productNum"); orderNum = dataObject.getStr("productionOrderNum"); } } //下发产品SN和工单号 if(!productNum.isEmpty() && !orderNum.isEmpty()){ miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".SNCode").value(productNum).build()); miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".WorkOrderNumber").value(orderNum).build()); miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordSNDone").value(11).build()); }else{ miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordSNDone").value(12).build()); } } catch (Exception e) { throw new RuntimeException(e); } } /** * 更新工单 * @param snCode 产品sn * @param orderStatus 工单状态 * @param materialCode 壳体条码 * @throws Exception e */ private static void updateOrderStatus(String snCode,String orderStatus,String materialCode){ try { //更新工单状态为已执行 LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.set(OmProductionOrdeInfo::getOrderStatus,orderStatus); if(!materialCode.isEmpty()){//上线 updateWrapper.set(OmProductionOrdeInfo::getTrolleyYard,materialCode);//壳体条码 updateWrapper.set(OmProductionOrdeInfo::getActualStartTime,new Date());//开始时间 }else{//下线 updateWrapper.set(OmProductionOrdeInfo::getActualEndTime,new Date());//结束时间 } updateWrapper.eq(OmProductionOrdeInfo::getProductNum,snCode); omProductionOrdeInfoService.update(new OmProductionOrdeInfo(),updateWrapper); } catch (Exception e) { throw new RuntimeException(e); } } /** * 工厂MES报工 * @param productNum 通道 * @param stationCode 工位 * @param confirmTime 时间 * @throws Exception e */ //{"code":"success","data":{"productNum":"LCV123456P0600036","stationCode":"1HZ01","resultCode":"S","resultText":"报工成功"},"message":"API调用成功"} public static void reportFactoryMes(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 { log.info("执行工厂MES报工方法start,序列号{}工位号{}url{}",productNum,stationCode ,url); HttpResponse response = HttpRequest.get(url).execute(); //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); } } } public static String TightenTheConversionOkNg(String param){ // 去除首尾的方括号,然后按照逗号分割字符串 String[] parts = param.substring(1, param.length() - 1).split(","); // 创建一个新的StringBuilder来构建替换后的字符串 StringBuilder sb = new StringBuilder(); sb.append('['); // 添加左方括号 for (int i = 0; i < parts.length; i++) { String part = parts[i].trim(); // 去除可能的空格 float value; try { value = Float.parseFloat(part); // 尝试将字符串转换为浮点数 String replacement; if(i<2){ replacement = part; }else{ if (value == 1f) { replacement = "OK"; } else{ replacement = "NG"; } } /* if (value == 1f) { replacement = "OK"; } else if (value == 2f) { replacement = "NG"; } else { replacement = part; // 如果不是1或2,则保持不变 }*/ sb.append(replacement); if (i < parts.length - 1) { sb.append(','); // 添加逗号(除了最后一个元素) } } catch (NumberFormatException e) { // 如果转换失败,则保持原样(或进行其他错误处理) sb.append(part); if (i < parts.length - 1) { sb.append(','); } } } sb.append(']'); // 添加右方括号 return sb.toString(); } /* public static void main(String[] args) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentDate = dateFormat.format(new Date()); System.out.println(currentDate); }*/ }