Browse Source

小模型视觉获取巡视结果接口

master
wangguangyuan 5 months ago
parent
commit
6082559b0e
9 changed files with 350 additions and 1 deletions
  1. +14
    -0
      src/main/java/com/inspect/simulator/controller/PatrolResultController.java
  2. +6
    -0
      src/main/java/com/inspect/simulator/domain/result/PatrolResult.java
  3. +28
    -0
      src/main/java/com/inspect/simulator/domain/result/PatrolResultParam.java
  4. +3
    -0
      src/main/java/com/inspect/simulator/domain/result/upper/MessageBody.java
  5. +17
    -0
      src/main/java/com/inspect/simulator/domain/result/upper/MessageData.java
  6. +1
    -0
      src/main/java/com/inspect/simulator/mapper/PatrolResultMapper.java
  7. +4
    -0
      src/main/java/com/inspect/simulator/service/PatrolResultService.java
  8. +231
    -0
      src/main/java/com/inspect/simulator/service/impl/PatrolResultServiceImpl.java
  9. +46
    -1
      src/main/resources/mapper/PatrolResultMapper.xml

+ 14
- 0
src/main/java/com/inspect/simulator/controller/PatrolResultController.java View File

@ -1,5 +1,7 @@
package com.inspect.simulator.controller;
import com.alibaba.fastjson.JSONObject;
import com.inspect.simulator.domain.result.PatrolResultParam;
import com.inspect.simulator.domain.result.upper.MessageBody;
import com.inspect.simulator.domain.result.upper.MessageResult;
import com.inspect.simulator.domain.result.upper.TokenParam;
@ -47,6 +49,18 @@ public class PatrolResultController {
patrolResultService.sendPatrolResultToUpperSystem();
}
@PostMapping("/patrol/result/get")
public JSONObject getPatrolResult(@RequestBody PatrolResultParam patrolResultParam) {
log.info("getPatrolResult: patrolResultParam={}", patrolResultParam);
JSONObject resultJson = new JSONObject();
MessageBody messageBody = patrolResultService.getPatrolResult(patrolResultParam);
resultJson.put("status", 200);
resultJson.put("message", "成功");
resultJson.put("checkResults", messageBody);
return resultJson;
}
//@Scheduled(cron = "*/6 * * * * ?")
public void sayHello() {
final long threadId = Thread.currentThread().getId();


+ 6
- 0
src/main/java/com/inspect/simulator/domain/result/PatrolResult.java View File

@ -129,6 +129,10 @@ public class PatrolResult extends BaseEntity {
private String imageNormalUrlPath;
private String resultType;
private String resValue;
private String phyAssetId;
public PatrolResult(String mainId) {
this.mainId = mainId;
@ -218,6 +222,8 @@ public class PatrolResult extends BaseEntity {
", resImgUrl='" + resImgUrl + '\'' +
", imageNormalUrlPath='" + imageNormalUrlPath + '\'' +
", resultType='" + resultType + '\'' +
", resValue='" + resValue + '\'' +
", phyAssetId='" + phyAssetId + '\'' +
'}';
}
}

+ 28
- 0
src/main/java/com/inspect/simulator/domain/result/PatrolResultParam.java View File

@ -0,0 +1,28 @@
package com.inspect.simulator.domain.result;
import lombok.Data;
@Data
public class PatrolResultParam {
/**
* 设备资产IDbasedata_device.phy_asset_id
*/
private String astId;
/**
* 设备类型
*/
private String deviceType;
/**
* 开始时间
*/
private String startTime;
/**
* 结束时间
*/
private String endTime;
/**
* 是否base64编码10
*/
private String isBase64;
}

+ 3
- 0
src/main/java/com/inspect/simulator/domain/result/upper/MessageBody.java View File

@ -16,6 +16,8 @@ public class MessageBody {
private List<MessageData> samples;
private List<MessageData> orignCheckData;
@Override
public String toString() {
return "MessageBody{" +
@ -23,6 +25,7 @@ public class MessageBody {
", stationCode='" + stationCode + '\'' +
", voltLevel=" + voltLevel +
", samples=" + samples +
", orignCheckData=" + orignCheckData +
'}';
}
}

+ 17
- 0
src/main/java/com/inspect/simulator/domain/result/upper/MessageData.java View File

@ -20,16 +20,30 @@ public class MessageData {
private String sampleRaw;
private String sampleRawBase64;
private String fitlerDefect;
private String fitlerDefectBase64;
private String mllmDefect;
private String mllmDefectBase64;
private String fitlerDiffBase;
private String fitlerDiffBaseBase64;
private String fitlerResultCode;
private String mllmResultCode;
private String resValue;
private String phyAssetId;
private String result;
@Override
public String toString() {
return "MessageData{" +
@ -45,6 +59,9 @@ public class MessageData {
", fitlerDiffBase='" + fitlerDiffBase + '\'' +
", fitlerResultCode='" + fitlerResultCode + '\'' +
", mllmResultCode='" + mllmResultCode + '\'' +
", resValue='" + resValue + '\'' +
", phyAssetId='" + phyAssetId + '\'' +
", result='" + result + '\'' +
'}';
}
}

+ 1
- 0
src/main/java/com/inspect/simulator/mapper/PatrolResultMapper.java View File

@ -94,4 +94,5 @@ public interface PatrolResultMapper {
List<PatrolResult> selectCurrentPatrolResultList(String createTimeStr);
List<PatrolResult> selectPatrolResults(PatrolResultParam patrolResultParam);
}

+ 4
- 0
src/main/java/com/inspect/simulator/service/PatrolResultService.java View File

@ -1,5 +1,7 @@
package com.inspect.simulator.service;
import com.alibaba.fastjson.JSONObject;
import com.inspect.simulator.domain.result.PatrolResultParam;
import com.inspect.simulator.domain.result.upper.MessageBody;
import com.inspect.simulator.domain.result.upper.MessageResult;
import com.inspect.simulator.domain.result.upper.TokenParam;
@ -12,4 +14,6 @@ public interface PatrolResultService {
MessageResult messageNotify(String token, MessageBody messageBody);
void sendPatrolResultToUpperSystem();
MessageBody getPatrolResult(PatrolResultParam patrolResultParam);
}

+ 231
- 0
src/main/java/com/inspect/simulator/service/impl/PatrolResultServiceImpl.java View File

@ -1,8 +1,10 @@
package com.inspect.simulator.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.config.common.GroupKey;
import com.google.gson.Gson;
import com.inspect.simulator.domain.result.PatrolResult;
import com.inspect.simulator.domain.result.PatrolResultParam;
import com.inspect.simulator.domain.result.PatrolUpdataLog;
import com.inspect.simulator.domain.result.upper.*;
import com.inspect.simulator.mapper.PatrolResultMapper;
@ -10,7 +12,10 @@ import com.inspect.simulator.service.IPatrolUpdataLogService;
import com.inspect.simulator.service.PatrolResultService;
import com.inspect.simulator.service.remote.UpperRemoteService;
import com.inspect.simulator.utils.DateUtils;
import com.inspect.simulator.utils.sftp.SftpClient;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -20,7 +25,11 @@ import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
@Service
@ -51,6 +60,9 @@ public class PatrolResultServiceImpl implements PatrolResultService {
@Autowired
private IPatrolUpdataLogService patrolUpdataLogService;
@Autowired
private SftpClient sftpClient;
public PatrolResultServiceImpl(PatrolResultMapper patrolResultMapper, UpperRemoteService upperRemoteService) {
this.patrolResultMapper = patrolResultMapper;
this.upperRemoteService = upperRemoteService;
@ -345,4 +357,223 @@ public class PatrolResultServiceImpl implements PatrolResultService {
}
}
@Override
public MessageBody getPatrolResult(PatrolResultParam patrolResultParam) {
List<PatrolResult> patrolResults = patrolResultMapper.selectPatrolResults(patrolResultParam);
if (patrolResults == null || patrolResults.size() <= 0) {
log.info("-----------------------getPatrolResult no data-------------");
return new MessageBody();
}
if (StringUtils.isEmpty(patrolResultParam.getEndTime()) && StringUtils.isEmpty(patrolResultParam.getStartTime())) {
List<PatrolResult> finalPatrolResults = patrolResults;
patrolResults = patrolResults.stream().filter(item -> finalPatrolResults.get(0).getTaskPatrolledId().equals(item.getTaskPatrolledId())).collect(Collectors.toList());
}
MessageBody messageBody = prepareMessageBodyToGetPatrolResults(patrolResults, patrolResultParam.getIsBase64());
return messageBody;
}
private MessageBody prepareMessageBodyToGetPatrolResults(List<PatrolResult> patrolResults, String isBase64) {
MessageBody messageBody = new MessageBody();
// 绍兴站
// messageBody.setProvinceCode("330000");
// messageBody.setStationCode("30000001-112967078");
// 灵州站
// messageBody.setProvinceCode("640000");
// messageBody.setStationCode("1cc010d8a78a598ccb52fb470301531cbff88e26c5");
// messageBody.setVoltLevel("800");
messageBody.setProvinceCode(provinceCode);
messageBody.setStationCode(stationCode);
messageBody.setVoltLevel(voltLevel);
List<MessageData> samples = new ArrayList<>();
if (patrolResults == null) {
return messageBody;
}
Map<GroupKey, List<PatrolResult>> groupedResults = patrolResults.stream()
.collect(Collectors.groupingBy(p -> new GroupKey(
p.getLineId(),
p.getDeviceName(),
p.getCreateTime(),
p.getAlgorithmsType()
)));
List<MessageData> finalSamples = samples;
groupedResults.forEach((key, group) -> {
MessageData sample = new MessageData();
PatrolResult firstPr = group.get(0);
sample.setAreaName(firstPr.getAreaName());
sample.setDeviceName(firstPr.getDeviceName());
sample.setPointName(firstPr.getPointName());
sample.setTime(DateUtils.format(DateUtils.yyyyMMddHHmmss2, firstPr.getCreateTime()));
sample.setSampleRaw(firstPr.getFilePath());
sample.setAlgorithmsName(firstPr.getAlgorithmsName());
sample.setAlgorithmsType(firstPr.getAlgorithmsType());
sample.setPhyAssetId(firstPr.getPhyAssetId());
// 处理同一分组内的不同filter结果
group.forEach(pr -> {
// result type是检测出来有缺陷的 0是缺陷 1是正常 2是异常(异常不算是缺陷比如图片抓拍失败为空算是异常或者是大模型服务挂掉分析失败也是异常这种的不算是缺陷)
String resultType = pr.getResultType();
if (StringUtils.isNotEmpty(resultType)) {
if ("1".equals(pr.getResultType())) {
resultType = "0";
} else if ("0".equals(pr.getResultType()) || "2".equals(pr.getResultType())) {
resultType = "1";
}
}
String filter = pr.getFilter();
if (MLLM_FILTER.equals(filter)) {
sample.setMllmDefect(pr.getResImgUrl());
sample.setMllmResultCode(resultType);
sample.setResValue(pr.getResValue());
} else if (FITLER_FILTER.equals(filter)) {
sample.setFitlerDefect(pr.getResImgUrl());
sample.setFitlerDiffBase(pr.getImageNormalUrlPath());
sample.setFitlerResultCode(resultType);
sample.setResValue(pr.getResValue());
}
});
finalSamples.add(sample);
});
log.info("finalSamples size: {}", finalSamples.size());
samples = finalSamples.stream().filter(item -> item.getFitlerResultCode() != null).map(item -> {
if (StringUtils.isEmpty(item.getMllmResultCode())) {
item.setMllmResultCode("0");
}
item.setResult("0".equals(item.getMllmResultCode()) ? "正常" : "缺陷");
return item;
}).collect(Collectors.toList());
if ("1".equals(isBase64)) {
Set<String> images = new HashSet<>();
for (MessageData sample : samples) {
String fitlerDefect = sample.getFitlerDefect();
String fitlerDiffBase = sample.getFitlerDiffBase();
String mllmDefect = sample.getMllmDefect();
String sampleRaw = sample.getSampleRaw();
if (StringUtils.isNotBlank(fitlerDefect)) {
images.add(fitlerDefect);
}
if (StringUtils.isNotBlank(fitlerDiffBase)) {
images.add(fitlerDiffBase);
}
if (StringUtils.isNotBlank(mllmDefect)) {
images.add(mllmDefect);
}
if (StringUtils.isNotBlank(sampleRaw)) {
images.add(sampleRaw);
}
}
Map<String, byte[]> streamHashMap = new HashMap<>();
// ExecutorService executorService = Executors.newFixedThreadPool(50); // 创建一个固定大小的线程池
// List<Future<?>> futures = new ArrayList<>();
// for (String algorithmBaseImagePath : images) {
// Future<?> future = executorService.submit(() -> {
// try {
// sftpClient.downLoad(algorithmBaseImagePath, inputStream -> {
// byte[] bytes = getBytesFromInputStream(inputStream);
// synchronized (streamHashMap) {
// streamHashMap.put(algorithmBaseImagePath, bytes);
// }
// });
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// futures.add(future);
// }
//
// // 等待所有下载任务完成
// for (Future<?> future : futures) {
// try {
// future.get();
// } catch (InterruptedException | ExecutionException e) {
// e.printStackTrace();
// }
// }
for (String algorithmBaseImagePath : images) {
try {
sftpClient.downLoad(algorithmBaseImagePath, (inputStream) -> {
byte[] bytes = streamHashMap.get(algorithmBaseImagePath);
byte[] byInputStream = getBytesFromInputStream(inputStream);
if (bytes == null) {
streamHashMap.put(algorithmBaseImagePath, byInputStream);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
for (MessageData sample : samples) {
String fitlerDefect = sample.getFitlerDefect();
String fitlerDiffBase = sample.getFitlerDiffBase();
String mllmDefect = sample.getMllmDefect();
String sampleRaw = sample.getSampleRaw();
if (StringUtils.isNotBlank(fitlerDefect)) {
sample.setFitlerDefectBase64(Base64.getEncoder().encodeToString(streamHashMap.getOrDefault(fitlerDefect, new byte[0])));
}
if (StringUtils.isNotBlank(fitlerDiffBase)) {
sample.setFitlerDiffBaseBase64(Base64.getEncoder().encodeToString(streamHashMap.getOrDefault(fitlerDiffBase, new byte[0])));
}
if (StringUtils.isNotBlank(mllmDefect)) {
sample.setMllmDefectBase64(Base64.getEncoder().encodeToString(streamHashMap.getOrDefault(mllmDefect, new byte[0])));
}
if (StringUtils.isNotBlank(sampleRaw)) {
sample.setSampleRawBase64(Base64.getEncoder().encodeToString(streamHashMap.getOrDefault(sampleRaw, new byte[0])));
}
}
// executorService.shutdown(); // 关闭线程池
// try {
// if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
// executorService.shutdownNow(); // 强制关闭线程池
// }
// } catch (InterruptedException e) {
// executorService.shutdownNow(); // 强制关闭线程池
// }
}
messageBody.setOrignCheckData(samples);
log.info("samples size: {}", samples.size());
return messageBody;
}
public static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException {
try (InputStream is = inputStream;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[10240];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toByteArray();
}
}
private String downloadToBase64(String imgUrl) {
if (StringUtils.isBlank(imgUrl)) {
return null;
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
sftpClient.downLoad(imgUrl, inputStream -> {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
});
return Base64.getEncoder().encodeToString(output.toByteArray());
} catch (Exception e) {
log.error("SFTP download failed", e);
return null;
} finally {
IOUtils.closeQuietly(output);
}
}
}

+ 46
- 1
src/main/resources/mapper/PatrolResultMapper.xml View File

@ -776,7 +776,8 @@
ra.res_img_url resImgUrl,
ra.image_normal_url_path imageNormalUrlPath,
ra.result_type resultType,
ra.filter filter
ra.filter filter,
ra.res_value resValue
from patrol_result f
left join basedata_patrolpoint a on f.device_id = a.patrol_point_id
left join basedata_device c on a.main_device_id = c.device_id
@ -789,4 +790,48 @@
where f.create_time between concat(#{createTimeStr}, ' 00:00:00') and concat(#{createTimeStr}, ' 23:59:59')
and f.check_time is null and a.patrol_point_id is not null
</select>
<select id="selectPatrolResults" resultType="com.inspect.simulator.domain.result.PatrolResult">
select f.line_id lineId,
s.task_patrolled_id taskPatrolledId,
s.start_time startTime,
d.area_name areaName,
c.device_name deviceName,
c.phy_asset_id phyAssetId,
a.patrol_point_name pointName,
f.create_time createTime,
f.file_path filePath,
bas.alg_subtype_name algorithmsName,
ra.alg_type algorithmsType,
ra.res_img_url resImgUrl,
ra.image_normal_url_path imageNormalUrlPath,
ra.result_type resultType,
ra.filter filter,
ra.res_value resValue
from patrol_result f
left join patrol_task_status s on f.task_patrolled_id = s.task_patrolled_id
left join basedata_patrolpoint a on f.device_id = a.patrol_point_id
left join basedata_device c on a.device_id = c.device_id
left join basedata_area e on a.area_id = e.area_id
left join basedata_area d on d.area_id = e.parent_id
left join result_analysis ra on f.line_id = ra.business_id and f.device_id = ra.objectId and
f.task_patrolled_id = ra.task_patrol_id
left join basedata_alg_subtype bas
on bas.alg_subtype_code = ra.alg_type and find_in_set(bas.alg_subtype_id, a.alg_subtype_ids)
where
a.patrol_point_id is not null and (ra.filter = '0' or ra.filter = '1')
<if test="astId != null and astId != ''">
and c.phy_asset_id = #{astId}
</if>
<if test="deviceType != null and deviceType != ''">
and c.device_type_name like concat('%',#{deviceType},'%')
</if>
<if test="startTime != null and startTime != ''">
and s.start_time &gt;= #{startTime}
</if>
<if test="endTime != null and endTime != ''">
and s.start_time &lt;= #{endTime}
</if>
order by s.task_patrolled_id desc
</select>
</mapper>

Loading…
Cancel
Save