Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz914 committed Sep 7, 2024
1 parent 3c98f45 commit 14dbab5
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 217 deletions.
79 changes: 39 additions & 40 deletions homebridge-ui/public/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<div style="text-align: center;">
</divstyle>
<img src="homebridge-xbox-tv.png" alt="Image" height="120" />
</divstyle>
<img src="homebridge-xbox-tv.png" alt="Image" height="120" />
</div><br>

<div id="authorizationManager" style="display: none;" class="card card-body">
<form id="configForm">
<div class="text-center">
<label id="deviceName" style="font-size: 23px">Xbox</label><br>
<label id="info" style="text-align: center; font-size: 17px;">Authorization Manager</label><br><br>
<label id="info" style="text-align: center; font-size: 17px;"></label><br>
<label id="info1" style="text-align: center; font-size: 15px;"></label><br>
</div>
<div class="form-group">
<label for="deviceHost">Host</label>
Expand All @@ -27,7 +28,7 @@
</form>
</div>

<div id="consoles"></div>
<div id="consoleButton"></div>
</div>

<script>
Expand All @@ -53,7 +54,7 @@
button.setAttribute("class", "btn btn-primary");
button.style.textTransform = 'none';
button.innerText = pluginConfig[0].devices[i].name;
document.getElementById("consoles").appendChild(button);
document.getElementById("consoleButton").appendChild(button);

//get actuall value on console select
document.getElementById(`button${i}`).addEventListener('click', async () => {
Expand All @@ -72,6 +73,7 @@
const setCheckboxState = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').checked = false : false;
const setCheckboxMode = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').disabled = true : document.getElementById('deviceWebApiControl').disabled = false;
const setWebApiControlMode = tokenLength <= 10 ? pluginConfig[0].devices[i].webApiControl = false : false;

await homebridge.updatePluginConfig(pluginConfig)
this.deviceIndex = i;
});
Expand Down Expand Up @@ -106,55 +108,50 @@
//clear token file
document.getElementById('clearTokenButton').addEventListener('click', async () => {
homebridge.showSpinner();
document.getElementById('info').innerHTML = 'Start clearing token...';
document.getElementById('info').style.color = 'yellow';

try {
const host = pluginConfig[0].devices[this.deviceIndex].host;
const response = await homebridge.request('/clearToken', { mode: 'clearToken', host: host });
await homebridge.request('/clearToken', { host: host });

const info = response.info;
const status = response.status;
const error = response.error;
//update value
pluginConfig[0].devices[this.deviceIndex].webApiToken = '';
pluginConfig[0].devices[this.deviceIndex].webApiControl = false;

switch (status) {
case 0:
document.getElementById('deviceToken').value = '';
document.getElementById('startAuthorizationButton').removeAttribute("disabled");
document.getElementById('startAuthorizationButton').innerText = "Start authorization";
document.getElementById('deviceWebApiControl').checked = false;
document.getElementById('deviceWebApiControl').disabled = true;
document.getElementById('info').innerHTML = info;
pluginConfig[0].devices[this.deviceIndex].webApiToken = '';
pluginConfig[0].devices[this.deviceIndex].webApiControl = false;
document.getElementById('info').innerHTML = 'Web api token cleared, now You can start new authorization process.';
document.getElementById('info').style.color = 'green';
document.getElementById('deviceToken').value = '';
document.getElementById('startAuthorizationButton').removeAttribute("disabled");
document.getElementById('startAuthorizationButton').innerText = "Start authorization";
document.getElementById('deviceWebApiControl').checked = false;
document.getElementById('deviceWebApiControl').disabled = true;

await homebridge.updatePluginConfig(pluginConfig);
await homebridge.savePluginConfig(pluginConfig);
break;
case 3:
document.getElementById('info').innerHTML = JSON.stringify(error, null, 2);
document.getElementById('info').style.color = 'red';
break;
};
await homebridge.updatePluginConfig(pluginConfig);
await homebridge.savePluginConfig(pluginConfig);

homebridge.hideSpinner();
} catch (e) {
homebridge.toast.error(`Clear Web Api Token error: ${e.message}`);
} catch (error) {
document.getElementById('info').innerHTML = 'Clear web api token error.';
document.getElementById('info').style.color = 'yellow';
document.getElementById('info1').innerHTML = `Error: ${error}`;
document.getElementById('info1').style.color = 'red';
}
});

//watch for click on the Start authorization button
document.getElementById('startAuthorizationButton').addEventListener('click', async () => {
homebridge.showSpinner();
document.getElementById('info').innerHTML = 'Starting authorization...';
document.getElementById('info').style.color = 'yellow';

try {
await homebridge.updatePluginConfig(pluginConfig)
document.getElementById('info').innerHTML = '';

const host = pluginConfig[0].devices[this.deviceIndex].host;
const webApiClientId = pluginConfig[0].devices[this.deviceIndex].webApiClientId;
const webApiClientSecret = pluginConfig[0].devices[this.deviceIndex].webApiClientSecret;
const webApiToken = pluginConfig[0].devices[this.deviceIndex].webApiToken;

const payload = {
mode: 'authorization',
host: host,
webApiClientId: webApiClientId,
webApiClientSecret: webApiClientSecret,
Expand All @@ -163,11 +160,11 @@
const response = await homebridge.request('/startAuthorization', payload);
const info = response.info;
const status = response.status;
const error = response.error;

switch (status) {
case 0:
document.getElementById('info').innerHTML = info;
document.getElementById('info').style.color = 'green';
document.getElementById('startAuthorizationButton').innerText = "Check state";
document.getElementById('deviceWebApiControl').disabled = false;
break;
Expand All @@ -183,14 +180,17 @@
open(info);
clearInterval(timerId);
document.getElementById('info').innerHTML = `Now paste the *code* to the *Web Api Token* and press *Activate Console*.`;
document.getElementById('info').style.color = 'yellow';
} else {
document.getElementById('info').innerHTML = `After ${timeLeft} sec. on the opened page, sign in to your Xbox Live account and give permission for Xbox TV. Next from the response URI copy the part after *?code=* and return to Authorization Manager.`;
timeLeft--;
document.getElementById('info').style.color = 'yellow';
}
}, 1000);
break;
case 2:
document.getElementById('info').innerHTML = info;
document.getElementById('info').style.color = 'green';
document.getElementById('startAuthorizationButton').innerText = "Check state";
document.getElementById('deviceWebApiControl').disabled = false;
document.getElementById('deviceWebApiControl').checked = true;
Expand All @@ -199,15 +199,14 @@
await homebridge.updatePluginConfig(pluginConfig)
await homebridge.savePluginConfig(pluginConfig);
break;
case 3:
document.getElementById('info').innerHTML = `Authorization error: ${JSON.stringify(error, null, 2)}`;
document.getElementById('info').style.color = 'red';
break;
};

homebridge.hideSpinner();
} catch (e) {
homebridge.toast.error(`Data error: ${e.message}`);
} catch (error) {
document.getElementById('info').innerHTML = 'Check Your credentials data and try again.';
document.getElementById('info').style.color = 'yellow';
document.getElementById('info1').innerHTML = `Error: ${error}`;
document.getElementById('info1').style.color = 'red';
} finally {
homebridge.hideSpinner();
};
Expand Down
99 changes: 51 additions & 48 deletions homebridge-ui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PluginUiServer extends HomebridgePluginUiServer {
super();

//clear web api token
this.onRequest('/clearToken', this.startAuthorization.bind(this));
this.onRequest('/clearToken', this.clearToken.bind(this));

//start console authorization
this.onRequest('/startAuthorization', this.startAuthorization.bind(this));
Expand All @@ -17,9 +17,31 @@ class PluginUiServer extends HomebridgePluginUiServer {
this.ready();
};

async startAuthorization(payload) {
async clearToken(payload) {
const host = payload.host;
const tokensFile = `${this.homebridgeStoragePath}/xboxTv/authToken_${host.split('.').join('')}`;

const authConfig = {
webApiClientId: payload.webApiClientId,
webApiClientSecret: payload.webApiClientSecret,
tokensFile: tokensFile
}
const authentication = new Authentication(authConfig);

const mode = payload.mode;
try {
const tokens = {
oauth: {},
user: {},
xsts: {}
};
await authentication.saveData(tokensFile, tokens);
return true;
} catch (error) {
throw new Error(`Clear token error: ${error.message ?? error}.`);
};
};

async startAuthorization(payload) {
const host = payload.host;
const webApiToken = payload.webApiToken;
const tokensFile = `${this.homebridgeStoragePath}/xboxTv/authToken_${host.split('.').join('')}`;
Expand All @@ -30,58 +52,39 @@ class PluginUiServer extends HomebridgePluginUiServer {
tokensFile: tokensFile
}
const authentication = new Authentication(authConfig);

let data = {};
switch (mode) {
case 'clearToken':
try {
await authentication.clearTokens();
data = {
info: 'Web Api Token cleared, now You can start new authorization process.',
status: 0
};
} catch (error) {
data = {
info: 'Clear token error:',
status: 3,
error: error
};

try {
try {
await authentication.checkAuthorization();
data = {
info: 'Console authorized and activated. To start new process please clear Web API Token first.',
status: 0
};
break;
case 'authorization':
try {
await authentication.checkAuthorization();
data = {
info: 'Console authorized and activated. To start new process please clear Web API Token first.',
status: 0
};
} catch (error) {
if (webApiToken) {
try {
await authentication.accessToken(webApiToken);
data = {
info: 'Activation successfull, now restart plugin and have fun!!!',
status: 2
};
} catch (error) {
data = {
info: 'Error',
status: 3,
error: error
};
};
} else {
const oauth2URI = await authentication.generateAuthorizationUrl();
} catch (error) {
if (webApiToken) {
try {
await authentication.accessToken(webApiToken);
data = {
info: oauth2URI,
status: 1
info: 'Activation console successfull, now restart plugin and have fun!!!',
status: 2
};
} catch (error) {
throw new Error(`Activation console error: ${error.message ?? error}.`);
};
} else {
const oauth2URI = await authentication.generateAuthorizationUrl();
data = {
info: oauth2URI,
status: 1
};
};
break;
};
};

return data;
return data;
} catch (error) {
throw new Error(`Authorization manager error: ${error.message ?? error}.`);
};
};
};

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": "3.1.6",
"version": "3.1.7",
"description": "Homebridge plugin to control Xbox game consoles.",
"license": "MIT",
"author": "grzegorz914",
Expand Down
14 changes: 9 additions & 5 deletions src/impulsegenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class ImpulseGenerator extends EventEmitter {
this.timersState = false;
}

start(timers) {
async start(timers) {
if (this.timersState) {
this.stop();
await this.stop();
}

this.timers = [];
Expand All @@ -22,18 +22,22 @@ class ImpulseGenerator extends EventEmitter {

//update state
this.timersState = true;
this.emit('state', true);
this.state();

return true;
}

stop() {
async stop() {
if (this.timersState) {
this.timers.forEach(timer => clearInterval(timer));
}

//update state
this.timers = [];
this.timersState = false;
this.emit('state', false);
this.state();

return true;
}

state() {
Expand Down
Loading

0 comments on commit 14dbab5

Please sign in to comment.