refactor(ops): 优化语音播报服务封装
1. VoiceBroadcastService 重构 - 移除未使用的 volume 参数(JT808 协议不支持) - 添加 TTS 播报标志常量定义 - 新增 broadcastNormal() 和 broadcastUrgent() 方法 - 支持自定义播报标志 2. 更新调用方 - CleanOrderConfirmEventHandler: 使用简化 API - CleanOrderAuditEventHandler: 使用简化 API TTS 参数标准: - identifier: "tts" - tts_text: 播报文本内容 - tts_flag: 0x01=静默, 0x08=普通, 0x09=紧急 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -200,7 +200,7 @@ public class CleanOrderAuditEventHandler implements RocketMQListener<String> {
|
||||
*/
|
||||
private void sendTts(Long deviceId, String text) {
|
||||
try {
|
||||
voiceBroadcastService.broadcast(deviceId, text, 80);
|
||||
voiceBroadcastService.broadcast(deviceId, text);
|
||||
log.info("[CleanOrderAuditEventHandler] TTS 下发成功: deviceId={}, text={}", deviceId, text);
|
||||
} catch (Exception e) {
|
||||
log.error("[CleanOrderAuditEventHandler] TTS 下发异常: deviceId={}", deviceId, e);
|
||||
|
||||
@@ -138,7 +138,7 @@ public class CleanOrderConfirmEventHandler implements RocketMQListener<String> {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
voiceBroadcastService.broadcast(deviceId, text, 80);
|
||||
voiceBroadcastService.broadcast(deviceId, text);
|
||||
log.info("[CleanOrderConfirmEventHandler] TTS 下发成功: deviceId={}, text={}", deviceId, text);
|
||||
} catch (Exception e) {
|
||||
log.error("[CleanOrderConfirmEventHandler] TTS 发送失败: deviceId={}", deviceId, e);
|
||||
|
||||
@@ -16,13 +16,17 @@ import org.springframework.stereotype.Service;
|
||||
* 职责:
|
||||
* 1. 统一所有 TTS 下发入口
|
||||
* 2. 提供同步/异步播报接口
|
||||
* 3. 管理播报音量参数
|
||||
* 4. 记录播报日志
|
||||
* 3. 记录播报日志
|
||||
* <p>
|
||||
* 设计原则:
|
||||
* - 接受 deviceId 参数(而非 cleanerId)
|
||||
* - 简单可靠,不实现复杂的去重逻辑,由调用方控制
|
||||
* - 直接调用 IoT 设备控制接口
|
||||
* <p>
|
||||
* JT808 TTS 播报标志 (tts_flag) 说明:
|
||||
* - 0x01: 静默执行 - 设备收到后解析指令,修改参数,回复 0x0001,但不发声
|
||||
* - 0x08: TTS 语音播报(普通通知) - 播放语音,设备将文本通过喇叭朗读
|
||||
* - 0x09: TTS 语音播报(紧急通知) - 播放语音,通常带显示
|
||||
*
|
||||
* @author lzh
|
||||
*/
|
||||
@@ -30,6 +34,32 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class VoiceBroadcastService {
|
||||
|
||||
/**
|
||||
* TTS 服务标识符
|
||||
*/
|
||||
private static final String TTS_IDENTIFIER = "TTS";
|
||||
|
||||
/**
|
||||
* TTS 播报标志:静默执行
|
||||
* <p>
|
||||
* 设备收到后解析指令,修改参数,回复 0x0001,但不发声
|
||||
*/
|
||||
public static final int TTS_FLAG_SILENT = 0x01;
|
||||
|
||||
/**
|
||||
* TTS 播报标志:普通通知
|
||||
* <p>
|
||||
* 播放语音,设备将文本内容通过喇叭朗读出来
|
||||
*/
|
||||
public static final int TTS_FLAG_NORMAL = 0x08;
|
||||
|
||||
/**
|
||||
* TTS 播报标志:紧急通知
|
||||
* <p>
|
||||
* 播放语音(通常带显示),用于重要通知
|
||||
*/
|
||||
public static final int TTS_FLAG_URGENT = 0x09;
|
||||
|
||||
@Resource
|
||||
private IotDeviceControlApi iotDeviceControlApi;
|
||||
|
||||
@@ -37,23 +67,25 @@ public class VoiceBroadcastService {
|
||||
private EventLogRecorder eventLogRecorder;
|
||||
|
||||
/**
|
||||
* 播报语音(同步)
|
||||
* 播报语音(紧急通知,默认)
|
||||
* <p>
|
||||
* 使用 tts_flag=0x09,适用于工单确认、到岗提醒等重要通知
|
||||
*
|
||||
* @param deviceId 设备ID
|
||||
* @param text 播报文本
|
||||
*/
|
||||
public void broadcast(Long deviceId, String text) {
|
||||
broadcast(deviceId, text, null);
|
||||
broadcast(deviceId, text, TTS_FLAG_URGENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 播报语音(带音量)
|
||||
* 播报语音(指定播报类型)
|
||||
*
|
||||
* @param deviceId 设备ID
|
||||
* @param text 播报文本
|
||||
* @param volume 音量(0-100)
|
||||
* @param ttsFlag 播报标志(0x01=静默, 0x08=普通, 0x09=紧急)
|
||||
*/
|
||||
public void broadcast(Long deviceId, String text, Integer volume) {
|
||||
public void broadcast(Long deviceId, String text, int ttsFlag) {
|
||||
if (deviceId == null || text == null) {
|
||||
return;
|
||||
}
|
||||
@@ -61,15 +93,15 @@ public class VoiceBroadcastService {
|
||||
try {
|
||||
IotDeviceServiceInvokeReqDTO reqDTO = new IotDeviceServiceInvokeReqDTO();
|
||||
reqDTO.setDeviceId(deviceId);
|
||||
reqDTO.setIdentifier("tts");
|
||||
reqDTO.setIdentifier(TTS_IDENTIFIER);
|
||||
reqDTO.setParams(MapUtil.<String, Object>builder()
|
||||
.put("text", text)
|
||||
.put("playMode", 1) // 立即播报
|
||||
.put("volume", volume != null ? volume : 50)
|
||||
.put("tts_text", text)
|
||||
.put("tts_flag", ttsFlag)
|
||||
.build());
|
||||
|
||||
iotDeviceControlApi.invokeService(reqDTO);
|
||||
log.debug("[VoiceBroadcast] 播报成功: deviceId={}, text={}", deviceId, text);
|
||||
log.debug("[VoiceBroadcast] 播报成功: deviceId={}, text={}, flag=0x{}",
|
||||
deviceId, text, Integer.toHexString(ttsFlag));
|
||||
|
||||
// 记录日志
|
||||
eventLogRecorder.info("clean", EventDomain.DEVICE, "TTS_SENT",
|
||||
@@ -84,6 +116,30 @@ public class VoiceBroadcastService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 播报语音(普通通知)
|
||||
* <p>
|
||||
* 使用 tts_flag=0x08,适用于一般性通知
|
||||
*
|
||||
* @param deviceId 设备ID
|
||||
* @param text 播报文本
|
||||
*/
|
||||
public void broadcastNormal(Long deviceId, String text) {
|
||||
broadcast(deviceId, text, TTS_FLAG_NORMAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 播报语音(紧急通知)
|
||||
* <p>
|
||||
* 使用 tts_flag=0x09,适用于工单确认、到岗提醒等重要通知
|
||||
*
|
||||
* @param deviceId 设备ID
|
||||
* @param text 播报文本
|
||||
*/
|
||||
public void broadcastUrgent(Long deviceId, String text) {
|
||||
broadcast(deviceId, text, TTS_FLAG_URGENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 播报语音(异步)
|
||||
*
|
||||
@@ -94,16 +150,4 @@ public class VoiceBroadcastService {
|
||||
public void broadcastAsync(Long deviceId, String text) {
|
||||
broadcast(deviceId, text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 播报语音(异步,带音量)
|
||||
*
|
||||
* @param deviceId 设备ID
|
||||
* @param text 播报文本
|
||||
* @param volume 音量(0-100)
|
||||
*/
|
||||
@Async("ops-task-executor")
|
||||
public void broadcastAsync(Long deviceId, String text, Integer volume) {
|
||||
broadcast(deviceId, text, volume);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user