bug修改
This commit is contained in:
@ -10,6 +10,7 @@ import com.fuyuanshen.app.http.HttpTtsClient;
|
|||||||
import com.fuyuanshen.app.mapper.AppBusinessFileMapper;
|
import com.fuyuanshen.app.mapper.AppBusinessFileMapper;
|
||||||
import com.fuyuanshen.common.core.domain.R;
|
import com.fuyuanshen.common.core.domain.R;
|
||||||
import com.fuyuanshen.common.satoken.utils.AppLoginHelper;
|
import com.fuyuanshen.common.satoken.utils.AppLoginHelper;
|
||||||
|
import com.fuyuanshen.common.satoken.utils.LoginHelper;
|
||||||
import com.fuyuanshen.equipment.utils.AlibabaTTSUtil;
|
import com.fuyuanshen.equipment.utils.AlibabaTTSUtil;
|
||||||
import com.fuyuanshen.equipment.utils.AudioProcessUtil;
|
import com.fuyuanshen.equipment.utils.AudioProcessUtil;
|
||||||
import com.fuyuanshen.equipment.utils.FileHashUtil;
|
import com.fuyuanshen.equipment.utils.FileHashUtil;
|
||||||
@ -20,6 +21,7 @@ import com.fuyuanshen.system.domain.vo.SysOssVo;
|
|||||||
import com.fuyuanshen.system.service.ISysOssService;
|
import com.fuyuanshen.system.service.ISysOssService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -63,6 +65,16 @@ public class AudioProcessService {
|
|||||||
private final IAppBusinessFileService appBusinessFileService;
|
private final IAppBusinessFileService appBusinessFileService;
|
||||||
private final AppBusinessFileMapper appBusinessFileMapper;
|
private final AppBusinessFileMapper appBusinessFileMapper;
|
||||||
|
|
||||||
|
// String accessKeyId = "LTAI5t66moCkhNC32TDJ5ReP";
|
||||||
|
// String accessKeySecret = "2F3sdoBJ08bYvJcuDgSkLnJwGXsvYH";
|
||||||
|
// String appKey = "lbGuq5K5bEH4uxmT";
|
||||||
|
@Value("${alibaba.tts.appKey}")
|
||||||
|
private String appKey;
|
||||||
|
@Value("${alibaba.tts.akId}")
|
||||||
|
private String accessKeyId;
|
||||||
|
@Value("${alibaba.tts.akSecret}")
|
||||||
|
private String accessKeySecret;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理上传的音频文件
|
* 处理上传的音频文件
|
||||||
*/
|
*/
|
||||||
@ -448,9 +460,9 @@ public class AudioProcessService {
|
|||||||
if (!contains) {
|
if (!contains) {
|
||||||
throw new IllegalArgumentException("不支持的音频格式");
|
throw new IllegalArgumentException("不支持的音频格式");
|
||||||
}
|
}
|
||||||
String accessKeyId = "LTAI5t66moCkhNC32TDJ5ReP";
|
// String accessKeyId = "LTAI5t66moCkhNC32TDJ5ReP";
|
||||||
String accessKeySecret = "2F3sdoBJ08bYvJcuDgSkLnJwGXsvYH";
|
// String accessKeySecret = "2F3sdoBJ08bYvJcuDgSkLnJwGXsvYH";
|
||||||
String appKey = "lbGuq5K5bEH4uxmT";
|
// String appKey = "lbGuq5K5bEH4uxmT";
|
||||||
String savedPath = null;
|
String savedPath = null;
|
||||||
String savedMp3VolumePath = null;
|
String savedMp3VolumePath = null;
|
||||||
try {
|
try {
|
||||||
@ -458,7 +470,7 @@ public class AudioProcessService {
|
|||||||
HttpTtsClient httpClient = new HttpTtsClient(accessKeyId, accessKeySecret, appKey);
|
HttpTtsClient httpClient = new HttpTtsClient(accessKeyId, accessKeySecret, appKey);
|
||||||
//
|
//
|
||||||
byte[] mp3Data = httpClient.synthesizeTextToMp3(text,fileSuffix);
|
byte[] mp3Data = httpClient.synthesizeTextToMp3(text,fileSuffix);
|
||||||
// byte[] mp3Data = alibabaTTSUtil.synthesizeTextToMp3(text);
|
// byte[] mp3Data = alibabaTTSUtil.synthesizeTextToMp3(text,fileSuffix);
|
||||||
|
|
||||||
|
|
||||||
AppBusinessFileBo appBusinessFileBo = new AppBusinessFileBo();
|
AppBusinessFileBo appBusinessFileBo = new AppBusinessFileBo();
|
||||||
@ -538,11 +550,12 @@ public class AudioProcessService {
|
|||||||
if(deviceId == null){
|
if(deviceId == null){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Long userId = LoginHelper.getUserId();
|
||||||
AppBusinessFileBo bo = new AppBusinessFileBo();
|
AppBusinessFileBo bo = new AppBusinessFileBo();
|
||||||
bo.setBusinessId(deviceId);
|
bo.setBusinessId(deviceId);
|
||||||
|
bo.setCreateBy(userId);
|
||||||
bo.setFileType(3L);
|
bo.setFileType(3L);
|
||||||
List<AppFileVo> appFileVos = appBusinessFileService.queryAppFileList(bo);
|
return appBusinessFileService.queryAppFileList(bo);
|
||||||
return appFileVos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public R<Void> deleteAudioFile(Long fileId,Long deviceId) {
|
public R<Void> deleteAudioFile(Long fileId,Long deviceId) {
|
||||||
|
|||||||
@ -6,10 +6,7 @@ import com.fuyuanshen.common.core.utils.ImageToCArrayConverter;
|
|||||||
import com.fuyuanshen.common.core.utils.StringUtils;
|
import com.fuyuanshen.common.core.utils.StringUtils;
|
||||||
import com.fuyuanshen.common.json.utils.JsonUtils;
|
import com.fuyuanshen.common.json.utils.JsonUtils;
|
||||||
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
||||||
import com.fuyuanshen.global.mqtt.base.MqttRuleContext;
|
import com.fuyuanshen.global.mqtt.base.*;
|
||||||
import com.fuyuanshen.global.mqtt.base.MqttRuleEngine;
|
|
||||||
import com.fuyuanshen.global.mqtt.base.MqttXinghanCommandType;
|
|
||||||
import com.fuyuanshen.global.mqtt.base.NewMqttRuleContext;
|
|
||||||
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
||||||
import com.fuyuanshen.global.queue.MqttMessageQueueConstants;
|
import com.fuyuanshen.global.queue.MqttMessageQueueConstants;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -35,6 +32,9 @@ public class ReceiverMessageHandler implements MessageHandler {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private MqttRuleEngine ruleEngine;
|
private MqttRuleEngine ruleEngine;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NewMqttRuleEngine newRuleEngine;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(Message<?> message) throws MessagingException {
|
public void handleMessage(Message<?> message) throws MessagingException {
|
||||||
Object payload = message.getPayload();
|
Object payload = message.getPayload();
|
||||||
@ -50,9 +50,18 @@ public class ReceiverMessageHandler implements MessageHandler {
|
|||||||
if (receivedTopic == null || payloadDict == null) {
|
if (receivedTopic == null || payloadDict == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String[] subStr = receivedTopic.split("/");
|
|
||||||
String deviceImei = subStr[1];
|
|
||||||
|
|
||||||
|
// 模版格式匹配
|
||||||
|
boolean flag = checkTemplateFormatMatching(payloadDict);
|
||||||
|
|
||||||
|
|
||||||
|
String[] subStr = receivedTopic.split("/");
|
||||||
|
String deviceImei = null;
|
||||||
|
if(flag){
|
||||||
|
deviceImei = payloadDict.getStr("imei");
|
||||||
|
} else {
|
||||||
|
deviceImei = subStr[1];
|
||||||
|
}
|
||||||
|
|
||||||
RedissonClient client = RedisUtils.getClient();
|
RedissonClient client = RedisUtils.getClient();
|
||||||
String lockKey = "mqtt:consumer:lock:";
|
String lockKey = "mqtt:consumer:lock:";
|
||||||
@ -74,7 +83,22 @@ public class ReceiverMessageHandler implements MessageHandler {
|
|||||||
RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1", Duration.ofSeconds(360));
|
RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1", Duration.ofSeconds(360));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 新的通信协议
|
||||||
|
if(flag){
|
||||||
|
String[] topicArr = receivedTopic.split("/");
|
||||||
|
String funcType = payloadDict.getStr("funcType");
|
||||||
|
NewMqttRuleContext context = new NewMqttRuleContext();
|
||||||
|
context.setCommandType(topicArr[2]+"_"+funcType);
|
||||||
|
context.setDeviceImei(deviceImei);
|
||||||
|
context.setPayloadDict(payloadDict);
|
||||||
|
|
||||||
|
boolean ruleExecuted = newRuleEngine.executeRule(context);
|
||||||
|
|
||||||
|
if (!ruleExecuted) {
|
||||||
|
log.warn("未找到匹配的规则来处理命令类型: {}", topicArr[2] + " : " +funcType);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (convertArr.length > 0) {
|
if (convertArr.length > 0) {
|
||||||
Byte val1 = (Byte) convertArr[0];
|
Byte val1 = (Byte) convertArr[0];
|
||||||
@ -89,6 +113,7 @@ public class ReceiverMessageHandler implements MessageHandler {
|
|||||||
if (!ruleExecuted) {
|
if (!ruleExecuted) {
|
||||||
log.warn("未找到匹配的规则来处理命令类型: {}", val1);
|
log.warn("未找到匹配的规则来处理命令类型: {}", val1);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ===== 追加:根据报文内容识别格式并统一解析 ===== */
|
/* ===== 追加:根据报文内容识别格式并统一解析 ===== */
|
||||||
@ -117,4 +142,18 @@ public class ReceiverMessageHandler implements MessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkTemplateFormatMatching(Dict payloadDict) {
|
||||||
|
// 检查是否包含指定的 key
|
||||||
|
boolean hasImei = payloadDict.containsKey("imei");
|
||||||
|
boolean hasFuncType = payloadDict.containsKey("funcType");
|
||||||
|
boolean hasStatus = payloadDict.containsKey("status");
|
||||||
|
boolean hasTimestamp = payloadDict.containsKey("timestamp");
|
||||||
|
boolean hasData = payloadDict.containsKey("data");
|
||||||
|
|
||||||
|
// 输出检查结果
|
||||||
|
log.info("包含 imei: {}, funcType: {}, status: {}, timestamp: {}, data: {}",
|
||||||
|
hasImei, hasFuncType, hasStatus, hasTimestamp, hasData);
|
||||||
|
return hasImei && hasFuncType && hasStatus && hasTimestamp && hasData;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,13 +29,13 @@ public class FuncType10StrobeMode implements NewMqttMessageRule {
|
|||||||
log.info("HBY100J爆闪模式开启/关闭,消息负载:{}", context.getPayloadDict());
|
log.info("HBY100J爆闪模式开启/关闭,消息负载:{}", context.getPayloadDict());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
// context.getDeviceImei() + ":strobeMode";
|
context.getDeviceImei() + ":strobeMode";
|
||||||
//
|
|
||||||
// Map<String, Object> payloadDict = context.getPayloadDict();
|
Map<String, Object> payloadDict = context.getPayloadDict();
|
||||||
// if (payloadDict != null) {
|
if (payloadDict != null) {
|
||||||
// RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
||||||
// }
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("HBY100J爆闪模式开启/关闭失败", e);
|
log.error("HBY100J爆闪模式开启/关闭失败", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,13 +29,13 @@ public class FuncType11Frequency implements NewMqttMessageRule {
|
|||||||
log.info("HBY100J修改警示灯爆闪频率,消息负载:{}", context.getPayloadDict());
|
log.info("HBY100J修改警示灯爆闪频率,消息负载:{}", context.getPayloadDict());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
// context.getDeviceImei() + ":frequency";
|
context.getDeviceImei() + ":frequency";
|
||||||
//
|
|
||||||
// Map<String, Object> payloadDict = context.getPayloadDict();
|
Map<String, Object> payloadDict = context.getPayloadDict();
|
||||||
// if (payloadDict != null) {
|
if (payloadDict != null) {
|
||||||
// RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
||||||
// }
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("HBY100J修改警示灯爆闪频率失败", e);
|
log.error("HBY100J修改警示灯爆闪频率失败", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,14 +30,14 @@ public class FuncType12ForceAudio implements NewMqttMessageRule {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 构建强制声光报警开关的Redis键
|
// 构建强制声光报警开关的Redis键
|
||||||
// String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
// context.getDeviceImei() + ":forceAudio";
|
context.getDeviceImei() + ":forceAudio";
|
||||||
//
|
|
||||||
// Map<String, Object> payloadDict = context.getPayloadDict();
|
Map<String, Object> payloadDict = context.getPayloadDict();
|
||||||
// if (payloadDict != null) {
|
if (payloadDict != null) {
|
||||||
// // 存储强制声光报警开关状态到Redis
|
// 存储强制声光报警开关状态到Redis
|
||||||
// RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
||||||
// }
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("HBY100J强制声光报警开启/关闭失败", e);
|
log.error("HBY100J强制声光报警开启/关闭失败", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,13 +29,13 @@ public class FuncType9UpdateVolume implements NewMqttMessageRule {
|
|||||||
log.info("HBY100J修改音量,消息负载:{}", context.getPayloadDict());
|
log.info("HBY100J修改音量,消息负载:{}", context.getPayloadDict());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
// context.getDeviceImei() + ":updateVolume";
|
context.getDeviceImei() + ":updateVolume";
|
||||||
//
|
|
||||||
// Map<String, Object> payloadDict = context.getPayloadDict();
|
Map<String, Object> payloadDict = context.getPayloadDict();
|
||||||
// if (payloadDict != null) {
|
if (payloadDict != null) {
|
||||||
// RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
|
||||||
// }
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("HBY100J修改音量失败", e);
|
log.error("HBY100J修改音量失败", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -173,6 +173,49 @@ public class DeviceHBY100JBizService {
|
|||||||
vo.setAlarmMode(sirenAlarm.getMode());
|
vo.setAlarmMode(sirenAlarm.getMode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
String strobeModeRedisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
|
device.getDeviceImei() + ":strobeMode";
|
||||||
|
String strobeModeStatus = RedisUtils.getCacheObject(strobeModeRedisKey);
|
||||||
|
if(StringUtils.isNotBlank(strobeModeStatus)){
|
||||||
|
FuncType10StrobeModeRequest strobeModeRequest = JSONObject.parseObject(strobeModeStatus, FuncType10StrobeModeRequest.class);
|
||||||
|
FuncType10StrobeModeRequest.Data data = strobeModeRequest.getData();
|
||||||
|
if(data != null){
|
||||||
|
vo.setStrobeMode(strobeModeRequest.getData().getMode());
|
||||||
|
vo.setStrobeEnable(strobeModeRequest.getData().getEnable());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String updateVolumeRedisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
|
device.getDeviceImei() + ":updateVolume";
|
||||||
|
String updateVolumeStatus = RedisUtils.getCacheObject(updateVolumeRedisKey);
|
||||||
|
if(StringUtils.isNotBlank(updateVolumeStatus)){
|
||||||
|
FuncType9UpdateVolumeRequest updateVolumeRequest = JSONObject.parseObject(updateVolumeStatus, FuncType9UpdateVolumeRequest.class);
|
||||||
|
if(updateVolumeRequest.getData() != null){
|
||||||
|
vo.setVolume(updateVolumeRequest.getData().getVolume());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String frequencyRedisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
|
device.getDeviceImei() + ":frequency";
|
||||||
|
String frequencyStatus = RedisUtils.getCacheObject(frequencyRedisKey);
|
||||||
|
if(StringUtils.isNotBlank(frequencyStatus)){
|
||||||
|
FuncType11FrequencyRequest frequencyRequest = JSONObject.parseObject(frequencyStatus, FuncType11FrequencyRequest.class);
|
||||||
|
if(frequencyRequest.getData() != null){
|
||||||
|
vo.setStrobeFrequency(frequencyRequest.getData().getFrequency());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String forceAudioRedisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
|
||||||
|
device.getDeviceImei() + ":forceAudio";
|
||||||
|
String forceAudioStatus = RedisUtils.getCacheObject(forceAudioRedisKey);
|
||||||
|
if(StringUtils.isNotBlank(forceAudioStatus)){
|
||||||
|
FuncType12ForceAudioRequest forceAudioRequest = JSONObject.parseObject(forceAudioStatus, FuncType12ForceAudioRequest.class);
|
||||||
|
if(forceAudioRequest.getData() != null){
|
||||||
|
vo.setVoiceStrobeAlarm(forceAudioRequest.getData().getVoiceStrobeAlarm());
|
||||||
|
vo.setAlarmMode(forceAudioRequest.getData().getMode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 获取经度纬度
|
// 获取经度纬度
|
||||||
|
|
||||||
|
|||||||
@ -56,4 +56,6 @@ public class AppBusinessFileBo extends BaseEntity {
|
|||||||
* 是否使用语音播报(0-否,1-是)
|
* 是否使用语音播报(0-否,1-是)
|
||||||
*/
|
*/
|
||||||
private Integer useStatus;
|
private Integer useStatus;
|
||||||
|
|
||||||
|
private Long createBy;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -113,14 +113,14 @@ public class AlibabaTTSUtil {
|
|||||||
DEFAULT_VOLUME, DEFAULT_SPEECH_RATE, DEFAULT_PITCH_RATE);
|
DEFAULT_VOLUME, DEFAULT_SPEECH_RATE, DEFAULT_PITCH_RATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] synthesizeTextToMp3(String text){
|
public byte[] synthesizeTextToMp3(String text,String fileSuffix){
|
||||||
try {
|
try {
|
||||||
// 获取访问令牌
|
// 获取访问令牌
|
||||||
String token = getValidAccessToken();
|
String token = getValidAccessToken();
|
||||||
// 使用HTTP方式调用
|
// 使用HTTP方式调用
|
||||||
HttpTtsClient httpClient = new HttpTtsClient(appkey,token);
|
HttpTtsClient httpClient = new HttpTtsClient(appkey,token);
|
||||||
|
|
||||||
return httpClient.synthesizeTextToMp3(text);
|
return httpClient.synthesizeTextToMp3(text,fileSuffix);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,8 @@ import static cn.dev33.satoken.SaManager.log;
|
|||||||
|
|
||||||
public class HttpTtsClient {
|
public class HttpTtsClient {
|
||||||
|
|
||||||
|
private String accessKeyId;
|
||||||
|
private String accessKeySecret;
|
||||||
private String appKey;
|
private String appKey;
|
||||||
private String token;
|
private String token;
|
||||||
/**
|
/**
|
||||||
@ -17,36 +19,57 @@ public class HttpTtsClient {
|
|||||||
*/
|
*/
|
||||||
private static final String BASE_URL = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
private static final String BASE_URL = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
||||||
|
|
||||||
public HttpTtsClient(String appKey, String token) {
|
public HttpTtsClient(String accessKeyId, String token) {
|
||||||
this.appKey = appKey;
|
this.accessKeyId = accessKeyId;
|
||||||
this.token = token;
|
this.token = token;
|
||||||
|
// this.accessKeySecret = accessKeySecret;
|
||||||
|
// this.appKey = appKey;
|
||||||
}
|
}
|
||||||
|
// private String refreshAccessToken() {
|
||||||
|
// try {
|
||||||
|
// // 调用阿里云API获取访问令牌
|
||||||
|
// AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
|
||||||
|
// accessToken.apply();
|
||||||
|
// String token = accessToken.getToken();
|
||||||
|
// log.info("访问令牌刷新成功");
|
||||||
|
//
|
||||||
|
// return token;
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.error("刷新访问令牌失败: {}", e.getMessage(), e);
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
/**
|
/**
|
||||||
* 使用HTTP POST方式调用阿里云TTS服务生成MP3格式语音
|
* 使用HTTP POST方式调用阿里云TTS服务生成MP3格式语音
|
||||||
*/
|
*/
|
||||||
public byte[] synthesizeTextToMp3(String text) throws IOException {
|
public byte[] synthesizeTextToMp3(String text,String fileSuffix) throws IOException {
|
||||||
String endpoint = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
// String endpoint = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
||||||
|
|
||||||
// 构建请求体
|
// 构建请求体
|
||||||
// String requestBody = String.format(
|
// String requestBody = String.format(
|
||||||
// "{\"appkey\":\"%s\",\"text\":\"%s\",\"voice\":\"zhifeng\",\"format\":\"MP3\",\"sample_rate\":24000,\"volume\":50,\"speech_rate\":0,\"pitch_rate\":0}",
|
// "{\"appkey\":\"%s\",\"text\":\"%s\",\"voice\":\"zhifeng\",\"format\":\"MP3\",\"sample_rate\":24000,\"volume\":50,\"speech_rate\":0,\"pitch_rate\":0}",
|
||||||
// appKey,
|
// appKey,
|
||||||
// text.replace("\"", "\\\"")
|
// text.replace("\"", "\\\"")zhide
|
||||||
// );
|
// );
|
||||||
|
|
||||||
|
// String token = refreshAccessToken();
|
||||||
String requestBody = " {\n" +
|
String requestBody = " {\n" +
|
||||||
" \"appkey\":\""+appKey+"\",\n" +
|
" \"appkey\":\""+appKey+"\",\n" +
|
||||||
|
" \"voice\":\"zhifeng\",\n" +
|
||||||
" \"text\":\""+text+"\",\n" +
|
" \"text\":\""+text+"\",\n" +
|
||||||
" \"token\":\""+token+"\",\n" +
|
" \"token\":\""+token+"\",\n" +
|
||||||
" \"format\":\"mp3\"\n" +
|
" \"volume\": 100,\n" +
|
||||||
|
" \"pitch_rate\": 0,\n" +
|
||||||
|
" \"format\":\""+fileSuffix+"\"\n" +
|
||||||
" }";
|
" }";
|
||||||
|
|
||||||
URL url = new URL(endpoint);
|
URL url = new URL(BASE_URL);
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
// 设置请求方法和头部
|
// 设置请求方法和头部
|
||||||
conn.setRequestMethod("POST");
|
conn.setRequestMethod("POST");
|
||||||
conn.setRequestProperty("Content-Type", "application/json");
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
// conn.setRequestProperty("Authorization", buildAuthorization());
|
conn.setRequestProperty("Authorization", buildAuthorization());
|
||||||
conn.setRequestProperty("Content-Length", String.valueOf(requestBody.getBytes().length));
|
conn.setRequestProperty("Content-Length", String.valueOf(requestBody.getBytes().length));
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
|
|
||||||
@ -73,12 +96,28 @@ public class HttpTtsClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建授权头部
|
||||||
|
*/
|
||||||
|
private String buildAuthorization() {
|
||||||
|
// 实际实现需要根据阿里云API规范构建签名
|
||||||
|
// 这里是简化示例
|
||||||
|
return "Bearer " + generateAccessToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成访问令牌
|
||||||
|
*/
|
||||||
|
private String generateAccessToken() {
|
||||||
|
// 实际实现需要调用阿里云获取token的API
|
||||||
|
return "your-access-token";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存MP3到文件
|
* 保存MP3到文件
|
||||||
*/
|
*/
|
||||||
public String saveMp3ToFile(String text, String outputPath) throws IOException {
|
public String saveMp3ToFile(String text, String outputPath) throws IOException {
|
||||||
byte[] mp3Data = synthesizeTextToMp3(text);
|
byte[] mp3Data = synthesizeTextToMp3(text,"mp3");
|
||||||
|
|
||||||
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
|
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
|
||||||
fos.write(mp3Data);
|
fos.write(mp3Data);
|
||||||
|
|||||||
Reference in New Issue
Block a user