Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
84 lines (68 loc) · 2.36 KB
/
Copy pathexample.py
File metadata and controls
84 lines (68 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Example script to test the Matter server and client."""
import argparse
import asyncio
import logging
import os
from os.path import abspath, dirname
from pathlib import Path
from sys import path
import aiohttp
from aiorun import run
import coloredlogs
path.insert(1, dirname(dirname(abspath(__file__))))
from matter_server.client.client import MatterClient # noqa: E402
from matter_server.server.server import MatterServer # noqa: E402
logging.basicConfig(level=logging.DEBUG)
_LOGGER = logging.getLogger(__name__)
DEFAULT_VENDOR_ID = 0xFFF1
DEFAULT_FABRIC_ID = 1
DEFAULT_PORT = 5580
DEFAULT_URL = f"http://127.0.0.1:{DEFAULT_PORT}/ws"
DEFAULT_STORAGE_PATH = os.path.join(Path.home(), ".matter_server")
# Get parsed passed in arguments.
parser = argparse.ArgumentParser(description="Matter Server Example.")
parser.add_argument(
"--storage-path",
type=str,
default=DEFAULT_STORAGE_PATH,
help=f"Storage path to keep persistent data, defaults to {DEFAULT_STORAGE_PATH}",
)
parser.add_argument(
"--port",
type=int,
default=DEFAULT_PORT,
help=f"TCP Port on which to run the Matter WebSockets Server, defaults to {DEFAULT_PORT}",
)
parser.add_argument(
"--log-level",
type=str,
default="info",
help="Provide logging level. Example --log-level debug, default=info, possible=(critical, error, warning, info, debug)",
)
args = parser.parse_args()
if __name__ == "__main__":
# configure logging
logging.basicConfig(level=args.log_level.upper())
coloredlogs.install(level=args.log_level.upper())
# make sure storage path exists
if not os.path.isdir(args.storage_path):
os.mkdir(args.storage_path)
# Init server
server = MatterServer(
args.storage_path, DEFAULT_VENDOR_ID, DEFAULT_FABRIC_ID, int(args.port)
)
async def run_matter():
"""Run the Matter server and client."""
# start Matter Server
await server.start()
# run the client
url = f"http://127.0.0.1:{args.port}/ws"
async with aiohttp.ClientSession() as session:
async with MatterClient(url, session) as client:
# start listening
await client.start_listening()
async def handle_stop(loop: asyncio.AbstractEventLoop):
"""Handle server stop."""
await server.stop()
# run the server
run(run_matter(), shutdown_callback=handle_stop)