文件下载bug消缺

This commit is contained in:
xy
2024-11-08 14:05:39 +08:00
parent 4b8e2488d2
commit 5c50e18fc9
2 changed files with 22 additions and 20 deletions

View File

@@ -1,18 +1,16 @@
package com.njcn.csdevice.controller.equipment;
import cn.hutool.core.collection.CollectionUtil;
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.service.DeviceFtpService;
import com.njcn.csharmonic.pojo.vo.MakeUpVo;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
@@ -25,7 +23,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Objects;
/**
* @author xy
@@ -63,9 +60,6 @@ public class DeviceFtpController extends BaseController {
public HttpResult<List<MakeUpVo>> askDeviceFileOrDir(@RequestParam("nDid") String nDid, @RequestParam("name") String name, @RequestParam("type") String type){
String methodDescribe = getMethodDescribe("askDeviceFileOrDir");
List<MakeUpVo> list = deviceFtpService.askDeviceFileOrDir(nDid,name,type);
if (CollectionUtil.isEmpty(list) && Objects.equals(type,"file")) {
throw new BusinessException(AlgorithmResponseEnum.ASK_DEVICE_DIR_ERROR);
}
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
}
@@ -79,6 +73,7 @@ public class DeviceFtpController extends BaseController {
@ApiImplicitParam(name = "fileCheck", value = "文件校验码", required = true)
})
public HttpResult<String> downloadFile(@RequestParam("nDid") String nDid, @RequestParam("name") String name, @RequestParam("size") Integer size, @RequestParam("fileCheck") String fileCheck){
redisUtil.saveByKeyWithExpire("fileDownUserId"+nDid+name,RequestUtil.getUserIndex(),600L);
deviceFtpService.downloadFile(nDid,name,size,fileCheck);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, "文件下载中,请稍等");
}

View File

@@ -45,7 +45,10 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
public MakeUpVo askDeviceRootPath(String nDid) {
MakeUpVo makeUpVo = new MakeUpVo();
try {
judgeClientOnline(nDid);
boolean mqttClient = judgeClientOnline(nDid);
if (!mqttClient){
throw new BusinessException(AccessResponseEnum.MISSING_CLIENT);
}
redisUtil.delete(AppRedisKey.DEVICE_ROOT_PATH + nDid);
askDeviceDataFeignClient.askDeviceRootPath(nDid);
Thread.sleep(3000);
@@ -55,7 +58,7 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
makeUpVo.setType("dir");
}
} catch (Exception e) {
throw new BusinessException(AlgorithmResponseEnum.ASK_DEVICE_DIR_ROOT_ERROR);
throw new BusinessException(e.getMessage());
}
return makeUpVo;
}
@@ -64,7 +67,10 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
public List<MakeUpVo> askDeviceFileOrDir(String nDid, String name, String type) {
List<MakeUpVo> result = new ArrayList<>();
try {
judgeClientOnline(nDid);
boolean mqttClient = judgeClientOnline(nDid);
if (!mqttClient){
throw new BusinessException(AccessResponseEnum.MISSING_CLIENT);
}
// 删除旧的缓存信息
redisUtil.delete(AppRedisKey.PROJECT_INFO + nDid);
// 请求设备文件或目录信息
@@ -78,7 +84,7 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
}
} catch (Exception e) {
// 捕获特定异常并抛出运行时异常
throw new BusinessException(AlgorithmResponseEnum.ASK_DEVICE_DIR_ERROR);
throw new BusinessException(e.getMessage());
}
if (CollUtil.isNotEmpty(result)) {
result.stream().sorted(Comparator.comparing(MakeUpVo::getType)).collect(Collectors.toList());
@@ -89,7 +95,10 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
@Override
@Async
public void downloadFile(String nDid, String name, Integer size, String fileCheck) {
judgeClientOnline(nDid);
boolean mqttClient = judgeClientOnline(nDid);
if (!mqttClient) {
throw new BusinessException(AccessResponseEnum.MISSING_CLIENT);
}
Object task = redisUtil.getObjectByKey("fileDowning:"+nDid);
if (Objects.nonNull(task)) {
throw new BusinessException(AlgorithmResponseEnum.FILE_DOWNLOADING);
@@ -104,7 +113,7 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
} catch (Exception e) {
redisUtil.delete("fileDowning:"+nDid);
redisUtil.delete("fileCheck"+nDid+name);
throw new BusinessException(AlgorithmResponseEnum.FILE_DOWNLOADING);
throw new BusinessException(e.getMessage());
}
}
@@ -119,6 +128,7 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
redisUtil.delete("fileCheck"+nDid+name);
redisUtil.delete("fileDowning:"+nDid);
redisUtil.delete(AppRedisKey.FILE_PART.concat(name));
redisUtil.delete("fileDownUserId"+nDid+name);
return result;
}
@@ -134,7 +144,7 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
result = true;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
throw new RuntimeException(e.getMessage());
}
return result;
}
@@ -151,7 +161,7 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
result = true;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
throw new RuntimeException(e.getMessage());
}
return result;
}
@@ -198,13 +208,10 @@ public class DeviceFtpServiceImpl implements DeviceFtpService {
return vo;
}
public void judgeClientOnline(String nDid) {
public boolean judgeClientOnline(String nDid) {
//判断客户端是否在线
String clientName = "NJCN-" + nDid.substring(nDid.length() - 6);
boolean mqttClient = mqttUtil.judgeClientOnline(clientName);
if (!mqttClient){
throw new BusinessException(AccessResponseEnum.MISSING_CLIENT);
}
return mqttUtil.judgeClientOnline(clientName);
}