flandre/src/nodes/Loader.py

49 lines
1.6 KiB
Python
Raw Normal View History

2025-01-19 18:27:26 +08:00
import io
import logging
import struct
from pathlib import Path
2025-01-06 11:21:04 +08:00
import zmq
2025-01-19 18:27:26 +08:00
from config import PLAYBACK_SOCKET_PORT
2025-01-06 11:21:04 +08:00
from nodes.Node import Node
2025-01-19 19:05:56 +08:00
from utils.Msg import MoveAxisMsg, KillMsg, SelectSeqMsg, SeqMetaMsg, SeqIdMinMax, SetBaseMsg, SeqListMsg
from utils.RfFile import RfSequence
2025-01-06 11:21:04 +08:00
2025-01-19 18:27:26 +08:00
logger = logging.getLogger(__name__)
2025-01-06 11:21:04 +08:00
class Loader(Node):
2025-01-19 18:27:26 +08:00
topics = [MoveAxisMsg, SelectSeqMsg, SetBaseMsg]
2025-01-06 11:21:04 +08:00
def loop(self):
s2 = self.context.socket(zmq.PUSH)
2025-01-19 18:27:26 +08:00
s2.bind(f"tcp://*:{PLAYBACK_SOCKET_PORT}")
# base = Path('/mnt/16T/private_dataset/us/')
base: Path | None = None
2025-01-06 11:21:04 +08:00
2025-01-19 18:27:26 +08:00
rff = None
2025-01-06 11:21:04 +08:00
while True:
r = self.recv()
2025-01-19 18:27:26 +08:00
if isinstance(r, MoveAxisMsg) and rff is not None:
logger.debug(f'Move axis: {rff}')
for frame in rff.frames:
if frame.meta.sequence_id == r.value:
buffer = io.BytesIO()
buffer.write(struct.pack('=iqi', 114514, frame.meta.sequence_id, frame.meta.encoder))
buffer.write(frame.bytes)
s2.send(buffer.getvalue())
elif isinstance(r, SelectSeqMsg):
if base is None:
continue
rff = RfSequence.from_folder(base / r.value)
self.send(SeqMetaMsg(rff.meta.name))
self.send(SeqIdMinMax(*rff.seq_id_minmax))
elif isinstance(r, SetBaseMsg):
base = Path(r.value)
self.send(SeqListMsg([f.name for f in base.glob('*')]))
2025-01-06 11:21:04 +08:00
elif isinstance(r, KillMsg):
if r.name == '':
break