Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
71e89ed5e6 | |||
0721edd431 | |||
2ae520f6b3 | |||
33715cb34e | |||
aefa4275d4 | |||
aca30088b0 |
5 changed files with 609 additions and 8 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
.direnv
|
||||
__pycache__/
|
||||
devices.db
|
||||
|
|
418
app.py
Normal file
418
app.py
Normal file
|
@ -0,0 +1,418 @@
|
|||
from __future__ import annotations
|
||||
from contextlib import asynccontextmanager
|
||||
from time import sleep
|
||||
from typing import Annotated
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from sqlmodel import SQLModel, Session, create_engine, select
|
||||
|
||||
import netbrite as nb
|
||||
from db import (
|
||||
MessageDB,
|
||||
MessageUpdate,
|
||||
NetBriteBase,
|
||||
NetBriteDB,
|
||||
NetBritePublic,
|
||||
NetBriteUpdate,
|
||||
ZoneBase,
|
||||
ZoneDB,
|
||||
ZonePublic,
|
||||
ZoneUpdate,
|
||||
)
|
||||
|
||||
DB_URL = "sqlite:///devices.db"
|
||||
engine = create_engine(DB_URL, connect_args={"check_same_thread": False})
|
||||
|
||||
|
||||
def get_session():
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
SessionDep = Annotated[Session, Depends(get_session)]
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
SQLModel.metadata.create_all(engine)
|
||||
load_devices_from_db()
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
active_devices: dict[int, nb.NetBrite] = {}
|
||||
|
||||
|
||||
# ---------- helper ----------
|
||||
def load_devices_from_db() -> None:
|
||||
with Session(engine) as session:
|
||||
for device in session.exec(select(NetBriteDB)).all():
|
||||
load_device(device)
|
||||
|
||||
|
||||
def load_device(device: NetBriteDB):
|
||||
id = device.id or 0
|
||||
try:
|
||||
active_devices[id] = nb.NetBrite(device.address, device.port)
|
||||
|
||||
load_zones(device.zones, active_devices[id])
|
||||
except nb.NetbriteConnectionException as exc:
|
||||
print(f"Could not connect to {device.address}:{device.port} — {exc}")
|
||||
|
||||
|
||||
def load_zones_id(session: Session, device_id: int, net_dev: nb.NetBrite):
|
||||
statement = select(ZoneDB).where(ZoneDB.netbrite_id == device_id)
|
||||
zones = list(session.exec(statement))
|
||||
load_zones(zones, net_dev)
|
||||
|
||||
|
||||
def load_zones(zones_in: list[ZoneDB], net_dev: nb.NetBrite) -> None:
|
||||
zones: dict[str, nb.Zone] = {}
|
||||
messages: dict[str, nb.Message] = {}
|
||||
|
||||
for zone in zones_in:
|
||||
msg = zone.default_message
|
||||
|
||||
default_msg = (
|
||||
nb.Message(
|
||||
activation_delay=msg.activation_delay,
|
||||
display_delay=msg.display_delay,
|
||||
priority=msg.priority,
|
||||
text=msg.text,
|
||||
ttl=msg.ttl,
|
||||
)
|
||||
if msg
|
||||
else nb.Message(f"Zone {zone.name}")
|
||||
)
|
||||
|
||||
zones[zone.name] = nb.Zone(
|
||||
x=zone.x,
|
||||
y=zone.y,
|
||||
width=zone.width,
|
||||
height=zone.height,
|
||||
scroll_speed=zone.scroll_speed,
|
||||
pause_duration=zone.pause_duration,
|
||||
volume=zone.volume,
|
||||
default_font=zone.default_font,
|
||||
default_color=zone.default_color,
|
||||
initial_text=default_msg,
|
||||
)
|
||||
messages[zone.name] = default_msg
|
||||
# print(f"{zone.name}: {default_msg.text}")
|
||||
|
||||
if zones:
|
||||
net_dev.zones(zones)
|
||||
sleep(0.2)
|
||||
|
||||
for zone, message in messages.items():
|
||||
net_dev.message(message, zone)
|
||||
sleep(0.2)
|
||||
|
||||
|
||||
def create_default_zone(session: Session, device_id: int) -> None:
|
||||
zone = ZoneDB(
|
||||
name="0",
|
||||
netbrite_id=device_id,
|
||||
)
|
||||
msg = MessageDB(
|
||||
text="{erase}Welcome",
|
||||
)
|
||||
session.add(msg)
|
||||
session.add(zone)
|
||||
|
||||
session.commit()
|
||||
session.refresh(zone)
|
||||
session.refresh(msg)
|
||||
|
||||
zone.default_message_id = msg.id
|
||||
session.commit()
|
||||
|
||||
|
||||
# ---------- routes ----------
|
||||
@app.post("/api/devices", response_model=NetBritePublic)
|
||||
def create_device(device: NetBriteBase, session: SessionDep):
|
||||
if session.exec(
|
||||
select(NetBriteDB).where(NetBriteDB.address == device.address)
|
||||
).first():
|
||||
raise HTTPException(400, "Device already exists")
|
||||
|
||||
db_device = NetBriteDB.model_validate(device)
|
||||
session.add(db_device)
|
||||
session.commit()
|
||||
session.refresh(db_device)
|
||||
|
||||
create_default_zone(session, db_device.id or 0)
|
||||
|
||||
load_device(db_device)
|
||||
return db_device
|
||||
|
||||
|
||||
@app.get("/api/devices", response_model=list[NetBritePublic])
|
||||
def get_devices(session: SessionDep):
|
||||
devices: list[NetBritePublic] = []
|
||||
|
||||
for device in session.exec(select(NetBriteDB)).all():
|
||||
device = NetBritePublic.model_validate(
|
||||
device, update={"active": (device.id or 0) in active_devices}
|
||||
)
|
||||
devices.append(device)
|
||||
|
||||
return devices
|
||||
|
||||
|
||||
@app.post(
|
||||
"/api/devices/{device_id}",
|
||||
response_model=NetBritePublic,
|
||||
description="**NOTE**: this **WILL** disconnect the device. You'll have to manually reconnect it.",
|
||||
)
|
||||
def edit_device(device_id: int, updated_device: NetBriteUpdate, session: SessionDep):
|
||||
db_dev = session.get(NetBriteDB, device_id)
|
||||
if not db_dev:
|
||||
raise HTTPException(404, "Device not found")
|
||||
|
||||
if device_id in active_devices:
|
||||
try:
|
||||
active_devices[device_id].sock.close()
|
||||
except OSError:
|
||||
print("Failed to close socket.")
|
||||
|
||||
del active_devices[device_id]
|
||||
|
||||
dev_data = updated_device.model_dump(exclude_unset=True)
|
||||
_ = db_dev.sqlmodel_update(dev_data)
|
||||
|
||||
session.add(db_dev)
|
||||
session.commit()
|
||||
session.refresh(db_dev)
|
||||
|
||||
return db_dev
|
||||
|
||||
|
||||
@app.delete("/api/devices/{device_id}")
|
||||
def delete_device(device_id: int, session: SessionDep):
|
||||
db_dev = session.get(NetBriteDB, device_id)
|
||||
if not db_dev:
|
||||
raise HTTPException(404, "Device not found")
|
||||
|
||||
delete: list[MessageDB | ZoneDB | NetBriteDB] = [db_dev]
|
||||
for zone in db_dev.zones:
|
||||
if zone.default_message != None:
|
||||
delete.append(zone.default_message)
|
||||
delete.append(zone)
|
||||
|
||||
if device_id in active_devices:
|
||||
try:
|
||||
active_devices[device_id].sock.close()
|
||||
except OSError:
|
||||
print("Failed to close socket.")
|
||||
del active_devices[device_id]
|
||||
|
||||
for i in delete:
|
||||
session.delete(i)
|
||||
|
||||
session.commit()
|
||||
return 200
|
||||
|
||||
|
||||
@app.post("/api/devices/{device_id}/reconnect")
|
||||
def reconnect_device(device_id: int, session: SessionDep):
|
||||
db_dev = session.get(NetBriteDB, device_id)
|
||||
if not db_dev:
|
||||
raise HTTPException(404, "Device not found")
|
||||
|
||||
try:
|
||||
new_netbrite = nb.NetBrite(db_dev.address, db_dev.port)
|
||||
active_devices[device_id] = new_netbrite
|
||||
load_zones_id(session, device_id, active_devices[device_id])
|
||||
return 200
|
||||
except nb.NetbriteConnectionException as exc:
|
||||
raise HTTPException(400, str(exc))
|
||||
|
||||
|
||||
@app.post(
|
||||
"/api/devices/{device_id}/sync",
|
||||
description="**NOTE**: This will recreate the zones on the device.",
|
||||
)
|
||||
def sync_device(
|
||||
device_id: int,
|
||||
session: SessionDep,
|
||||
):
|
||||
if device_id not in active_devices:
|
||||
raise HTTPException(500, "Device not active, try reconnecting")
|
||||
|
||||
try:
|
||||
load_zones_id(session, device_id, active_devices[device_id])
|
||||
except nb.NetbriteTransferException:
|
||||
del active_devices[device_id]
|
||||
raise HTTPException(500, "Failed to send zones. Device inactive now.")
|
||||
|
||||
return 200
|
||||
|
||||
|
||||
@app.post(
|
||||
"/api/devices/{device_id}/restart",
|
||||
)
|
||||
def restart_device(
|
||||
device_id: int,
|
||||
):
|
||||
if device_id not in active_devices:
|
||||
raise HTTPException(500, "Device not active, try reconnecting")
|
||||
|
||||
try:
|
||||
active_devices[device_id].reboot()
|
||||
del active_devices[device_id]
|
||||
except nb.NetbriteTransferException:
|
||||
raise HTTPException(500, "Failed to send reboot command. Device inactive now.")
|
||||
|
||||
return 200
|
||||
|
||||
|
||||
@app.post(
|
||||
"/api/devices/{device_id}/zones",
|
||||
response_model=ZonePublic,
|
||||
description="**NOTE**: this does not update the device.",
|
||||
)
|
||||
def create_zone(device_id: int, new_zone: ZoneBase, session: SessionDep):
|
||||
device = session.get(NetBriteDB, device_id)
|
||||
if not device:
|
||||
raise HTTPException(404, "Device not found")
|
||||
|
||||
new_zone_data = new_zone.model_dump(exclude_unset=True)
|
||||
extra_data = {"netbrite_id": device_id}
|
||||
zone = ZoneDB.model_validate(new_zone_data, update=extra_data)
|
||||
|
||||
msg = MessageDB(
|
||||
text="{erase}Welcome",
|
||||
)
|
||||
session.add(zone)
|
||||
session.add(msg)
|
||||
|
||||
session.commit()
|
||||
session.refresh(zone)
|
||||
session.refresh(msg)
|
||||
|
||||
zone.default_message_id = msg.id
|
||||
session.commit()
|
||||
|
||||
return zone
|
||||
|
||||
|
||||
@app.get("/api/devices/{device_id}/zones", response_model=list[ZonePublic])
|
||||
def get_zones(device_id: int, session: SessionDep):
|
||||
device = session.get(NetBriteDB, device_id)
|
||||
if not device:
|
||||
raise HTTPException(404, "Device not found")
|
||||
|
||||
return device.zones
|
||||
|
||||
|
||||
@app.delete(
|
||||
"/api/zone/{zone_id}",
|
||||
description="**NOTE**: this does not update the device.",
|
||||
)
|
||||
def delete_zone(
|
||||
zone_id: int,
|
||||
session: SessionDep,
|
||||
):
|
||||
zone = session.get(ZoneDB, zone_id)
|
||||
if not zone:
|
||||
raise HTTPException(404, "Zone not found")
|
||||
|
||||
message = zone.default_message
|
||||
if message:
|
||||
session.delete(message)
|
||||
|
||||
session.delete(zone)
|
||||
session.commit()
|
||||
|
||||
return 200
|
||||
|
||||
|
||||
@app.post(
|
||||
"/api/zone/{zone_id}",
|
||||
response_model=ZonePublic,
|
||||
description="**NOTE**: this does not update the device.",
|
||||
)
|
||||
def edit_zone(zone_id: int, zone: ZoneUpdate, session: SessionDep):
|
||||
zone_db = session.get(ZoneDB, zone_id)
|
||||
|
||||
if not zone_db:
|
||||
raise HTTPException(404, "Zone not found")
|
||||
|
||||
zone_update_data = zone.model_dump(exclude_unset=True)
|
||||
print(zone_update_data)
|
||||
_ = zone_db.sqlmodel_update(zone_update_data)
|
||||
|
||||
session.add(zone_db)
|
||||
session.commit()
|
||||
session.refresh(zone_db)
|
||||
|
||||
return zone_db
|
||||
|
||||
|
||||
@app.post(
|
||||
"/api/zone/{zone_id}/message",
|
||||
response_model=ZonePublic,
|
||||
description="**NOTE**: this does not update the device.",
|
||||
)
|
||||
def edit_message(zone_id: int, message: MessageUpdate, session: SessionDep):
|
||||
zone_db = session.get(ZoneDB, zone_id)
|
||||
|
||||
if not zone_db:
|
||||
raise HTTPException(404, "Zone not found")
|
||||
|
||||
if not zone_db.default_message:
|
||||
db_message = MessageDB.model_validate(message)
|
||||
|
||||
session.add(db_message)
|
||||
session.commit()
|
||||
session.refresh(db_message)
|
||||
|
||||
zone_db.default_message_id = db_message.id
|
||||
session.add(zone_db)
|
||||
session.commit()
|
||||
session.refresh(zone_db)
|
||||
else:
|
||||
db_message = zone_db.default_message
|
||||
data = message.model_dump(
|
||||
exclude_unset=True,
|
||||
)
|
||||
_ = db_message.sqlmodel_update(data, update={"id": db_message.id})
|
||||
|
||||
session.add(db_message)
|
||||
session.commit()
|
||||
session.refresh(zone_db)
|
||||
|
||||
return zone_db
|
||||
|
||||
|
||||
@app.post(
|
||||
"/api/zone/{zone_id}/adhoc_message",
|
||||
description="**NOTE**: this updates the device temporarily. Edit the zone message for permanent changes.",
|
||||
)
|
||||
def adhoc_message(zone_id: int, message: MessageUpdate, session: SessionDep):
|
||||
zone_db = session.get(ZoneDB, zone_id)
|
||||
|
||||
if not zone_db:
|
||||
raise HTTPException(404, "Zone not found")
|
||||
|
||||
device_id = zone_db.netbrite_id
|
||||
if not device_id in active_devices:
|
||||
raise HTTPException(500, "Device inactive")
|
||||
|
||||
nb_message = nb.Message(
|
||||
text=message.text or "",
|
||||
activation_delay=message.activation_delay or 0,
|
||||
display_delay=message.display_delay or 0,
|
||||
display_repeat=message.display_repeat or 0,
|
||||
priority=message.priority or nb.Priorities.OVERRIDE,
|
||||
sound_alarm=message.sound_alarm or False,
|
||||
ttl=message.ttl or 0,
|
||||
)
|
||||
|
||||
try:
|
||||
active_devices[device_id].message(nb_message, zone_db.name)
|
||||
except nb.NetbriteTransferException:
|
||||
del active_devices[device_id]
|
||||
raise HTTPException(500, "Failed to send message. Device inactive now.")
|
||||
|
||||
return 200
|
111
db.py
Normal file
111
db.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
# from __future__ import annotations
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
from netbrite import Colors, Fonts, Priorities, ScrollSpeeds, Message
|
||||
|
||||
MAX_WIDTH = 120
|
||||
MAX_HEIGHT = 7
|
||||
|
||||
|
||||
# --- Message ---
|
||||
class MessageBase(SQLModel):
|
||||
text: str = ""
|
||||
activation_delay: int = 0
|
||||
display_delay: int = 0
|
||||
display_repeat: int = 0
|
||||
priority: Priorities = Priorities.OVERRIDE
|
||||
sound_alarm: bool = False
|
||||
ttl: int = 0
|
||||
|
||||
|
||||
class MessageUpdate(SQLModel):
|
||||
text: str | None = ""
|
||||
activation_delay: int | None = 0
|
||||
display_delay: int | None = 0
|
||||
display_repeat: int | None = 0
|
||||
priority: Priorities | None = Priorities.OVERRIDE
|
||||
sound_alarm: bool = False
|
||||
ttl: int | None = 0
|
||||
|
||||
|
||||
class MessageDB(MessageBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
zone: "ZoneDB" = Relationship( # pyright: ignore[reportAny]
|
||||
back_populates="default_message"
|
||||
)
|
||||
|
||||
|
||||
class MessagePublic(MessageBase):
|
||||
id: int
|
||||
|
||||
|
||||
# --- Device ---
|
||||
class NetBriteBase(SQLModel):
|
||||
address: str = Field(unique=True, index=True)
|
||||
port: int = 700
|
||||
|
||||
|
||||
class NetBriteUpdate(SQLModel):
|
||||
address: str = Field(unique=True, index=True)
|
||||
port: int = 700
|
||||
|
||||
|
||||
class NetBriteDB(NetBriteBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
zones: list["ZoneDB"] = Relationship( # pyright: ignore[reportAny]
|
||||
back_populates="netbrite"
|
||||
)
|
||||
|
||||
|
||||
class NetBritePublic(NetBriteBase):
|
||||
id: int
|
||||
zones: list["ZoneDB"]
|
||||
active: bool
|
||||
|
||||
|
||||
# --- Zone ---
|
||||
class ZoneBase(SQLModel):
|
||||
name: str
|
||||
x: int = 0
|
||||
y: int = 0
|
||||
width: int = MAX_WIDTH
|
||||
height: int = MAX_HEIGHT
|
||||
scroll_speed: ScrollSpeeds = ScrollSpeeds.NORMAL
|
||||
pause_duration: int = 1000
|
||||
volume: int = 4
|
||||
default_font: Fonts = Fonts.NORMAL_7
|
||||
default_color: Colors = Colors.RED
|
||||
|
||||
|
||||
class ZoneUpdate(SQLModel):
|
||||
name: str | None = None
|
||||
x: int | None = None
|
||||
y: int | None = None
|
||||
width: int | None = None
|
||||
height: int | None = None
|
||||
scroll_speed: ScrollSpeeds | None = ScrollSpeeds.NORMAL
|
||||
pause_duration: int | None = 1000
|
||||
volume: int | None = 4
|
||||
default_font: Fonts | None = Fonts.NORMAL_7
|
||||
default_color: Colors | None = Colors.RED
|
||||
|
||||
|
||||
class ZoneDBBase(ZoneBase):
|
||||
default_message_id: int | None = Field(default=None, foreign_key="messagedb.id")
|
||||
netbrite_id: int = Field(default=None, foreign_key="netbritedb.id")
|
||||
|
||||
|
||||
class ZoneDB(ZoneDBBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
default_message: MessageDB | None = Relationship( # pyright: ignore[reportAny]
|
||||
back_populates="zone"
|
||||
)
|
||||
netbrite: NetBriteDB = Relationship( # pyright: ignore[reportAny]
|
||||
back_populates="zones"
|
||||
)
|
||||
|
||||
|
||||
class ZonePublic(ZoneDBBase):
|
||||
id: int
|
||||
default_message: MessagePublic
|
||||
# netbrite: NetBritePublic
|
|
@ -15,7 +15,7 @@
|
|||
nativeBuildInputs = [
|
||||
pkgs.entr
|
||||
pkgs.fastapi-cli
|
||||
(pkgs.python3.withPackages (x: [x.crc x.fastapi]))
|
||||
(pkgs.python3.withPackages (x: [x.crc x.fastapi x.sqlmodel x.sqlalchemy]))
|
||||
];
|
||||
};
|
||||
};
|
||||
|
|
85
netbrite.py
85
netbrite.py
|
@ -1,3 +1,4 @@
|
|||
import select
|
||||
from typing import Callable
|
||||
from crc import Calculator, Crc16
|
||||
from enum import Enum
|
||||
|
@ -10,6 +11,14 @@ import re
|
|||
DEFAULT_PORT = 700
|
||||
|
||||
|
||||
class NetbriteConnectionException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class NetbriteTransferException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Colors(Enum):
|
||||
RED = 0x01
|
||||
GREEN = 0x02
|
||||
|
@ -55,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")\}"
|
||||
|
||||
|
@ -101,7 +117,7 @@ class Message:
|
|||
(rb"\{right\}", b"\x10\x28"),
|
||||
(rb"\{pause\}", b"\x10\x05"),
|
||||
(rb"\{erase\}", b"\x10\x03"),
|
||||
(rb"\{serial\}", b"\x10\x09"),
|
||||
(rb"\{serialnum\}", b"\x10\x09"),
|
||||
(rb"\{bell\}", b"\x10\x05"),
|
||||
(rb"\{red\}", b"\x10\x0c" + pack("B", Colors.RED.value)),
|
||||
(rb"\{green\}", b"\x10\x0c" + pack("B", Colors.GREEN.value)),
|
||||
|
@ -183,16 +199,55 @@ class NetBrite:
|
|||
|
||||
try:
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sock.settimeout(5000)
|
||||
self.sock.settimeout(2)
|
||||
self.connect()
|
||||
except OSError as e:
|
||||
raise ConnectionError(f"Error while opening network socket. {e}")
|
||||
raise NetbriteConnectionException(
|
||||
f"Error while opening network socket. {e}"
|
||||
)
|
||||
|
||||
def connect(self):
|
||||
self.sock.connect((self.address, self.port))
|
||||
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(
|
||||
f"Error while opening network socket. {e}"
|
||||
)
|
||||
|
||||
def tx(self, pkt: bytes):
|
||||
_ = self.sock.send(pkt)
|
||||
try:
|
||||
_ = 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)
|
||||
|
@ -249,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):
|
||||
|
@ -264,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
|
||||
|
@ -378,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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue