Skip to content

Commit

Permalink
try limiting via cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveicedgreentea committed Jan 14, 2023
1 parent 2023be7 commit 35b73c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
47 changes: 25 additions & 22 deletions madvr/madvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
self.port = port
self.connect_timeout: int = connect_timeout
self.logger = logger
self._lock = threading.Lock()
self._cmd_running = False

# Const values
self.MADVR_OK: Final = Connections.welcome.value
Expand Down Expand Up @@ -268,7 +268,7 @@ def send_command(self, command: str) -> str:
command: str - command to send like KeyPress, MENU
Raises RetryExceededError
"""

self._cmd_running = False
# Verify the command is supported
try:
cmd, is_info, enum_type = self._construct_command(command)
Expand All @@ -286,6 +286,7 @@ def send_command(self, command: str) -> str:
self._reconnect()

while retry_count < 5:
self._cmd_running = True
# Send the command
self.client.send(cmd)

Expand Down Expand Up @@ -330,8 +331,10 @@ def send_command(self, command: str) -> str:
# so polling isn't required

# process the output
self._cmd_running = False
return self._process_info(res, enum_type["msg"].value)


self._cmd_running = False
return ""

except socket.timeout:
Expand All @@ -340,6 +343,7 @@ def send_command(self, command: str) -> str:
continue

# raise if we got here
self._cmd_running = False
raise RetryExceededError("Retry count exceeded")

def read_notifications(self, wait_forever: bool) -> None:
Expand Down Expand Up @@ -379,27 +383,26 @@ def poll_status(self) -> None:
"""
Poll the status for attributes and write them to state
"""
# If its locked, return
if not self._lock.acquire(blocking=False): # pylint: disable=consider-using-with

# dont run this multiple times as HA loves doing
if self._cmd_running:
return

# lock so HA doesn't trip over itself
with self._lock:
try:
# send heartbeat so it doesnt close our connection
self._send_heartbeat()
# Get incoming signal info
for cmd in [
"GetIncomingSignalInfo",
"GetAspectRatio",
"GetTemperatures",
"GetOutgoingSignalInfo",
]:
res = self.send_command(cmd)
self.logger.debug("poll_status resp: %s", res)
self._process_notifications(res)
except (socket.timeout, socket.error, HeartBeatError) as err:
self.logger.error("Error getting update: %s", err)
try:
# send heartbeat so it doesnt close our connection
self._send_heartbeat()
# Get incoming signal info
for cmd in [
"GetIncomingSignalInfo",
"GetAspectRatio",
"GetTemperatures",
"GetOutgoingSignalInfo",
]:
res = self.send_command(cmd)
self.logger.debug("poll_status resp: %s", res)
self._process_notifications(res)
except (socket.timeout, socket.error, HeartBeatError) as err:
self.logger.error("Error getting update: %s", err)

def _process_notifications(self, input_data: Union[bytes, str]) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="py_madvr",
version="1.1.3",
version="1.1.4",
author="iloveicedgreentea",
description="A package to control MadVR Envy over IP",
long_description=long_description,
Expand Down

0 comments on commit 35b73c7

Please sign in to comment.