From ad50a85f0d2bd4d8802cc1957279a49abab2a8ba Mon Sep 17 00:00:00 2001 From: flatsiedatsie Date: Mon, 14 Nov 2022 15:55:59 +0100 Subject: [PATCH] Add files via upload --- images/menu-icon-old.svg | 66 ++++++++++++++++++++++++++ images/menu-icon-yellow.svg | 1 + images/menu-icon.svg | 67 +------------------------- js/extension.js | 95 ++++++++++++++++++++++++++++--------- manifest.json | 2 +- pkg/api_handler.py | 16 +++---- pkg/candle_adapter.py | 22 +++++---- pkg/static/css/main.css | 7 +++ pkg/templates/index.html | 3 +- views/content.html | 2 +- 10 files changed, 172 insertions(+), 109 deletions(-) create mode 100644 images/menu-icon-old.svg create mode 100644 images/menu-icon-yellow.svg diff --git a/images/menu-icon-old.svg b/images/menu-icon-old.svg new file mode 100644 index 0000000..81188c0 --- /dev/null +++ b/images/menu-icon-old.svg @@ -0,0 +1,66 @@ + + + + + + + + image/svg+xml + + fire + + + + + + fire + + + diff --git a/images/menu-icon-yellow.svg b/images/menu-icon-yellow.svg new file mode 100644 index 0000000..7b6fc7c --- /dev/null +++ b/images/menu-icon-yellow.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/images/menu-icon.svg b/images/menu-icon.svg index 81188c0..241c029 100644 --- a/images/menu-icon.svg +++ b/images/menu-icon.svg @@ -1,66 +1 @@ - - - - - - - - image/svg+xml - - fire - - - - - - fire - - - + \ No newline at end of file diff --git a/js/extension.js b/js/extension.js index a35b9e1..c22d5e7 100644 --- a/js/extension.js +++ b/js/extension.js @@ -4,8 +4,10 @@ super('Candle-manager-addon'); this.addMenuEntry('Candle manager'); - this.content = '

Failed to load Candle Manager add-on

- The gateway did not respond. Perhaps it crashed? You could try rebooting it.

- You can try to reload this webpage.

'; - + this.content = '

Failed to load Candle Manager add-on

- The gateway did not respond. Perhaps it crashed? You could try rebooting it.

- You can try to reload this page.

'; + + this.debug = false; + fetch(`/extensions/Candle-manager-addon/views/content.html`) .then((res) => res.text()) .then((text) => { @@ -20,6 +22,8 @@ this.view.innerHTML = this.content; + this.warning_needed = true; + var attachFuncEvent = "message"; var attachFunc = window.addEventListener ; if (! window.addEventListener) { @@ -29,7 +33,8 @@ attachFunc(attachFuncEvent, function(event) { if (event.data == 'iframeIsDone') { // iframe has succesfully loaded the Candle manager callback - //console.log("Candle manager loaded in iframe succesfully"); + console.log("Candle manager loaded in iframe succesfully"); + this.warning_needed = false; const ssl_frame_warning = document.getElementById('extension-Candle-manager-addon-ssl-frame-warning'); if(ssl_frame_warning != null){ ssl_frame_warning.parentNode.removeChild(ssl_frame_warning); // remove the warning if the Candle manager loaded succesfully @@ -45,19 +50,27 @@ this.view.innerHTML = this.content; //console.log(this.content); - + const iframe_el = document.getElementById('extension-Candle-manager-addon-iframe'); //var full_lan_path = "http://gateway.local:8686" + //console.log("window.location.protocol: ", window.location.protocol); - /* - var the_protocol = "https://"; - if (location.protocol == 'http:'){ + var http_local_address = window.location.protocol + '//' + window.location.hostname + ':8686'; // this is only used as a backup guess for where the flask server may be + + /* + //var the_protocol = "https://"; + if (window.location.protocol == 'http:'){ // If the connection is unsecured, we assume it's a local lan connection, and can simply load the Candle Manager into the iframe. Perhaps a more thorough check would be if `mozilla-iot` is in the URL bar string. //the_protocol = "http://"; - document.getElementById('extension-Candle-manager-addon-iframe').src = 'http://' + window.location.hostname + ':8686'; + if(iframe_el != null){ + iframe_el.src = http_local_address; + } + } else{ - */ + console.log("Candle manager: seems to be a HTTPS connection"); + } + */ var https_in_url = 0; if (location.protocol == 'https:'){ @@ -67,38 +80,76 @@ // If the user is using https and/or the tunneling feature, find out what the actual local IP address is from the controller. + //console.log("https_in_url : ", https_in_url ); window.API.postJson( `/extensions/${this.id}/api/full_lan_path`, {'ssl':https_in_url,'hostname':window.location.hostname} ).then((body) => { //full_lan_path = body['full_lan_path']; - //console.log("API response:"); - //console.log(body); //if( 'https://' + window.location.hostname + ':8686' != body['full_lan_path'] ){ // document.getElementById('extension-Candle-manager-addon-iframe').src = 'data:text/html,\'

The manager cannot be displayed (yet)

- It might still be starting up. You can try reloading the page in a few seconds.

- Alternatively, you can try to visit directly. There you may have to create a security exception.

\''; //} - document.getElementById('extension-Candle-manager-addon-iframe').src = body['full_lan_path']; - document.getElementById('extension-Candle-manager-addon-support-link').href = body['full_lan_path']; + + + this.debug = body['debug']; + + if(this.debug){ + console.log("Candle manager full_lan_path API response:"); + console.log(body); + } + + + var flask_protocol = 'http'; + if( body['ssl_certificate_detected'] ){ + flask_protocol = 'https'; + } + + + if(document.getElementById('extension-Candle-manager-addon-iframe') != null){ + if(this.debug){ + console.log("candle manager debug: setting iframe to:", flask_protocol + "://" + body['full_lan_path']); + } + document.getElementById('extension-Candle-manager-addon-iframe').src = flask_protocol + "://" + body['full_lan_path']; + } + else{ + console.log('error, candle manager iframe_el was null while trying to set candle manager iframe to : ', flask_protocol + "://" + body['full_lan_path']); + } + + //iframe_el.src = ; + document.getElementById('extension-Candle-manager-addon-support-link').href = flask_protocol + "://" + body['full_lan_path']; //console.log(body['full_lan_path']); //console.log(body['ssl_enabled']); setTimeout(function() { //console.log("timeout is now"); - try { - document.getElementById('extension-Candle-manager-addon-ssl-frame-warning').classList.remove("extension-Candle-manager-addon-hidden"); - } - catch(err) { - // + if(this.warning_needed){ + try { + document.getElementById('extension-Candle-manager-addon-ssl-frame-warning').classList.remove("extension-Candle-manager-addon-hidden"); + } + catch(err) { + if(this.debug){ + console.log("debug: candle manager timeout error: ", err); + } + } + } + else{ + console.log("candle manager: iframe loaded succesfully, no need to show warning"); } }, 3000); }).catch((e) => { - var error_string = e.toString(); - console.log("Error calling API, will try backup option"); - console.log(error_string); - document.getElementById('extension-Candle-manager-addon-iframe').src = "https://gateway.local:8686"; + if(this.debug){ + console.log("Error calling API, will try backup option. Error was: ", e); + } + if(document.getElementById('extension-Candle-manager-addon-iframe') != null){ + document.getElementById('extension-Candle-manager-addon-iframe').src = http_local_address; + } + else{ + console.log('error, iframe_el was null while trying to set candle manager iframe to backup address: ', http_local_address); + } + }); //} } diff --git a/manifest.json b/manifest.json index 4adc0e8..31ca78b 100644 --- a/manifest.json +++ b/manifest.json @@ -70,7 +70,7 @@ } }, "short_name": "Candle Mgr", - "version": "1.2.7", + "version": "1.3.0", "web_accessible_resources": [ "css/*.css", "images/*.svg", diff --git a/pkg/api_handler.py b/pkg/api_handler.py index 5939843..487d2ef 100644 --- a/pkg/api_handler.py +++ b/pkg/api_handler.py @@ -66,7 +66,7 @@ def handle_request(self, request): - self.full_lan_path = "gateway.local:8686" + self.full_lan_path = "candle.local:8686" try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -75,8 +75,8 @@ def handle_request(self, request): s.connect(('192.126.199.199', 1)) lan_ip = s.getsockname()[0] except: - print("Socket attempts to find IP failed: falling back to gateway.local") - lan_ip = 'gateway.local' + print("Socket attempts to find IP failed: falling back to candle.local") + lan_ip = 'candle.local' finally: s.close() @@ -86,12 +86,12 @@ def handle_request(self, request): - if self.adapter.ssl_enabled: - self.full_lan_path = "https://" + self.full_lan_path - else: - self.full_lan_path = "http://" + self.full_lan_path + #if self.adapter.ssl_enabled: + # self.full_lan_path = "https://" + self.full_lan_path + #else: + # self.full_lan_path = "http://" + self.full_lan_path - response = {'ssl_enabled':self.adapter.ssl_enabled, 'full_lan_path':self.full_lan_path} + response = {'ssl_certificate_detected':self.adapter.ssl_certificate_detected, 'full_lan_path':self.full_lan_path, 'debug':self.adapter.DEBUG} if self.adapter.DEBUG: print("Returning local path: " + str(response)) diff --git a/pkg/candle_adapter.py b/pkg/candle_adapter.py index d28e9d5..5f4643a 100644 --- a/pkg/candle_adapter.py +++ b/pkg/candle_adapter.py @@ -1,4 +1,4 @@ -"""Candle adapter for WebThings Gateway.""" +"""Candle adapter for Candle Controller / WebThings Gateway.""" import os @@ -126,7 +126,7 @@ def __init__(self, verbose=False): self.DEVELOPMENT = False self.port = 8686 - self.ssl_enabled = False + self.ssl_certificate_detected = False self.json_sketches_url = "" self.simple_password = "" self.arduino_type = "nano" @@ -169,18 +169,20 @@ def __init__(self, verbose=False): if os.path.isfile(self.certificate_path): if self.DEBUG: - print("found a certificate, assuming SSL/Tunnel is enabled") - self.ssl_enabled = True - + print("spotted a SSL certificate file, assuming SSL/Tunnel is enabled") + #self.ssl_certificate_detected = True # TODO: tmeporarily disabled https for the flask server. Might give better user experience. + else: + if self.DEBUG: + print("did not spot ssl certificate file, assuming SSL/Tunnel is disabled") - self.full_lan_path = "gateway.local:8686" + self.full_lan_path = "candle.local:8686" try: lan_ip = get_ip() self.full_lan_path = str(lan_ip) + ":8686/" except Exception as ex: print("Error, unable to get local lan path: " + str(ex)) - if self.ssl_enabled: + if self.ssl_certificate_detected: self.full_lan_path = "https://" + self.full_lan_path else: self.full_lan_path = "http://" + self.full_lan_path @@ -400,7 +402,7 @@ def dated_url_for(endpoint, **values): values['q'] = int(os.stat(file_path).st_mtime) return url_for(endpoint, **values) - if self.ssl_enabled: + if self.ssl_certificate_detected: print("Running Flask as https") app.run(host='0.0.0.0', debug=True, use_reloader=False, port=self.port, ssl_context=(self.certificate_path, self.privatekey_path) ) else: @@ -1030,7 +1032,7 @@ def change_settings(self, source_id, generate_new_code=False, new_values_list=[] except: pass print("library name:" + str(library_name)) - if library_name is not "" and library_name is not None: + if library_name != "" and library_name != None: if library_name not in self.installed_libraries and library_name not in self.required_libraries: @@ -1638,7 +1640,7 @@ def get_ip(): s.connect(('10.255.255.255', 1)) IP = s.getsockname()[0] except: - IP = 'gateway.local' + IP = 'candle.local' finally: s.close() return IP diff --git a/pkg/static/css/main.css b/pkg/static/css/main.css index ad6252a..2be8468 100644 --- a/pkg/static/css/main.css +++ b/pkg/static/css/main.css @@ -27,6 +27,7 @@ body{ + #logo-container{ margin-bottom:2rem; } @@ -36,6 +37,10 @@ body{ width:auto; } +#wizard-container{ + margin-top:2rem; +} + #content{ margin-top:3rem; background-color: rgba(0,0,0,.4); @@ -384,6 +389,8 @@ ul, @media screen and (min-width: 900px) { #centered{ width:70%; + max-width:60rem; + margin:0 auto; } #content{ diff --git a/pkg/templates/index.html b/pkg/templates/index.html index 29826c3..c887939 100644 --- a/pkg/templates/index.html +++ b/pkg/templates/index.html @@ -18,10 +18,11 @@
- +