From 4cd97ec94f7b863ca92dbebb8eaeb87e77ac7bda Mon Sep 17 00:00:00 2001 From: "baran.wang" Date: Fri, 10 Nov 2023 10:24:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E8=AE=BE=E5=A4=87=20?= =?UTF-8?q?ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 103 +------------------------------------- packages/api/package.json | 3 +- packages/api/src/index.ts | 66 +++++++++++++----------- 3 files changed, 39 insertions(+), 133 deletions(-) diff --git a/package-lock.json b/package-lock.json index e733325..61b6928 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6407,11 +6407,6 @@ "integrity": "sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==", "dev": true }, - "node_modules/@socket.io/component-emitter": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.1.0.tgz", - "integrity": "sha512-+9jVqKhRSpsc591z5vX+X5Yyw+he/HCB4iQ/RYxw35CEPaY1gnsNE43nf9n9AaYjAQrTiI/mOwKUKdUs9vf7Xg==" - }, "node_modules/@svgr/babel-plugin-add-jsx-attribute": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/@svgr/babel-plugin-add-jsx-attribute/-/babel-plugin-add-jsx-attribute-8.0.0.tgz", @@ -10099,46 +10094,6 @@ "node": ">=10.0.0" } }, - "node_modules/engine.io-client": { - "version": "6.5.2", - "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.5.2.tgz", - "integrity": "sha512-CQZqbrpEYnrpGqC07a9dJDz4gePZUgTPMU3NKJPSeQOyw27Tst4Pl3FemKoFGAlHzgZmKjoRmiJvbWfhCXUlIg==", - "dependencies": { - "@socket.io/component-emitter": "~3.1.0", - "debug": "~4.3.1", - "engine.io-parser": "~5.2.1", - "ws": "~8.11.0", - "xmlhttprequest-ssl": "~2.0.0" - } - }, - "node_modules/engine.io-client/node_modules/engine.io-parser": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.2.1.tgz", - "integrity": "sha512-9JktcM3u18nU9N2Lz3bWeBgxVgOKpw7yhRaoxQA3FUDZzzw+9WlA6p4G4u0RixNkg14fH7EfEc/RhpurtiROTQ==", - "engines": { - "node": ">=10.0.0" - } - }, - "node_modules/engine.io-client/node_modules/ws": { - "version": "8.11.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz", - "integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": "^5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } - }, "node_modules/engine.io-parser": { "version": "5.0.7", "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.7.tgz", @@ -19243,32 +19198,6 @@ "integrity": "sha512-W4N+o69rkMEGVuk2D/cvca3uYsvGlMwsySWV447y99gUPghxq42BxqLNMndb+a1mm/5/7NeXVQS7RLa2XyXvYg==", "dev": true }, - "node_modules/socket.io-client": { - "version": "4.7.2", - "resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.7.2.tgz", - "integrity": "sha512-vtA0uD4ibrYD793SOIAwlo8cj6haOeMHrGvwPxJsxH7CeIksqJ+3Zc06RvWTIFgiSqx4A3sOnTXpfAEE2Zyz6w==", - "dependencies": { - "@socket.io/component-emitter": "~3.1.0", - "debug": "~4.3.2", - "engine.io-client": "~6.5.2", - "socket.io-parser": "~4.2.4" - }, - "engines": { - "node": ">=10.0.0" - } - }, - "node_modules/socket.io-client/node_modules/socket.io-parser": { - "version": "4.2.4", - "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz", - "integrity": "sha512-/GbIKmo8ioc+NIWIhwdecY0ge+qVBSMdgxGygevmdHj24bsfgtCmcUUcQ5ZzcylGFHsN3k4HB4Cgkl96KVnuew==", - "dependencies": { - "@socket.io/component-emitter": "~3.1.0", - "debug": "~4.3.1" - }, - "engines": { - "node": ">=10.0.0" - } - }, "node_modules/socket.io-parser": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.0.5.tgz", @@ -21014,7 +20943,6 @@ "version": "9.0.1", "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", - "dev": true, "funding": [ "https://github.com/sponsors/broofa", "https://github.com/sponsors/ctavan" @@ -21771,14 +21699,6 @@ "node": ">=4.0" } }, - "node_modules/xmlhttprequest-ssl": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-2.0.0.tgz", - "integrity": "sha512-QKxVRxiRACQcVuQEYFsI1hhkrMlrXHPegbbd1yn9UHOmRxY+si12nQYzri3vbzt8VdTTRviqcKxcyllFas5z2A==", - "engines": { - "node": ">=0.4.0" - } - }, "node_modules/xregexp": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/xregexp/-/xregexp-4.4.1.tgz", @@ -21915,8 +21835,7 @@ "@hb-haier/utils": "1.1.0", "axios": "^1.5.1", "homebridge": "^1.6.1", - "socket.io-client": "^4.7.2", - "ws": "^8.14.2" + "uuid": "^9.0.1" }, "devDependencies": { "@types/node": "^18.16.20", @@ -21926,26 +21845,6 @@ "node": ">=18.0.0" } }, - "packages/api/node_modules/ws": { - "version": "8.14.2", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz", - "integrity": "sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": ">=5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } - }, "packages/homebridge-plugin": { "name": "homebridge-plugin-haier", "version": "0.1.0", diff --git a/packages/api/package.json b/packages/api/package.json index c5cbdda..814d391 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -14,8 +14,7 @@ "@hb-haier/utils": "1.1.0", "axios": "^1.5.1", "homebridge": "^1.6.1", - "socket.io-client": "^4.7.2", - "ws": "^8.14.2" + "uuid": "^9.0.1" }, "devDependencies": { "@types/node": "^18.16.20", diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index b2ddfa4..4c6ca57 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -5,6 +5,7 @@ import { URL } from 'url'; import { HttpError, getSn, sha256 } from '@hb-haier/utils'; import axios from 'axios'; import { Logger } from 'homebridge/lib/logger'; +import { v4 as uuid } from 'uuid'; import type { DevDigitalModel, @@ -15,7 +16,6 @@ import type { } from './types'; import type { AxiosInstance } from 'axios'; import type { API } from 'homebridge'; -import type { io } from 'socket.io-client'; export * from './types'; @@ -25,7 +25,6 @@ const APP_KEY = '5dfca8714eb26e3a776e58a8273c8752'; // const APP_KEY = '79ce99cc7f9804663939676031b8a427'; const APP_VERSION = '7.19.2'; // const APP_SECRET = 'E46602789309498CEEDAACB585B00F40'; -const CLIENT_ID = 'CCD4B90C-E195-4A9B-9F71-15A92AA1DE87'; const LOGIN_URL = '/oauthserver/account/v1/login'; export interface HaierApiConfig { @@ -40,9 +39,29 @@ export class HaierApi { logger = new Logger('HaierApi'); - socket!: ReturnType; + get storagePath() { + return path.resolve(this.api?.user.storagePath() ?? path.dirname(__dirname), '.hb-haier'); + } + + get tokenPath() { + const tokenDir = path.resolve(this.storagePath, 'token'); + if (!fs.existsSync(tokenDir)) { + fs.mkdirSync(tokenDir, { recursive: true }); + } + return path.resolve(tokenDir, `${this.config.username}.json`); + } - // lastHeartbeatAt = Date.now(); + get clientId() { + const cacheClientIdPath = path.resolve(this.storagePath, 'client-id'); + if (fs.existsSync(cacheClientIdPath)) { + return fs.readFileSync(cacheClientIdPath, 'utf-8'); + } + const clientId = uuid(); + fs.writeFileSync(cacheClientIdPath, clientId); + // 如果 clientId 变了,那么 token 也要重新获取 + this.tokenInfo = undefined; + return clientId; + } constructor(readonly config: HaierApiConfig, readonly api?: API) { this.getTokenInfo(); @@ -50,13 +69,12 @@ export class HaierApi { this.axios = axios.create({ baseURL: 'https://zj.haier.net', headers: { - Accept: '*/*', appId: APP_ID, appKey: APP_KEY, appVersion: APP_VERSION, - clientId: CLIENT_ID, + clientId: this.clientId, language: 'zh-cn', - timezone: '+8', + timezone: '8', 'User-Agent': `Uplus/${APP_VERSION} (iPhone; iOS 17.0.2; Scale/2.00)`, }, }); @@ -116,7 +134,7 @@ export class HaierApi { async getDevDigitalModel(deviceId: string) { return this.axios - .post<{ detailInfo: string }>('https://uws.haier.net/shadow/v1/devdigitalmodels', { + .post<{ detailInfo: Record }>('https://uws.haier.net/shadow/v1/devdigitalmodels', { deviceInfoList: [{ deviceId }], }) .then(res => { @@ -137,16 +155,6 @@ export class HaierApi { }); } - get tokenPath() { - const storagePath = this.api?.user.storagePath() ?? path.dirname(__dirname); - const tokenDir = path.resolve(storagePath, '.hb-haier/token'); - if (!fs.existsSync(tokenDir)) { - fs.mkdirSync(tokenDir, { recursive: true }); - } - console.log('tokenDir', tokenDir); - return path.resolve(tokenDir, `${this.config.username}.json`); - } - /** * 获取 websocket 地址, * 但是 token 的问题还没解决 @@ -165,7 +173,7 @@ export class HaierApi { url.protocol = 'wss:'; url.pathname = '/userag'; url.searchParams.set('token', accessToken); - url.searchParams.set('clientId', CLIENT_ID); + url.searchParams.set('clientId', this.clientId); return url.toString(); }); } @@ -176,16 +184,16 @@ export class HaierApi { }); } - wsMessageSender(topic: string, content: Record) { - this.socket.send( - JSON.stringify({ - topic, - content, - agClientId: this.tokenInfo?.uhomeAccessToken, - }), - ); - this.logger.info('ws send', topic, content); - } + // wsMessageSender(topic: string, content: Record) { + // this.socket.send( + // JSON.stringify({ + // topic, + // content, + // agClientId: this.tokenInfo?.uhomeAccessToken, + // }), + // ); + // this.logger.info('ws send', topic, content); + // } private setTokenInfo() { fs.writeFileSync(this.tokenPath, JSON.stringify(this.tokenInfo));