Created all the basic routes. Still requires some testing and cleaning

This commit is contained in:
Jurn Wubben 2025-08-28 09:46:56 +02:00
parent 33715cb34e
commit 2ae520f6b3
3 changed files with 293 additions and 53 deletions

View file

@ -1,3 +1,4 @@
import select
from typing import Callable
from crc import Calculator, Crc16
from enum import Enum
@ -63,6 +64,13 @@ def pkt_escape(pkt: bytes) -> bytes:
return bytes(buf)
def is_socket_alive(sock: SocketType):
readable, _, exceptional = select.select([sock], [], [sock], 1)
if sock in exceptional:
return False
return bool(readable)
COLORS = [i.name.lower() for i in Colors]
COLORS_PATTERH = rb"\{(" + "|".join(COLORS).encode("ascii") + rb")\}"
@ -200,6 +208,8 @@ class NetBrite:
def connect(self):
try:
if not is_socket_alive(self.sock):
raise OSError("Socket dead")
self.sock.connect((self.address, self.port))
except OSError as e:
raise NetbriteConnectionException(
@ -208,10 +218,37 @@ class NetBrite:
def tx(self, pkt: bytes):
try:
_ = self.sock.send(pkt)
_ = self.sock.sendall(pkt_escape(pkt))
except OSError as e:
raise NetbriteTransferException(f"Error while opening network socket. {e}")
def reboot(self):
pkt = pack(
f"<3B H H 3B 2B 4B 4B 1B",
0x16,
0x16,
0x01, # msg start
2, # body length
self.seqno, # packet count
0x00,
0x01,
0x00,
0x01,
0x01, # msg type: reset
0x00,
0xC8,
0x01,
0x00, # sign id
0x00,
0x01,
0x0F,
0x00, # reset msg
0x17, # crc follows
)
pkt += pack("<HB", checksum(pkt), 0x04)
self.tx(pkt)
def message(self, msg: Message, zoneName: str):
z = self.zones_list.get(zoneName)
if z == None:
@ -267,7 +304,7 @@ class NetBrite:
)
footer = pack("<HB", checksum(header + body), 0x04)
self.tx(pkt_escape(header + body + footer))
self.tx(header + body + footer)
# print(f"Sent message to zone {zoneName}")
def zones(self, zones: dict[str, Zone] | None = None):
@ -282,6 +319,22 @@ class NetBrite:
ztext = z.initial_text.parse_msg()
zlen = len(ztext)
rect: list[int] = list(z.rect)
if rect[0] > 254:
rect[0] = 254
if rect[1] > 254:
rect[1] = 254
if rect[0] >= rect[2]:
rect[2] = rect[0] + 1
if rect[1] >= rect[3]:
rect[3] = rect[1] + 1
z.rect = tuple(rect) # pyright: ignore[reportAttributeAccessIssue]
body = pack(
f"<4B B4B 3B BH 8B B 4B 4B H5B 10B 3B 20BH3B11B{zlen}s B",
0x0F, # Body start
@ -396,7 +449,7 @@ class NetBrite:
footer = pack("<HB", crc, 0x04)
self.tx(pkt_escape(header + body + footer))
self.tx(header + body + footer)
print(f"Sent zone {zname}")
self.seqno += 1