homebridge-xbox-tv 2.8.2-beta.4 → 2.8.2-beta.41

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.
@@ -1,37 +1,35 @@
1
- <body>
2
- <div>
3
- <img src="homebridge-xbox-tv.png" style="text-align: center;" height="150" />
4
- </div>
5
-
6
- <div id="authorizationManager" style="display: none;" class="card card-body">
7
- <form id="configForm">
8
- <div class="text-center">
9
- <label id="deviceNameInput" style="font-size: 23px">Xbox</label><br>
10
- </div>
11
- <div class="text-center">
12
- <div class="text-center">
13
- <label id="info" style="text-align: center; font-size: 17px; color: white;">Authorization
14
- Manager</label><br><br>
15
- </div>
16
- </div>
17
- <div class="form-group">
18
- <label for="deviceHostInput">Host</label>
19
- <input type="text" class="form-control" id="deviceHostInput"><br>
20
- <label for="deviceLiveId">Xbox Live ID</label>
21
- <input type="text" class="form-control" id="deviceLiveId"><br>
22
- <label for="deviceTokenInput">Web Api Token</label>
23
- <input type="text" class="form-control" id="deviceTokenInput"><br>
24
- <label for="deviceWebApiControl">Web Api Control</label>
25
- <input type="checkbox" class="form-control" id="deviceWebApiControl">
26
- </div>
1
+ <div>
2
+ <img src="homebridge-xbox-tv.png" style="text-align: center;" height="150" />
3
+ </div>
4
+
5
+ <div id="authorizationManager" style="display: none;" class="card card-body">
6
+ <form id="configForm">
7
+ <div class="text-center">
8
+ <label id="deviceNameInput" style="font-size: 23px">Xbox</label><br>
9
+ </div>
10
+ <div class="text-center">
27
11
  <div class="text-center">
28
- <button id="startAuthorizationButton" type="button" class="btn btn-primary">Start authorization</button>
29
- <button id="clearTokenButton" type="button" class="btn btn-primary">Clear Web Api Token</button>
30
- <button id="configButton" type="button" class="btn btn-primary"><i class="fas fa-gear"></i></button>
12
+ <label id="info" style="text-align: center; font-size: 17px; color: white;">Authorization
13
+ Manager</label><br><br>
31
14
  </div>
32
- </form>
33
- </div>
34
- </body>
15
+ </div>
16
+ <div class="form-group">
17
+ <label for="deviceHostInput">Host</label>
18
+ <input type="text" class="form-control" id="deviceHostInput"><br>
19
+ <label for="deviceLiveId">Xbox Live ID</label>
20
+ <input type="text" class="form-control" id="deviceLiveId"><br>
21
+ <label for="deviceTokenInput">Web Api Token</label>
22
+ <input type="text" class="form-control" id="deviceTokenInput"><br>
23
+ <label for="deviceWebApiControl">Web Api Control</label>
24
+ <input type="checkbox" class="form-control" id="deviceWebApiControl">
25
+ </div>
26
+ <div class="text-center">
27
+ <button id="startAuthorizationButton" type="button" class="btn btn-primary">Start authorization</button>
28
+ <button id="clearTokenButton" type="button" class="btn btn-primary">Clear Web Api Token</button>
29
+ <button id="configButton" type="button" class="btn btn-primary"><i class="fas fa-gear"></i></button>
30
+ </div>
31
+ </form>
32
+ </div>
35
33
 
36
34
  <script>
37
35
  (async () => {
@@ -44,7 +42,7 @@
44
42
  const devices = { 'devices': [] };
45
43
  pluginConfig.push(devices);
46
44
  await homebridge.updatePluginConfig(pluginConfig);
47
- await homebridge.showSchemaForm();
45
+ homebridge.showSchemaForm();
48
46
  return;
49
47
  }
50
48
 
@@ -54,13 +52,12 @@
54
52
  //get devices count
55
53
  const devicesCount = pluginConfig[0].devices.length;
56
54
 
57
- // get the intial from the config and add it to the form
58
- this.deviceIndex = 0;
55
+ //get the intial from the config and add it to the form
59
56
  for (let i = 0; i < devicesCount; i++) {
60
57
  //create consoles buttons
61
58
  const button = document.createElement("button");
62
59
  button.setAttribute("type", "button");
63
- button.setAttribute("id", "button" + i);
60
+ button.setAttribute("id", `button${i}`);
64
61
  button.setAttribute("class", "btn btn-secondary");
65
62
  button.innerText = pluginConfig[0].devices[i].name || 'Not set';
66
63
  button.style.width = '160px';
@@ -68,64 +65,62 @@
68
65
  document.body.appendChild(button);
69
66
 
70
67
  //get actuall value on console select
71
- document.getElementById('button' + i).addEventListener('click', async () => {
68
+ document.getElementById(`button${i}`).addEventListener('click', async () => {
72
69
  document.querySelector('#deviceNameInput').innerHTML = pluginConfig[0].devices[i].name || '';
73
70
  document.querySelector('#deviceHostInput').value = pluginConfig[0].devices[i].host || '';
74
71
  document.querySelector('#deviceLiveId').value = pluginConfig[0].devices[i].xboxLiveId || '';
75
72
  document.querySelector('#deviceTokenInput').value = pluginConfig[0].devices[i].xboxWebApiToken || '';
76
73
  document.querySelector('#deviceWebApiControl').checked = pluginConfig[0].devices[i].webApiControl || false;
74
+
75
+ const tokenLength = (pluginConfig[0].devices[i].xboxWebApiToken).length || 0;
76
+ const setCheckboxState = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').checked = false : false;
77
+ const setCheckboxMode = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').disabled = true : document.getElementById('deviceWebApiControl').disabled = false;
78
+ const setWebApiControlMode = tokenLength <= 10 ? pluginConfig[0].devices[i].webApiControl = false : false;
77
79
  this.deviceIndex = i;
78
80
  });
81
+ this.deviceIndex = 0;
79
82
  };
80
83
 
81
- //add button show/hide config
82
- document.getElementById('configButton').addEventListener('click', async () => {
83
- const configButton = document.getElementById('configButton');
84
- if (this.configButtonState) {
85
- await homebridge.hideSchemaForm();
86
- this.configButtonState = false;
87
- } else {
88
- await homebridge.showSchemaForm();
89
- this.configButtonState = true;
90
- };
91
- });
92
-
93
- //load values on first run
84
+ //load values on first run, check token length and set buttons name and checkbock state after open
94
85
  document.querySelector('#deviceNameInput').innerHTML = pluginConfig[0].devices[0].name || '';
95
86
  document.querySelector('#deviceHostInput').value = pluginConfig[0].devices[0].host || '';
96
87
  document.querySelector('#deviceLiveId').value = pluginConfig[0].devices[0].xboxLiveId || '';
97
88
  document.querySelector('#deviceTokenInput').value = pluginConfig[0].devices[0].xboxWebApiToken || '';
98
89
  document.querySelector('#deviceWebApiControl').checked = pluginConfig[0].devices[0].webApiControl || false;
99
90
 
100
- //check token length and adapt buttons
101
91
  const tokenLength = (pluginConfig[0].devices[0].xboxWebApiToken).length || 0;
102
- document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
103
92
  const setButtonMode = tokenLength <= 10 ? document.getElementById('startAuthorizationButton').innerText = "Start authorization" : document.getElementById('startAuthorizationButton').innerText = "Check state";
104
93
  const setCheckboxState = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').checked = false : false;
105
- const setCheckboxMode = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').disabled = true : false;
94
+ const setCheckboxMode = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').disabled = true : document.getElementById('deviceWebApiControl').disabled = false;
95
+ const setWebApiControlMode = tokenLength <= 10 ? pluginConfig[0].devices[0].webApiControl = false : false;
96
+ await homebridge.updatePluginConfig(pluginConfig);
97
+ await homebridge.savePluginConfig(pluginConfig);
106
98
 
107
- //watch for changes to the form and update the config
99
+ //watch for changes to the config button
100
+ document.getElementById('configButton').addEventListener('click', async () => {
101
+ const showHideSettings = this.configButtonState ? homebridge.hideSchemaForm() : homebridge.showSchemaForm();
102
+ this.configButtonState = !this.configButtonState;
103
+ });
104
+
105
+ //watch for changes to the form
108
106
  document.getElementById('configForm').addEventListener('input', async () => {
109
107
  pluginConfig[0].devices[this.deviceIndex].host = document.querySelector('#deviceHostInput').value;
110
108
  pluginConfig[0].devices[this.deviceIndex].xboxLiveId = document.querySelector('#deviceLiveId').value;
111
109
  pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = document.querySelector('#deviceTokenInput').value;
112
110
  pluginConfig[0].devices[this.deviceIndex].webApiControl = document.querySelector('#deviceWebApiControl').checked;
113
111
 
114
- const tokenLength = (pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken).length;
115
- if (tokenLength <= 10) {
116
- document.getElementById('startAuthorizationButton').innerText = "Start authorization";
117
- document.getElementById('startAuthorizationButton').removeAttribute('disabled');
118
- }
112
+ const tokenLength1 = (document.querySelector('#deviceTokenInput').value).length || 0;
113
+ const setButtonName = tokenLength1 <= 10 ? document.getElementById('startAuthorizationButton').innerText = "Start authorization" : false;
114
+ const setButtonMode = tokenLength1 <= 10 ? document.getElementById('startAuthorizationButton').removeAttribute('disabled') : false;
119
115
 
120
- await homebridge.savePluginConfig(pluginConfig);
121
116
  await homebridge.updatePluginConfig(pluginConfig);
117
+ await homebridge.savePluginConfig(pluginConfig);
122
118
  });
123
119
 
124
120
  //clear token file
125
121
  document.getElementById('clearTokenButton').addEventListener('click', async () => {
126
- homebridge.showSpinner();
127
-
128
122
  try {
123
+ homebridge.showSpinner();
129
124
  const host = pluginConfig[0].devices[this.deviceIndex].host || '';
130
125
  const payload = {
131
126
  host: host
@@ -133,7 +128,6 @@
133
128
  const response = await homebridge.request('/clearToken', payload);
134
129
 
135
130
  if (response) {
136
- pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = '';
137
131
  document.querySelector('#deviceTokenInput').value = '';
138
132
  document.getElementById('startAuthorizationButton').removeAttribute("disabled");
139
133
  document.getElementById('startAuthorizationButton').innerText = "Start authorization";
@@ -141,25 +135,26 @@
141
135
  document.getElementById('deviceWebApiControl').disabled = true;
142
136
  document.getElementById('info').innerHTML = 'Web Api Token cleared, now You can start new authorization process.';
143
137
  document.getElementById('info').style.color = 'green';
144
- homebridge.toast.info('Web Api Token cleared!', 'Info!');
138
+ pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = '';
139
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
145
140
 
146
- await homebridge.savePluginConfig(pluginConfig);
147
141
  await homebridge.updatePluginConfig(pluginConfig);
142
+ await homebridge.savePluginConfig(pluginConfig);
148
143
  };
149
144
 
150
145
  homebridge.hideSpinner();
151
146
  } catch (e) {
152
- homebridge.toast.error('Web Api Token clear failed.', 'Error!');
147
+ homebridge.toast.error(`Clear Web Api Token error: ${JSON.stringify(e, null, 2)}`);
153
148
  homebridge.hideSpinner();
154
149
  };
155
150
  });
156
151
 
157
152
  //watch for click on the Start authorization button
158
153
  document.getElementById('startAuthorizationButton').addEventListener('click', async () => {
159
- await homebridge.updatePluginConfig(pluginConfig);
160
- homebridge.showSpinner();
161
-
162
154
  try {
155
+ homebridge.showSpinner();
156
+ await homebridge.updatePluginConfig(pluginConfig)
157
+
163
158
  const host = pluginConfig[0].devices[this.deviceIndex].host || '';
164
159
  this.clientId = pluginConfig[0].devices[this.deviceIndex].clientId || '5e5ead27-ed60-482d-b3fc-702b28a97404';
165
160
  this.clientSecret = pluginConfig[0].devices[this.deviceIndex].clientSecret || '';
@@ -192,29 +187,47 @@
192
187
  document.getElementById('startAuthorizationButton').innerText = "Activate console";
193
188
  document.getElementById('deviceWebApiControl').checked = false;
194
189
  document.getElementById('deviceWebApiControl').disabled = true;
195
- await open(info)
190
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
191
+
192
+ // open the oauth2 URI for authentication
193
+ const authWindow = window.open(info, 'Xbox TV');
194
+ document.getElementById('info').innerHTML = `1: ${JSON.stringify(authWindow, null, 2)}`;
195
+ authWindow.addEventListener('load', async () => {
196
+ const locationHash = this.location.hash.substring(1);
197
+ document.getElementById('info').innerHTML = `2: ${JSON.stringify(locationHash, null, 2)}`;
198
+ const urlParams = await homebridge.request('/queryString', locationHash);
199
+ document.getElementById('info').innerHTML = `3: ${JSON.stringify(urlParams, null, 2)}`;
200
+ const code = urlParams.code;
201
+ if (code !== null) {
202
+ document.querySelector('#deviceTokenInput').value = code;
203
+ }
204
+ });
196
205
  break;
197
206
  case 2:
198
207
  document.getElementById('info').innerHTML = 'Activation done, now enable *Web Api Control*, restart plugin and have fun!!!';
199
208
  document.getElementById('info').style.color = 'green';
200
209
  document.getElementById('startAuthorizationButton').innerText = "Check state";
201
210
  document.getElementById('deviceWebApiControl').disabled = false;
211
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
202
212
 
203
- await homebridge.savePluginConfig(pluginConfig);
204
213
  await homebridge.updatePluginConfig(pluginConfig)
214
+ await homebridge.savePluginConfig(pluginConfig);
205
215
  break;
206
216
  case 3:
207
- document.getElementById('info').innerHTML = 'Authorization or Token save error!!!.';
217
+ document.getElementById('info').innerHTML = `Authorization error: ${JSON.stringify(info, null, 2)}`;
208
218
  document.getElementById('info').style.color = 'red';
209
219
  document.getElementById('startAuthorizationButton').innerText = "Start authorization";
210
220
  document.getElementById('deviceWebApiControl').checked = false;
211
221
  document.getElementById('deviceWebApiControl').disabled = true;
212
- homebridge.toast.error(info, 'Error!');
222
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
223
+
224
+ await homebridge.updatePluginConfig(pluginConfig)
225
+ await homebridge.savePluginConfig(pluginConfig);
213
226
  break;
214
227
  };
215
228
 
216
229
  } catch (e) {
217
- homebridge.toast.error('Failed to get data try again.', 'Error!');
230
+ homebridge.toast.error(`Data error: ${JSON.stringify(e, null, 2)}`);
218
231
  } finally {
219
232
  homebridge.hideSpinner();
220
233
  };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const { HomebridgePluginUiServer, RequestError } = require('@homebridge/plugin-ui-utils');
4
+ const QueryString = require('querystring');
4
5
  const Authentication = require('../src/webApi/authentication.js')
5
6
  const fs = require('fs');
6
7
  const fsPromises = fs.promises;
@@ -14,22 +15,21 @@ class PluginUiServer extends HomebridgePluginUiServer {
14
15
  // clear token
15
16
  this.onRequest('/clearToken', this.clearToken.bind(this));
16
17
 
18
+ // clear token
19
+ this.onRequest('/queryString', this.queryString.bind(this));
20
+
17
21
  // start console authorization
18
- this.onRequest('/startAuthorization', this.getWebApiToken.bind(this));
22
+ this.onRequest('/startAuthorization', this.startAuthorization.bind(this));
19
23
 
20
24
  // this MUST be called when you are ready to accept requests
21
25
  this.ready();
22
26
  };
23
27
 
24
28
  async clearToken(payload) {
25
- const host = payload.host;
26
- const authTokenFile = `${this.homebridgeStoragePath}/xboxTv/authToken_${host.split('.').join('')}`;
27
-
28
29
  try {
29
- if (fs.existsSync(authTokenFile)) {
30
- await fsPromises.writeFile(authTokenFile, JSON.stringify({}));
31
- };
32
-
30
+ const host = payload.host;
31
+ const authTokenFile = `${this.homebridgeStoragePath}/xboxTv/authToken_${host.split('.').join('')}`;
32
+ await fsPromises.writeFile(authTokenFile, JSON.stringify({}));
33
33
  return true;
34
34
  } catch (e) {
35
35
  throw new RequestError('Clear token file failed.', {
@@ -38,21 +38,32 @@ class PluginUiServer extends HomebridgePluginUiServer {
38
38
  };
39
39
  };
40
40
 
41
- async getWebApiToken(payload) {
41
+ async queryString(locationHash) {
42
+ try {
43
+ const urlParams = QueryString.parse(locationHash);
44
+ return urlParams;
45
+ } catch (e) {
46
+ throw new RequestError('Query string failed.', {
47
+ message: e.message
48
+ });
49
+ };
50
+ };
51
+
52
+ async startAuthorization(payload) {
42
53
 
43
54
  try {
44
55
  const host = payload.host;
45
56
  const webApiToken = payload.webApiToken;
46
57
  const authTokenFile = `${this.homebridgeStoragePath}/xboxTv/authToken_${host.split('.').join('')}`;
47
58
 
48
- const authenticationConfig = {
59
+ const authConfig = {
49
60
  clientId: payload.clientId,
50
61
  clientSecret: payload.clientSecret,
51
62
  userToken: payload.userToken,
52
63
  uhs: payload.uhs,
53
64
  tokensFile: authTokenFile
54
65
  }
55
- const authentication = new Authentication(authenticationConfig);
66
+ const authentication = new Authentication(authConfig);
56
67
 
57
68
  try {
58
69
  await authentication.isAuthenticated();
@@ -70,15 +81,22 @@ class PluginUiServer extends HomebridgePluginUiServer {
70
81
  };
71
82
  } catch (error) {
72
83
  this.data = {
73
- info: 'Authorization or save token file error.',
84
+ info: error,
74
85
  status: 3
75
86
  };
76
87
  };
77
88
  } else {
78
- const oauth2URI = await authentication.generateAuthorizationUrl();
79
- this.data = {
80
- info: oauth2URI,
81
- status: 1
89
+ try {
90
+ const oauth2URI = await authentication.generateAuthorizationUrl();
91
+ this.data = {
92
+ info: oauth2URI,
93
+ status: 1
94
+ };
95
+ } catch (error) {
96
+ this.data = {
97
+ info: error,
98
+ status: 3
99
+ };
82
100
  };
83
101
  };
84
102
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Xbox TV",
3
3
  "name": "homebridge-xbox-tv",
4
- "version": "2.8.2-beta.4",
4
+ "version": "2.8.2-beta.41",
5
5
  "description": "Homebridge plugin (https://github.com/homebridge/homebridge) to control Xbox game consoles.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -242,7 +242,7 @@ class AUTHENTICATION {
242
242
  "grant_type": 'authorization_code',
243
243
  "scope": CONSTANS.Scopes.join(' '),
244
244
  "code": webApiToken,
245
- "redirect_uri": 'http://localhost/auth/callback'
245
+ "redirect_uri": 'http://localhost:8080/auth/callback'
246
246
  }
247
247
 
248
248
  if (this.clientSecret !== '') {
@@ -272,7 +272,7 @@ class AUTHENTICATION {
272
272
  "response_type": 'code',
273
273
  "approval_prompt": 'auto',
274
274
  "scope": CONSTANS.Scopes.join(' '),
275
- "redirect_uri": 'http://localhost/auth/callback'
275
+ "redirect_uri": 'http://localhost:8080/auth/callback'
276
276
  }
277
277
  const params = QueryString.stringify(paramsObject);
278
278
  const oauth2URI = `https://login.live.com/oauth20_authorize.srf?${params}`;