From c0a300cb37884f19427745a731b191e0ac848668 Mon Sep 17 00:00:00 2001 From: Grzegorz Date: Fri, 6 Sep 2024 14:05:25 +0200 Subject: [PATCH] fix display duplicated dev info and cleanup --- CHANGELOG.md | 10 ++++++-- package.json | 2 +- src/localApi/xboxlocalapi.js | 50 +++++++++++++++++++----------------- src/xboxdevice.js | 4 ++- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56251e0..e4b2dab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### NOTE -### After update to 2.x.x the plugin settings (xboxLiveId) need to be updated +- After update to 2.x.x the plugin settings (xboxLiveId) need to be updated +- After update to v3.0.0 RESTFull and MQTT config settings need to be updated -### After update to v3.0.0 RESTFull and MQTT config settings need to be updated +## [3.1.4] - (06.09.2024) + +## Changes + +- fix display duplicated dev info +- cleanup ## [3.1.3] - (06.09.2024) diff --git a/package.json b/package.json index 0220b58..aa527a3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "displayName": "Xbox TV", "name": "homebridge-xbox-tv", - "version": "3.1.3", + "version": "3.1.4", "description": "Homebridge plugin to control Xbox game consoles.", "license": "MIT", "author": "grzegorz914", diff --git a/src/localApi/xboxlocalapi.js b/src/localApi/xboxlocalapi.js index 4ff101b..62a35f5 100644 --- a/src/localApi/xboxlocalapi.js +++ b/src/localApi/xboxlocalapi.js @@ -31,7 +31,6 @@ class XBOXLOCALAPI extends EventEmitter { this.channels = []; this.channelRequestId = 0; this.mediaRequestId = 0; - this.emitDevInfo = true; this.startPrepareAccessory = true; }; @@ -151,14 +150,15 @@ class XBOXLOCALAPI extends EventEmitter { connectRequest.set('publicKey', data.publicKey); connectRequest.set('iv', data.iv); - const tokenData = await this.readToken(); - const tokenExist = tokenData ? true : false; - if (tokenExist) { + const token = await this.readData(this.tokensFile); + const parseTokenData = JSON.parse(token); + const tokenData = parseTokenData.xsts.Token.length > 0 ? parseTokenData : false; + if (tokenData) { connectRequest.set('userHash', tokenData.xsts.DisplayClaims.xui[0].uhs, true); connectRequest.set('jwt', tokenData.xsts.Token, true); + this.isAuthorized = true; } - this.isAuthorized = tokenExist; - const debug = this.debugLog ? this.emit('debug', `Client connecting using: ${tokenExist ? 'XSTS token' : 'Anonymous'}.`) : false; + const debug = this.debugLog ? this.emit('debug', `Client connecting using: ${this.isAuthorized ? 'XSTS token' : 'Anonymous'}.`) : false; const message = connectRequest.pack(this.crypto); await this.sendSocketMessage(message, 'connectRequest'); @@ -240,15 +240,27 @@ class XBOXLOCALAPI extends EventEmitter { }; if (this.emitDevInfo) { + //connect to deice success + this.emit('success', `Connect Success.`) + const majorVersion = packet.payloadProtected.majorVersion; const minorVersion = packet.payloadProtected.minorVersion; const buildNumber = packet.payloadProtected.buildNumber; const locale = packet.payloadProtected.locale; const firmwareRevision = `${majorVersion}.${minorVersion}.${buildNumber}`; + const obj = { + manufacturer: 'Microsoft', + modelName: 'Xbox', + serialNumber: this.xboxLiveId, + firmwareRevision: firmwareRevision, + locale: locale + }; + //save device info to the file - await this.saveDevInfo(this.devInfoFile, firmwareRevision, locale) + await this.saveData(this.devInfoFile, obj) + //emit device info this.emit('deviceInfo', firmwareRevision, locale); this.emitDevInfo = false; }; @@ -319,6 +331,7 @@ class XBOXLOCALAPI extends EventEmitter { const address = socket.address(); const debug = this.debugLog ? this.emit('debug', `Server start listening: ${address.address}:${address.port}.`) : false; this.socket = socket; + this.emitDevInfo = true; //ping console setInterval(async () => { @@ -374,29 +387,19 @@ class XBOXLOCALAPI extends EventEmitter { } }; - async readToken() { + async readData(path) { try { - const data = await fsPromises.readFile(this.tokensFile); - const parseData = JSON.parse(data); - const tokenData = parseData.xsts.Token.length > 0 ? parseData : false; - return tokenData; + const data = await fsPromises.readFile(path); + return data; } catch (error) { throw new Error(`Read token error: ${error}`); } } - async saveDevInfo(path, firmwareRevision, locale) { + async saveData(path, data) { try { - const obj = { - manufacturer: 'Microsoft', - modelName: 'Xbox', - serialNumber: this.xboxLiveId, - firmwareRevision: firmwareRevision, - locale: locale - }; - const devInfo = JSON.stringify(obj, null, 2); - await fsPromises.writeFile(path, devInfo); - const debug = this.debugLog ? this.emit('debug', `Saved device info: ${devInfo}`) : false; + await fsPromises.writeFile(path, JSON.stringify(data, null, 2)); + const debug = this.debugLog ? this.emit('debug', `Saved data: ${JSON.stringify(data, null, 2)}`) : false; return true; } catch (error) { @@ -603,7 +606,6 @@ class XBOXLOCALAPI extends EventEmitter { this.channels = []; this.channelRequestId = 0; this.mediaRequestId = 0; - this.emitDevInfo = true; await new Promise(resolve => setTimeout(resolve, 3000)); this.emit('stateChanged', false, 0, true, 0, -1, -1); this.emit('disconnected', 'Disconnected.'); diff --git a/src/xboxdevice.js b/src/xboxdevice.js index 12d13eb..0aba94a 100644 --- a/src/xboxdevice.js +++ b/src/xboxdevice.js @@ -197,7 +197,6 @@ class XboxDevice extends EventEmitter { }); this.xboxLocalApi.on('deviceInfo', (firmwareRevision, locale) => { - this.emit('message', 'Connected.'); if (!this.disableLogDeviceInfo) { this.emit('devInfo', `-------- ${this.name} --------'`); this.emit('devInfo', `Manufacturer: Microsoft`); @@ -403,6 +402,9 @@ class XboxDevice extends EventEmitter { throw new Error(`prepare accessory error: ${error}`); }; }) + .on('success', (message) => { + this.emit('success', message); + }) .on('message', (message) => { this.emit('message', message); })