lxw
2023-04-25 a3dd5e747bc4b8e8ceaead18f1055c07f51a0cf0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.io.File;
import java.util.LinkedList;
import java.util.List;
 
@Api(tags = "语音")
@RestController
@RequestMapping("voice")
public class VoiceController {
 
    @Autowired
    private AlarmDataService alarmDataService;
 
    @Autowired
    private AlarmVoiceSetService alarmVoiceSetService;
 
    /**
     * 用户的语音文件路径层级
     * /fg_file/wav/alarm/uId/unread/
     * /fg_file/wav/alarm/uId/read/
     * @return
     */
    @ApiOperation("查询用户未读的wav语音文件列表")
    @GetMapping("wavUnread")
    public Response getWavUnread(){
        UserInf user = ActionUtil.getUser();
        //获取绝对文件夹路径
        String unreadSuffixPath = "wav" + File.separator + "alarm" + File.separator + user.getUId() + File.separator + "unread";
        String unreadDirPath = CommonUtil.getRootFile() + unreadSuffixPath;
        File unreadDir = new File(unreadDirPath);
        //保存用户的告警语音文件
        alarmDataService.saveToWav(unreadDirPath);
        if(!unreadDir.exists()){
            return new Response().set(1,false,"无未读告警语音");
        }else{
            String[] arr = unreadDir.list();
            if(arr.length==0){
                return new Response().set(1,false,"无未读告警语音");
            }
            List<String> fileNameList = new LinkedList<>();
            for (String fileName : arr) {
                fileName="fg_file"+File.separator+unreadSuffixPath+File.separator+fileName;
                fileNameList.add(fileName);
            }
            return new Response().setII(1,true,fileNameList,null);
        }
    }
 
    @ApiOperation("删除用户wav语音文件")
    @DeleteMapping("wavRead")
    public void deleteWavRead(@RequestParam(required = false) String fileFullName){
        UserInf user = ActionUtil.getUser();
        //获取绝对文件夹路径
        String unreadSuffixPath = "wav" + File.separator + "alarm" + File.separator + user.getUId() + File.separator + "unread";
        if(fileFullName!=null) { //删除用户下指定的语音
            String deletedFilePath = CommonUtil.getRootFile() + unreadSuffixPath + File.separator + fileFullName;
            File deletedFile = new File(deletedFilePath);
            deletedFile.delete();
        }else{ //清空用户下的所有语音
            String deletedDirPath = CommonUtil.getRootFile() + unreadSuffixPath;
            File deletedDir = new File(deletedDirPath);
            File[] files = deletedDir.listFiles();
            for (File file:files) {
                file.delete();
            }
        }
    }
 
    @ApiOperation("用户wav语音文件播放完毕")
    @PutMapping("wavRead")
    public void updateWavRead(@RequestParam(required = false) String 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();
    }
 
}