whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/controller/VoiceController.java
@@ -1,8 +1,10 @@
package com.whyc.controller;
import com.whyc.dto.Response;
import com.whyc.pojo.AlarmVoiceSet;
import com.whyc.pojo.UserInf;
import com.whyc.service.AlarmDataService;
import com.whyc.service.AlarmVoiceSetService;
import com.whyc.service.MSTTSSpeechService;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
@@ -23,6 +25,9 @@
    @Autowired
    private AlarmDataService alarmDataService;
    @Autowired
    private AlarmVoiceSetService alarmVoiceSetService;
    /**
     * 用户的语音文件路径层级
     * /fg_file/wav/alarm/uId/unread/
@@ -30,7 +35,7 @@
     * @return
     */
    @ApiOperation("查询用户未读的wav语音文件列表")
    @GetMapping
    @GetMapping("wavUnread")
    public Response getWavUnread(){
        UserInf user = ActionUtil.getUser();
        //获取绝对文件夹路径
@@ -56,8 +61,10 @@
    }
    @ApiOperation("删除用户wav语音文件")
    @DeleteMapping
    @PostMapping("deleteWavRead")
    public void deleteWavRead(@RequestParam(required = false) String fileFullName){
        //过滤特殊字符,避免路径遍历攻击
        fileFullName = ActionUtil.filterFileName(fileFullName);
        UserInf user = ActionUtil.getUser();
        //获取绝对文件夹路径
        String unreadSuffixPath = "wav" + File.separator + "alarm" + File.separator + user.getUId() + File.separator + "unread";
@@ -75,4 +82,63 @@
        }
    }
    @ApiOperation("用户wav语音文件播放完毕")
    @PostMapping("wavRead")
    public void updateWavRead(@RequestParam(required = false) String fileFullName){
        //过滤特殊字符,避免路径遍历攻击
        fileFullName = ActionUtil.filterFileName(fileFullName);
        UserInf user = ActionUtil.getUser();
        long currentTimeMillis = System.currentTimeMillis();
        Long specialId = null;
        int type = 0;
        int alarmType = 0;
        //获取绝对文件夹路径
        String readSuffixPath = "wav" + File.separator + "alarm" + File.separator + user.getUId() + File.separator;
        String fromFilePath = CommonUtil.getRootFile() + readSuffixPath + fileFullName;
        //语音文件,命名规则 {alarmStartTime}_device_{num}_{deviceId}_{almType}_{readableVoiceTime}.wav
        //更新下次播放时间:如果set表没有对应的时间间隔,则默认5分钟后,有则按照表中的时间间隔(单位:分钟).
        if(fileFullName.contains("batt")){
            type=1;
        }else if(fileFullName.contains("device")){
            type=2;
        }else{
            type=3;
        }
        String[] nameSplit = fileFullName.split("_");
        specialId = Long.parseLong(nameSplit[3]);
        alarmType = Integer.parseInt(nameSplit[4]);
        String readableVoiceTimeWithWav = nameSplit[5];
        AlarmVoiceSet nextTimeIntervalSet = alarmVoiceSetService.getNextTimeInterval(user.getUId(), type, specialId, alarmType);
        int addedTimeMillis = 0;
        if(nextTimeIntervalSet !=null){
            //间隔时间(单位分钟)失效,删除
            Integer nextTimeInterval = nextTimeIntervalSet.getNextTimeInterval();
            addedTimeMillis = nextTimeInterval*60*1000;
            alarmVoiceSetService.delete(nextTimeIntervalSet.getId());
        }else{ //默认5分钟
            addedTimeMillis = 5*60*1000;
            //addedTimeMillis = 1*60*1000;
        }
        String readableVoiceTimeWithWavNew = currentTimeMillis+addedTimeMillis+".wav";
        //文件下次播放时间确定新的文件名
        String renameToFileFullName = fileFullName.replace(readableVoiceTimeWithWav,readableVoiceTimeWithWavNew);
        String renameToFilePath = CommonUtil.getRootFile() + readSuffixPath + renameToFileFullName;
        //播报完毕后,将播报时间更新到文件名上
        File fromFile = new File(fromFilePath);
        File renameToFile = new File(renameToFilePath);
        fromFile.renameTo(renameToFile);
    }
    /**
     * 用户的语音文件路径层级
     * /fg_file/wav/alarm/uId/
     * @return
     */
    @ApiOperation("查询用户目前可读的wav语音文件列表")
    @GetMapping("wavRead")
    public Response getWavRead(){
        return alarmDataService.getWavRead();
    }
}