diff --git a/CHANGELOG.md b/CHANGELOG.md index c1d0180..6debf67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/index.js b/index.js index e513ae3..547ac5d 100644 --- a/index.js +++ b/index.js @@ -253,7 +253,7 @@ class xboxTvDevice { this.powerState = false; this.volume = 0; - this.muteState = false; + this.muteState = true; this.mediaState = 0; this.setStartInput = false; @@ -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(); @@ -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) { @@ -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); }); diff --git a/package-lock.json b/package-lock.json index 4f808ad..8d083a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "homebridge-xbox-tv", - "version": "2.1.0", + "version": "2.1.3", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "2.1.0", + "version": "2.1.3", "license": "MIT", "dependencies": { "@homebridge/plugin-ui-utils": ">=0.0.19", diff --git a/package.json b/package.json index e49feeb..86229e0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/packet/structure.js b/src/packet/structure.js index e550372..48bdea9 100644 --- a/src/packet/structure.js +++ b/src/packet/structure.js @@ -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); diff --git a/src/smartglass.js b/src/smartglass.js index 13a1e11..2046264 100644 --- a/src/smartglass.js +++ b/src/smartglass.js @@ -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 = { @@ -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; @@ -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) => { @@ -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); @@ -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(); @@ -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