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

View File

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

View File

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