homebridge-xbox-tv 3.6.5 → 3.6.7

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
@@ -149,6 +149,7 @@ Homebridge plugin for Microsoft game Consoles. Tested with Xbox One X/S and Xbox
149
149
  ### RESTFul Integration
150
150
 
151
151
  * POST data as a JSON Object `{Power: true}`, content type must be `application/json`
152
+ * Path `status` response all available paths.
152
153
 
153
154
  | Method | URL | Path | Response | Type |
154
155
  | --- | --- | --- | --- | --- |
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Xbox TV",
3
3
  "name": "homebridge-xbox-tv",
4
- "version": "3.6.5",
4
+ "version": "3.6.7",
5
5
  "description": "Homebridge plugin to control Xbox game consoles.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/restful.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import express, { json } from 'express';
2
2
  import EventEmitter from 'events';
3
3
 
4
+ const DEFAULT_MESSAGE = 'This data is not available at this time.';
5
+
4
6
  class RestFul extends EventEmitter {
5
7
  constructor(config) {
6
8
  super();
@@ -8,45 +10,57 @@ class RestFul extends EventEmitter {
8
10
  this.restFulDebug = config.debug;
9
11
 
10
12
  this.restFulData = {
11
- info: 'This data is not available in your system.',
12
- state: 'This data is not available in your system.',
13
- consoleslist: 'This data is not available in your system.',
14
- profile: 'This data is not available in your system.',
15
- apps: 'This data is not available in your system.',
16
- storages: 'This data is not available in your system.',
17
- status: 'This data is not available in your system.'
13
+ info: DEFAULT_MESSAGE,
14
+ state: DEFAULT_MESSAGE,
15
+ consoleslist: DEFAULT_MESSAGE,
16
+ profile: DEFAULT_MESSAGE,
17
+ apps: DEFAULT_MESSAGE,
18
+ storages: DEFAULT_MESSAGE,
19
+ status: DEFAULT_MESSAGE
18
20
  }
19
21
  this.connect();
20
22
  }
21
23
 
22
24
  connect() {
23
25
  try {
24
- const restFul = express();
25
- restFul.set('json spaces', 2);
26
- restFul.use(json());
26
+ const app = express();
27
+ app.set('json spaces', 2);
28
+ app.use(json());
29
+
30
+ // Register GET routes for all keys
31
+ for (const key of Object.keys(this.restFulData)) {
32
+ app.get(`/${key}`, (req, res) => {
33
+ res.json(this.restFulData[key]);
34
+ });
35
+ }
27
36
 
28
- // GET Routes
29
- restFul.get('/info', (req, res) => { res.json(this.restFulData.info) });
30
- restFul.get('/state', (req, res) => { res.json(this.restFulData.state) });
31
- restFul.get('/consoleslist', (req, res) => { res.json(this.restFulData.consoleslist) });
32
- restFul.get('/profile', (req, res) => { res.json(this.restFulData.profile) });
33
- restFul.get('/apps', (req, res) => { res.json(this.restFulData.apps) });
34
- restFul.get('/storages', (req, res) => { res.json(this.restFulData.storages) });
35
- restFul.get('/status', (req, res) => { res.json(this.restFulData.status) });
37
+ // Health check route
38
+ app.get('/status', (req, res) => {
39
+ res.json({
40
+ status: 'online',
41
+ uptime: process.uptime(),
42
+ available_paths: Object.keys(this.restFulData).map(k => `/${k}`)
43
+ });
44
+ });
36
45
 
37
- // POST Route
38
- restFul.post('/', (req, res) => {
46
+ // POST route to update values
47
+ app.post('/', (req, res) => {
39
48
  try {
40
49
  const obj = req.body;
41
50
  if (!obj || typeof obj !== 'object' || Object.keys(obj).length === 0) {
42
- this.emit('warn', `RESTFul Invalid JSON payload`);
51
+ this.emit('warn', 'RESTFul Invalid JSON payload');
43
52
  return res.status(400).json({ error: 'RESTFul Invalid JSON payload' });
44
53
  }
54
+
45
55
  const key = Object.keys(obj)[0];
46
56
  const value = obj[key];
47
57
  this.emit('set', key, value);
58
+ this.update(key, value);
59
+
60
+ if (this.restFulDebug) {
61
+ this.emit('debug', `RESTFul post data: ${JSON.stringify(obj, null, 2)}`);
62
+ }
48
63
 
49
- const emitDebug = this.restFulDebug ? this.emit('debug', `RESTFul post data: ${JSON.stringify(obj, null, 2)}`) : false;
50
64
  res.json({ success: true, received: obj });
51
65
  } catch (error) {
52
66
  this.emit('warn', `RESTFul Parse error: ${error}`);
@@ -54,43 +68,26 @@ class RestFul extends EventEmitter {
54
68
  }
55
69
  });
56
70
 
57
- // Start server
58
- restFul.listen(this.restFulPort, () => {
71
+ // Start the server
72
+ app.listen(this.restFulPort, () => {
59
73
  this.emit('connected', `RESTful started on port: ${this.restFulPort}`);
60
74
  });
61
75
  } catch (error) {
62
- this.emit('warn', `RESTful Connect error: ${error}`)
76
+ this.emit('warn', `RESTful Connect error: ${error}`);
63
77
  }
64
- };
78
+ }
65
79
 
66
80
  update(path, data) {
67
- switch (path) {
68
- case 'info':
69
- this.restFulData.info = data;
70
- break;
71
- case 'state':
72
- this.restFulData.state = data;
73
- break;
74
- case 'consoleslist':
75
- this.restFulData.consoleslist = data;
76
- break;
77
- case 'profile':
78
- this.restFulData.profile = data;
79
- break;
80
- case 'apps':
81
- this.restFulData.apps = data;
82
- break;
83
- case 'storages':
84
- this.restFulData.storages = data;
85
- break;
86
- case 'status':
87
- this.restFulData.status = data;
88
- break;
89
- default:
90
- this.emit('warn', `RESTFul update unknown path: ${path}, data: ${data}`)
91
- break;
81
+ if (this.restFulData.hasOwnProperty(path)) {
82
+ this.restFulData[path] = data;
83
+ } else {
84
+ this.emit('warn', `Unknown RESTFul update path: ${path}, data: ${JSON.stringify(data)}`);
85
+ return;
86
+ }
87
+
88
+ if (this.restFulDebug) {
89
+ this.emit('debug', `RESTFul update path: ${path}, data: ${JSON.stringify(data)}`);
92
90
  }
93
- const emitDebug = this.restFulDebug ? this.emit('debug', `RESTFul update path: ${path}, data: ${JSON.stringify(data, null, 2)}`) : false;
94
91
  }
95
92
  }
96
93
  export default RestFul;
@@ -26,14 +26,20 @@ class XboxWebApi extends EventEmitter {
26
26
  }
27
27
  this.authentication = new Authentication(authConfig);
28
28
 
29
- //create impulse generator
29
+ //impulse generator
30
+ this.call = false;
30
31
  this.impulseGenerator = new ImpulseGenerator();
31
32
  this.impulseGenerator.on('checkAuthorization', async () => {
33
+ if (this.call) return;
34
+
32
35
  try {
36
+ this.call = true;
33
37
  await this.checkAuthorization();
38
+ this.call = false;
34
39
  } catch (error) {
35
- this.emit('error', error);
36
- }
40
+ this.call = false;
41
+ this.emit('error', `Inpulse generator error: ${error}`);
42
+ };
37
43
  }).on('state', (state) => {
38
44
  const emitState = state ? this.emit('success', `Web Api monitoring started`) : this.emit('warn', `Web Api monitoring stopped`);
39
45
  });
package/src/xboxdevice.js CHANGED
@@ -257,8 +257,8 @@ class XboxDevice extends EventEmitter {
257
257
  this.mqtt1 = new Mqtt({
258
258
  host: this.mqtt.host,
259
259
  port: this.mqtt.port || 1883,
260
- clientId: this.mqtt.clientId || `microsoft_${Math.random().toString(16).slice(3)}`,
261
- prefix: this.mqtt.prefix || `microsoft/${this.name}`,
260
+ clientId: this.mqtt.clientId ? `microsoft_${this.mqtt.clientId}_${Math.random().toString(16).slice(3)}` : `microsoft_${Math.random().toString(16).slice(3)}`,
261
+ prefix: this.mqtt.prefix ? `microsoft/${this.mqtt.prefix}/${this.name}` : `microsoft/${this.name}`,
262
262
  user: this.mqtt.user,
263
263
  passwd: this.mqtt.passwd,
264
264
  debug: this.mqtt.debug || false