Skip to content

Commit

Permalink
chore: push code
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Feb 14, 2024
1 parent af147bc commit dd2c596
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 2 additions & 1 deletion homebridge-ui/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/// <reference types='@modern-js/module-tools/types' />
/// <reference types="@homebridge/plugin-ui-utils/dist/ui.interface" />
/// <reference types="@homebridge/plugin-ui-utils/dist/ui.interface" />
/// <reference path="../../src/types.d.ts" />
10 changes: 10 additions & 0 deletions homebridge-ui/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { HomebridgePluginUiServer } from '@homebridge/plugin-ui-utils';
import { AqaraApi, AqaraApiOption } from "@api/index";

class UiServer extends HomebridgePluginUiServer {
constructor() {
super()

this.onRequest('/device', this.getAuthCode.bind(this));

this.ready();
}

getAuthCode(config: AqaraApiOption) {
const aqaraApi = new AqaraApi(config);
return aqaraApi.getAuthCode();
}
}

Expand Down
3 changes: 2 additions & 1 deletion homebridge-ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"lib": ["DOM", "ESNext"],
"moduleResolution": "node",
"paths": {
"@/*": ["./src/*"]
"@/*": ["./src/*"],
"@api/*": ["../src/api/*"],
},
"resolveJsonModule": true,
"rootDir": "src",
Expand Down
22 changes: 16 additions & 6 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import axios, { AxiosInstance } from 'axios';
import { createHash } from 'crypto';

export interface AqaraApiOption {
region: 'cn' | 'us' | 'kr' | 'ru' | 'eu' | 'sg';
appId: string;
appKey: string;
keyId: string;

export interface AqaraApiOption extends Aqara.AppConfig {
region: 'cn' | 'us' | 'kr' | 'ru' | 'eu' | 'sg';
}

const API_DOMAIN = {
Expand Down Expand Up @@ -37,7 +35,7 @@ export class AqaraApi {
});
}

sign(accessToken?: string) {
private sign(accessToken?: string) {
const { appId, keyId, appKey } = this.option;
const nonce = Math.random().toString(36).slice(2);
const timestamp = Date.now();
Expand All @@ -54,4 +52,16 @@ export class AqaraApi {
sign,
};
}

async getAuthCode() {
const { data } = await this.axios.post('/', {
"intent": "config.auth.getAuthCode",
"data": {
"account": "189000123456",
"accountType": 0,
"accessTokenValidity": "1h"
}
});
return data;
}
}
7 changes: 7 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare namespace Aqara {
interface AppConfig {
appId: string;
appKey: string;
keyId: string;
}
}

0 comments on commit dd2c596

Please sign in to comment.