Skip to content

Commit

Permalink
release 2.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz914 committed Feb 20, 2023
1 parent c536ccb commit 6ae6552
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 97 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### NOTE!!!
## After update to 2.x.x the plugin settings (xboxLiveId) need to be updated.

## [2.8.0] - (20.02.2023)
## Changes
- fix load plugin gui on first start after install
- authorization manager updated
- added possibility to set IP Address and Xbox Live ID from Authorization Manager.
- added possibility to enable Web Api Control from Authorization Manager after authorization successfull done.
- cleanup

## [2.7.0] - (13.02.2023)
## Changes
- standarize function of display type and volume control, now volume control -1 None/Disabled, 0 Slider, 1 Fan, please see in readme
Expand Down
213 changes: 121 additions & 92 deletions homebridge-ui/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@
<img src="homebridge-xbox-tv.png" style="text-align: center;" height="150" />
</div>

<div id="authorizationRequired" style="display:none;" class="card card-body">
<div id="authorizationManager" style="display: none;" class="card card-body">
<form id="configForm">
<div class="text-center">
<label id="info" style="font-size: 17px">Authorization Manager</label><br><br>
<label id="deviceNameInput" style="font-size: 23px">Xbox</label><br>
</div>
<div class="text-center">
<div class="text-center">
<label id="info" style="text-align: center; font-size: 17px; color: white;">Authorization Manager</label><br><br>
</div>
</div>
<div class="form-group">
<label for="deviceNameInput">Name</label>
<input type="text" class="form-control" id="deviceNameInput" readonly><br>
<label for="deviceHostInput">Host</label>
<input type="text" class="form-control" id="deviceHostInput" readonly><br>
<input type="text" class="form-control" id="deviceHostInput"><br>
<label for="deviceLiveId">Xbox Live ID</label>
<input type="text" class="form-control" id="deviceLiveId"><br>
<label for="deviceTokenInput">Web Api Token</label>
<input type="text" class="form-control" id="deviceTokenInput">
<input type="text" class="form-control" id="deviceTokenInput"><br>
<label for="deviceWebApiControl">Web Api Control</label>
<input type="checkbox" class="form-control" id="deviceWebApiControl">
</div>
<div class="text-center">
<button id="startAuthorizationButton" type="button" class="btn btn-primary">Start authorization</button>
Expand All @@ -26,83 +33,50 @@

<script>
(async () => {
// get the initial config - this is an array potentially containing multiple config blocks
//get the plugin config array
const pluginConfig = await homebridge.getPluginConfig();
const devicesCount = pluginConfig[0].devices.length;
const objKeys = Object.keys(pluginConfig[0]);
const devicesExist = objKeys.includes('devices');

if (!devicesCount) {
pluginConfig.push({});
if (!devicesExist) {
const devices = { 'devices': [] };
pluginConfig.push(devices);
await homebridge.updatePluginConfig(pluginConfig);
await homebridge.showSchemaForm();
} else {
// get the intial from the config and add it to the form
document.getElementById('authorizationRequired').style.display = 'block';
for (let i = 0; i < devicesCount; i++) {
//create consoles buttons
const button = document.createElement("button");
button.setAttribute("type", "button");
button.setAttribute("id", "button" + i);
button.setAttribute("class", "btn btn-secondary");
button.innerText = pluginConfig[0].devices[i].name;
button.style.width = '160px';
button.style.height = '34x';
document.body.appendChild(button);

//get actuall value on console select
document.getElementById('button' + i).addEventListener('click', async () => {
this.name = pluginConfig[0].devices[i].name;
this.host = pluginConfig[0].devices[i].host;
this.clientId = pluginConfig[0].devices[i].clientId || '5e5ead27-ed60-482d-b3fc-702b28a97404'
this.clientSecret = pluginConfig[0].devices[i].clientSecret || '';
this.userToken = pluginConfig[0].devices[i].userToken || '';
this.uhs = pluginConfig[0].devices[i].uhs || '';
this.xboxWebApiToken = pluginConfig[0].devices[i].xboxWebApiToken || '';
document.querySelector('#deviceNameInput').value = this.name;
document.querySelector('#deviceHostInput').value = this.host;
document.querySelector('#deviceTokenInput').value = this.xboxWebApiToken;
this.deviceIndex = i;
});
};
return;
}

//load on first run
this.name = pluginConfig[0].devices[0].name;
this.host = pluginConfig[0].devices[0].host;
this.xboxWebApiToken = pluginConfig[0].devices[0].xboxWebApiToken || '';
this.clientId = pluginConfig[0].devices[0].clientId || '5e5ead27-ed60-482d-b3fc-702b28a97404'
this.clientSecret = pluginConfig[0].devices[0].clientSecret || '';
this.userToken = pluginConfig[0].devices[0].userToken || '';
this.uhs = pluginConfig[0].devices[0].uhs || '';
document.querySelector('#deviceNameInput').value = this.name;
document.querySelector('#deviceHostInput').value = this.host;
document.querySelector('#deviceTokenInput').value = this.xboxWebApiToken;
const tokenLength = this.xboxWebApiToken.length || 0;
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
if (tokenLength <= 10) {
document.getElementById('startAuthorizationButton').innerText = "Start authorization";
} else {
document.getElementById('startAuthorizationButton').innerText = "Check state";
}
};

// watch for changes to the form and update the config
document.getElementById('configForm').addEventListener('input', async () => {
pluginConfig[0].devices[this.deviceIndex].name = document.querySelector('#deviceNameInput').value;
pluginConfig[0].devices[this.deviceIndex].host = document.querySelector('#deviceHostInput').value;
pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = document.querySelector('#deviceTokenInput').value;
//load form
document.getElementById('authorizationManager').style.display = 'block';

const tokenLength = (document.querySelector('#deviceTokenInput').value).length;
if (tokenLength <= 10) {
document.getElementById('startAuthorizationButton').innerText = "Start authorization";
document.getElementById('startAuthorizationButton').removeAttribute('disabled');
} else {
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
}
//get devices count
const devicesCount = pluginConfig[0].devices.length;

await homebridge.updatePluginConfig(pluginConfig);
await homebridge.savePluginConfig(pluginConfig);
});
// get the intial from the config and add it to the form
this.deviceIndex = 0;
for (let i = 0; i < devicesCount; i++) {
//create consoles buttons
const button = document.createElement("button");
button.setAttribute("type", "button");
button.setAttribute("id", "button" + i);
button.setAttribute("class", "btn btn-secondary");
button.innerText = pluginConfig[0].devices[i].name || 'Not set';
button.style.width = '160px';
button.style.height = '34x';
document.body.appendChild(button);

//get actuall value on console select
document.getElementById('button' + i).addEventListener('click', async () => {
document.querySelector('#deviceNameInput').innerHTML = pluginConfig[0].devices[i].name || '';
document.querySelector('#deviceHostInput').value = pluginConfig[0].devices[i].host || '';
document.querySelector('#deviceLiveId').value = pluginConfig[0].devices[i].xboxLiveId || '';
document.querySelector('#deviceTokenInput').value = pluginConfig[0].devices[i].xboxWebApiToken || '';
document.querySelector('#deviceWebApiControl').checked = pluginConfig[0].devices[i].webApiControl || false;
this.deviceIndex = i;
});
};

//show hide config
//add button show/hide config
document.getElementById('configButton').addEventListener('click', async () => {
const configButton = document.getElementById('configButton');
if (this.configButtonState) {
Expand All @@ -116,26 +90,63 @@
};
});

// clear token file
//load values on first run
document.querySelector('#deviceNameInput').innerHTML = pluginConfig[0].devices[0].name || '';
document.querySelector('#deviceHostInput').value = pluginConfig[0].devices[0].host || '';
document.querySelector('#deviceLiveId').value = pluginConfig[0].devices[0].xboxLiveId || '';
document.querySelector('#deviceTokenInput').value = pluginConfig[0].devices[0].xboxWebApiToken || '';
document.querySelector('#deviceWebApiControl').checked = pluginConfig[0].devices[0].webApiControl || false;

//check token length and adapt buttons
const tokenLength = (pluginConfig[0].devices[0].xboxWebApiToken).length || 0;
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
const setButtonMode = tokenLength <= 10 ? document.getElementById('startAuthorizationButton').innerText = "Start authorization" : document.getElementById('startAuthorizationButton').innerText = "Check state";
const setCheckboxState = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').checked = false : false;
const setCheckboxMode = tokenLength <= 10 ? document.getElementById('deviceWebApiControl').disabled = true : false;

//watch for changes to the form and update the config
document.getElementById('configForm').addEventListener('input', async () => {
pluginConfig[0].devices[this.deviceIndex].host = document.querySelector('#deviceHostInput').value;
pluginConfig[0].devices[this.deviceIndex].xboxLiveId = document.querySelector('#deviceLiveId').value;
pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = document.querySelector('#deviceTokenInput').value;
pluginConfig[0].devices[this.deviceIndex].webApiControl = document.querySelector('#deviceWebApiControl').checked;

const tokenLength = (pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken).length;
if (tokenLength <= 10) {
document.getElementById('startAuthorizationButton').innerText = "Start authorization";
document.getElementById('startAuthorizationButton').removeAttribute('disabled');
} else {
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
}

await homebridge.savePluginConfig(pluginConfig);
await homebridge.updatePluginConfig(pluginConfig);
});

//clear token file
document.getElementById('clearTokenButton').addEventListener('click', async () => {
homebridge.showSpinner();

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

if (response) {
document.getElementById('info').innerHTML = 'Web Api Token cleared, now You can start new authorization process.';
pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = '';
document.querySelector('#deviceTokenInput').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 = 'Web Api Token cleared, now You can start new authorization process.';
document.getElementById('info').style.color = 'green';
homebridge.toast.info('Web Api Token cleared!', 'Info!');

pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken = '';
document.querySelector('#deviceTokenInput').value = '';
await homebridge.updatePluginConfig(pluginConfig);
await homebridge.savePluginConfig(pluginConfig);
await homebridge.updatePluginConfig(pluginConfig);
};

homebridge.hideSpinner();
Expand All @@ -145,19 +156,25 @@
};
});

// watch for click events on the startAuthorizationButton
//watch for click on the Start authorization button
document.getElementById('startAuthorizationButton').addEventListener('click', async () => {
await homebridge.updatePluginConfig(pluginConfig);
homebridge.showSpinner();

try {
const webApiToken = document.querySelector('#deviceTokenInput').value;
const host = pluginConfig[0].devices[this.deviceIndex].host || '';
this.clientId = pluginConfig[0].devices[this.deviceIndex].clientId || '5e5ead27-ed60-482d-b3fc-702b28a97404';
this.clientSecret = pluginConfig[0].devices[this.deviceIndex].clientSecret || '';
this.userToken = pluginConfig[0].devices[this.deviceIndex].userToken || '';
this.uhs = pluginConfig[0].devices[this.deviceIndex].uhs || '';
this.xboxWebApiToken = pluginConfig[0].devices[this.deviceIndex].xboxWebApiToken || '';
const payload = {
host: this.host,
host: host,
clientId: this.clientId,
clientSecret: this.clientSecret,
userToken: this.userToken,
uhs: this.uhs,
webApiToken: webApiToken,
webApiToken: this.xboxWebApiToken
};

const response = await homebridge.request('/startAuthorization', payload);
Expand All @@ -166,44 +183,56 @@

switch (status) {
case 0:
document.getElementById('info').innerHTML = 'Console authorized and activated. To start new process, clear the Web API Token first.';
document.getElementById('info').innerHTML = 'Console authorized and activated. To start new process please clear Web API Token first.';
document.getElementById('info').style.color = 'green';
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
document.getElementById('authorizeConsoleButton').innerText = "Authorize console";
homebridge.toast.info(info, 'Info!');
document.getElementById('deviceWebApiControl').disabled = false;
break;
case 1:
document.getElementById('info').innerHTML = 'Authorization process started, now press the *Authorize Console*.';
document.getElementById('info').style.color = 'yellow';
document.getElementById('startAuthorizationButton').setAttribute('disabled', 'disabled');
document.getElementById('authorizeConsoleButton').removeAttribute('disabled');
document.getElementById('authorizeConsoleButton').innerText = "Authorize console";
homebridge.toast.info('Authorization process started!', 'Info!');
document.getElementById('deviceWebApiControl').checked = false;
document.getElementById('deviceWebApiControl').disabled = true;
this.authorizeConsole = false;
document.getElementById('authorizeConsoleButton').addEventListener('click', async () => {
if (!this.authorizeConsole) {
document.getElementById('info').innerHTML = 'Now go to the pop-up window, login to Your Xbox Live account and accept permission for Xbox TV. Next copy the part after *?code=* from the response URI to the *Web Api Token* and press *Activate Console*.';
document.getElementById('info').innerHTML = 'Now go to the pop-up window, login to Your Xbox Live account and allow this plugin. Next from the response URI copy the part after *?code=* and paste it to the *Web Api Token*, next press *Activate Console*.';
document.getElementById('info').style.color = 'yellow';
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
document.getElementById('startAuthorizationButton').removeAttribute('disabled');
document.getElementById('startAuthorizationButton').innerText = "Activate console";
homebridge.toast.info('Authorization page requested.', 'Info!');
document.getElementById('deviceWebApiControl').checked = false;
document.getElementById('deviceWebApiControl').disabled = true;
await open(info)
this.authorizeConsole = true;
};
});
break;
case 2:
document.getElementById('info').innerHTML = 'Activation console done, now enable *Web Api* in plugin config, save, restart and have fun!!!';
document.getElementById('info').innerHTML = 'Activation done, now enable *Web Api Control*, restart plugin and have fun!!!';
document.getElementById('info').style.color = 'green';
document.getElementById('startAuthorizationButton').setAttribute('disabled', 'disabled');
document.getElementById('startAuthorizationButton').innerText = "Start authorization";
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
document.getElementById('authorizeConsoleButton').innerText = "Authorize console";
homebridge.toast.success(info, 'Success!');
document.getElementById('deviceWebApiControl').disabled = false;

await homebridge.savePluginConfig(pluginConfig);
await homebridge.updatePluginConfig(pluginConfig)
break;
case 3:
document.getElementById('info').innerHTML = 'Authorization or Token save error!!!.';
document.getElementById('info').style.color = 'red';
startAuthorizationButton.removeAttribute('disabled');
startAuthorizationButton.innerText = "Start authorization";
document.getElementById('authorizeConsoleButton').setAttribute('disabled', 'disabled');
document.getElementById('authorizeConsoleButton').innerText = "Authorize console";
document.getElementById('deviceWebApiControl').checked = false;
document.getElementById('deviceWebApiControl').disabled = true;
homebridge.toast.error(info, 'Error!');
break;
};
Expand Down
5 changes: 1 addition & 4 deletions homebridge-ui/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use strict";

const {
HomebridgePluginUiServer,
RequestError
} = require('@homebridge/plugin-ui-utils');
const { HomebridgePluginUiServer, RequestError } = require('@homebridge/plugin-ui-utils');
const Authentication = require('../src/webApi/authentication.js')
const fs = require('fs');
const fsPromises = fs.promises;
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": "2.7.10",
"version": "2.8.0",
"description": "Homebridge plugin (https://github.com/homebridge/homebridge) to control Xbox game consoles.",
"license": "MIT",
"author": "grzegorz914",
Expand Down

0 comments on commit 6ae6552

Please sign in to comment.