Skip to content

Commit

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

## [2.1.1] - (21.01.2022)
### Changed
- refactor debug message logging
- update readme

### Fixed
- wrong variables
- removed unnecessary async
- report unknown message if power on fail

## [2.1.0] - (21.01.2022)
### Added
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Homebridge plugin for Microsoft game Consoles. Tested with Xbox One X/S and Xbox
| [Xbox TV](https://www.npmjs.com/package/homebridge-xbox-tv) | `npm install -g homebridge-xbox-tv` | Plug-In | Required |

## Note
* For v1.4.0 and above the minimum required version of Homebridge is 1.3.x.
* For v2.0.0 and above the minimum required version of Node.js is 14.x.x.
* If upgrade from 1.x.x to 2.x.x the `xboxLiveId` need to be set again in config.
* For v1.4.0 and above the required version of Homebridge >= 1.3.x.
* For v2.0.0 and above the required version of Node.js >= 14.x.x.
* If upgrade from 1.x.x to 2.x.x the `xboxLiveId` need to be set again.

## Know Issues
* Console connected to WLAN network some times lose its connection to the network after *Power OFF* and the *Power ON* command may not work.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class xboxTvDevice {

this.firmwareRevision = firmwareRevision;
})
.on('stateChanged', async (decodedMessage, mediaState) => {
.on('stateChanged', (decodedMessage, mediaState) => {
const appsArray = new Array();
const appsCount = decodedMessage.apps.length;
for (let i = 0; i < appsCount; i++) {
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": "2.1.0",
"version": "2.1.1",
"description": "Homebridge plugin (https://github.com/homebridge/homebridge) to control Xbox game consoles.",
"license": "MIT",
"author": "grzegorz914",
Expand Down
59 changes: 31 additions & 28 deletions src/smartglass.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ class SMARTGLASS extends EventEmitter {
// Prepare buffer for JSON
this.fragments[jsonMessage.datagramId] = {

getValue: () => {
const buffer = Buffer.from('');
getValue() {
let buffer = Buffer.from('');
for (let partial in this.partials) {
buffer = Buffer.concat([
buffer,
Expand All @@ -142,22 +142,25 @@ class SMARTGLASS extends EventEmitter {
const bufferDecoded = Buffer(buffer.toString(), 'base64');
return bufferDecoded;
},
isValid: () => {
isValid() {
const json = this.getValue();
let isValid = false;
try {
JSON.parse(json.toString());
isValid = true;
} catch (error) {
return false;
isValid = false;
this.emit('error', `Valid packet error: ${error}`);
};
return true;
return isValid;
},
partials: {}
};
};

this.fragments[jsonMessage.datagramId].partials[jsonMessage.fragmentOffset] = jsonMessage.fragmentData;
if (this.fragments[jsonMessage.datagramId].isValid() == true) {
this.emit('debug', 'json: Completed fragmented packet.');
if (this.fragments[jsonMessage.datagramId].isValid()) {
this.emit('debug', 'Json completed fragmented packet.');
this.response.packetDecoded.protectedPayload.json = this.fragments[jsonMessage.datagramId].getValue().toString();
this.fragments[jsonMessage.datagramId] = undefined;
};
Expand All @@ -174,8 +177,8 @@ class SMARTGLASS extends EventEmitter {
this.message = decodedMessage;
};

this.emit(this.function, this.response);
this.emit('debug', `Received event type: ${this.function}`);
this.emit(this.function, this.response);
})
.on('listening', () => {
const address = this.socket.address();
Expand Down Expand Up @@ -256,12 +259,12 @@ class SMARTGLASS extends EventEmitter {
const lastMessageReceivedTime = (Math.trunc(((new Date().getTime()) / 1000) - this.messageReceivedTime));
this.emit('debug', `Start check message timeout, last received was ${lastMessageReceivedTime} seconds ago.`);
if (lastMessageReceivedTime == 5) {
this.emit('debug', `Last message was: ${lastMessageReceivedTime} seconds ago, send acknowledge.`);
this.emit('acknowledge');
this.emit('debug', `Last message was ${lastMessageReceivedTime} seconds ago, send acknowledge.`);
};
if (lastMessageReceivedTime > 12) {
this.emit('debug', `Last message was: ${lastMessageReceivedTime} seconds ago, send disconnect.`);
this.disconnect();
this.emit('debug', `Last message was ${lastMessageReceivedTime} seconds ago, send disconnect.`);
};
};
}, 1000);
Expand Down Expand Up @@ -348,6 +351,7 @@ class SMARTGLASS extends EventEmitter {

if (this.channelRequestId == 0) {
if (command in systemMediaCommands) {

let mediaRequestId = 0;
let requestId = '0000000000000000';
const requestIdLength = requestId.length;
Expand All @@ -359,8 +363,8 @@ class SMARTGLASS extends EventEmitter {
mediaCommand.set('command', systemMediaCommands[command]);
mediaCommand.setChannel(this.channelTargetId);
const message = mediaCommand.pack(this);
this.sendSocketMessage(message);
this.emit('debug', `System media send command: ${command}`);
this.sendSocketMessage(message);
} else {
this.emit('debug', `Unknown media input command: ${command}`);
};
Expand All @@ -374,8 +378,8 @@ class SMARTGLASS extends EventEmitter {
gamepadPress.set('buttons', systemInputCommands[command]);
gamepadPress.setChannel(this.channelTargetId);
const message = gamepadPress.pack(this);
this.emit('debug', `System input send press, command: ${command}`);
this.sendSocketMessage(message);
this.emit('message', `System input send press, command: ${command}`);

setTimeout(() => {
const timeStampUnpress = new Date().getTime();
Expand All @@ -384,8 +388,8 @@ class SMARTGLASS extends EventEmitter {
gamepadUnpress.set('buttons', systemInputCommands['unpress']);
gamepadUnpress.setChannel(this.channelTargetId);
const message = gamepadUnpress.pack(this);
this.sendSocketMessage(message);
this.emit('debug', `System input send unpress, command: unpress`);
this.sendSocketMessage(message);
}, 150);
} else {
this.emit('debug', `Unknown system input command: ${command}`);
Expand All @@ -407,8 +411,8 @@ class SMARTGLASS extends EventEmitter {
json.set('json', JSON.stringify(jsonRequest));
json.setChannel(this.channelTargetId);
const message = json.pack(this);
this.sendSocketMessage(message);
this.emit('debug', `TV remote send command: ${command}`);
this.sendSocketMessage(message);
} else {
this.emit('debug', `Unknown tv remote command: ${command}`);
};
Expand All @@ -427,35 +431,35 @@ class SMARTGLASS extends EventEmitter {
json.set('json', JSON.stringify(jsonRequest));
json.setChannel(this.channelTargetId);
const message = json.pack(this);
this.sendSocketMessage(message);
this.emit('debug', `System config send: ${configName}`);
this.sendSocketMessage(message);
};
};
})
.on('json', (message) => {
const response = JSON.parse(message.packetDecoded.protectedPayload.json);
if (response.response == "Error") {
this.emit('debug', `Got Error: ${response}`);
this.emit('error', `Json error: ${response}`);
} else {
if (response.response == 'GetConfiguration') {
this.emit('debug', 'Got tvRemote Configuration.');
this.configuration = response.params;
this.emit('debug', `TV remote configuration: ${this.configuration }`);
};
if (response.response == 'GetHeadendInfo') {
this.emit('debug', 'Got Headend Info.');
this.headendInfo = response.params;
this.emit('debug', `Headend info: ${this.headendInfo }`);
};
if (response.response == 'GetLiveTVInfo') {
this.emit('debug', 'Got Live TV Info.');
this.liveTv = response.params;
this.emit('debug', `Live TV info: ${this.liveTv }`);
};
if (response.response == 'GetTunerLineups') {
this.emit('debug', 'Got Tuner Lineups.');
this.tunerLineups = response.params;
this.emit('debug', `Tuner lineups: ${this.tunerLineups }`);
};
if (response.response == 'GetAppChannelLineups') {
this.emit('debug', 'Got App Channel Lineups.');
this.appChannelLineups = response.params;
this.emit('debug', `App channel lineups: ${this.appChannelLineups }`);
};
};
})
Expand All @@ -479,19 +483,18 @@ class SMARTGLASS extends EventEmitter {
return new Promise((resolve, reject) => {
if (!this.isConnected) {
this.emit('message', 'Send power On.');
const bootStartTime = (new Date().getTime()) / 1000;
const powerOnStartTime = (new Date().getTime()) / 1000;

this.boot = setInterval(() => {
const powerOn = new Packer('simple.powerOn');
powerOn.set('liveId', this.xboxLiveId);
const message = powerOn.pack();
this.sendSocketMessage(message);

const lastBootTime = (Math.trunc(((new Date().getTime()) / 1000) - bootStartTime));
this.emit('debug', `Last boot time was ${lastBootTime} seconds ago.`);
if (lastBootTime > 15) {
this.emit('disconnected');
const lastPowerOnTime = (Math.trunc(((new Date().getTime()) / 1000) - powerOnStartTime));
if (lastPowerOnTime > 15) {
clearInterval(this.boot)
this.emit('disconnected', 'Power On failed, please try again.');
};
}, 500);

Expand Down Expand Up @@ -533,7 +536,7 @@ class SMARTGLASS extends EventEmitter {
recordGameDvr() {
return new Promise((resolve, reject) => {
if (this.isConnected && this.isAuthorized) {
this.emit('debug', 'Send record game.');
this.emit('message', 'Send record game.');

const recordGameDvr = new Packer('message.recordGameDvr');
recordGameDvr.set('startTimeDelta', -60);
Expand Down Expand Up @@ -563,8 +566,8 @@ class SMARTGLASS extends EventEmitter {
channelRequest.set('service', Buffer.from(channelUuids[channelName], 'hex'));
channelRequest.set('activityId', 0);
const message = channelRequest.pack(this);
this.sendSocketMessage(message);
this.emit('debug', `Send channel request name: ${channelName}, id: ${channelIds[channelName]}`);
this.sendSocketMessage(message);

setTimeout(() => {
this.emit('sendCommand', command)
Expand Down

0 comments on commit 08d2637

Please sign in to comment.