UNPKG

homebridge-myplace

Version:

Exec Plugin bringing Advanatge Air MyPlace system to Homekit

190 lines (171 loc) 7.67 kB
<link rel="stylesheet" href="css/style.css"> <div class="card card-body"> <img src="images/MyPlace.png" alt="MyPlace" width="150px" class="center-it myplaceLogo"> <h5 class="text-center"><b style="font-weight: 600;">Configuration Creator and Checker</b></h5> <div class="text-center"> <button id="ConfigCreatorButton" type="button" class="btn btn-blue">Create Confiuration</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">&times;</span> </button> </div> <div class="modal-body" <p></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 var 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]); }) } AAIP1 = ""; AAname1 = ""; AAport1 = "2025"; AAextraTimers1 = "false"; AAdebug1 = "false"; AAIP2 = ""; AAname2 = ""; AAport2 = "2025"; AAextraTimers2 = "false"; AAdebug2 = "false"; AAIP3 = ""; AAname3 = ""; AAport3 = "2025"; AAextraTimers3 = "false"; AAdebug3 = "false"; feedback = ""; // get the intial from the config and add it to the form if (pluginConfig.length) { if (pluginConfig[0].devices.length >= 1) { AAIP1 = pluginConfig[0].devices[0].ipAddress; AAname1 = pluginConfig[0].devices[0].name; AAport1 = pluginConfig[0].devices[0].port; AAextraTimers1 = pluginConfig[0].devices[0].extraTimers; AAdebug1 = pluginConfig[0].devices[0].debug; } if (pluginConfig[0].devices.length >= 2) { AAIP2 = pluginConfig[0].devices[1].ipAddress; AAname2 = pluginConfig[0].devices[1].name; AAport2 = pluginConfig[0].devices[1].port; AAextraTimers2 = pluginConfig[0].devices[1].extraTimers; AAdebug2 = pluginConfig[0].devices[1].debug; } if (pluginConfig[0].devices.length >= 3) { AAIP3 = pluginConfig[0].devices[2].ipAddress; AAname3 = pluginConfig[0].devices[2].name; AAport3 = pluginConfig[0].devices[2].port; AAextraTimers3 = pluginConfig[0].devices[2].extraTimers; AAdebug3 = pluginConfig[0].devices[2].debug; } } // 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 ( AAIP1 === undefined || !AAIP1) { // create a error / red toast notification if the required input is not provided. homebridge.toast.error('An ip address of your AdvantageAir system must be provided.', 'Error'); return; } // Assign default values if undefined if ( AAport1 == undefined ) { AAport1 = "2025" } if ( AAport2 == undefined ) { AAport2 = "2025" } if ( AAport3 == undefined ) { AAport3 = "2025" } if ( AAextraTimers1 == undefined ) { AAextraTimers1 = "false" } if ( AAextraTimers2 == undefined ) { AAextraTimers2 = "false" } if ( AAextraTimers3 == undefined ) { AAextraTimers3 = "false" } if ( pluginConfig[0].devices.length == 3 ) { homebridge.toast.info(`Processing 3 Aircon systems - may take up to 3 minutes`, 'Info'); homebridge.toast.info(`(1) ${AAextraTimers1}. (2) ${AAextraTimers2}. (3) ${AAextraTimers3}.`, 'Include extraTimers?'); } else if ( pluginConfig[0].devices.length == 2 ) { homebridge.toast.info(`Processing 2 Aircon systems - may take up to 2 minutes`, 'Info'); homebridge.toast.info(`(1) ${AAextraTimers1}. (2) ${AAextraTimers2}.`, 'Include extraTimers?'); } else if ( pluginConfig[0].devices.length == 1 ) { homebridge.toast.info(`Processing 1 Aircon system - may take up to 1 minute`, 'Info'); homebridge.toast.info(`(1) ${AAextraTimers1}.`, 'Include extraTimers?'); } // starting the request, show the loading spinner homebridge.showSpinner(); // request key paths from the server try { const response = await homebridge.request('/configcreator', { ip1: AAIP1, name1: AAname1, port1: AAport1, extraTimers1: AAextraTimers1, debug1: AAdebug1, ip2: AAIP2, name2: AAname2, port2: AAport2, extraTimers2: AAextraTimers2, debug2: AAdebug2, ip3: AAIP3, name3: AAname3, port3: AAport3, extraTimers3: AAextraTimers3, debug3: AAdebug3, 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'); // extract the pluginConfigNew from response.feedback const pluginConfigNew = response.feedback.split(";"); // empty the existing pluginConfig array and populate it with the newly created one pluginConfig = []; pluginConfig.push( JSON.parse(pluginConfigNew[1]) ); // now update the pluginConfig and Save it await homebridge.updatePluginConfig(pluginConfig); await homebridge.savePluginConfig(); // show a Config SAVED notification 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>