Skip to content

Commit

Permalink
Add examples instead of __main__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tannewt committed Oct 17, 2024
1 parent db186e6 commit b21b35f
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 269 deletions.
24 changes: 19 additions & 5 deletions circuitmatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,35 @@
from . import session
from .device_types.utility.root_node import RootNode

__version__ = "0.0.0"
__version__ = "0.1.0"


class CircuitMatter:
def __init__(
self,
socketpool,
mdns_server,
random_source,
state_filename,
socketpool=None,
mdns_server=None,
random_source=None,
state_filename="matter-device-state.json",
vendor_id=0xFFF1,
product_id=0x8000,
):
if socketpool is None:
import socket

socketpool = socket
self.socketpool = socketpool

if mdns_server is None:
from circuitmatter.utility.mdns.avahi import Avahi

mdns_server = Avahi()
self.mdns_server = mdns_server

if random_source is None:
from circuitmatter.utility import random

random_source = random
self.random = random_source

self.nonvolatile = nonvolatile.PersistentDictionary(state_filename)
Expand Down
264 changes: 0 additions & 264 deletions circuitmatter/__main__.py

This file was deleted.

11 changes: 11 additions & 0 deletions circuitmatter/utility/mdns/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class DummyMDNS:
def advertise_service(
self,
service_type,
protocol,
port,
txt_records=[],
subtypes=[],
instance_name="",
):
print(f"Advertise service {service_type} {protocol} {port} {txt_records}")
41 changes: 41 additions & 0 deletions circuitmatter/utility/mdns/avahi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import subprocess


class Avahi:
def __init__(self):
self.active_services = {}
self.publish_address = None

def advertise_service(
self,
service_type,
protocol,
port,
txt_records={},
subtypes=[],
instance_name="",
):
subtypes = [f"--subtype={subtype}" for subtype in subtypes]
txt_records = [f"{key}={value}" for key, value in txt_records.items()]
command = [
"avahi-publish-service",
*subtypes,
instance_name,
f"{service_type}.{protocol}",
str(port),
*txt_records,
]
self.active_services[service_type + instance_name] = subprocess.Popen(command)
if self.publish_address is None:
command = [
"avahi-publish-address",
"dalinar.local",
"fd98:bbab:bd61:8040:642:1aff:fe0c:9f2a", # "fe80::642:1aff:fe0c:9f2a",
]
self.publish_address = subprocess.Popen(command)

def __del__(self):
for active_service in self.active_services.values():
active_service.kill()
if self.publish_address is not None:
self.publish_address.kill()
6 changes: 6 additions & 0 deletions circuitmatter/utility/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os
import secrets

urandom = os.urandom

randbelow = secrets.randbelow
Loading

0 comments on commit b21b35f

Please sign in to comment.