udpate
This commit is contained in:
@@ -1,2 +1,37 @@
|
||||
import { LightHA } from "@kevisual/ha-api";
|
||||
export const lightHA = new LightHA({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
|
||||
|
||||
export const callText = async (text: string) => {
|
||||
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 = '关闭';
|
||||
}
|
||||
let endTime = Date.now();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user