Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
flatsiedatsie authored Dec 25, 2023
1 parent 9df7622 commit 148cd92
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 27 deletions.
8 changes: 6 additions & 2 deletions css/extension.css
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ extension-matter-adapter-
justify-content:space-between;
margin:2rem 0;
}

#extension-matter-adapter-pairing-qr-choose-scanner-area > *{
position:relative;
width:calc(50% - 1rem);
Expand All @@ -400,7 +401,10 @@ extension-matter-adapter-
#extension-matter-adapter-pairing-qr-choose-scanner-area > *:hover .extension-matter-adapter-pairing-qr-choose-scanner-icon{
opacity:.5;
}

#extension-matter-adapter-pairing-qr-choose-scanner-camera > *,
#extension-matter-adapter-pairing-qr-choose-scanner-phone > *{
pointer-events:none;
}
#extension-matter-adapter-pairing-qr-choose-scanner-area h3{
font-size:4rem;
color:white;
Expand Down Expand Up @@ -492,7 +496,7 @@ extension-matter-adapter-
display:inline-block;
}


#extension-matter-adapter-busy-pairing-indicator,
.extension-matter-adapter-busy-pairing-hint{
display:none;
}
Expand Down
45 changes: 39 additions & 6 deletions js/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
this.uuid == null; // used with qr scanner

this.retried_init = false;

this.scan_window = null;

window.matter_adapter_poll_interval = null;
// We'll try and get this data from the addon backend
Expand Down Expand Up @@ -200,6 +202,15 @@
if(this.debug){
console.log("pair device via commission_with_code response: ", body);
}
if(typeof body.state != 'undefined'){
if(body.state == false){
document.getElementById('extension-matter-adapter-pairing-failed-hint').classList.remove('extension-matter-adapter-hidden');
document.getElementById('extension-matter-adapter-second-page').classList.remove('extension-matter-adapter-busy-pairing');
}
else if(body.state == true){
console.log("Matter server pairing process seems to have started succesfully");
}
}


}).catch((e) => {
Expand Down Expand Up @@ -276,6 +287,13 @@
document.getElementById('extension-matter-adapter-provide-wifi-container').classList.remove('extension-matter-adapter-hidden');
});

document.getElementById('extension-matter-adapter-pairing-qr-choose-scanner-camera').addEventListener('click', (event) => {
event.preventDefault();
console.log("opening in new window: event.target: ", event.target);
console.log("opening in new window: url:", event.target.href);
this.scan_window = window.open(event.target.href,'_blank');
});


// Choose to scan with camera
/*
Expand Down Expand Up @@ -412,6 +430,13 @@
document.getElementById('extension-matter-adapter-save-manual-input-pairing-code-button').classList.remove('extension-matter-adapter-hidden');
}, 4000);
this.show_pairing_start_area();

if(this.scan_window){
console.log("closing previously opened scan window");
this.scan_window.close();
this.scan_window = null;
}

}

//document.getElementById('extension-matter-adapter-other-pairing-options-container').classList.remove('extension-matter-adapter-hidden');
Expand Down Expand Up @@ -869,15 +894,21 @@

document.getElementById('extension-matter-adapter-pairing-code-input').value = this.pairing_code;

if(this.scan_window){
console.log("closing previously opened scan window");
this.scan_window.close();
this.scan_window = null;
}

this.show_pairing_start_area();
}
/*

else{
if(this.debug){
console.log("pairing code did not start with MT: yet: ", body.pairing_code);
//console.log("pairing code did not start with MT: yet: ", this.pairing_code);
}
}
*/

//this.regenenerate_items();
}

Expand All @@ -903,9 +934,11 @@

}
else{
if(this.debug){
console.log("pairing code did not start with MT: yet: ", body.pairing_code);
}
if(body.pairing_failed){
if(this.debug){
console.log("User has not yet started pairing process. Pairing failed state: ", body.pairing_failed);
}
}
}
//this.regenenerate_items();
}
Expand Down
51 changes: 47 additions & 4 deletions pkg/matter_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(self, verbose=False):

# set up some variables
self.DEBUG = True
self.DEBUG2 = False
self.DEBUG2 = True

self.should_save = False

Expand Down Expand Up @@ -156,6 +156,7 @@ def __init__(self, verbose=False):

self.busy_discovering = False
self.pairing_failed = False
self.busy_pairing = False

self.brightness_transition_time = 0

Expand Down Expand Up @@ -583,6 +584,9 @@ def client_thread(self):

#url = f"http://127.0.0.1:{self.port}/ws"
url = "ws://127.0.0.1:" + str(self.port) + "/ws"
if self.DEBUG:
print("attempting to open websocked connection to matter server. URL: " + str(url))

websocket.enableTrace(False)
self.ws = websocket.WebSocketApp(url, #"wss://127.0.0.1",
on_open=self.on_open,
Expand All @@ -605,7 +609,7 @@ def client_thread(self):

def on_message(self, ws, message="{}"):
if self.DEBUG:
self.s_print("\n.\nclient: on_message: " + str(message) + "\n\n")
self.s_print("\n.\nclient: in on_message. Message: " + str(message)[:100] + "...etc" + "\n\n")
try:

# matter_server.common.models.message.SuccessResultMessage
Expand Down Expand Up @@ -752,6 +756,15 @@ def on_message(self, ws, message="{}"):
if self.DEBUG:
self.s_print("Warning, there was no _type in the message")


if 'error_code' in message:
if self.DEBUG:
print("message contained an error code.")

if 'details' in message:
if self.DEBUG:
print("Error details: " + str(message['details']))

#self.should_save = True

except Exception as ex:
Expand Down Expand Up @@ -939,7 +952,7 @@ def download_certs(self):
if download_certs_output != None:
self.certificates_updated = True

if len(download_certs_output) < 5:
if len(str(download_certs_output)) > 5:
self.certificates_updated = True
#self.last_certificates_download_time = time.time()
self.persistent_data['last_certificates_download_time'] = int(time.time())
Expand All @@ -951,6 +964,8 @@ def download_certs(self):
return False

else:
if self.DEBUG:
self.s_print("Error, certificates didn't seem to download (download_certs_output was None)")
return False
else:
self.certificates_updated = True
Expand Down Expand Up @@ -1021,19 +1036,30 @@ def start_matter_pairing(self,pairing_type,code):
self.s_print("\n\n\n\nin start_matter_pairing. Pairing type: " + str(pairing_type) + ", Code: " + str(code))
self.pairing_failed = False
# Download the latest certificates if they haven't been updated in a while
self.download_certs()
#self.download_certs()


try:
if self.client_connected:
if self.DEBUG:
print("start_pairing: Client is connected, so sending commissioning code to Matter server.")

self.busy_pairing = True



# Set the wifi credentials
self.set_wifi_credentials()

time.sleep(6) # TODO: Dodgy


os.system('sudo btmgmt -i hci0 power off')
os.system('sudo btmgmt -i hci0 bredr off')
os.system('sudo btmgmt -i hci0 power on')



# create pairing message
message = None
if pairing_type == 'commission_with_code':
Expand Down Expand Up @@ -1061,6 +1087,10 @@ def start_matter_pairing(self,pairing_type,code):

return True

else:
if self.DEBUG:
self.s_print("start_matter_pairing: error, client is not connected")

except Exception as ex:
self.s_print("Error in start_pairing: " + str(ex))

Expand Down Expand Up @@ -1136,6 +1166,19 @@ def clock(self):
line = line.decode()
if self.DEBUG:
self.s_print("CAPTURED STDERR: " + str(line.rstrip()))
if 'Traceback' in line:
self.pairing_failed = True
self.send_pairing_prompt("Error, Matter server crashed")
if 'over BLE failed' in line:
self.pairing_failed = True
self.send_pairing_prompt("Bluetooth commissioning failed")
if 'error.NodeInterviewFailed' in line:
self.pairing_failed = True
self.send_pairing_prompt("Interviewing Matter device failed")
if 'Commission with code failed for node' in line:
self.pairing_failed = True
self.send_pairing_prompt("Interviewing Matter device just failed")


if 'address already in use' in line:
self.s_print("ERROR, THERE ALREADY IS A MATTER SERVER RUNNING")
Expand Down
29 changes: 16 additions & 13 deletions pkg/matter_adapter_api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,21 @@ def handle_request(self, request):

if self.adapter.DEBUG2:
print("poll: doing request to candle webserver. parameters: " + str(parameters))
q = requests.post( "https://www.candlesmarthome.com/qr/ajax.php", data = parameters )
if self.adapter.DEBUG2:
print("q.content = " + str(q.content))
print("q.json = " + str(q.json))
if len(str(q.content)) > 4:
qr_json = q.json()
if 'code' in qr_json:
code = qr_json['code']
else:
if self.adapter.DEBUG2:
print('no code in post json')
else:
if not self.adapter.busy_pairing:
q = requests.post( "https://www.candlesmarthome.com/qr/ajax.php", data = parameters )
if self.adapter.DEBUG2:
print('Matter adapter debug: poll: response not long enough')
print("q.content = " + str(q.content))
print("q.json = " + str(q.json))
if len(str(q.content)) > 4:
qr_json = q.json()
if 'code' in qr_json:
code = qr_json['code']
else:
if self.adapter.DEBUG2:
print('no code in post json')
else:
if self.adapter.DEBUG2:
print('Matter adapter debug: poll: response not long enough')

except Exception as ex:
if self.DEBUG:
Expand All @@ -169,6 +170,7 @@ def handle_request(self, request):
'discovered': self.adapter.discovered, # deprecated, but might be interesting to see if it's every populated
'busy_discovering':self.adapter.busy_discovering,
'pairing_code': code,
'busy_pairing':self.adapter.busy_pairing,
'pairing_failed':self.adapter.pairing_failed,
#'nodes': self.adapter.nodes,
'nodez': self.adapter.persistent_data['nodez']
Expand Down Expand Up @@ -259,6 +261,7 @@ def handle_request(self, request):
if self.DEBUG:
print("\n\nAPI: in reset_pairing")

self.adapter.busy_pairing = False
self.adapter.pairing_failed = False
self.adapter.pairing_code = ""

Expand Down
4 changes: 2 additions & 2 deletions views/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ <h4>Ready to pair</h4>
<div id="extension-matter-adapter-pairing-bluetooth-hint">
<ul>
<li>The device must be within Bluetooth range of the Candle Controller</li>
<li>It's recommended to factory reset the device now</li>
<li>Please get your new device into pairing mode. If possible it's recommended to factory reset it.</li>
</ul>
</div>

Expand All @@ -264,7 +264,7 @@ <h4>Ready to pair</h4>
</div>

<div id="extension-matter-adapter-pairing-busy-step">
<div id="extension-matter-adapter-busy-pairing-indicator" class="extension-matter-adapter-area extension-matter-adapter-hidden">
<div id="extension-matter-adapter-busy-pairing-indicator" class="extension-matter-adapter-area">
<div class="extension-matter-adapter-spinner">
<div></div><div></div><div></div><div></div>
</div>
Expand Down

0 comments on commit 148cd92

Please sign in to comment.