style:
This commit is contained in:
parent
b6115bbb18
commit
2111cb3f52
@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from typing import Any, Callable
|
||||||
|
|
||||||
import zmq
|
import zmq
|
||||||
from zmq import Context, Socket
|
from zmq import Context, Socket
|
||||||
@ -12,17 +13,17 @@ class BusClient:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*msgs: type(Msg),
|
*msgs: type[Msg],
|
||||||
ctx=None,
|
ctx: Context[zmq.Socket[bytes]] | None = None,
|
||||||
pub=True,
|
pub: bool = True,
|
||||||
sub=True,
|
sub: bool = True,
|
||||||
conflare=False,
|
conflare: bool = False,
|
||||||
poller=False,
|
poller: bool = False,
|
||||||
req_socket_str: str = None,
|
req_socket_str: str | None = None,
|
||||||
):
|
):
|
||||||
self.sub: Socket = None
|
self.sub: Socket[bytes] | None = None
|
||||||
if ctx is None:
|
if ctx is None:
|
||||||
self.ctx: Context = zmq.Context()
|
self.ctx: Context[zmq.Socket[bytes]] = zmq.Context()
|
||||||
else:
|
else:
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
if sub:
|
if sub:
|
||||||
@ -40,7 +41,7 @@ class BusClient:
|
|||||||
if pub:
|
if pub:
|
||||||
self.pub = self.ctx.socket(zmq.PUB)
|
self.pub = self.ctx.socket(zmq.PUB)
|
||||||
self.pub.connect(f"tcp://127.0.0.1:{self.fp}")
|
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:
|
if req_socket_str is not None:
|
||||||
self.poller_for_interrupt = zmq.Poller()
|
self.poller_for_interrupt = zmq.Poller()
|
||||||
self.sub_for_interrupt = self.ctx.socket(zmq.SUB)
|
self.sub_for_interrupt = self.ctx.socket(zmq.SUB)
|
||||||
@ -56,10 +57,12 @@ class BusClient:
|
|||||||
# todo fix poller
|
# todo fix poller
|
||||||
|
|
||||||
def recv(self):
|
def recv(self):
|
||||||
|
assert self.sub
|
||||||
b = self.sub.recv()
|
b = self.sub.recv()
|
||||||
return Msg.decode_msg(b)
|
return Msg.decode_msg(b)
|
||||||
|
|
||||||
def poll(self, timeout):
|
def poll(self, timeout: int):
|
||||||
|
assert self.sub
|
||||||
b = self.sub.poll(timeout)
|
b = self.sub.poll(timeout)
|
||||||
if b != 0:
|
if b != 0:
|
||||||
return self.recv()
|
return self.recv()
|
||||||
@ -68,6 +71,7 @@ class BusClient:
|
|||||||
return self.pub.send(msg.encode_msg())
|
return self.pub.send(msg.encode_msg())
|
||||||
|
|
||||||
async def recv_async(self):
|
async def recv_async(self):
|
||||||
|
assert self.sub
|
||||||
b = await self.sub.recv()
|
b = await self.sub.recv()
|
||||||
return Msg.decode_msg(b)
|
return Msg.decode_msg(b)
|
||||||
|
|
||||||
@ -78,12 +82,12 @@ class BusClient:
|
|||||||
self,
|
self,
|
||||||
data: bytes,
|
data: bytes,
|
||||||
interrupt_name: str,
|
interrupt_name: str,
|
||||||
timeout=3000,
|
timeout: int = 3000,
|
||||||
retry_times=114514,
|
retry_times: int = 114514,
|
||||||
cb_retry=None,
|
cb_retry: Callable[..., Any] | None = None,
|
||||||
):
|
):
|
||||||
self.sub_for_interrupt.connect(f"tcp://127.0.0.1:{self.bp}")
|
self.sub_for_interrupt.connect(f"tcp://127.0.0.1:{self.bp}")
|
||||||
|
assert self.req_socket
|
||||||
for _ in range(retry_times):
|
for _ in range(retry_times):
|
||||||
self.req_socket.send(data)
|
self.req_socket.send(data)
|
||||||
r = dict(self.poller_for_interrupt.poll(timeout))
|
r = dict(self.poller_for_interrupt.poll(timeout))
|
||||||
|
|||||||
@ -29,7 +29,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class Adv(QMainWindow, Ui_MainWindow):
|
class Adv(QMainWindow, Ui_MainWindow):
|
||||||
ee2 = QtCore.pyqtSignal("QByteArray")
|
camera_bytes_found = QtCore.pyqtSignal("QByteArray")
|
||||||
|
|
||||||
def __init__(self, p: Node, parent=None):
|
def __init__(self, p: Node, parent=None):
|
||||||
super(Adv, self).__init__(parent)
|
super(Adv, self).__init__(parent)
|
||||||
@ -48,10 +48,12 @@ class Adv(QMainWindow, Ui_MainWindow):
|
|||||||
self.zoom = 1.0
|
self.zoom = 1.0
|
||||||
self.need_fit = False
|
self.need_fit = False
|
||||||
self.frame1: RfFrameFile | None = None
|
self.frame1: RfFrameFile | None = None
|
||||||
self.ee2.connect(self.draw1)
|
self.camera_bytes_found.connect(self.on_camera_bytes)
|
||||||
threading.Thread(target=self.e1, daemon=True).start()
|
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()
|
t = a0.text()
|
||||||
match t:
|
match t:
|
||||||
case "m":
|
case "m":
|
||||||
@ -72,8 +74,8 @@ class Adv(QMainWindow, Ui_MainWindow):
|
|||||||
if a0.angleDelta().y() < 0:
|
if a0.angleDelta().y() < 0:
|
||||||
self.zoom -= 0.1
|
self.zoom -= 0.1
|
||||||
|
|
||||||
def draw1(self, d: QByteArray):
|
def on_camera_bytes(self, raw: QByteArray):
|
||||||
image_data = d.data()
|
image_data = raw.data()
|
||||||
im = Image.open(io.BytesIO(image_data))
|
im = Image.open(io.BytesIO(image_data))
|
||||||
pixmap = QPixmap.fromImage(
|
pixmap = QPixmap.fromImage(
|
||||||
QImage(im.tobytes(), im.size[0], im.size[1], QImage.Format.Format_RGB888)
|
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
|
self.s.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio
|
||||||
)
|
)
|
||||||
|
|
||||||
def e1(self):
|
def zip2bytes_thread(self):
|
||||||
last_frame = self.frame1
|
last_frame = self.frame1
|
||||||
while True:
|
while True:
|
||||||
if last_frame != self.frame1 and self.frame1 is not None:
|
if last_frame != self.frame1 and self.frame1 is not None:
|
||||||
@ -92,7 +94,7 @@ class Adv(QMainWindow, Ui_MainWindow):
|
|||||||
self.frame1.seq.path,
|
self.frame1.seq.path,
|
||||||
Path(self.frame1.filename).with_suffix(".avif").__str__(),
|
Path(self.frame1.filename).with_suffix(".avif").__str__(),
|
||||||
)
|
)
|
||||||
self.ee2.emit(b)
|
self.camera_bytes_found.emit(b)
|
||||||
last_frame = self.frame1
|
last_frame = self.frame1
|
||||||
|
|
||||||
def on_zmq_event(self, raw_msg: QByteArray):
|
def on_zmq_event(self, raw_msg: QByteArray):
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import zmq
|
import zmq
|
||||||
|
|
||||||
@ -16,11 +17,11 @@ class Node:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
enable_init=True,
|
enable_init: bool = True,
|
||||||
level=logging.INFO,
|
level: int = logging.INFO,
|
||||||
conflare=False,
|
conflare: bool = False,
|
||||||
broker=False,
|
broker: bool = False,
|
||||||
req=None,
|
req: str | None = None,
|
||||||
):
|
):
|
||||||
self.enable_init = enable_init
|
self.enable_init = enable_init
|
||||||
self.isalive = True
|
self.isalive = True
|
||||||
@ -50,7 +51,7 @@ class Node:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def base_setup(self):
|
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'
|
FORMAT = '"%(pathname)s:%(lineno)d" [%(asctime)s.%(msecs)03d] %(levelname)s - %(message)s'
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=self.level,
|
level=self.level,
|
||||||
@ -78,13 +79,16 @@ class Node:
|
|||||||
self.context = zmq.Context()
|
self.context = zmq.Context()
|
||||||
if self.enable_init:
|
if self.enable_init:
|
||||||
self.c = BusClient(
|
self.c = BusClient(
|
||||||
*([KillMsg, Msg1, Msg2] + self.topics),
|
KillMsg,
|
||||||
|
Msg1,
|
||||||
|
Msg2,
|
||||||
|
*self.topics,
|
||||||
poller=True,
|
poller=True,
|
||||||
conflare=self.conflare,
|
conflare=self.conflare,
|
||||||
req_socket_str=self.req,
|
req_socket_str=self.req,
|
||||||
)
|
)
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, **kwargs: dict[str, Any]):
|
||||||
assert "software_config" in kwargs, self.__class__
|
assert "software_config" in kwargs, self.__class__
|
||||||
flandre.C.copy_form(kwargs["software_config"])
|
flandre.C.copy_form(kwargs["software_config"])
|
||||||
self.setup()
|
self.setup()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user