homebridge-xbox-tv 2.8.2-beta.9 → 2.8.3

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,34 @@
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
+ <div id="authorizationManager" style="display: none;" class="card card-body">
5
+ <form id="configForm">
6
+ <div class="text-center">
7
+ <label id="deviceNameInput" style="font-size: 23px">Xbox</label><br>
8
+ </div>
9
+ <div class="text-center">
27
10
  <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>
11
+ <label id="info" style="text-align: center; font-size: 17px; color: white;">Authorization
12
+ Manager</label><br><br>
31
13
  </div>
32
- </form>
33
- </div>
34
- </body>
14
+ </div>
15
+ <div class="form-group">
16
+ <label for="deviceHostInput">Host</label>
17
+ <input type="text" class="form-control" id="deviceHostInput"><br>
18
+ <label for="deviceLiveId">Xbox Live ID</label>
19
+ <input type="text" class="form-control" id="deviceLiveId"><br>
20
+ <label for="deviceTokenInput">Web Api Token</label>
21
+ <input type="text" class="form-control" id="deviceTokenInput"><br>
22
+ <label for="deviceWebApiControl">Web Api Control</label>
23
+ <input type="checkbox" class="form-control" id="deviceWebApiControl">
24
+ </div>
25
+ <div class="text-center">
26
+ <button id="startAuthorizationButton" type="button" class="btn btn-primary">Start authorization</button>
27
+ <button id="clearTokenButton" type="button" class="btn btn-primary">Clear Web Api Token</button>
28
+ <button id="configButton" type="button" class="btn btn-primary"><i class="fas fa-gear"></i></button>
29
+ </div>
30
+ </form>
31
+ </div>
35
32
 
36
33
  <script>
37
34
  (async () => {
@@ -44,7 +41,7 @@
44
41
  const devices = { 'devices': [] };
45
42
  pluginConfig.push(devices);
46
43
  await homebridge.updatePluginConfig(pluginConfig);
47
- await homebridge.showSchemaForm();
44
+ homebridge.showSchemaForm();
48
45
  return;
49
46
  }
50
47
 
@@ -53,14 +50,13 @@
53
50
 
54
51
  //get devices count
55
52
  const devicesCount = pluginConfig[0].devices.length;
56
- this.deviceIndex = 0;
57
53
 
58
54
  //get the intial from the config and add it to the form
59
55
  for (let i = 0; i < devicesCount; i++) {
60
56
  //create consoles buttons
61
57
  const button = document.createElement("button");
62
58
  button.setAttribute("type", "button");
63
- button.setAttribute("id", "button" + i);
59
+ button.setAttribute("id", `button${i}`);
64
60
  button.setAttribute("class", "btn btn-secondary");
65
61
  button.innerText = pluginConfig[0].devices[i].name || 'Not set';
66
62
  button.style.width = '160px';
@@ -68,29 +64,28 @@
68
64
  document.body.appendChild(button);
69
65
 
70
66
  //get actuall value on console select
71
- document.getElementById('button' + i).addEventListener('click', async () => {
67
+ document.getElementById(`button${i}`).addEventListener('click', async () => {
72
68
  document.querySelector('#deviceNameInput').innerHTML = pluginConfig[0].devices[i].name || '';
73
69
  document.querySelector('#deviceHostInput').value = pluginConfig[0].devices[i].host || '';
74
70
  document.querySelector('#deviceLiveId').value = pluginConfig[0].devices[i].xboxLiveId || '';
75
71
  document.querySelector('#deviceTokenInput').value = pluginConfig[0].devices[i].xboxWebApiToken || '';
76
72
  document.querySelector('#deviceWebApiControl').checked = pluginConfig[0].devices[i].webApiControl || false;
73
+
74
+ const tokenLength = (pluginConfig[0].devices[i].xboxWebApiToken).length || 0;
75
+ const setButtonName = tokenLength <= 10 ? document.getElementById('startAuthorizationButton').innerText = "Start authorization" : document.getElementById('startAuthorizationButton').innerText = "Check state";
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
+ const click = i === devicesCount - 1 ? document.getElementById(`button0`).click() : false;
82
+ const update = i === devicesCount - 1 ? await homebridge.updatePluginConfig(pluginConfig) : false;
83
+ const save = i === devicesCount - 1 ? await homebridge.savePluginConfig(pluginConfig) : false;
79
84
  };
80
85
 
81
- //set page to the erste console after open
82
- document.getElementById('button0').trigger('click');
83
-
84
- //check token length and set buttons name and checkbock state after open
85
- const tokenLength = (document.querySelector('#deviceTokenInput').value).length || 0;
86
- const setButtonMode = tokenLength <= 10 ? document.getElementById('startAuthorizationButton').innerText = "Start authorization" : document.getElementById('startAuthorizationButton').innerText = "Check state";
87
- const setCheckboxState = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').checked = false : false;
88
- const setCheckboxMode = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').disabled = true : false;
89
-
90
86
  //watch for changes to the config button
91
87
  document.getElementById('configButton').addEventListener('click', async () => {
92
- const configButton = document.getElementById('configButton');
93
- const showHideSettings = this.configButtonState ? await homebridge.hideSchemaForm() : await homebridge.showSchemaForm();
88
+ const showHideSettings = this.configButtonState ? homebridge.hideSchemaForm() : homebridge.showSchemaForm();
94
89
  this.configButtonState = !this.configButtonState;
95
90
  });
96
91
 
@@ -105,15 +100,14 @@
105
100
  const setButtonName = tokenLength1 <= 10 ? document.getElementById('startAuthorizationButton').innerText = "Start authorization" : false;
106
101
  const setButtonMode = tokenLength1 <= 10 ? document.getElementById('startAuthorizationButton').removeAttribute('disabled') : false;
107
102
 
108
- await homebridge.savePluginConfig(pluginConfig);
109
103
  await homebridge.updatePluginConfig(pluginConfig);
104
+ await homebridge.savePluginConfig(pluginConfig);
110
105
  });
111
106
 
112
107
  //clear token file
113
108
  document.getElementById('clearTokenButton').addEventListener('click', async () => {
114
- homebridge.showSpinner();
115
-
116
109
  try {
110
+ homebridge.showSpinner();
117
111
  const host = pluginConfig[0].devices[this.deviceIndex].host || '';
118
112
  const payload = {
119
113
  host: host
@@ -121,7 +115,6 @@
121
115
  const response = await homebridge.request('/clearToken', payload);
122
116
 
123
117
  if (response) {
124
- pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = '';
125
118
  document.querySelector('#deviceTokenInput').value = '';
126
119
  document.getElementById('startAuthorizationButton').removeAttribute("disabled");
127
120
  document.getElementById('startAuthorizationButton').innerText = "Start authorization";
@@ -129,25 +122,26 @@
129
122
  document.getElementById('deviceWebApiControl').disabled = true;
130
123
  document.getElementById('info').innerHTML = 'Web Api Token cleared, now You can start new authorization process.';
131
124
  document.getElementById('info').style.color = 'green';
132
- homebridge.toast.info('Web Api Token cleared!', 'Info!');
125
+ pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = '';
126
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
133
127
 
134
- await homebridge.savePluginConfig(pluginConfig);
135
128
  await homebridge.updatePluginConfig(pluginConfig);
129
+ await homebridge.savePluginConfig(pluginConfig);
136
130
  };
137
131
 
138
132
  homebridge.hideSpinner();
139
133
  } catch (e) {
140
- homebridge.toast.error('Web Api Token clear failed.', 'Error!');
134
+ homebridge.toast.error(`Clear Web Api Token error: ${JSON.stringify(e, null, 2)}`);
141
135
  homebridge.hideSpinner();
142
136
  };
143
137
  });
144
138
 
145
139
  //watch for click on the Start authorization button
146
140
  document.getElementById('startAuthorizationButton').addEventListener('click', async () => {
147
- await homebridge.updatePluginConfig(pluginConfig);
148
- homebridge.showSpinner();
149
-
150
141
  try {
142
+ homebridge.showSpinner();
143
+ await homebridge.updatePluginConfig(pluginConfig)
144
+
151
145
  const host = pluginConfig[0].devices[this.deviceIndex].host || '';
152
146
  this.clientId = pluginConfig[0].devices[this.deviceIndex].clientId || '5e5ead27-ed60-482d-b3fc-702b28a97404';
153
147
  this.clientSecret = pluginConfig[0].devices[this.deviceIndex].clientSecret || '';
@@ -180,29 +174,46 @@
180
174
  document.getElementById('startAuthorizationButton').innerText = "Activate console";
181
175
  document.getElementById('deviceWebApiControl').checked = false;
182
176
  document.getElementById('deviceWebApiControl').disabled = true;
183
- await open(info)
177
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
178
+
179
+ // open the oauth2 URI for authentication
180
+ const authWindow = open(info);
181
+ authWindow.addEventListener('load', async () => {
182
+ document.getElementById('info').innerHTML = `2: ${JSON.stringify(authWindow, null, 2)}`;
183
+ const locationHash = this.location.hash.substring(1);
184
+ const urlParams = await homebridge.request('/queryString', locationHash);
185
+ const code = urlParams.code;
186
+ if (code != null) {
187
+ document.querySelector('#deviceTokenInput').value = code;
188
+ }
189
+ });
184
190
  break;
185
191
  case 2:
186
- document.getElementById('info').innerHTML = 'Activation done, now enable *Web Api Control*, restart plugin and have fun!!!';
192
+ document.getElementById('info').innerHTML = 'Activation successfull, now restart plugin and have fun!!!';
187
193
  document.getElementById('info').style.color = 'green';
188
194
  document.getElementById('startAuthorizationButton').innerText = "Check state";
189
195
  document.getElementById('deviceWebApiControl').disabled = false;
196
+ document.getElementById('deviceWebApiControl').checked = true;
197
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = true;
190
198
 
191
- await homebridge.savePluginConfig(pluginConfig);
192
199
  await homebridge.updatePluginConfig(pluginConfig)
200
+ await homebridge.savePluginConfig(pluginConfig);
193
201
  break;
194
202
  case 3:
195
- document.getElementById('info').innerHTML = 'Authorization or Token save error!!!.';
203
+ document.getElementById('info').innerHTML = `Authorization error: ${JSON.stringify(info, null, 2)}`;
196
204
  document.getElementById('info').style.color = 'red';
197
205
  document.getElementById('startAuthorizationButton').innerText = "Start authorization";
198
206
  document.getElementById('deviceWebApiControl').checked = false;
199
207
  document.getElementById('deviceWebApiControl').disabled = true;
200
- homebridge.toast.error(info, 'Error!');
208
+ pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
209
+
210
+ await homebridge.updatePluginConfig(pluginConfig)
211
+ await homebridge.savePluginConfig(pluginConfig);
201
212
  break;
202
213
  };
203
214
 
204
215
  } catch (e) {
205
- homebridge.toast.error('Failed to get data try again.', 'Error!');
216
+ homebridge.toast.error(`Data error: ${JSON.stringify(e, null, 2)}`);
206
217
  } finally {
207
218
  homebridge.hideSpinner();
208
219
  };
@@ -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.9",
4
+ "version": "2.8.3",
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}`;