Commit ab298873 by 苗权威

9.9

parent 76df569d
......@@ -20,7 +20,8 @@ public class Config {
private static Properties properties;
static {
String filename = "tts.properties";
//String filename = "tts.properties";
String filename = "ttsTest.properties";
properties = new Properties();
try {
properties.load(new InputStreamReader(Config.class.getClassLoader().getResourceAsStream(filename), StandardCharsets.UTF_8));
......
......@@ -99,14 +99,13 @@ public class OkHttp3Utils {
return doPost(url , new HashMap<>(), new HashMap<>(), file);
}
public static String doPostJsonStr(String url, Map<String, String> headers, String requestJsonStr) throws IOException {
public static String doPostJsonStr(String url, String requestJsonStr) throws IOException {
Request.Builder builder = new Request.Builder();
Request request = builder.url(url).build();
Headers.Builder headBuilder = request.headers().newBuilder();
Iterator<Map.Entry<String, String>> headIterator = headers.entrySet().iterator();
headIterator.forEachRemaining(e -> headBuilder.add(e.getKey(), e.getValue()));
headBuilder.add("Content-Type","application/json; charset=utf-8");
builder.headers(headBuilder.build()).post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestJsonStr));
try (Response response = client.newCall(builder.build()).execute()){
......
package ai.qiwu.com.tts_service.common;
import ai.qiwu.com.tts_service.service.Initialize;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
......@@ -25,6 +26,7 @@ import java.util.zip.ZipFile;
@Slf4j
public class TTSUtils {
private static final String STRIP = " /'\"";
private static final List<Character> TRIP_ENTER = Arrays.asList('\r','\n');
public static String charset(File file) {
String charset = "GB18030";
......@@ -80,6 +82,13 @@ public class TTSUtils {
return charset;
}
public static String tripEnter(String str) {
str = str.trim();
int i = str.length()-1;
while (TRIP_ENTER.contains(str.charAt(i))) --i;
return str.substring(0, i+1);
}
public static boolean checkZipFile(String zipFilePath) {
try (ZipFile zipFile = new ZipFile(zipFilePath,Charset.forName("GBK"))){
Enumeration<?> entries = zipFile.entries();
......@@ -162,11 +171,20 @@ public class TTSUtils {
public static File createFileReturnIts(String path) throws IOException {
File file = new File(path);
if (file.exists()) {
log.info("createFileReturnIts:文件:{}已存在",path);
if (file.delete()) {
if (file.createNewFile()) return new File(path);
log.info("createFileReturnIts:文件:{}已存在,删除成功",path);
if (file.createNewFile()) {
log.info("createFileReturnIts:文件:{}已存在,删除成功,创建成功",path);
return new File(path);
}
}
}else {
if (file.createNewFile()) return new File(path);
log.info("createFileReturnIts:文件:{}不存在",path);
if (file.createNewFile()) {
log.info("createFileReturnIts:文件:{}不存在,创建成功",path);
return new File(path);
}
}
return null;
}
......@@ -220,7 +238,6 @@ public class TTSUtils {
}
public static boolean doesFileExist(String filePath) {
if (filePath == null) return false;
File file = new File(filePath);
if (file.exists()) {
//log.info("文件:{},大小为:{}", filePath, file.length());
......@@ -394,7 +411,10 @@ public class TTSUtils {
public static void main(String[] args) {
File deleteBotDir = new File("/home/yf01/springbootdemo");
File[] needDelete = deleteBotDir.listFiles((dir, name) -> !name.equals("record"));
System.out.println(needDelete.length);
System.out.println(Arrays.toString(needDelete));
//System.out.println(needDelete.length);
//System.out.println(Arrays.toString(needDelete));
File file = new File("/home/yf01/en.mp3");
file.renameTo(new File("/home/yf01/springbootdemo/nihao.mp3"));
}
}
......@@ -89,12 +89,23 @@ public class TTSController {
response.setStatus(200);
}
@PostMapping("/api/upload/one_sen")
@PostMapping("/api/upload/record/one_sen")
public void uploadOneSen(MultipartFile file, Integer tid, String botName, String npcName,
String text, HttpServletResponse response) {
UploadOneSenData data = new UploadOneSenData(tid, botName, npcName, text, file);
ttService.uploadOneSen(data, response);
}
@PostMapping("/api/upload/record/one_npc")
public void uploadOneNpc(MultipartFile file, String botName, String npcName,
HttpServletResponse response) {
UploadOneSenData data = new UploadOneSenData(botName, npcName, file);
ttService.uploadOneNpc(data, response);
}
@DeleteMapping("/api/delete/record/one_sen")
public void deleteOneSen(@RequestBody UploadOneSenData data,HttpServletResponse response) {
ttService.delOneSen(data, response);
}
}
package ai.qiwu.com.tts_service.pojo.upload;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
/**
* @author 苗权威
* @dateTime 19-9-9 下午8:17
*/
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class UploadCallbackResponse {
private Integer code;
private String msg;
}
......@@ -35,4 +35,10 @@ public class UploadOneSenData {
this.npcName = npcName;
this.file = file;
}
public UploadOneSenData(String botName, String npcName, String text) {
this.botName = botName;
this.npcName = npcName;
this.text = text;
}
}
......@@ -44,6 +44,7 @@ public class Initialize implements InitializingBean {
private static final String CHANGE_SOUND;
private static final String UPLOADED_DIR;
private static final String delCacheCallbackUrl;
private static final String delRecordCallbackUrl;
private static final String record_callback_url;
private final SpeakerInfoRepository speakerInfoRepository;
......@@ -63,6 +64,7 @@ public class Initialize implements InitializingBean {
AUDIO_DIR = Config.getProperty("audio_dir");
UPLOADED_DIR = Config.getProperty("record_dir");
delCacheCallbackUrl = Config.getProperty("del_cache_callback_url");
delRecordCallbackUrl = Config.getProperty("del_record_callback_url");
record_callback_url = Config.getProperty("record_callback_url");
String[] online_sever_list = Config.getProperty("online_sever").split(",");
......@@ -233,6 +235,10 @@ public class Initialize implements InitializingBean {
return delCacheCallbackUrl;
}
public static String getDelRecordCallbackUrl() {
return delRecordCallbackUrl;
}
private static String getOnlineServer(int withOnline) {
return ONLINE_SERVER.get(withOnline);
}
......
......@@ -19,16 +19,16 @@ spring:
ansi:
enabled: detect
logging:
path: /home/ubuntu/project/tts/logs
#path: /root/GameEditor/mqw/project/ttsTest/logs
#path: /home/ubuntu/project/tts/logs
path: /root/GameEditor/mqw/project/ttsTest/logs
level:
root: info
server:
port: 9663
#port: 8188
#port: 9663
port: 8188
tomcat:
accesslog:
enabled: true
pattern: '%h %l %u %t "%r" %s %b bytes %D ms'
directory: /home/ubuntu/project/tts/tomcat
#directory: /root/GameEditor/mqw/project/ttsTest/tomcat
\ No newline at end of file
#directory: /home/ubuntu/project/tts/tomcat
directory: /root/GameEditor/mqw/project/ttsTest/tomcat
\ No newline at end of file
#server_name=hz1
server_name=aws-nx2
server_url=http://aws-nx2.chewrobot.com:9663
#server_url=http://aliyun-hz1.chewrobot.com:9663
audio_dir=/root/TTS-service/audio
record_dir=record
#audio_dir=/root/GameEditor/mqw/project/tts/audio
online_sever=1:http://dev01.centaurstech.com,2:http://dev01.centaurstech.com
offline_server=http://dev01.centaurstech.com:9001
#offline_server=http://aliyun-hz1.chewrobot.com:9001
#第三方tts标记
third_party_tts_text =third_party_tts
......@@ -16,3 +12,4 @@ python_interpreter=/usr/bin/python3
change_sound_path=/root/GameEditor/mqw/project/tts/pythonCode/audios.py
del_cache_callback_url=http://localhost:18082/text/offline/delCache/callback
record_callback_url=http://localhost:18082/text/offline/upload/callback
del_record_callback_url=http://localhost:18082/text/offline/delUpload/callback
\ No newline at end of file
server_name=hz1
server_url=https://aliyun-hz1.chewrobot.com/mqw
record_dir=record
audio_dir=/root/GameEditor/mqw/project/tts/audio
online_sever=1:http://dev01.centaurstech.com,2:http://dev01.centaurstech.com
offline_server=http://dev01.centaurstech.com:9001
#第三方tts标记
third_party_tts_text =third_party_tts
del_cache_callback_url=http://localhost:18082/text/offline/delCache/callback
record_callback_url=http://localhost:18082/text/offline/upload/callback
del_record_callback_url=http://localhost:18082/text/offline/delUpload/callback
python_interpreter=/usr/bin/python3
#change_sound_path=/root/GameEditor/mqw/project/tts/pythonCode/audios.py
......@@ -2,6 +2,7 @@ package ai.qiwu.mi.xiaoai.commons;
import ai.qiwu.mi.xiaoai.model.QiWuRequest;
import ai.qiwu.mi.xiaoai.model.QiWuResponse;
import ai.qiwu.mi.xiaoai.service.XiaoAiService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
......@@ -24,7 +25,12 @@ public class RequestTerminal {
//log.info("向终端发出请求的参数信息:{}", requestJsonStr);
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json;charset=utf-8");
String resStr = OkHttp3Utils.doPostJsonStr(XiaoAiConstants.QI_WU_TERMINATE, headers, requestJsonStr);
String resStr;
if (userId.equals(XiaoAiService.deviceId)) {
resStr = OkHttp3Utils.doPostJsonStr(XiaoAiConstants.QI_WU_TERMINATE_DIDI, headers, requestJsonStr);
}else {
resStr = OkHttp3Utils.doPostJsonStr(XiaoAiConstants.QI_WU_TERMINATE_nx2, headers, requestJsonStr);
}
if (resStr == null) {
return null;
}
......
......@@ -5,8 +5,8 @@ package ai.qiwu.mi.xiaoai.commons;
* @dateTime 19-7-5 下午8:56
*/
public interface XiaoAiConstants {
String QI_WU_TERMINATE = "http://didi-gz2.jiaoyou365.com:8070/api/chat2";
//String QI_WU_TERMINATE = "http://aws-nx2.chewrobot.com:8070/api/chat2";
String QI_WU_TERMINATE_DIDI = "http://didi-gz2.jiaoyou365.com:8070/api/chat2";
String QI_WU_TERMINATE_nx2 = "http://aws-nx2.chewrobot.com:8070/api/chat2";
String DEVICE_NAME = "XIAOAI";
String CONTENT_TYPE = "application/json;charset=utf-8";
String PLAYBACK_NEARLY_FINISHED = "mediaplayer.playbacknearlyfinished";
......@@ -23,7 +23,7 @@ public interface XiaoAiConstants {
String WELCOME_WORD = "欢迎来到交游天下,海量的作品在这等您";
String END_BYE = "我们的作品库在不断扩充,欢迎再来玩";
String END_WORD = "退出";
String USER_IDLE = "小悟在等你的回复哦";
String USER_IDLE = "小悟在等你的回复";
String ANOTHER_USER_IDLE = "再不回复我,那我可退出喽";
}
}
......@@ -36,11 +36,13 @@ public class XiaoAiService {
2500, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(CORE), new ThreadPoolExecutor.CallerRunsPolicy());
private static final ConcurrentHashMap<String, XiaoAiResponse> LAST_OUTTIME_RESULT = new ConcurrentHashMap<>();
public static volatile String deviceId;
public XiaoAiResponse getResponse(XiaoAiRequest request) {
String userId = getUserId(request);
int type = request.getRequest().getType();
switch (type) {
case 0 : return dealWithLaunchRequest();
case 0 : return dealWithLaunchRequest(request);
case 1 : return dealWithIntentRequest(request, userId);
case 2 : return dealWithEndRequest(userId);
default: return buildTextResponse(SORRY_REPEAT);
......@@ -130,7 +132,8 @@ public class XiaoAiService {
return response;
}
private XiaoAiResponse dealWithLaunchRequest() {
private XiaoAiResponse dealWithLaunchRequest(XiaoAiRequest request) {
if (deviceId == null) deviceId = getUserId(request);
return buildTextResponse(TTS.WELCOME_WORD);
}
......@@ -141,7 +144,14 @@ public class XiaoAiService {
if (response != null) return response;
//log.info("用户没说话");
//return buildTextResponse(".");
return buildTextResponse((new Random(47).nextInt(666) & 1) == 1 ? TTS.USER_IDLE : TTS.ANOTHER_USER_IDLE);
try {
//log.info("开始等待");
Thread.sleep(1800);
//log.info("等待OVER");
} catch (InterruptedException e) {
log.warn("等待中断");
}
return buildTextResponse((new Random(47).nextInt() & 1) == 1 ? TTS.USER_IDLE : TTS.ANOTHER_USER_IDLE);
}
XiaoAiResponse response = getLastResult(userId);
if (response != null) return response;
......@@ -184,12 +194,8 @@ public class XiaoAiService {
return Utils.modifyDeviceId(userId);
}
public XiaoAiResponse getLastResult(String userId) {
XiaoAiResponse response;
if ((response = LAST_OUTTIME_RESULT.get(userId)) != null) {
LAST_OUTTIME_RESULT.remove(userId);
}
return response;
private XiaoAiResponse getLastResult(String userId) {
return LAST_OUTTIME_RESULT.remove(userId);
}
class Task implements Runnable {
......
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<property resource="logback.properties"/>
<appender name="CONSOLE-LOG" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>[%d{yyyy-MM-dd' 'HH:mm:ss.sss}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
</layout>
</appender>
<!--获取比info级别高(包括info级别)但除error级别的日志-->
<appender name="INFO-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>[%d{yyyy-MM-dd' 'HH:mm:ss.sss}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_INFO_HOME}//%d.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="ERROR-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>[%d{yyyy-MM-dd' 'HH:mm:ss.sss}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>${LOG_ERROR_HOME}//%d.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!-- 异步输出 -->
<appender name="ASYNC-INFO" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>256</queueSize>
<!-- 添加附加的appender,最多只能添加一个 -->
<appender-ref ref="INFO-LOG"/>
</appender>
<appender name="ASYNC-ERROR" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>256</queueSize>
<!-- 添加附加的appender,最多只能添加一个 -->
<appender-ref ref="ERROR-LOG"/>
</appender>
<root level="info">
<appender-ref ref="CONSOLE-LOG" />
<appender-ref ref="ASYNC-INFO" />
<appender-ref ref="ASYNC-ERROR" />
</root>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment