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 Feb 1, 2023
1 parent 5263864 commit 1335646
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 74 deletions.
6 changes: 6 additions & 0 deletions js/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@
}
}

if(typeof body.nodes != 'undefined'){
console.log("nodes: ", body.nodes);
const nodes_string = JSON.stringify(body.nodes, null, 4)
document.getElementById('extension-matter-adapter-paired-devices-list-pre').innerHTML = nodes_string;
}

/*
// Generate the list of items
if(typeof body.items_list != 'undefined'){
Expand Down
98 changes: 45 additions & 53 deletions pkg/matter_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def __init__(self, verbose=False):
self.message_counter = 0
self.client_connected = 0

self.nodes = []

# Hotspot
self.use_hotspot = True
self.hotspot_ssid = ""
Expand Down Expand Up @@ -421,6 +423,10 @@ def on_message(self, ws, message):
print("\n\nRECEIVED MATTER SERVER INFO\n\n")
self.client_connected = True

# Request Matter nodes list
self.get_nodes()

# Pass WiFi credentials to Matter
if self.wifi_ssid != "" and self.wifi_password != "":
if self.DEBUG:
print("Sharing wifi credentials with Matter server")
Expand All @@ -440,8 +446,22 @@ def on_message(self, ws, message):

elif message['_type'] == "matter_server.common.models.message.SuccessResultMessage":
if self.DEBUG:
print("\n\nWIFI CREDENTIALS WERE SET SUCCESFULLY\n\n")
self.message_counter = 1
print("\n\nOK message.SuccessResultMessage\n\n")

if 'result' in message.keys():
if message['result'] == None:
print("\nThe result was None")

elif isinstance(message['result'], list):
print("\nThe result was a list")
self.nodes = message['result'];

else:
print("\nThe result was something else")
else:
print("Warning, message does not have result attribute")

#self.message_counter = 1

else:
print("Warning, there was no _type in the message")
Expand All @@ -467,23 +487,40 @@ def on_open(self, ws):
print("\n.\nclient: opened connection")
#print("ws: " + str(ws))


def get_nodes(self):
try:
if self.client_connected:

if self.DEBUG:
print("start_pairing: Client is connected, so asking for latest node list")
self.message_counter += 1
message = {
"message_id": str(self.message_counter),
"command": "get_nodes"
}
json_message = json.dumps(message)
self.ws.send(json_message)

return True

except Exception as ex:
print("Error in start_pairing: " + str(ex))

return False





def start_pairing(self):
def start_matter_pairing(self):
try:
if self.client_connected:

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

self.message_counter += 1
commission_with_code_message = {
"message_id": str(self.message_counter),
"command": "set_wifi_credentials",
"command": "commission_with_code",
"args": {
"code": "MT:Y.ABCDEFG123456789"
}
Expand Down Expand Up @@ -602,51 +639,6 @@ def set_state(self,state):
print("error in set_state: " + str(ex))




def set_slider(self,value):
try:
if self.DEBUG:
print("in set_slider with value: " + str(value))

# saves the new state in the persistent data file, so that the addon can restore the correct state if it restarts
self.persistent_data['slider'] = value
self.save_persistent_data()

try:
self.devices['matter-thing'].properties['slider'].update( value )
except Exception as ex:
if self.DEBUG:
print("error setting slider value on thing: " + str(ex))

except Exception as ex:
if self.DEBUG:
print("error in set_slider: " + str(ex))



def set_dropdown(self,value):
try:
if self.DEBUG:
print("in set_dropdown with value: " + str(value))

# saves the new state in the persistent data file, so that the addon can restore the correct state if it restarts
self.persistent_data['dropdown'] = value
self.save_persistent_data()

# A cool feature: you can create popups in the interface this way:
self.send_pairing_prompt("new dropdown value: " + str(value))

try:
self.devices['matter-thing'].properties['dropdown'].update( value )
except Exception as ex:
print("error setting dropdown value on thing: " + str(ex))

except Exception as ex:
print("error in set_dropdown: " + str(ex))




#
# The methods below are called by the controller
Expand Down
10 changes: 7 additions & 3 deletions pkg/matter_adapter_api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))

import json
#import time
import time

from gateway_addon import APIHandler, APIResponse

Expand Down Expand Up @@ -98,6 +98,9 @@ def handle_request(self, request):
if self.DEBUG:
print("API: in init")

self.adapter.get_nodes()
time.sleep(2)

wifi_credentials_available = False
if self.adapter.wifi_ssid != "" and self.adapter.wifi_password != "":
wifi_credentials_available = True
Expand All @@ -111,18 +114,19 @@ def handle_request(self, request):
'hotspot_addon_installed': self.adapter.hotspot_addon_installed,
'wifi_credentials_available': wifi_credentials_available,
'client_connected': self.adapter.client_connected,
'nodes': self.adapter.nodes
}),
)


# START PAIRING
elif action == 'start_pairing':
if self.DEBUG:
print("API: in start_pairing")
print("\n\nAPI: in start_pairing")
state = False

try:
state = self.adapter.start_pairing()
state = self.adapter.start_matter_pairing()
except Exception as ex:
if self.DEBUG:
print("Error in start_pairing request: " + str(ex))
Expand Down
24 changes: 6 additions & 18 deletions views/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h3 id="extension-matter-adapter-title">Matter</h3> <!-- The icon is loaded into

<div class="extension-matter-adapter-area">
<h2>This addon is in very early development</h2>
<p>Currently it already creates a Matter server and listens for incoming messages. But it doesn't turn those into devices yet.</p>
<p>Currently it creates a Matter server and listens for incoming messages. But it doesn't turn those into devices yet.</p>
</div>

<div id="extension-matter-adapter-install-hotspot-hint" class="extension-matter-adapter-area extension-matter-adapter-hidden">
Expand All @@ -54,24 +54,12 @@ <h2>This addon is in very early development</h2>

</div>

<div style="opacity:0">



<div id="extension-matter-adapter-main-items-list">(Items list will be loaded here)</div>

<p>Or you could show the state of the thing here. At least, the state of the thing when the addon loaded. </p>

<div class="extension-matter-adapter-area">
<p>Slider value when the addon loaded: <span id="extension-matter-adapter-slider-value-output">...</span></p>
</div>

<p>You could create an interval to continuously get the latest values, but that's something for a more advanced example.</p>

<!-- By having this button inside of this tab, it will be automatically hidden when it's pressed-->
<button id="extension-matter-adapter-show-second-page-button" class="icon-button"></button>
</div>
<pre id="extension-matter-adapter-paired-devices-list-pre">
Devices list is currently empty
</pre>

<button id="extension-matter-adapter-show-second-page-button" class="icon-button"></button>

</div>

</div>
Expand Down

0 comments on commit 1335646

Please sign in to comment.