Skip to content

Commit

Permalink
Merge pull request #11 from gri-gus/v1.0.1
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
gri-gus authored Aug 31, 2024
2 parents 8c4c0d0 + 6a00d9b commit 6c5c648
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 49 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import setuptools

VERSION = "1.0.0"
VERSION = "1.0.1"
PACKAGE_DIR = "."
REQUIREMENTS_FILE = "./requirements.txt"
README = "README-PYPI.md"
Expand Down
50 changes: 50 additions & 0 deletions streamdeck_sdk/debug_server_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import logging
from typing import Set

from websockets.sync.server import (
ServerConnection,
WebSocketServer, # noqa
serve,
)

from .mixins import SendMixin
from .simple_ws.client import WebSocketClientApp
from .utils.in_separate_thread import in_separate_thread

logger = logging.getLogger(__name__)


class DebugServerMixin(SendMixin):
debug_clients: Set[ServerConnection]
debug_port: int

@in_separate_thread(daemon=True)
def _debug_server_run(self):
server: WebSocketServer = serve(
handler=self._debug_server_handler,
host="localhost",
port=self.debug_port,
)
server.serve_forever()

def _debug_server_handler(self, websocket: ServerConnection):
self.debug_clients.add(websocket)
logger.debug(f"Client {websocket} added, clients: {self.debug_clients}.")
try:
for message in websocket:
logger.debug(f"Message {message} received from StreamDeckPlugin.")
self.ws.send(message)
logger.debug(f"Message {message} sent to StreamDeck.")
except Exception: # noqa
self.debug_clients.remove(websocket)
logger.debug(f"Client {websocket} removed, clients: {self.debug_clients}")

def _debug_server_on_message(
self,
ws: WebSocketClientApp, # noqa
message: str,
) -> None:
logger.debug(f"Received message from StreamDeck. message={message}.")
for client in self.debug_clients:
client.send(message)
logger.debug(f"Message sent to StreamDeckPlugin {client}. message={message}.")
6 changes: 3 additions & 3 deletions streamdeck_sdk/executable/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@


def main():
parser = argparse.ArgumentParser(description='StreamDeckSDK')
parser.add_argument('command')
parser.add_argument('-i', default=None, required=False, type=str, help="Input file", )
parser = argparse.ArgumentParser(description="StreamDeckSDK")
parser.add_argument("command")
parser.add_argument("-i", default=None, required=False, type=str, help="Input file", )
args = parser.parse_args()
logger.info(args)
command = args.command
Expand Down
68 changes: 24 additions & 44 deletions streamdeck_sdk/sdk.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import argparse
import json
import logging
import sys
from os import PathLike
from pathlib import Path
from typing import Optional, List, Dict, Set

from websockets.sync.server import (
ServerConnection,
WebSocketServer, # noqa
serve,
)

from .debug_server_mixin import DebugServerMixin
from .event_routing_mixin import EventRoutingMixin
from .logger import (
init_root_logger,
Expand All @@ -20,7 +20,6 @@
from .mixins import Base
from .sd_objs import registration_objs
from .simple_ws.client import WebSocketClientApp
from .utils.in_separate_thread import in_separate_thread

logger = logging.getLogger(__name__)

Expand All @@ -34,7 +33,11 @@ def __init__(self):
self.info: Optional[registration_objs.Info] = None


class StreamDeck(Base, EventRoutingMixin):
class StreamDeck(
Base,
EventRoutingMixin,
DebugServerMixin,
):
def __init__(
self,
actions: Optional[List[Action]] = None,
Expand Down Expand Up @@ -95,26 +98,28 @@ def run(self):
def run_debug(self):
logger.warning("Debug mode.")

try:
logger.info("Trying to connect to the debug server.")
self.ws = WebSocketClientApp(
uri=f'ws://localhost:{self.debug_port}/',
on_message=self.ws_on_message,
)
logger.info(f"Successfully connected to the debug server.")
self.__init_actions()
self.ws.run_forever()
if len(sys.argv) == 1: # is client
try:
logger.info("Trying to connect to the debug server.")
self.ws = WebSocketClientApp(
uri=f'ws://localhost:{self.debug_port}/',
on_message=self.ws_on_message,
)
logger.info(f"Successfully connected to the debug server.")
self.__init_actions()
self.ws.run_forever()
except Exception as err:
logger.exception(err)
logger.debug(f"Failed to connect to debug server.")
return
except Exception:
logger.debug(f"Failed to connect to debug server.")

logger.info("Trying to create a debug server.")
self.__parse_args()
self.__debug_server_run()
self._debug_server_run()
logger.debug("Debug server is running.")
self.ws = WebSocketClientApp(
uri=f'ws://localhost:{self.port}/',
on_message=self.__debug_server_on_message,
on_message=self._debug_server_on_message,
on_open=self.ws_on_open,
)
logger.debug("Debug server connected to StreamDeck.")
Expand All @@ -139,12 +144,14 @@ def __init_actions(self) -> None:
self.actions[action_uuid] = action

def __parse_args(self):
logger.debug("Trying to parse arguments.")
parser = argparse.ArgumentParser(description='StreamDeck Plugin')
parser.add_argument('-port', dest='port', type=int, help="Port", required=True)
parser.add_argument('-pluginUUID', dest='pluginUUID', type=str, help="pluginUUID", required=True)
parser.add_argument('-registerEvent', dest='registerEvent', type=str, help="registerEvent", required=True)
parser.add_argument('-info', dest='info', type=str, help="info", required=True)
args = parser.parse_args()
logger.debug("Trying to parse arguments was successful.")
logger.debug(f"{args=}")

self.port = args.port
Expand All @@ -157,30 +164,3 @@ def __parse_args(self):

self.registration_dict = {"event": self.register_event, "uuid": self.plugin_uuid}
logger.debug(f"{self.registration_dict=}")

@in_separate_thread(daemon=True)
def __debug_server_run(self):
server: WebSocketServer = serve(handler=self.__debug_server_handler, host="localhost", port=self.debug_port)
server.serve_forever()

def __debug_server_handler(self, websocket: ServerConnection):
self.debug_clients.add(websocket)
logger.debug(f"Client {websocket} added, clients: {self.debug_clients}.")
try:
for message in websocket:
logger.debug(f"Message {message} received from StreamDeckPlugin.")
self.ws.send(message)
logger.debug(f"Message {message} sent to StreamDeck.")
except Exception: # noqa
self.debug_clients.remove(websocket)
logger.debug(f"Client {websocket} removed, clients: {self.debug_clients}")

def __debug_server_on_message(
self,
ws: WebSocketClientApp, # noqa
message: str,
) -> None:
logger.debug(f"Received message from StreamDeck. message={message}.")
for client in self.debug_clients:
client.send(message)
logger.debug(f"Message sent to StreamDeckPlugin {client}. message={message}.")
2 changes: 1 addition & 1 deletion streamdeck_sdk/simple_ws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def run_forever(self):
while True:
try:
message = self.client.recv()
logger.debug(f"Сообщение {message} получено.")
logger.debug(f"Message received. {message=}")
except ConnectionClosedOK as err:
self._handle_connection_close(err=err)
break
Expand Down

0 comments on commit 6c5c648

Please sign in to comment.