Skip to content

Commit

Permalink
bump 2.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz914 committed Jan 28, 2022
1 parent 71c95d1 commit 81e27a5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 57 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ 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.

## [2.1.3] - (28.01.2022)
### Fixed
- offset out of range
- code refactor

## [2.1.2] - (21.01.2022)
### Fixed
- [#136](https://github.com/grzegorz914/homebridge-xbox-tv/issues/136)
Expand Down
44 changes: 9 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class xboxTvDevice {

this.powerState = false;
this.volume = 0;
this.muteState = false;
this.muteState = true;
this.mediaState = 0;

this.setStartInput = false;
Expand Down Expand Up @@ -313,13 +313,6 @@ class xboxTvDevice {
});

this.xbox.on('connected', (message) => {
this.powerState = true;

if (this.televisionService) {
this.televisionService
.updateCharacteristic(Characteristic.Active, true)
};

if (this.webApiControl) {
this.checkAuthorizationState = setInterval(() => {
this.getAuthorizationState();
Expand Down Expand Up @@ -362,29 +355,15 @@ class xboxTvDevice {

this.firmwareRevision = firmwareRevision;
})
.on('stateChanged', (decodedMessage, mediaState) => {
const appsArray = new Array();
const appsCount = decodedMessage.apps.length;
for (let i = 0; i < appsCount; i++) {
const titleId = decodedMessage.apps[i].titleId;
const reference = decodedMessage.apps[i].aumId;
const app = {
titleId: titleId,
reference: reference
};
appsArray.push(app);
}
const titleId = appsArray[appsCount - 1].titleId;
const inputReference = appsArray[appsCount - 1].reference;
.on('stateChanged', (power, titleId, inputReference, volume, mute, mediaState) => {

//get states
const volume = this.volume;
const muteState = this.powerState ? this.muteState : true;
const powerState = power;
const inputIdentifier = this.inputsReference.indexOf(inputReference) >= 0 ? this.inputsReference.indexOf(inputReference) : this.inputsTitleId.indexOf(titleId) >= 0 ? this.inputsTitleId.indexOf(titleId) : this.inputIdentifier;

//update characteristics
if (this.televisionService) {
this.televisionService
.updateCharacteristic(Characteristic.Active, powerState)
.updateCharacteristic(Characteristic.ActiveIdentifier, inputIdentifier);

if (this.setStartInput) {
Expand All @@ -398,31 +377,26 @@ class xboxTvDevice {
if (this.speakerService) {
this.speakerService
.updateCharacteristic(Characteristic.Volume, volume)
.updateCharacteristic(Characteristic.Mute, muteState);
.updateCharacteristic(Characteristic.Mute, mute);
if (this.volumeService && this.volumeControl == 1) {
this.volumeService
.updateCharacteristic(Characteristic.Brightness, volume)
.updateCharacteristic(Characteristic.On, !muteState);
.updateCharacteristic(Characteristic.On, !mute);
};
if (this.volumeServiceFan && this.volumeControl == 2) {
this.volumeServiceFan
.updateCharacteristic(Characteristic.RotationSpeed, volume)
.updateCharacteristic(Characteristic.On, !muteState);
.updateCharacteristic(Characteristic.On, !mute);
};
};

this.powerState = powerState;
this.volume = volume;
this.muteState = muteState;
this.muteState = mute;
this.mediaState = mediaState;
this.inputIdentifier = inputIdentifier;
})
.on('disconnected', (message) => {
this.powerState = false;

if (this.televisionService) {
this.televisionService
.updateCharacteristic(Characteristic.Active, false)
};
const stopInterval = this.webApiControl ? clearInterval(this.checkAuthorizationState) : false;
this.log('Device: %s %s, %s', this.host, this.name, message);
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "2.1.2",
"version": "2.1.3",
"description": "Homebridge plugin (https://github.com/homebridge/homebridge) to control Xbox game consoles.",
"license": "MIT",
"author": "grzegorz914",
Expand Down
4 changes: 4 additions & 0 deletions src/packet/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class STRUCTURE {
this.offset = 0;
};

setOffset(offset) {
this.offset = offset;
};

writeSGString(data) {
const lengthBuffer = Buffer.allocUnsafe(2);
lengthBuffer.writeUInt16BE(data.length, 0);
Expand Down
43 changes: 24 additions & 19 deletions src/smartglass.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const EOL = require('os').EOL;
const jsrsasign = require('jsrsasign');
const EventEmitter = require('events').EventEmitter;
const Packer = require('./packet/packer');
const Structure = require('./packet/structure');
const SGCrypto = require('./sgcrypto');

const systemMediaCommands = {
Expand Down Expand Up @@ -70,11 +71,9 @@ class SMARTGLASS extends EventEmitter {
this.userHash = config.uhs;

this.crypto = new SGCrypto();
this.structure = new Structure();
this.isConnected = false;
this.isAuthorized = false;
this.titleId = '';
this.currentApp = '';
this.mediaState = 0;
this.fragments = {};

this.requestNum = 0;
Expand Down Expand Up @@ -315,23 +314,25 @@ class SMARTGLASS extends EventEmitter {
this.emit('deviceInfo', firmwareRevision);
};

if (this.currentApp != decodedMessage.apps[0].aumId) {
const appsArray = new Array();
const appsCount = decodedMessage.apps.length;
for (let i = 0; i < appsCount; i++) {
const titleId = decodedMessage.apps[i].titleId;
const reference = decodedMessage.apps[i].aumId;
const app = {
titleId: titleId,
reference: reference
};
appsArray.push(app);
this.emit('debug', `Status changed, app Id: ${titleId}, reference: ${reference}`);
const appsArray = new Array();
const appsCount = decodedMessage.apps.length;
for (let i = 0; i < appsCount; i++) {
const titleId = decodedMessage.apps[i].titleId;
const reference = decodedMessage.apps[i].aumId;
const app = {
titleId: titleId,
reference: reference
};
this.titleId = appsArray[appsCount - 1].titleId;
this.currentApp = appsArray[appsCount - 1].reference;
this.emit('stateChanged', decodedMessage, this.mediaState);
};
appsArray.push(app);
this.emit('debug', `Status changed, app Id: ${titleId}, reference: ${reference}`);
}
const power = this.isConnected;
const volume = 0;
const mute = power ? power : true;
const titleId = appsArray[appsCount - 1].titleId;
const inputReference = appsArray[appsCount - 1].reference;
const mediaState = 0;
this.emit('stateChanged', power, titleId, inputReference, volume, mute, mediaState);
};
})
.on('channelResponse', (message) => {
Expand Down Expand Up @@ -472,6 +473,7 @@ class SMARTGLASS extends EventEmitter {
this.emit('debug', 'Start discovery.');
this.discovery = setInterval(() => {
if (!this.isConnected) {
this.structure.setOffset(0);
const discoveryPacket = new Packer('simple.discoveryRequest');
const message = discoveryPacket.pack();
this.sendSocketMessage(message);
Expand All @@ -486,6 +488,7 @@ class SMARTGLASS extends EventEmitter {
const powerOnStartTime = (new Date().getTime()) / 1000;

this.boot = setInterval(() => {
this.structure.setOffset(0);
const powerOn = new Packer('simple.powerOn');
powerOn.set('liveId', this.xboxLiveId);
const message = powerOn.pack();
Expand Down Expand Up @@ -610,10 +613,12 @@ class SMARTGLASS extends EventEmitter {
this.sendSocketMessage(message);

setTimeout(() => {
this.structure.setOffset(0);
this.isConnected = false;
this.requestNum = 0;
this.channelTargetId = null;
this.channelRequestId = null;
this.emit('stateChanged', false, 0, 0, 0, true, 0);
this.emit('disconnected', 'Disconnected.');

// Start discovery
Expand Down

0 comments on commit 81e27a5

Please sign in to comment.