Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
  • Loading branch information
baran.wang committed Feb 16, 2024
1 parent f02d543 commit 489f2de
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-camels-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"homebridge-plugin-aqara": patch
---

优化代码
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# WIP: homebridge-plugin-aqara
# WIP: homebridge-plugin-aqara

用于一些不支持 HomeKit 的 Aqara 设备的 Homebridge 插件

目前支持的设备有:

- [x] Aqara 智能晾衣机 Lite
23 changes: 12 additions & 11 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export class AqaraApi {

get configPath() {
const { storagePath, account } = this.option;
const configPath = path.resolve(storagePath, 'aqara');
if (!fs.existsSync(configPath)) {
fs.mkdirSync(configPath);
const configDir = path.resolve(storagePath, 'aqara');
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
}
return path.resolve(configPath, `${account}.json`);
return path.resolve(configDir, `${account}.json`);
}

get accountConfig() {
Expand All @@ -48,7 +48,13 @@ export class AqaraApi {
}

get accessToken() {
return this.accountConfig?.accessToken;
if (!this.accountConfig) {
return undefined;
}
if (this.accountConfig.expiresAt < Date.now()) {
return undefined;
}
return this.accountConfig.accessToken;
}

retry = false;
Expand Down Expand Up @@ -115,10 +121,6 @@ export class AqaraApi {
};
}

setAccount(account: string) {
this.option.account = account;
}

private request<T>(intent: string, data: any) {
this.logger.info('Request:', intent, JSON.stringify(data));
return this.axios.post('/v3.0/open/api', {
Expand Down Expand Up @@ -158,8 +160,7 @@ export class AqaraApi {
private saveAccountConfig({ expiresIn, ...rest }: Aqara.GetTokenResponse) {
const expiresAt = Date.now() + parseInt(expiresIn) * 1000;
const result = { ...rest, expiresAt, account: this.option.account };
const configPath = path.resolve(this.configPath, `${this.option.account}.json`);
fs.writeFileSync(configPath, JSON.stringify(result, null, 2));
fs.writeFileSync(this.configPath, JSON.stringify(result, null, 2));
}

queryDeviceInfo(params: Aqara.QueryDeviceInfoRequest) {
Expand Down
35 changes: 13 additions & 22 deletions ui-src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,33 @@ export default () => {
const [config, setConfig] = useState<any>({});

const {
data: token,
data: accessToken,
refresh,
cancel,
} = useRequest(
async () => {
const tokenInfo = await window.homebridge.request('/token/local', { account: config.account });
if (tokenInfo && tokenInfo.expiresAt > Date.now()) {
return tokenInfo;
} = useRequest(() => window.homebridge.request('/token/local', config), {
pollingInterval: 1000 * 2,
ready: !!config.account,
refreshDeps: [config.account],
onSuccess: (data) => {
if (data) {
cancel();
}
return null;
},
{
pollingInterval: 1000 * 2,
ready: !!config.account,
refreshDeps: [config.account],
onSuccess: (data) => {
if (data) {
cancel();
}
},
}
);
});

return (
<ConfigContext.Provider value={{ config, setConfig }}>
<div hidden={step !== 0 || !!token}>
<div hidden={step !== 0 || !!accessToken}>
<AppConfig onNext={() => setStep(1)} />
</div>
<div hidden={step !== 1 || !!token}>
<div hidden={step !== 1 || !!accessToken}>
<UserConfig onNext={() => refresh()} />
</div>

{!!token && (
{!!accessToken && (
<Form.Group className='mb-3'>
<Form.Label>Access Token</Form.Label>
<Form.Control type='text' value={token.accessToken} />
<Form.Control type='text' value={accessToken} />
</Form.Group>
)}
</ConfigContext.Provider>
Expand Down
16 changes: 3 additions & 13 deletions ui-src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,9 @@ class UiServer extends HomebridgePluginUiServer {
}
}

getLocalToken({ account }: { account: string }) {
if (!this.aqaraApi) {
return Promise.resolve(null);
}
this.aqaraApi.setAccount(account);
const config = this.aqaraApi.accountConfig;
if (!config) {
return Promise.resolve(null);
}
if (config.expiresAt < Date.now()) {
return Promise.resolve(null);
}
return Promise.resolve(config);
getLocalToken(config: AqaraApiOption) {
const aqaraApi = this.getAqaraApi(config);
return Promise.resolve(aqaraApi.accessToken);
}

private getAqaraApi(config: AqaraApiOption) {
Expand Down

0 comments on commit 489f2de

Please sign in to comment.