This commit is contained in:
flandre 2025-06-12 13:36:26 +08:00
parent b6115bbb18
commit 2111cb3f52
3 changed files with 41 additions and 31 deletions

View File

@ -1,4 +1,5 @@
import logging
from typing import Any, Callable
import zmq
from zmq import Context, Socket
@ -12,17 +13,17 @@ class BusClient:
def __init__(
self,
*msgs: type(Msg),
ctx=None,
pub=True,
sub=True,
conflare=False,
poller=False,
req_socket_str: str = None,
*msgs: type[Msg],
ctx: Context[zmq.Socket[bytes]] | None = None,
pub: bool = True,
sub: bool = True,
conflare: bool = False,
poller: bool = False,
req_socket_str: str | None = None,
):
self.sub: Socket = None
self.sub: Socket[bytes] | None = None
if ctx is None:
self.ctx: Context = zmq.Context()
self.ctx: Context[zmq.Socket[bytes]] = zmq.Context()
else:
self.ctx = ctx
if sub:
@ -40,7 +41,7 @@ class BusClient:
if pub:
self.pub = self.ctx.socket(zmq.PUB)
self.pub.connect(f"tcp://127.0.0.1:{self.fp}")
self.req_socket = None
self.req_socket: zmq.Socket[bytes] | None = None
if req_socket_str is not None:
self.poller_for_interrupt = zmq.Poller()
self.sub_for_interrupt = self.ctx.socket(zmq.SUB)
@ -56,10 +57,12 @@ class BusClient:
# todo fix poller
def recv(self):
assert self.sub
b = self.sub.recv()
return Msg.decode_msg(b)
def poll(self, timeout):
def poll(self, timeout: int):
assert self.sub
b = self.sub.poll(timeout)
if b != 0:
return self.recv()
@ -68,6 +71,7 @@ class BusClient:
return self.pub.send(msg.encode_msg())
async def recv_async(self):
assert self.sub
b = await self.sub.recv()
return Msg.decode_msg(b)
@ -78,12 +82,12 @@ class BusClient:
self,
data: bytes,
interrupt_name: str,
timeout=3000,
retry_times=114514,
cb_retry=None,
timeout: int = 3000,
retry_times: int = 114514,
cb_retry: Callable[..., Any] | None = None,
):
self.sub_for_interrupt.connect(f"tcp://127.0.0.1:{self.bp}")
assert self.req_socket
for _ in range(retry_times):
self.req_socket.send(data)
r = dict(self.poller_for_interrupt.poll(timeout))

View File

@ -29,7 +29,7 @@ logger = logging.getLogger(__name__)
class Adv(QMainWindow, Ui_MainWindow):
ee2 = QtCore.pyqtSignal("QByteArray")
camera_bytes_found = QtCore.pyqtSignal("QByteArray")
def __init__(self, p: Node, parent=None):
super(Adv, self).__init__(parent)
@ -48,10 +48,12 @@ class Adv(QMainWindow, Ui_MainWindow):
self.zoom = 1.0
self.need_fit = False
self.frame1: RfFrameFile | None = None
self.ee2.connect(self.draw1)
threading.Thread(target=self.e1, daemon=True).start()
self.camera_bytes_found.connect(self.on_camera_bytes)
threading.Thread(target=self.zip2bytes_thread, daemon=True).start()
def keyPressEvent(self, a0: QKeyEvent):
def keyPressEvent(self, a0: QKeyEvent | None) -> None:
if a0 is None:
return
t = a0.text()
match t:
case "m":
@ -72,8 +74,8 @@ class Adv(QMainWindow, Ui_MainWindow):
if a0.angleDelta().y() < 0:
self.zoom -= 0.1
def draw1(self, d: QByteArray):
image_data = d.data()
def on_camera_bytes(self, raw: QByteArray):
image_data = raw.data()
im = Image.open(io.BytesIO(image_data))
pixmap = QPixmap.fromImage(
QImage(im.tobytes(), im.size[0], im.size[1], QImage.Format.Format_RGB888)
@ -84,7 +86,7 @@ class Adv(QMainWindow, Ui_MainWindow):
self.s.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio
)
def e1(self):
def zip2bytes_thread(self):
last_frame = self.frame1
while True:
if last_frame != self.frame1 and self.frame1 is not None:
@ -92,7 +94,7 @@ class Adv(QMainWindow, Ui_MainWindow):
self.frame1.seq.path,
Path(self.frame1.filename).with_suffix(".avif").__str__(),
)
self.ee2.emit(b)
self.camera_bytes_found.emit(b)
last_frame = self.frame1
def on_zmq_event(self, raw_msg: QByteArray):

View File

@ -1,6 +1,7 @@
import datetime
import logging
from abc import abstractmethod
from typing import Any
import zmq
@ -16,11 +17,11 @@ class Node:
def __init__(
self,
enable_init=True,
level=logging.INFO,
conflare=False,
broker=False,
req=None,
enable_init: bool = True,
level: int = logging.INFO,
conflare: bool = False,
broker: bool = False,
req: str | None = None,
):
self.enable_init = enable_init
self.isalive = True
@ -50,7 +51,7 @@ class Node:
pass
def base_setup(self):
FORMAT = "[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s"
# FORMAT = "[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s"
FORMAT = '"%(pathname)s:%(lineno)d" [%(asctime)s.%(msecs)03d] %(levelname)s - %(message)s'
logging.basicConfig(
level=self.level,
@ -78,13 +79,16 @@ class Node:
self.context = zmq.Context()
if self.enable_init:
self.c = BusClient(
*([KillMsg, Msg1, Msg2] + self.topics),
KillMsg,
Msg1,
Msg2,
*self.topics,
poller=True,
conflare=self.conflare,
req_socket_str=self.req,
)
def __call__(self, *args, **kwargs):
def __call__(self, **kwargs: dict[str, Any]):
assert "software_config" in kwargs, self.__class__
flandre.C.copy_form(kwargs["software_config"])
self.setup()