Skip to content

Commit

Permalink
fix display duplicated dev info and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz914 committed Sep 6, 2024
1 parent 4242d86 commit c0a300c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
50 changes: 26 additions & 24 deletions src/localApi/xboxlocalapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class XBOXLOCALAPI extends EventEmitter {
this.channels = [];
this.channelRequestId = 0;
this.mediaRequestId = 0;
this.emitDevInfo = true;
this.startPrepareAccessory = true;
};

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.');
Expand Down
4 changes: 3 additions & 1 deletion src/xboxdevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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);
})
Expand Down

0 comments on commit c0a300c

Please sign in to comment.