homebridge-bondbridge
Version:
Catered shell script to integrate BondBridge units by Bond
197 lines (179 loc) • 8.01 kB
HTML
<link rel="stylesheet" href="css/style.css">
<div class="card card-body">
<img src="images/BondBridge.png" alt="Bond Bridge" width="150px" class="center-it cmd4Logo">
<h5 class="text-center"><b style="font-weight: 600;">Bond Bridge Configuration Creator and Checker</b></h5>
<h6 class="text-center">This script will create and augment the MyPlace configuration file for Bond Bridge devices</h6>
<div class="text-center">
<button id="ConfigCreatorButton" type="button" class="btn btn-blue">Create Configuration</button>
<button id="checkInstallationButton" type="submit" class="btn btn-green">Check Configuration</button>
</div>
</div>
<div id="advErrorModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Error:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body"
<p>Modal body text goes here.</p>
</div>
</div>
</div>
</div>
<!-- Modules bootstrap is supposedly already there, but I have problems with modal being undefined. I believe this is because bootstrap only allows One Modal and this is the Error Dialog is the second. The solution is to reload them again in the proper order
-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous">var $jq = jQuery.noConflict(true);</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<!-- Main -->
<script type="text/javascript" src="js/main.js?v=0.0.7">
</script>
<script>
(async () => {
// get the initial config - this is an array potentially containing multiple config blocks
const pluginConfig = await homebridge.getPluginConfig();
const configSchema = await homebridge.getPluginConfigSchema();
if (!pluginConfig.length) {
pluginConfig.push({});
}
const configuration = pluginConfig[0];
configuration.devices = configuration.devices || [];
function createForm(configSchema, configuration) {
const configForm = homebridge.createForm(configSchema, configuration);
configForm.onChange(async (changes) => {
await homebridge.updatePluginConfig([changes]);
})
}
// Bond bridge device variables
BBIP = "";
BBtoken = "";
BBdebug = "false";
BBIP2 = "";
BBtoken2 = "";
BBdebug2 = "false";
BBIP3 = "";
BBtoken3 = "";
BBdebug3 = "false";
feedback = "";
// Ceiling Fan settings variables
BBtimerSetup = "false";
var CFsetupOption;
var CFtimerSetup;
BBtiserSetup2 = "false";
var CFsetupOption2;
var CFtimerSetup2;
BBtimerSetup3 = "false";
var CFsetupOption3;
var CFtimerSetup3;
// get the intial from the config and add it to the form
if (pluginConfig.length) {
if (pluginConfig[0].devices.length >= 1) {
BBIP = pluginConfig[0].devices[0].ipAddress;
BBtoken = pluginConfig[0].devices[0].token;
BBdebug = pluginConfig[0].devices[0].debug;
CFsetupOption = pluginConfig[0].devices[0].CFsettings.setupOption;
BBtimerSetup = pluginConfig[0].devices[0].CFsettings.timerSetup;
}
if (pluginConfig[0].devices.length >= 2) {
BBIP2 = pluginConfig[0].devices[1].ipAddress;
BBtoken2 = pluginConfig[0].devices[1].token;
BBdebug2 = pluginConfig[0].devices[1].debug;
CFsetupOption2 = pluginConfig[0].devices[1].CFsettings.setupOption;
BBtimerSetup2 = pluginConfig[0].devices[1].CFsettings.timerSetup;
}
if (pluginConfig[0].devices.length >= 3) {
BBIP3 = pluginConfig[0].devices[2].ipAddress;
BBtoken3 = pluginConfig[0].devices[2].token;
BBdebug3 = pluginConfig[0].devices[2].debug;
CFsetupOption3 = pluginConfig[0].devices[2].CFsettings.setupOption;
BBtimerSetup3 = pluginConfig[0].devices[2].CFsettings.timerSetup;
}
}
// watch for click events on the ConfigCreatorButton
document.querySelector('#ConfigCreatorButton').addEventListener('click', async () => {
if (pluginConfig[0].devices.length === 0) {
homebridge.toast.error(`No device defined yet. Please define at least 1 device and SAVE it.`, 'Error');
return;
}
// validate an ip was provided
if ( BBIP == undefined || !BBIP ) {
// create a error / red toast notification if the required input is not provided.
homebridge.toast.error('An ip address of your Bond Bridge device must be provided.', 'Error');
return;
}
// starting the request, show the loading spinner
homebridge.showSpinner();
// request key paths from the server
if(pluginConfig[0].devices.length === 1) {
if ( CFsetupOption == undefined ) { CFsetupOption = "doNotConfigure"; }
if ( BBtimerSetup ) {
CFtimerSetup = "includeTimers";
} else {
CFtimerSetup = "noTimers";
}
homebridge.toast.info(`This may take up to 1 minute to process!`, 'Info');
homebridge.toast.info(`(1) ${CFsetupOption} ${CFtimerSetup}.`, 'Setup Info');
}
if(pluginConfig[0].devices.length === 2) {
if ( CFsetupOption2 == undefined ) { CFsetupOption2 = "doNotConfigure"; }
if ( BBtimerSetup2 ) {
CFtimerSetup2 = "includeTimers";
} else {
CFtimerSetup2 = "noTimers";
}
homebridge.toast.info(`This may take up to 2 minutes to process!`, 'Info');
homebridge.toast.info(`(1) ${CFsetupOption} ${CFtimerSetup}, (2) ${CFsetupOption2} ${CFtimerSetup2}.`, 'Setup Info');
}
if(pluginConfig[0].devices.length === 3) {
if ( CFsetupOption3 == undefined ) { CFsetupOption3 = "doNotConfigure"; }
if ( BBtimerSetup3 ) {
CFtimerSetup3 = "includeTimers";
} else {
CFtimerSetup3 = "noTimers";
}
homebridge.toast.info(`This may take up to 3 minutes to process!`, 'Info');
homebridge.toast.info(`(1) ${CFsetupOption} ${CFtimerSetup}, (2) ${CFsetupOption2} ${CFtimerSetup2}, (3) ${CFsetupOption3} ${CFtimerSetup3}.`, 'Setup Info');
}
try {
const response = await homebridge.request('/configcreator', {
ip: BBIP,
token: BBtoken,
debug: BBdebug,
CFsetupOption: CFsetupOption,
CFtimerSetup: CFtimerSetup,
ip2: BBIP2,
token2: BBtoken2,
debug2: BBdebug2,
CFsetupOption2: CFsetupOption2,
CFtimerSetup2: CFtimerSetup2,
ip3: BBIP3,
token3: BBtoken3,
debug3: BBdebug3,
CFsetupOption3: CFsetupOption3,
CFtimerSetup3: CFtimerSetup3,
feedback: feedback
});
if(response.feedback.includes("ERROR")) {
// create a red toast notification of the error
homebridge.toast.error(`${response.feedback}`, 'Error');
return;
} else if(response.feedback.includes("DONE")) {
// show a success toast notification
homebridge.toast.success('ConfigCreator completed!', 'Success');
homebridge.toast.success('Click SAVE and RESTART HOMEBRIDGE to apply the changes', 'Config SAVED');
return;
} else {
homebridge.toast.error(`ConfigCreator did not run!`, 'Error');
}
} catch (e) {
homebridge.toast.error(e.error, e.message);
} finally {
// remember to un-hide the spinner
homebridge.hideSpinner();
}
});
createForm(configSchema, configuration);
})();
</script>