lxw
2023-11-09 539ee846f2a3a828b7c2b057325fbc2c85e04af4
远程升级和停止
3个文件已修改
1个文件已添加
111 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/DevUpdateStateController.java 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/pojo/DevState.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/DevUpdateStateService.java 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/FileDirPath.java 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/DevUpdateStateController.java
@@ -1,11 +1,34 @@
package com.whyc.controller;
import com.whyc.pojo.DevUpdateState;
import com.whyc.pojo.Response;
import com.whyc.service.DevUpdateStateService;
import com.whyc.util.ActionUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RequestMapping("devUpdate")
@RestController
@Api(tags = "设备升级")
public class DevUpdateStateController {
    @Autowired
    private DevUpdateStateService service;
    @ApiOperation(value = "远程升级")
    @PostMapping("updateDfu")
    @ApiImplicitParam(name = "file", value = "上传的文件", dataTypeClass = MultipartFile.class, required = false,paramType = "form")
    public Response updateDfu(@RequestPart(value = "file",required = false) MultipartFile file, @RequestParam String json){
        DevUpdateState dfu= ActionUtil.getGson().fromJson(json,DevUpdateState.class);
        return service.updateDfu(file,dfu);
    }
    @ApiOperation(value = "远程升级停止")
    @GetMapping("stopDfu")
    public Response stopDfu(@RequestParam int devId){
        return service.stopDfu(devId);
    }
}
src/main/java/com/whyc/pojo/DevState.java
@@ -39,7 +39,7 @@
    private String devVersion;
    @ApiModelProperty(value = "设备id")
    private Long devId;
    private int devId;
    @ApiModelProperty(value = "更新时间")
    private String recordDatetime;
src/main/java/com/whyc/service/DevUpdateStateService.java
@@ -1,7 +1,67 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.whyc.mapper.DevUpdateStateMapper;
import com.whyc.pojo.DevUpdateState;
import com.whyc.pojo.Response;
import com.whyc.util.FileDirPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import static com.whyc.util.ActionUtil.createFilefolderIFNotExist;
@Service
public class DevUpdateStateService {
    @Autowired(required = false)
    private DevUpdateStateMapper mapper;
    //检测是否存在重新上传的人脸
    public boolean updateDfu(MultipartFile file,int devId){
        boolean bl=false;
        String fileDirName = FileDirPath.getFileDirName();
        String root=fileDirName+ File.separator+"FDFiles"+File.separator+devId+File.separator+"update";
        String fileFileName = file.getOriginalFilename();
        String filePath = root + fileFileName;
        createFilefolderIFNotExist(filePath);
        try {
            file.transferTo(new File(filePath));
            bl=true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bl;
    }
    //远程升级
    public Response updateDfu(MultipartFile file, DevUpdateState dfu) {
        if(file==null){
            return new Response().set(1,false,"文件失败,文件不存在");
        }
        String fileFileName = file.getOriginalFilename();
        boolean bl=updateDfu(file,dfu.getDevId());
        if(bl){
            UpdateWrapper wrapper=new UpdateWrapper();
            wrapper.set("dfu_file",fileFileName);
            wrapper.set("dfu_en",1);
            wrapper.set("dfu_wr_stat",dfu.getDfuWrStat());
            wrapper.set("dfu_data_blocknum",dfu.getDfuDataBlocknum());
            wrapper.eq("dev_id",dfu.getDevId());
            mapper.update(null,wrapper);
            return new Response().set(1,bl,"升级文件上传成功");
        }else {
            return new Response().set(1,bl,"升级文件上传失败");
        }
    }
   //远程升级停止
    public Response stopDfu(int devId) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.set("dfu_en",3);
        wrapper.eq("dev_id",devId);
        mapper.update(null,wrapper);
        return new Response().set(1,true);
    }
}
src/main/java/com/whyc/util/FileDirPath.java
New file
@@ -0,0 +1,22 @@
package com.whyc.util;
import com.whyc.constant.YamlProperties;
import org.springframework.boot.system.ApplicationHome;
import java.io.File;
public class FileDirPath {
    //当前项目在dev和prod模式下访问路径
    public static String getFileDirName(){
        String fileDirName = "";
        ApplicationHome applicationHome = new ApplicationHome(FileDirPath.class);
        File jarFile = applicationHome.getDir();
        if( 1 == YamlProperties.runModel){
            fileDirName = jarFile.getParentFile().toString();
        }else{
            //打包版
            fileDirName = jarFile.toString();
        }
        return fileDirName;
    }
}