Browse Source

Merge branch 'prod-5.1.3' of http://175.6.40.68:8081/Jone/hzims-back into prod-5.1.3

zhongwei
liwen 3 months ago
parent
commit
651294a748
  1. 175
      hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java
  2. 2
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisResponseDTO.java
  3. 2
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/ControlEntity.java
  4. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java
  5. 30
      hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java

175
hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java

@ -1,10 +1,14 @@
package com.hnac.hzims.common.utils; package com.hnac.hzims.common.utils;
import lombok.NonNull;
import lombok.Value;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.*;
import java.io.FileInputStream; import java.net.MalformedURLException;
import java.io.IOException; import java.net.URL;
import java.net.URLDecoder;
import java.util.Base64; import java.util.Base64;
/** /**
@ -35,6 +39,131 @@ public class FileUtil {
return bytes; return bytes;
} }
public static byte[] getUrlByte(URL url) {
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
byte[] fileBytes = null;
try {
inputStream = url.openStream();
outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileBytes = outputStream.toByteArray();
} catch (Exception e) {
log.error("获取url文件失败", e);
} finally {
try {
if (null != inputStream) {
inputStream.close();
}
if (null != outputStream) {
outputStream.close();
}
} catch (Exception ignore) {
}
}
return fileBytes;
}
public static byte[] getUrlByte(String urlString) {
try {
return getUrlByte(new URL(urlString));
} catch (MalformedURLException e) {
log.error("获取url文件失败", e);
}
return null;
}
public static String getContentTypeFromUrl(String url) {
String result = "";
String fileType = url.toLowerCase();
if (fileType.indexOf(".png") >= 0) {
result = "image/png";
} else if (fileType.indexOf(".gif") >= 0) {
result = "image/gif";
} else if (fileType.indexOf(".jpg") >= 0 || fileType.indexOf(".jpeg") >= 0) {
result = "image/jpeg";
} else if (fileType.indexOf(".svg") >= 0) {
result = "image/svg+xml";
} else if (fileType.indexOf(".doc") >= 0) {
result = "application/msword";
} else if (fileType.indexOf(".xls") >= 0) {
result = "application/x-excel";
} else if (fileType.indexOf(".zip") >= 0) {
result = "application/zip";
} else if (fileType.indexOf(".pdf") >= 0) {
result = "application/pdf";
} else if (fileType.indexOf(".mpeg") >= 0) { //MP3
result = "audio/mpeg";
} else if (fileType.indexOf(".mp4") >= 0) {
result = "video/mp4";
} else if (fileType.indexOf(".plain") >= 0) {
result = "text/plain";
} else if (fileType.indexOf(".html") >= 0) {
result = "text/html";
} else if (fileType.indexOf(".json") >= 0) {
result = "application/json";
} else {
result = "application/octet-stream";
}
return result;
}
public static String getPureUrl(String url) {
String result = removeUrlParam(url);
try {
result = URLDecoder.decode(result, "utf-8");
int index = result.lastIndexOf("http");
if (index > 0) {
result = result.substring(index);
}
} catch (UnsupportedEncodingException e) {
log.error("解码url失败");
}
return result;
}
public static String getOriginalFilename(String path) {
String result = path;
int index = path.lastIndexOf("/");
if (index > 0) {
result = path.substring(index + 1);
}
return result;
}
public static String removeUrlParam(String url) {
int index = url.indexOf("&");
if (index > 0) {
return url.substring(0, index);
}
return url;
}
public static MultipartFile getMultipartFileFromUrl(String urlStr) {
String pureUrl = getPureUrl(urlStr);
return getMultipartFileFromPureUrl(pureUrl);
}
public static MultipartFile getMultipartFileFromPureUrl(String pureUrlStr) {
MultipartFile result = null;
try {
URL url = new URL(pureUrlStr);
byte[] bytes = getUrlByte(url);
String name = url.getPath();
String originalName = getOriginalFilename(name);
String contentType = getContentTypeFromUrl(originalName);
result = new ByteArrayMultipartFile(name, originalName, contentType, bytes);
} catch (Exception e) {
log.error("获取url文件失败", e);
}
return result;
}
public static String getBase64(byte[] bytes) { public static String getBase64(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes); return Base64.getEncoder().encodeToString(bytes);
} }
@ -42,4 +171,44 @@ public class FileUtil {
public static String getBase64(String filePath) { public static String getBase64(String filePath) {
return getBase64(getFileBytes(filePath)); return getBase64(getFileBytes(filePath));
} }
@Value
public static class ByteArrayMultipartFile implements MultipartFile {
String name;
String originalFilename;
String contentType;
@NonNull
byte[] bytes;
@Override
public boolean isEmpty() {
return bytes.length == 0;
}
@Override
public long getSize() {
return bytes.length;
}
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(bytes);
}
@Override
public void transferTo(File destination) throws IOException {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(destination);
outputStream.write(bytes);
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}
}
} }

2
hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisResponseDTO.java

@ -51,6 +51,6 @@ public class BigModelAnalysisResponseDTO implements Serializable {
} }
public Boolean getSuccess() { public Boolean getSuccess() {
return Optional.ofNullable(success).orElse(Boolean.FALSE); return Optional.ofNullable(success).orElse(data != null);
} }
} }

2
hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/ControlEntity.java

@ -3,6 +3,7 @@ package com.hnac.hzims.bigmodel.maintenance.entity;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.SqlCondition; import com.baomidou.mybatisplus.annotation.SqlCondition;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ -19,6 +20,7 @@ import java.io.Serializable;
@Data @Data
@EqualsAndHashCode @EqualsAndHashCode
@ApiModel("设备控制实体类") @ApiModel("设备控制实体类")
@TableName("data_yk")
public class ControlEntity extends TenantEntity implements Serializable { public class ControlEntity extends TenantEntity implements Serializable {
@ApiModelProperty(value = "itemId",required = true) @ApiModelProperty(value = "itemId",required = true)

4
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java

@ -1,6 +1,6 @@
package com.hnac.hzims.bigmodel.zhipuai.handler; package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzinfo.exception.HzServiceException;
import com.zhipu.oapi.Constants; import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest; import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage; import com.zhipu.oapi.service.v4.model.ChatMessage;
@ -79,6 +79,8 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
} catch (Exception e) { } catch (Exception e) {
log.info("get data from response error", e); log.info("get data from response error", e);
} }
} else {
throw new HzServiceException(response.getMsg());
} }
return null; return null;
} }

30
hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java

@ -5,7 +5,10 @@ import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisRequestDTO;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO;
import com.hnac.hzims.bigmodel.api.feign.IBigModelAnalysisClient; import com.hnac.hzims.bigmodel.api.feign.IBigModelAnalysisClient;
import com.hnac.hzims.common.utils.FileUtil;
import com.hnac.hzinfo.datasearch.soe.ISoeClient; import com.hnac.hzinfo.datasearch.soe.ISoeClient;
import com.hnac.hzinfo.datasearch.soe.vo.DeviceSoeVO; import com.hnac.hzinfo.datasearch.soe.vo.DeviceSoeVO;
import com.hnac.hzinfo.inspect.ai.entity.CameraInfoEntity; import com.hnac.hzinfo.inspect.ai.entity.CameraInfoEntity;
@ -33,6 +36,8 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger; import com.xxl.job.core.log.XxlJobLogger;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil; import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
@ -42,10 +47,12 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.hnac.hzinfo.inspect.task.schedule.XxlJobConstants.EXECUTE_VIDEO_TASK; import static com.hnac.hzinfo.inspect.task.schedule.XxlJobConstants.EXECUTE_VIDEO_TASK;
@ -173,6 +180,7 @@ public class VideoTaskSchedule {
String picUrl = hikApiService.manualCapture(camera.getPointCode()); String picUrl = hikApiService.manualCapture(camera.getPointCode());
record.setFileUrl(picUrl); record.setFileUrl(picUrl);
record.setCurrentValue(picUrl); record.setCurrentValue(picUrl);
pictureHandle(picUrl, content.getContentId(), record);
} }
} }
return record; return record;
@ -225,15 +233,35 @@ public class VideoTaskSchedule {
return null; return null;
} }
private void pictureHandle(String url, Long contentId) { private void pictureHandle(String url, Long contentId, EventRecordEntity record) {
MultipartFile multipartFile = FileUtil.getMultipartFileFromUrl(url);
R<BladeFile> fileResponse = ossClient.putFile(multipartFile);
if (fileResponse.isSuccess()) {
record.setFileUrl(Optional.ofNullable(fileResponse.getData().getLink()).orElse(record.getFileUrl()));
}
ContentEntity content = contentService.getById(contentId); ContentEntity content = contentService.getById(contentId);
if (StringUtil.isBlank(content.getMultiCheckTypeSon())) { if (StringUtil.isBlank(content.getMultiCheckTypeSon())) {
return; return;
} }
try {
String[] typeArray = content.getMultiCheckTypeSon().split(","); String[] typeArray = content.getMultiCheckTypeSon().split(",");
List<String> checkTypeSonList = Arrays.asList(typeArray); List<String> checkTypeSonList = Arrays.asList(typeArray);
String text = FileUtil.getBase64(multipartFile.getBytes());
BigModelAnalysisRequestDTO analysisRequest = new BigModelAnalysisRequestDTO();
analysisRequest.setCheckTypeSonList(checkTypeSonList);
analysisRequest.setUrl(text);
BigModelAnalysisResponseDTO analysisResponse = bigModelClient.analysis(analysisRequest);
if (!analysisResponse.getSuccess() || CollectionUtil.isEmpty(analysisResponse.getData())) {
return;
}
} catch (Exception e) {
log.error("video task get big model analysis result fail", e);
}
} }
} }

Loading…
Cancel
Save