| | |
| | | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.alibaba.druid.sql.visitor.functions.Substring; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.billion.main.api.domain.StationST180; |
| | | import com.billion.main.da.domain.DaParamCollection; |
| | | import com.billion.main.da.domain.DaStationCollection; |
| | | 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.billion.system.domain.SysNotice; |
| | | import com.billion.system.service.impl.SysNoticeServiceImpl; |
| | | import com.kangaroohy.milo.model.ReadWriteEntity; |
| | | import com.kangaroohy.milo.runner.subscription.SubscriptionCallback; |
| | | import com.kangaroohy.milo.service.MiloService; |
| | |
| | | public static IDaParamCollectionService paramCollectionService; |
| | | public static IDaStationCollectionService stationCollectionService; |
| | | |
| | | public static SysNoticeServiceImpl sysNoticeService; |
| | | |
| | | public OPCUaSubscription(MiloService miloService, IScCollectionParamConfService collectionParamConfService |
| | | ,IDaParamCollectionService paramCollectionService,IDaStationCollectionService stationCollectionService) { |
| | | ,IDaParamCollectionService paramCollectionService,IDaStationCollectionService stationCollectionService,SysNoticeServiceImpl sysNoticeService) { |
| | | OPCUaSubscription.miloService = miloService; |
| | | OPCUaSubscription.collectionParamConfService = collectionParamConfService; |
| | | OPCUaSubscription.paramCollectionService = paramCollectionService; |
| | | OPCUaSubscription.stationCollectionService = stationCollectionService; |
| | | OPCUaSubscription.sysNoticeService = sysNoticeService; |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onSubscribe(String identifier, Object value) { |
| | | log.info("地址:"+identifier+"值:"+value); |
| | | try { |
| | | if(null != value && !Constants.ZERO.equals(value.toString())) { |
| | | if(ObjUtil.isNotNull(value) && !Constants.ZERO.equals(value.toString())) { |
| | | String[] nodes = identifier.split("[.]"); |
| | | String thoroughfare = nodes[0];//通道 |
| | | String device = nodes[1];//设备 |
| | |
| | | try{ |
| | | //监听recordData |
| | | if(tab.equals(Constants.RECORD_DATA)){ |
| | | //如果是2做出站处理 |
| | | if(valueString.equals(Constants.TWO)){ |
| | | if(valueString.equals(Constants.ONE)){ |
| | | // Object StationStatusObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus").getValue(); |
| | | // if (ObjUtil.isNotNull(StationStatusObject)){ |
| | | // String StationStatus = StationStatusObject.toString(); |
| | | // if (!Constants.ONE.equals(StationStatus)){ |
| | | // miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.INTEGER_ONE_TWO).build()); |
| | | // } else { |
| | | // miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.INTEGER_ONE_ONE).build()); |
| | | // } |
| | | // } |
| | | |
| | | miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(Constants.INTEGER_ONE_ONE).build()); |
| | | |
| | | } else if(valueString.equals(Constants.TWO)){ |
| | | //如果是2做出站处理 |
| | | Object SNCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".SNCode").getValue(); |
| | | if (ObjUtil.isNotNull(SNCodeObject)){ |
| | | String SnCode = SNCodeObject.toString(); |
| | | if (StrUtil.isNotBlank(SnCode)){ |
| | | savePassingStation(thoroughfare,device,"snCode"); |
| | | saveParamCollection(device, "snCode", "stationStatus"); |
| | | } |
| | | } |
| | | Integer result = Constants.INTEGER_TWO_ONE; |
| | | result = saveParamCollection(device, "snCode", "stationStatus"); |
| | | miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(result).build()); |
| | | } |
| | | } else if (tab.startsWith("OP")){ |
| | | //处理设备报警 |
| | | if (Constants.FIVE.equals(valueString)){ |
| | | //重置 |
| | | List<SysNotice> list = sysNoticeService.list(new LambdaQueryWrapper<SysNotice>() |
| | | .eq(SysNotice::getNoticeTitle, tab) |
| | | .eq(SysNotice::getStatus, Constants.ONE) |
| | | .eq(SysNotice::getNoticeType,valueString)); |
| | | if (CollUtil.isNotEmpty(list)){ |
| | | list.forEach(x ->{ |
| | | x.setStatus(Constants.ZERO); |
| | | }); |
| | | sysNoticeService.updateBatchById(list); |
| | | } |
| | | } else { |
| | | SysNotice sysNotice = new SysNotice(); |
| | | sysNotice.setNoticeTitle(tab); |
| | | sysNotice.setNoticeType(valueString); |
| | | String type = ""; |
| | | if (Constants.ONE.equals(valueString)){ |
| | | type = "缺料"; |
| | | } else if (Constants.TWO.equals(valueString)){ |
| | | type = "故障"; |
| | | } else if (Constants.THREE.equals(valueString)){ |
| | | type = "设备"; |
| | | } else if (Constants.FOUR.equals(valueString)){ |
| | | type = "急停"; |
| | | } |
| | | String content = tab + "工位" +type+ "报警"; |
| | | sysNotice.setNoticeContent(content); |
| | | sysNotice.setStatus(Constants.ONE); |
| | | sysNoticeService.insertNotice(sysNotice); |
| | | } |
| | | |
| | | } |
| | | }catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | |
| | | * @return Integer |
| | | * @throws Exception e |
| | | */ |
| | | private static Integer savePassingStation(String device, String snCode,String stationStatus){ |
| | | Integer result = 21; |
| | | private static void savePassingStation(String thoroughfare, String device, String snCode){ |
| | | try { |
| | | |
| | | |
| | | DaStationCollection daStationCollection = new DaStationCollection(); |
| | | daStationCollection.setSfcCode(snCode); |
| | | daStationCollection.setLocationCode(device); |
| | | daStationCollection.setInboundTime(new Date()); |
| | | daStationCollection.setOutboundTime(new Date()); |
| | | Object StationStatusObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus").getValue(); |
| | | if (ObjUtil.isNotNull(StationStatusObject)){ |
| | | String StationStatus = StationStatusObject.toString(); |
| | | daStationCollection.setStatus(StationStatus); |
| | | } |
| | | daStationCollection.setCollectTime(new Date()); |
| | | stationCollectionService.insertDaStationCollection(daStationCollection); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return result |
| | | * @throws Exception e |
| | | */ |
| | | private static Integer saveParamCollection(String device, String snCode, String stationStatus) { |
| | | Integer result = Constants.INTEGER_TWO_ONE; |
| | | private static void saveParamCollection(String device, String snCode, String stationStatus) { |
| | | try { |
| | | // 查询参数配置表 |
| | | List<ScCollectionParamConf> list = collectionParamConfService.list(new LambdaQueryWrapper<ScCollectionParamConf>() |
| | |
| | | .eq(ScCollectionParamConf::getSubscribe, Constants.ONE) |
| | | .orderByDesc(ScCollectionParamConf::getOrderNum) |
| | | ); |
| | | |
| | | |
| | | if (CollUtil.isNotEmpty(list)) { |
| | | // 获取参数值 |
| | | List<String> collect = list.stream() |
| | | .map(ScCollectionParamConf::getNode) |
| | | .collect(Collectors.toList()); |
| | | List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(collect); |
| | | |
| | | |
| | | // 保存采集数据 |
| | | saveCollectionData(device, snCode, list, readWriteEntityList); |
| | | |
| | | // 创建工位对象并赋值 |
| | | Object stationObject = createStationObject(device, list, readWriteEntityList); |
| | | if (stationObject != null) { |
| | | System.out.println("工位" + device + "对象创建成功:" + stationObject); |
| | | // 这里可以对创建的对象进行进一步处理 |
| | | handleMesRequest(stationObject, device); |
| | | } |
| | | |
| | | // // 创建工位对象并赋值 |
| | | // Object stationObject = createStationObject(device, list, readWriteEntityList); |
| | | // if (stationObject != null) { |
| | | // System.out.println("工位" + device + "对象创建成功:" + stationObject); |
| | | // // 这里可以对创建的对象进行进一步处理 |
| | | // |
| | | // } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("保存数据异常", e); |
| | | throw new RuntimeException("保存数据发送工厂MES异常"); |
| | | throw new RuntimeException("保存数据异常"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 保存采集数据 |
| | | */ |
| | | private static void saveCollectionData(String device, String snCode, |
| | | private static void saveCollectionData(String device, String snCode, |
| | | List<ScCollectionParamConf> list, List<ReadWriteEntity> readWriteEntityList) { |
| | | ArrayList<DaParamCollection> collectionList = new ArrayList<>(); |
| | | for (int i = 0; i < readWriteEntityList.size(); i++) { |
| | |
| | | /** |
| | | * 创建工位对象并赋值 |
| | | */ |
| | | private static Object createStationObject(String device, |
| | | private static Object createStationObject(String device, |
| | | List<ScCollectionParamConf> list, List<ReadWriteEntity> readWriteEntityList) { |
| | | try { |
| | | // 构建完整的类名 |
| | |
| | | |
| | | // 获取所有属性 |
| | | Field[] fields = stationClass.getDeclaredFields(); |
| | | |
| | | |
| | | // 创建参数值Map,方便查找 |
| | | Map<String, String> paramValueMap = new HashMap<>(); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | paramValueMap.put(list.get(i).getParamCode(), |
| | | paramValueMap.put(list.get(i).getParamCode(), |
| | | readWriteEntityList.get(i).getValue().toString()); |
| | | } |
| | | |
| | |
| | | field.setAccessible(true); |
| | | String paramCode = field.getName(); // 假设属性名与参数编码一致 |
| | | String value = paramValueMap.get(paramCode); |
| | | |
| | | |
| | | if (value != null) { |
| | | // 根据字段类型转换值 |
| | | Object convertedValue = convertValue(value, field.getType()); |
| | |
| | | } |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | private static void handleMesRequest(Object stationObject, String device) { |
| | | try { |
| | | // 发送HTTP请求 |
| | | HttpResponse response = HttpRequest.post(Constants.SY_MES_URL) |
| | | .body(JSONUtil.toJsonStr(stationObject)) |
| | | .timeout(5000) // 设置超时时间 |
| | | .execute(); |
| | | |
| | | // 解析响应 |
| | | if (response.isOk()) { // 请求成功 |
| | | JSONObject jsonResponse = JSONUtil.parseObj(response.body()); |
| | | String message = jsonResponse.getStr("message"); |
| | | |
| | | if (!"ok".equals(message)) { |
| | | // 响应不是ok,保存失败记录 |
| | | saveMesFailRecord(stationObject, device, "响应异常:" + message); |
| | | } |
| | | } else { |
| | | // HTTP请求失败,保存失败记录 |
| | | saveMesFailRecord(stationObject, device, "HTTP状态码:" + response.getStatus()); |
| | | } |
| | | } catch (Exception e) { |
| | | // 发生异常(比如连接超时、服务不可用等),保存失败记录 |
| | | saveMesFailRecord(stationObject, device, "请求异常:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存MES推送失败记录 |
| | | */ |
| | | private static void saveMesFailRecord(Object stationObject, String device, String errorMsg) { |
| | | try { |
| | | MesFailRecord failRecord = new MesFailRecord(); |
| | | failRecord.setDeviceCode(device); |
| | | failRecord.setRequestData(JSONUtil.toJsonStr(stationObject)); |
| | | failRecord.setErrorMsg(errorMsg); |
| | | failRecord.setCreateTime(new Date()); |
| | | failRecord.setStatus(0); // 0:未处理 1:已处理 |
| | | failRecord.setRetryCount(0); // 重试次数 |
| | | |
| | | // 保存到数据库 |
| | | mesFailRecordService.save(failRecord); |
| | | |
| | | log.error("MES推送失败,已保存失败记录。设备:{},错误:{}", device, errorMsg); |
| | | } catch (Exception e) { |
| | | log.error("保存MES失败记录时发生错误", e); |
| | | } |
| | | } |
| | | |
| | | } |