|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |