From 20539edf5ca33aceb455746e0c22967902327895 Mon Sep 17 00:00:00 2001 From: flandre Date: Sat, 12 Apr 2025 14:36:23 +0800 Subject: [PATCH] fix config --- flandre/config.py | 36 ++++++++++++++++++++++++++---------- flandre/nodes/Loader.py | 4 ++-- flandre/nodes/MainUI.py | 6 +++--- flandre/nodes/Mi.py | 30 ++++++++++++------------------ flandre/utils/mi.py | 10 +++------- flandre/utils/network.py | 6 +++++- 6 files changed, 51 insertions(+), 41 deletions(-) diff --git a/flandre/config.py b/flandre/config.py index 8eb1691..0e56b40 100644 --- a/flandre/config.py +++ b/flandre/config.py @@ -11,16 +11,6 @@ ISDEV = False if '--dev' in sys.argv: ISDEV = True -PLAYBACK_SOCKET_PORT = 5003 -PLAYBACK_SOCKET = f'127.0.0.1:{PLAYBACK_SOCKET_PORT}' -MI_REP_SOCKET_PORT = 5557 -DEVICE_PY_REP_SOCKET_PORT = 5558 - -SWITCH1_IP = 'c1' -SWITCH1_TOKEN = '7ad51e0016e7a9d22f753d5110f76c7d' -SWITCH2_IP = 'c2' -SWITCH2_TOKEN = 'bf5a7b77a1ba3761ea63fafd8427b7d6' - CONFIG_FOLDER = platformdirs.user_config_path('Flandre', 'Scarlet') if ISDEV: @@ -34,6 +24,9 @@ SOFTWARE_CONFIG_PATH = CONFIG_FOLDER / 'software.json' @dataclasses.dataclass class SoftwareConfig: + def s(self, host, port, proto='tcp'): + return f'{proto}://{host}:{port}' + data_folder: Path = DS @property @@ -54,6 +47,7 @@ class SoftwareConfig: live_ip: str = '11.6.1.71' live_push_port: int = 5555 live_rep_port: int = 5556 + device_py_rep_port: int = 5558 @property def live_push_socket(self): @@ -67,6 +61,28 @@ class SoftwareConfig: def live_rep_socket_http(self): return f'http://{self.live_ip}:{self.live_rep_port}' + @property + def device_py_rep_socket(self): + return f'tcp://{self.live_ip}:{self.device_py_rep_port}' + + playback_port: int = 5003 + + @property + def playback_socket(self): + return f'tcp://127.0.0.1:{self.playback_port}' + + mi_rep_port: int = 5557 + local_ip: str = '127.0.0.1' + + @property + def mi_rep_socket(self): + return self.s(self.local_ip, self.device_py_rep_port) + + switch1_ip: str = 'c1' + switch1_token: str = '7ad51e0016e7a9d22f753d5110f76c7d' + switch2_ip: str = 'c2' + switch2_token: str = 'bf5a7b77a1ba3761ea63fafd8427b7d6' + @staticmethod def read_config(path: Path): j = json.loads(path.read_text(encoding='utf-8')) diff --git a/flandre/nodes/Loader.py b/flandre/nodes/Loader.py index 61b02fa..186bad0 100644 --- a/flandre/nodes/Loader.py +++ b/flandre/nodes/Loader.py @@ -5,7 +5,7 @@ from pathlib import Path import zmq -from flandre.config import PLAYBACK_SOCKET_PORT, C +from flandre.config import C from flandre.nodes.Node import Node from flandre.utils.Msg import MoveAxisMsg, KillMsg, SetSeqMetaMsg, SeqIdMinMax, SetBaseMsg, SeqListMsg, SeqIdList, \ SetSidMsg, RfFrameWithMetaMsg @@ -19,7 +19,7 @@ class Loader(Node): def loop(self): playback_socket = self.context.socket(zmq.PUSH) - playback_socket.bind(f"tcp://*:{PLAYBACK_SOCKET_PORT}") + playback_socket.bind(f"tcp://*:{C.playback_port}") # base = Path('/mnt/16T/private_dataset/us/') base: Path | None = None diff --git a/flandre/nodes/MainUI.py b/flandre/nodes/MainUI.py index a10c120..b1be164 100644 --- a/flandre/nodes/MainUI.py +++ b/flandre/nodes/MainUI.py @@ -14,7 +14,7 @@ from PyQt6.QtWidgets import QMainWindow, QApplication, QFrame, QMessageBox, QFil from flandre import ASSETS from flandre.pyqt.Main import Ui_MainWindow from flandre.pyqt.ZMQReceiver import ZMQReceiver -from flandre.config import DS, C, MI_REP_SOCKET_PORT +from flandre.config import DS, C from flandre.nodes.Node import Node from flandre.utils.Msg import KillMsg, Msg, ImageArgMsg, SeqIdMinMax, MoveAxisMsg, SeqListMsg, SetBaseMsg, \ SetSeqMetaMsg, SetPlayMode, DeviceConnectedMsg, DeviceEnabledMsg, DeviceOnlineMsg, SetDeviceEnabledMsg, \ @@ -134,7 +134,7 @@ class Adv(QMainWindow, Ui_MainWindow): self.b_us_refresh.clicked.connect(self.on_deivce_refresh) self.mi_req_socket = zmq.Context().socket(zmq.REQ) - self.mi_req_socket.connect(f"tcp://127.0.0.1:{MI_REP_SOCKET_PORT}") + self.mi_req_socket.connect(C.mi_rep_socket) def on_deivce_switch(self): match self.device_switch_state: @@ -542,4 +542,4 @@ class MainUI(Node): MainWindow.show() app.exec() except Exception as e: - print(e) + traceback.print_exception(e) diff --git a/flandre/nodes/Mi.py b/flandre/nodes/Mi.py index 802e9bf..75e4a8c 100644 --- a/flandre/nodes/Mi.py +++ b/flandre/nodes/Mi.py @@ -1,25 +1,20 @@ import logging -import struct -import subprocess import time from threading import Thread import zmq -from config import LIVE_REP_SOCKET, CONFIG, DEVICE_CONFIG, MI_REP_SOCKET_PORT, DEVICE_PY_REP_SOCKET_PORT, LIVE_SOCKET_IP -from nodes.Node import Node -from utils.Msg import ImageArgMsg, KillMsg, SetDeviceConnectedMsg, SetDeviceEnabledMsg, DeviceEnabledMsg, \ - DeviceConnectedMsg, SetDeviceConfigMsg, DeviceOnlineMsg, DeviceConfigListMsg, RequestRfFrameMsg, RfFrameMsg, \ - RfFrameWithMetaMsg, DeviceZero, SetDeviceSwitchMsg, DeviceSwitchMsg, RefreshDeviceMsg -from utils.RfMeta import RfFrameMeta -from utils.mi import c1_connect, c1_connected, c1_disconnect -from utils.network import check_port +from flandre.config import C +from flandre.nodes.Node import Node +from flandre.utils.Msg import KillMsg, SetDeviceSwitchMsg, DeviceSwitchMsg, RefreshDeviceMsg +from flandre.utils.mi import c1_connect, c1_connected, c1_disconnect +from flandre.utils.network import check_port, check_socket logger = logging.getLogger(__name__) class Mi(Node): - topics = [SetDeviceSwitchMsg,RefreshDeviceMsg] + topics = [SetDeviceSwitchMsg, RefreshDeviceMsg] def __init__(self, level=logging.INFO): super(Mi, self).__init__(level=level) @@ -28,16 +23,16 @@ class Mi(Node): def custom_setup(self): self.mi_rep_socket = self.context.socket(zmq.REP) - self.mi_rep_socket.bind(f"tcp://*:{MI_REP_SOCKET_PORT}") + self.mi_rep_socket.bind(f"tcp://*:{C.mi_rep_port}") self.device_py_req_socket = self.context.socket(zmq.REQ) - self.device_py_req_socket.connect(f"tcp://{LIVE_SOCKET_IP}:{DEVICE_PY_REP_SOCKET_PORT}") + self.device_py_req_socket.connect(C.device_py_rep_socket) self.device_py_req_socket1s = self.context.socket(zmq.REQ) - self.device_py_req_socket1s.connect(f"tcp://{LIVE_SOCKET_IP}:{DEVICE_PY_REP_SOCKET_PORT}") + self.device_py_req_socket1s.connect(C.device_py_rep_socket) self.device_py_req_socket60s = self.context.socket(zmq.REQ) - self.device_py_req_socket60s.connect(f"tcp://{LIVE_SOCKET_IP}:{DEVICE_PY_REP_SOCKET_PORT}") + self.device_py_req_socket60s.connect(C.device_py_rep_socket) self.ping1s_thread = Thread(target=self.ping1s) self.ping1s_thread.start() @@ -80,7 +75,7 @@ class Mi(Node): time.sleep(10) def loop(self): - if c1_connected() and check_port(LIVE_SOCKET_IP, DEVICE_PY_REP_SOCKET_PORT): + if c1_connected() and check_socket(C.device_py_rep_socket): self.device_py_req_socket.send(b'get_device_status') r = self.device_py_req_socket.recv() if r == b'on': @@ -121,11 +116,10 @@ class Mi(Node): self.device_py_req_socket.send(b'kill') self.device_py_req_socket.recv() self.send(DeviceSwitchMsg('RED')) - elif isinstance(msg,RefreshDeviceMsg): + elif isinstance(msg, RefreshDeviceMsg): self.device_py_req_socket.send(b'kill') self.device_py_req_socket.recv() self.device_py_req_socket.send(b'start') self.device_py_req_socket.recv() self.send(DeviceSwitchMsg('GREEN')) self.ping60enable = True - diff --git a/flandre/utils/mi.py b/flandre/utils/mi.py index d24709a..db1ff79 100644 --- a/flandre/utils/mi.py +++ b/flandre/utils/mi.py @@ -1,19 +1,15 @@ import click from miio.miioprotocol import MiIOProtocol -from flandre.config import SWITCH1_IP, SWITCH1_TOKEN, SWITCH2_IP, SWITCH2_TOKEN +from flandre.config import C def c1(): - return MiIOProtocol( - SWITCH1_IP, SWITCH1_TOKEN, - ) + return MiIOProtocol(C.switch1_ip, C.switch1_token) def c2(): - return MiIOProtocol( - SWITCH2_IP, SWITCH2_TOKEN, - ) + return MiIOProtocol(C.switch2_ip, C.switch2_token) def c1_set_connect(status: bool): diff --git a/flandre/utils/network.py b/flandre/utils/network.py index 1173a79..74b5fb6 100644 --- a/flandre/utils/network.py +++ b/flandre/utils/network.py @@ -16,6 +16,10 @@ def check_port(host, port): print(f"连接过程中发生错误: {e}") return False + +def check_socket(socket: str): + host, port = socket.split('//')[-1].split(':') + return check_port(host, int(port)) # def c2(): # code = subprocess.run(['curl', '-m', '1', f'http://{LIVE_REP_SOCKET}'], stderr=subprocess.DEVNULL, # stdout=subprocess.DEVNULL).returncode @@ -26,4 +30,4 @@ def check_port(host, port): # return False # case _: # self.send(DeviceOnlineMsg(True)) -# return True \ No newline at end of file +# return True