upate
This commit is contained in:
@@ -3,6 +3,7 @@ import { LRUStorage } from './cache.ts';
|
|||||||
export type HACoreOptions = {
|
export type HACoreOptions = {
|
||||||
token: string;
|
token: string;
|
||||||
homeassistantURL?: string;
|
homeassistantURL?: string;
|
||||||
|
ttl?: number;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* https://developers.home-assistant.io/docs/api/rest/
|
* https://developers.home-assistant.io/docs/api/rest/
|
||||||
@@ -11,10 +12,11 @@ export class HACore {
|
|||||||
token: string;
|
token: string;
|
||||||
homeassistantURL?: string;
|
homeassistantURL?: string;
|
||||||
static serviceName = '';
|
static serviceName = '';
|
||||||
cache = new LRUStorage({ max: 200, ttl: 1000 * 60 * 5 }); // 5分钟缓存
|
cache: LRUStorage;
|
||||||
constructor(options: HACoreOptions) {
|
constructor(options: HACoreOptions) {
|
||||||
this.token = options?.token;
|
this.token = options?.token;
|
||||||
this.homeassistantURL = options?.homeassistantURL || 'http://localhost:8123';
|
this.homeassistantURL = options?.homeassistantURL || 'http://localhost:8123';
|
||||||
|
this.cache = new LRUStorage({ max: 200, ttl: options?.ttl || 1000 * 60 * 30 }); // 30分钟缓存
|
||||||
}
|
}
|
||||||
async get(opts: { url: string, params?: Record<string, any> }): Promise<any> {
|
async get(opts: { url: string, params?: Record<string, any> }): Promise<any> {
|
||||||
const _u = new URL(opts.url, this.homeassistantURL);
|
const _u = new URL(opts.url, this.homeassistantURL);
|
||||||
|
|||||||
@@ -122,6 +122,6 @@ export const button = new ButtonHA({ token: process.env.HAAS_TOKEN || '', homeas
|
|||||||
// console.log(showMore(entityType));
|
// console.log(showMore(entityType));
|
||||||
|
|
||||||
|
|
||||||
const yinxiang = await hacore.getEntities((item) => item.attributes?.friendly_name?.includes?.('音箱'));
|
// const yinxiang = await hacore.getEntities((item) => item.attributes?.friendly_name?.includes?.('音箱'));
|
||||||
|
|
||||||
console.log(showMore(yinxiang));
|
// console.log(showMore(yinxiang));
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { hacore, showMore } from "./common.ts";
|
import { hacore, showMore } from "./common.ts";
|
||||||
import Fuse from 'fuse.js';
|
import Fuse from 'fuse.js';
|
||||||
|
|
||||||
|
const beginTime = Date.now();
|
||||||
const devices = await hacore.getInfoList();
|
const devices = await hacore.getInfoList();
|
||||||
console.log(showMore(devices));
|
console.log('getInfo', Date.now() - beginTime, 'ms');
|
||||||
|
// console.log(showMore(devices));
|
||||||
const fuse = new Fuse(devices, {
|
const fuse = new Fuse(devices, {
|
||||||
keys: ['name'], // 搜索字段
|
keys: ['name'], // 搜索字段
|
||||||
threshold: 0.4, // 匹配宽松度:0~1,越小越严格
|
threshold: 0.4, // 匹配宽松度:0~1,越小越严格
|
||||||
@@ -14,7 +16,17 @@ const fuse = new Fuse(devices, {
|
|||||||
// const searchKeyword = '晾衣机的 灯';
|
// const searchKeyword = '晾衣机的 灯';
|
||||||
const searchKeyword = ' 阳台 灯';
|
const searchKeyword = ' 阳台 灯';
|
||||||
const result = fuse.search(searchKeyword);
|
const result = fuse.search(searchKeyword);
|
||||||
|
console.log('fuse search', Date.now() - beginTime, 'ms');
|
||||||
// 输出搜索结果
|
// 输出搜索结果
|
||||||
console.log(`搜索关键词: "${searchKeyword}"`);
|
console.log(`搜索关键词: "${searchKeyword}"`);
|
||||||
console.log(showMore(result.map(r => r.item)));
|
console.log(showMore(result.map(r => r.item)));
|
||||||
|
|
||||||
|
const toggleDevice = await hacore.runService({
|
||||||
|
entity_id: result[0].item.entity_id,
|
||||||
|
service: 'toggle',
|
||||||
|
});
|
||||||
|
console.log('toggleDevice', Date.now() - beginTime, 'ms');
|
||||||
|
// devices 2
|
||||||
|
const devices2 = await hacore.getInfoList();
|
||||||
|
console.log('getInfo 2', Date.now() - beginTime, 'ms');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user