plc日志文件查询,下载,配置更新接口新增

This commit is contained in:
huguanghan 2024-11-14 17:45:17 +08:00
parent 71a3e989b5
commit a8d8a6c1bc
5 changed files with 244 additions and 2 deletions

View File

@ -119,4 +119,9 @@ public class SysEquipmentVo implements Serializable {
*/ */
private Double nominalCapacity; private Double nominalCapacity;
/**
* 解析文件配置
*/
private String options;
} }

View File

@ -108,12 +108,12 @@ public class FaultRecorderServiceImpl implements FaultRecorderService {
} }
if (StringUtils.isBlank(sysEquipment.getOptions())){ if (StringUtils.isBlank(sysEquipment.getOptions())){
throw new ServerException("请添加设备文件解析配置"); throw new ServiceException("请添加设备文件解析配置");
} }
FileParseConfig fileParseConfig = JSON.parseObject(sysEquipment.getOptions(), FileParseConfig.class); FileParseConfig fileParseConfig = JSON.parseObject(sysEquipment.getOptions(), FileParseConfig.class);
FdrFormatVo fdrFormatVo = fileParseConfig.getFdrFormat(); FdrFormatVo fdrFormatVo = fileParseConfig.getFdrFormat();
if (fdrFormatVo == null){ if (fdrFormatVo == null){
throw new ServerException("请添加故障录波配置"); throw new ServiceException("请添加故障录波配置");
} }
// 解析文件内容 // 解析文件内容
resultMap = parseFile(fileStream, fdrFormatVo.getTimeFormat(), fdrFormatVo.getDelimiter(), fdrFormatVo.getValidStartLine()); resultMap = parseFile(fileStream, fdrFormatVo.getTimeFormat(), fdrFormatVo.getDelimiter(), fdrFormatVo.getValidStartLine());

View File

@ -0,0 +1,59 @@
package com.das.modules.plc.controller;
import com.alibaba.fastjson.JSONObject;
import com.das.common.result.R;
import com.das.modules.equipment.entity.SysEquipment;
import com.das.modules.fdr.domain.FileNode;
import com.das.modules.fdr.domain.dto.FileDownloadDto;
import com.das.modules.fdr.service.FaultRecorderService;
import com.das.modules.plc.service.PlcLogService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* 故障录波controller
*/
@Slf4j
@RequestMapping("/api/plc")
@RestController
public class PlcLogsController {
@Autowired
private PlcLogService plcLogService;
@RequestMapping(value = "/files", method = RequestMethod.POST)
public R<List<FileNode>> findList(@RequestBody JSONObject jsonObject) {
String code = jsonObject.getString("deviceCode");
String startTime = jsonObject.getString("startTime");
String endTime = jsonObject.getString("endTime");
List<FileNode> result = plcLogService.getDirOrFileList("日志",code,startTime,endTime);
return R.success(result);
}
@RequestMapping(value = "/parseData", method = RequestMethod.POST)
public R<Map<String, List<Object>>> parseData(@RequestBody JSONObject jsonObject) throws IOException {
Map<String, List<Object>> dataCurve = plcLogService.getDataCurve(jsonObject.getString("url"), jsonObject.getString("deviceCode"));
return R.success(dataCurve);
}
@RequestMapping(value = "/updatePlcLogConfig", method = RequestMethod.POST)
public R<Void> updateFdrConfig(@RequestBody SysEquipment sysEquipment){
plcLogService.updatePlcConfig(sysEquipment);
return R.success();
}
@RequestMapping(value = "/download", method = RequestMethod.POST)
public void downloadFdrFile(@RequestBody FileDownloadDto fileDownloadDto, HttpServletResponse httpServletResponse) throws IOException {
plcLogService.download(fileDownloadDto.getUrl(),httpServletResponse);
}
}

View File

@ -0,0 +1,20 @@
package com.das.modules.plc.service;
import com.das.modules.equipment.entity.SysEquipment;
import com.das.modules.fdr.domain.FileNode;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public interface PlcLogService {
List<FileNode> getDirOrFileList(String fileType, String name, String startTime, String endTime);
Map<String, List<Object>> getDataCurve(String url, String deviceCode) throws IOException;
void updatePlcConfig(SysEquipment sysEquipment);
void download(String path, HttpServletResponse httpServletResponse) throws IOException;
}

View File

@ -0,0 +1,158 @@
package com.das.modules.plc.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.das.common.exceptions.ServiceException;
import com.das.modules.equipment.entity.SysEquipment;
import com.das.modules.equipment.mapper.SysEquipmentMapper;
import com.das.modules.fdr.domain.FileNode;
import com.das.modules.fdr.domain.vo.FdrFormatVo;
import com.das.modules.fdr.domain.vo.FileParseConfig;
import com.das.modules.fdr.service.FaultRecorderService;
import com.das.modules.fdr.service.MinioViewsServcie;
import com.das.modules.plc.service.PlcLogService;
import io.micrometer.common.util.StringUtils;
import io.minio.MinioClient;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
@Slf4j
public class PlcLogsServiceImpl implements PlcLogService {
@Autowired
private MinioViewsServcie minioViewsServcie;
@Autowired
MinioClient minioClient;
@Autowired
private FaultRecorderService faultRecorderService;
@Autowired
private SysEquipmentMapper sysEquipmentMapper;
@Override
public List<FileNode> getDirOrFileList(String fileType, String name, String startTime, String endTime) {
return faultRecorderService.getDirOrFileList(fileType, name, startTime, endTime);
}
@Override
public Map<String, List<Object>> getDataCurve(String url, String deviceCode) throws IOException {
Map<String, List<Object>> resultMap = null;
try (InputStream fileStream = minioViewsServcie.getFileStream(url)) {
//根据device Code查询故障录波格式
QueryWrapper<SysEquipment> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("CODE", deviceCode);
SysEquipment sysEquipment = sysEquipmentMapper.selectOne(queryWrapper);
if (sysEquipment == null) {
throw new ServiceException("设备不存在,请选择正确设备");
}
if (StringUtils.isBlank(sysEquipment.getOptions())){
throw new ServiceException("请添加设备文件解析配置");
}
FileParseConfig fileParseConfig = JSON.parseObject(sysEquipment.getOptions(), FileParseConfig.class);
FdrFormatVo fdrFormatVo = fileParseConfig.getPlcFormat();
if (fdrFormatVo == null){
throw new ServiceException("请添加plclog配置");
}
// 解析文件内容
resultMap = parseFile(fileStream, fdrFormatVo.getTimeFormat(), fdrFormatVo.getDelimiter(), fdrFormatVo.getValidStartLine());
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
@Override
public void updatePlcConfig(SysEquipment sysEquipment) {
String options = sysEquipment.getOptions();
FileParseConfig fdrConfig = JSON.parseObject(options, FileParseConfig.class);
SysEquipment equipment = sysEquipmentMapper.selectById(sysEquipment.getId());
//合并配置
if (StringUtils.isNotEmpty(equipment.getOptions())){
FileParseConfig allConfig = JSON.parseObject(equipment.getOptions(), FileParseConfig.class);
allConfig.setPlcFormat(fdrConfig.getPlcFormat());
sysEquipment.setOptions(JSON.toJSONString(allConfig));
}
sysEquipmentMapper.updateById(sysEquipment);
}
@Override
public void download(String path, HttpServletResponse httpServletResponse) throws IOException {
faultRecorderService.download(path,httpServletResponse);
}
public Map<String, List<Object>> parseFile(InputStream inputStream, String timeFormat, String delimiter, int validStartLine) {
List<List<String>> result = new ArrayList<>();
Map<String, List<Object>> stringListMap = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
// 忽略有效行之前的行
if (lineNumber < validStartLine) {
continue;
}
// 按照分隔符分割行数据
List<String> lineData = Arrays.stream(line.split(delimiter)).toList();
result.add(lineData);
}
stringListMap = parseDataCurve(result, timeFormat);
} catch (Exception e) {
log.error("文件解析失败{}", e);
}
return stringListMap;
}
public Map<String, List<Object>> parseDataCurve(List<List<String>> data, String timeFormat) throws ParseException {
List<String> listField = data.get(0);
Map<String, List<Object>> map = new HashMap<>();
data.remove(0);
for (List<String> item : data) {
for (int i = 0; i < item.size(); i++) {
if (map.get(listField.get(i)) == null) {
if (i == 0){
List<Object> timeList = new ArrayList<>();
long timestamp = convertToTimestamp(item.get(i), timeFormat);
timeList.add(timestamp);
map.put(listField.get(i),timeList);
}else {
List<Object> valueList = new ArrayList<>();
valueList.add(Double.valueOf(item.get(i)));
map.put(listField.get(i), valueList);
}
} else {
List<Object> valueList = map.get(listField.get(i));
if (i == 0){
valueList.add(convertToTimestamp(item.get(i),timeFormat));
}
else {
valueList.add(Double.valueOf(item.get(i)));
}
}
}
}
return map;
}
public long convertToTimestamp(String time, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(time).getTime();
}
}