Skip to content

Commit

Permalink
Fix config arg parsing
Browse files Browse the repository at this point in the history
CLI args always took precedence, even when not provided.
Also improve polling interval accuracy.
  • Loading branch information
tomquist committed Jan 25, 2025
1 parent db4367e commit 45429d5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
64 changes: 38 additions & 26 deletions b2500/b2500.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,22 @@ def value(self, value):
def udp_server(self):
udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_sock.bind(("", self._udp_port))
print("UDP server is listening...")

try:
while not self._stop:
data, addr = udp_sock.recvfrom(1024)
decoded = data.decode()
current_time = time.time()

print(f"Received '{decoded}' ({data.hex()}) from {addr}")
if decoded == "hame":
if (
addr not in self._last_response_time
or (current_time - self._last_response_time[addr])
> self.dedupe_time_window
):
print(f"Received 'hame' from {addr}")
response_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
local_ip = udp_sock.getsockname()[0]
response_sock.bind((local_ip, 0))
Expand All @@ -100,7 +103,8 @@ def udp_server(self):
f"Received 'hame' from {addr} but ignored due to dedupe window"
)
else:
print(f"Received unknown UDP message: {decoded}")
print(f"Ignoring unknown message")

finally:
udp_sock.close()

Expand All @@ -115,34 +119,42 @@ def handle_tcp_client(self, conn, addr):
if self.on_connect:
self.on_connect(addr)

last_send_time = 0
while not self._stop:
if self.before_send:
self.before_send(addr)

with self._value_mutex:
value1, value2, value3 = self.value

value1 = round(value1)
value2 = round(value2)
value3 = round(value3)

value1 = abs(value1)
value2 = abs(value2)
value3 = abs(value3)

message = f"HM:{value1}|{value2}|{value3}"
try:
conn.send(message.encode())
print(f"Sent message to {addr}: {message}")
if self.after_send:
self.after_send(addr)

time.sleep(self.poll_interval)
except BrokenPipeError:
current_time = time.time()
time_since_last_send = current_time - last_send_time

if time_since_last_send >= self.poll_interval:
if self.before_send:
self.before_send(addr)

with self._value_mutex:
value1, value2, value3 = self.value

value1 = round(value1)
value2 = round(value2)
value3 = round(value3)

message = f"HM:{value1}|{value2}|{value3}"
try:
conn.send(message.encode())
last_send_time = current_time
print(f"Sent message to {addr}: {message}")
if self.after_send:
self.after_send(addr)

time.sleep(self.poll_interval)
except BrokenPipeError:
print(
f"Connection with {addr} broken. Waiting for a new connection."
)
break
else:
print(
f"Connection with {addr} broken. Waiting for a new connection."
f"Waiting for {self.poll_interval - time_since_last_send} seconds"
)
break
# Sleep a small amount to prevent busy waiting
time.sleep(0.01)
else:
print(f"Received unknown TCP message: {decoded}")
finally:
Expand Down
20 changes: 17 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ def main():
type=str,
)
parser.add_argument(
"-s", "--disable-sum", help="Disable sum of all phases", type=bool
"-s", "--disable-sum", help="Disable sum of all phases", type=bool, default=None
)
parser.add_argument(
"-a", "--disable-absolute", help="Disable absolute values", type=bool
"-a",
"--disable-absolute",
help="Disable absolute values",
type=bool,
default=None,
)
parser.add_argument(
"-t", "--skip-powermeter-test", help="Skip powermeter test on start", type=bool
"-t",
"--skip-powermeter-test",
help="Skip powermeter test on start",
type=bool,
default=None,
)
parser.add_argument(
"-p", "--poll-interval", help="Poll interval in seconds", type=int
Expand Down Expand Up @@ -70,6 +78,12 @@ def main():
else cfg.getint("GENERAL", "POLL_INTERVAL", fallback=1)
)

print(f"General Settings:")
print(f"Disable Sum Phases: {disable_sum_phases}")
print(f"Disable Absolute Values: {disable_absolut_values}")
print(f"Skip Test: {skip_test}")
print(f"Poll Interval: {poll_interval}")

# Fetch powermeter values once to check if the configuration is correct
if not skip_test:
test_powermeter(powermeter)
Expand Down

0 comments on commit 45429d5

Please sign in to comment.