feat: 添加 debug 开关

This commit is contained in:
WJG 2024-06-01 11:45:24 +08:00
parent 0c4c83eccf
commit fca974761a
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
4 changed files with 64 additions and 26 deletions

View File

@ -5,7 +5,7 @@ import {
getMiIOT,
getMiNA,
} from "mi-service-lite";
import { clamp, sleep } from "../../utils/base";
import { clamp, jsonEncode, sleep } from "../../utils/base";
import { Logger } from "../../utils/log";
import { Http } from "../http";
import { StreamResponse } from "./stream";
@ -22,6 +22,7 @@ type Speaker = {
type ActionCommand = [number, number];
export type BaseSpeakerConfig = MiServiceConfig & {
debug?: boolean;
/**
*
*/
@ -56,7 +57,7 @@ export class BaseSpeaker {
logger = Logger.create({ tag: "Speaker" });
MiNA?: MiNA;
MiIOT?: MiIOT;
debug = false;
checkInterval: number;
tts: TTSProvider;
ttsCommand: ActionCommand;
@ -65,12 +66,14 @@ export class BaseSpeaker {
constructor(config: BaseSpeakerConfig) {
this.config = config;
const {
debug = false,
checkInterval = 1000,
tts = "xiaoai",
ttsCommand = [5, 1],
wakeUpCommand = [5, 3],
audioBeep = process.env.audioBeep,
} = config;
this.debug = debug;
this.audioBeep = audioBeep;
this.checkInterval = clamp(checkInterval, 500, Infinity);
this.tts = tts;
@ -207,7 +210,7 @@ export class BaseSpeaker {
// 播放回复
const play = async (args?: { tts?: string; url?: string }) => {
// 播放开始提示音
if (playSFX) {
if (playSFX && this.audioBeep) {
await this.MiNA!.play({ url: this.audioBeep });
}
// 在播放 TTS 语音之前,先取消小爱音箱的唤醒状态,防止将 TTS 语音识别成用户指令
@ -225,6 +228,9 @@ export class BaseSpeaker {
// 等待回答播放完毕
while (true) {
const res = await this.MiNA!.getStatus();
if (this.debug) {
this.logger.debug(jsonEncode(res));
}
if (
!this.responding || // 有新消息
(res?.status === "playing" && res?.media_type) // 小爱自己开始播放音乐
@ -232,13 +238,13 @@ export class BaseSpeaker {
// 响应被中断
return "break";
}
if (res && res?.status !== "playing") {
if (res && res.status !== "playing") {
break;
}
await sleep(this.checkInterval);
}
// 播放结束提示音
if (playSFX) {
if (playSFX && this.audioBeep) {
await this.MiNA!.play({ url: this.audioBeep });
}
// 保持唤醒状态

View File

@ -42,6 +42,13 @@ class _LoggerManager {
}
}
debug(tag: string, args: any[]) {
const logs = this._getLogs(tag + " 🐛", ...args);
if (logs.length > 0) {
console.log(...logs);
}
}
success(tag: string, args: any[]) {
const logs = this._getLogs(tag + " ✅", ...args);
if (logs.length > 0) {
@ -90,6 +97,12 @@ class _Logger {
}
}
debug(...args: any[]) {
if (!this.disable) {
LoggerManager.debug(this.tag, args);
}
}
success(...args: any[]) {
if (!this.disable) {
LoggerManager.success(this.tag, args);

View File

@ -13,11 +13,11 @@ dotenv.config();
async function main() {
// println(kBannerASCII);
// testDB();
// testSpeaker();
testSpeaker();
// testOpenAI();
// testMyBot();
// testLog();
testMiGPT();
// testMiGPT();
}
main();

View File

@ -3,21 +3,20 @@ import { StreamResponse } from "../src/services/speaker/stream";
import { sleep } from "../src/utils/base";
export async function testSpeaker() {
const config: any = {
const speaker = new AISpeaker({
userId: process.env.MI_USER!,
password: process.env.MI_PASS!,
did: process.env.MI_DID,
tts: "doubao",
};
const speaker = new AISpeaker(config);
tts: "xiaoai",
debug: true,
});
await speaker.initMiServices();
// await testSpeakerResponse(speaker);
// await testSpeakerStreamResponse(speaker);
await testSpeakerStreamResponse(speaker);
// await testSpeakerGetMessages(speaker);
// await testSwitchSpeaker(speaker);
// await testSpeakerUnWakeUp(speaker);
await testAISpeaker(speaker);
// await testAISpeaker(speaker);
}
async function testAISpeaker(speaker: AISpeaker) {
@ -61,23 +60,43 @@ async function testSpeakerResponse(speaker: AISpeaker) {
async function testSpeakerStreamResponse(speaker: AISpeaker) {
const stream = new StreamResponse();
const text = `
###
1. ****
2. ****
3. ****西
4. ****
5. ****
###
1. ****
2. **西**西
3. ****
4. ****
5. ****
`;
const add = async (text: string) => {
stream.addResponse(text);
await sleep(100);
};
setTimeout(async () => {
await add(`地球是圆的主要原因`);
await add(`是由于地球的引力和自转。`);
await add(`地球的引力使得地球在形成过程中变得更加圆滑,因为引力会使得地球`);
await add(`的物质向地心靠拢,从而使得地球的形状更接近于一个球体。此外,`);
await add(
`地球的自转也会导致地球呈现出圆形,因为地球自转会使得地球的物质在赤道附近向外扩散,从而使得`
);
await add(
`地球在赤道处稍微膨胀,而在极地处稍微收缩,最终形成一个近似于球体的形状。因此,地球是圆的`
);
await add(`主要原因是由于地球的引力和自转共同作用所致。`);
console.log("finished!");
for (const s of text.split("")) {
await add(s);
}
stream.finish();
});
await speaker.response({ stream });