udpate
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { QwenAsrRelatime } from "@kevisual/video-tools/src/asr/index.ts";
|
||||
|
||||
import { Listener, WebSocketListenerFun, WebSocketReq } from "@kevisual/router";
|
||||
import { lightHA } from "@/routes/ha-api/ha.ts";
|
||||
import { callText } from "@/routes/ha-api/ha.ts";
|
||||
import { assistantConfig } from "@/app.ts";
|
||||
|
||||
const func: WebSocketListenerFun = async (req: WebSocketReq<{ asr: QwenAsrRelatime, msgId: string, startTime?: number, loading?: boolean }>, res) => {
|
||||
@@ -61,37 +61,7 @@ const func: WebSocketListenerFun = async (req: WebSocketReq<{ asr: QwenAsrRelati
|
||||
text,
|
||||
}));
|
||||
if (!text) return;
|
||||
const command = text?.trim().slice(0, 20);
|
||||
type ParseCommand = {
|
||||
type?: '打开' | '关闭',
|
||||
appName?: string,
|
||||
command?: string,
|
||||
}
|
||||
let obj: ParseCommand = {};
|
||||
if (command.startsWith('打开')) {
|
||||
obj.appName = command.replace('打开', '').trim();
|
||||
obj.type = '打开';
|
||||
} else if (command.startsWith('关闭')) {
|
||||
obj.appName = command.replace('关闭', '').trim();
|
||||
obj.type = '关闭';
|
||||
}
|
||||
if (obj.type) {
|
||||
try {
|
||||
const search = await lightHA.searchLight(obj.appName || '');
|
||||
console.log('searchTime', Date.now() - endTime);
|
||||
if (search.id) {
|
||||
await lightHA.runService({ entity_id: search.id, service: obj.type === '打开' ? 'turn_on' : 'turn_off' });
|
||||
} else if (search.hasMore) {
|
||||
const [first] = search.result;
|
||||
await lightHA.runService({ entity_id: first.entity_id, service: obj.type === '打开' ? 'turn_on' : 'turn_off' });
|
||||
} else {
|
||||
console.log('未找到对应设备:', obj.appName);
|
||||
}
|
||||
console.log('解析到控制指令', obj);
|
||||
} catch (e) {
|
||||
console.error('控制失败', e);
|
||||
}
|
||||
}
|
||||
await callText(text);
|
||||
console.log('toogle light time', Date.now() - endTime);
|
||||
});
|
||||
asr.start();
|
||||
|
||||
@@ -8,6 +8,7 @@ export { parseHomeArg, parseHelpArg };
|
||||
export type AssistantInitOptions = {
|
||||
path?: string;
|
||||
init?: boolean;
|
||||
initWorkspace?: boolean;
|
||||
};
|
||||
const randomId = () => Math.random().toString(36).substring(2, 8);
|
||||
/**
|
||||
@@ -16,12 +17,14 @@ const randomId = () => Math.random().toString(36).substring(2, 8);
|
||||
*/
|
||||
export class AssistantInit extends AssistantConfig {
|
||||
#query: Query
|
||||
initWorkspace: boolean = false;
|
||||
constructor(opts?: AssistantInitOptions) {
|
||||
const configDir = opts?.path || process.cwd();
|
||||
super({
|
||||
configDir,
|
||||
init: false,
|
||||
});
|
||||
this.initWorkspace = opts?.initWorkspace ?? true;
|
||||
if (opts?.init) {
|
||||
this.init();
|
||||
}
|
||||
@@ -35,8 +38,10 @@ export class AssistantInit extends AssistantConfig {
|
||||
if (!this.checkConfigPath()) {
|
||||
console.log(chalk.blue('助手路径不存在,正在创建...'));
|
||||
super.init(configDir);
|
||||
if (!this.initWorkspace) { return }
|
||||
} else {
|
||||
super.init(configDir);
|
||||
if (!this.initWorkspace) { return }
|
||||
const assistantConfig = this;
|
||||
console.log(chalk.yellow('助手路径已存在'), chalk.green(assistantConfig.configDir));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user