homebridge-xbox-tv 2.13.1 → 2.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -144,8 +144,8 @@ Homebridge plugin for Microsoft game Consoles. Tested with Xbox One X/S and Xbox
144
144
 
145
145
  | Direction | Topic | Message | Payload Data |
146
146
  | --- | --- | --- | --- |
147
- | Publish | `Info`, `State`, `Consoles List`, `Profile`, `Apps`, `Storages`, `Status` | `{power: true, app: Xbox.Dashboard_8wekyb3d8bbwe!Xbox.Dashboard.Application}` | JSON object. |
148
- | Subscribe | `Set` | `{Power: true}` | JSON object. |
147
+ | Publish | `Info`, `State`, `Consoles List`, `Profile`, `Apps`, `Storages`, `Status` | `{"power": true, "app": Xbox.Dashboard_8wekyb3d8bbwe!Xbox.Dashboard.Application}` | JSON object. |
148
+ | Subscribe | `Set` | `{"Power": true}` | JSON object. |
149
149
 
150
150
  | Subscribe | Key | Value | Type | Description |
151
151
  | --- | --- | --- | --- | --- |
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Xbox TV",
3
3
  "name": "homebridge-xbox-tv",
4
- "version": "2.13.1",
4
+ "version": "2.13.2",
5
5
  "description": "Homebridge plugin to control Xbox game consoles.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/mqtt.js CHANGED
@@ -2,70 +2,52 @@
2
2
  const AsyncMqtt = require("async-mqtt");
3
3
  const EventEmitter = require('events');
4
4
 
5
- class MQTTCLIENT extends EventEmitter {
5
+ class Mqtt extends EventEmitter {
6
6
  constructor(config) {
7
7
  super();
8
- this.mqttHost = config.host;
9
- this.mqttPort = config.port;
10
- this.mqttClientId = config.clientId;
11
- this.mqttPrefix = config.prefix;
12
- this.mqttUser = config.user;
13
- this.mqttPasswd = config.passwd;
14
- this.mqttDebug = config.debug;
15
- this.isConnected = false;
16
-
17
- this.connect();
18
- };
19
-
20
- async connect() {
21
- try {
22
- const options = {
23
- clientId: this.mqttClientId,
24
- username: this.mqttUser,
25
- password: this.mqttPasswd
26
- }
27
- const url = `mqtt://${this.mqttHost}:${this.mqttPort}`;
28
- this.mqttClient = await AsyncMqtt.connectAsync(url, options);
29
- this.emit('connected', 'MQTT Connected.');
30
- this.isConnected = true;
31
-
32
- this.subscribe();
33
- } catch (error) {
34
- this.isConnected = false;
35
- this.emit('error', `MQTT Connect error: ${error}`);
36
- };
37
- };
38
-
39
- async subscribe() {
40
- try {
41
- this.mqttClient.on('message', (topic, message) => {
42
- const subscribehMessage = JSON.parse(message.toString());
43
- const emitDebug = this.mqttDebug ? this.emit('debug', `MQTT Received topic: ${topic}, message: ${JSON.stringify(subscribehMessage, null, 2)}`) : false;
44
- this.emit('changeState', subscribehMessage);
45
- });
46
-
47
- const topic = `${this.mqttPrefix}/Set`;
48
- await this.mqttClient.subscribe(topic);
49
- this.emit('connected', `MQTT Subscribe topic: ${topic}.`);
50
- } catch (error) {
51
- this.emit('error', `MQTT Subscribe error: ${error}`);
52
- };
53
- };
54
-
55
- async send(topic, message) {
56
- if (!this.isConnected) {
57
- const debug = this.mqttDebug ? this.emit('debug', `MQTT client not connected.`) : false;
58
- return
59
- };
60
-
61
- try {
62
- const fullTopic = `${this.mqttPrefix}/${topic}`;
63
- const publishMessage = JSON.stringify(message, null, 2);
64
- await this.mqttClient.publish(fullTopic, publishMessage);
65
- const debug = this.mqttDebug ? this.emit('debug', `MQTT publish: ${fullTopic}: ${publishMessage}`) : false;
66
- } catch (error) {
67
- this.emit('error', `MQTT Publish error: ${error}`);
68
- };
8
+ const options = {
9
+ clientId: config.clientId,
10
+ username: config.user,
11
+ password: config.passwd
12
+ }
13
+ const url = `mqtt://${config.host}:${config.port}`;
14
+ const subscribeTopic = `${config.prefix}/Set`;
15
+
16
+ this.on('connect', async () => {
17
+ try {
18
+ //connect
19
+ this.mqttClient = await AsyncMqtt.connectAsync(url, options);
20
+ this.emit('connected', 'MQTT Connected.');
21
+
22
+ //subscribe
23
+ await this.mqttClient.subscribe(subscribeTopic);
24
+ this.emit('subscribed', `MQTT Subscribe topic: ${subscribeTopic}.`);
25
+
26
+ //subscribed message
27
+ this.mqttClient.on('message', (topic, message) => {
28
+ try {
29
+ const subscribedMessage = JSON.parse(message.toString());
30
+ const emitDebug = config.debug ? this.emit('debug', `MQTT Received topic: ${topic}, message: ${JSON.stringify(subscribedMessage, null, 2)}`) : false;
31
+ this.emit('subscribedMessage', subscribedMessage);
32
+ } catch (error) {
33
+ this.emit('error', `MQTT Parse message error: ${error}`);
34
+ };
35
+ });
36
+ } catch (error) {
37
+ this.emit('error', `MQTT Connect error: ${error}`);
38
+ };
39
+ }).on('publish', async (topic, message) => {
40
+ try {
41
+ const fullTopic = `${config.prefix}/${topic}`;
42
+ const publishMessage = JSON.stringify(message, null, 2);
43
+ await this.mqttClient.publish(fullTopic, publishMessage);
44
+ const emitDebug = config.debug ? this.emit('debug', `MQTT Publish topic: ${fullTopic}, message: ${publishMessage}`) : false;
45
+ } catch (error) {
46
+ this.emit('error', `MQTT Publish error: ${error}`);
47
+ };
48
+ });
49
+
50
+ this.emit('connect');
69
51
  };
70
52
  };
71
- module.exports = MQTTCLIENT;
53
+ module.exports = Mqtt;
package/src/xboxdevice.js CHANGED
@@ -280,8 +280,8 @@ class XboxDevice extends EventEmitter {
280
280
  });
281
281
 
282
282
  this.restFul.on('connected', (message) => {
283
- this.emit('message', message);
284
283
  this.restFulConnected = true;
284
+ this.emit('message', message);
285
285
  })
286
286
  .on('error', (error) => {
287
287
  this.emit('error', error);
@@ -305,8 +305,8 @@ class XboxDevice extends EventEmitter {
305
305
  });
306
306
 
307
307
  this.mqtt.on('connected', (message) => {
308
- this.emit('message', message);
309
308
  this.mqttConnected = true;
309
+ this.emit('message', message);
310
310
  })
311
311
  .on('changeState', async (data) => {
312
312
  const key = Object.keys(data)[0];
@@ -368,7 +368,7 @@ class XboxDevice extends EventEmitter {
368
368
  break;
369
369
  };
370
370
  } catch (error) {
371
- this.emit('error', `set: ${key}, over MQTT, error: ${error}`);
371
+ this.emit('error', `MQTT send error: ${error}.`);
372
372
  };
373
373
  })
374
374
  .on('debug', (debug) => {
@@ -427,7 +427,7 @@ class XboxDevice extends EventEmitter {
427
427
  const restFul = this.restFulConnected ? this.restFul.update(path, data) : false;
428
428
  })
429
429
  .on('mqtt', (topic, message) => {
430
- const mqtt = this.mqttConnected ? this.mqtt.send(topic, message) : false;
430
+ const mqtt = this.mqttConnected ? this.mqtt.emit('publish', topic, message) : false;
431
431
  });
432
432
  }
433
433