From 31f5b1cfa23a8a807488c93b18a0cb2c76c1de25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Wed, 14 Jan 2026 21:03:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/config_manager.py | 10 ---------- xiaomusic/file_watcher.py | 13 +++++++++++-- xiaomusic/xiaomusic.py | 20 +++++++++----------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/xiaomusic/config_manager.py b/xiaomusic/config_manager.py index 8bb17e7..dfe4443 100644 --- a/xiaomusic/config_manager.py +++ b/xiaomusic/config_manager.py @@ -88,20 +88,10 @@ class ConfigManager: Args: data: 配置数据字典 - - Returns: - dict: 更新前的配置快照(用于检测变化) """ - # 保存更新前的配置快照 - snapshot = { - "enable_file_watch": self.config.enable_file_watch, - } - # 自动赋值相同字段的配置 self.config.update_config(data) - return snapshot - def get_config(self): """获取当前配置 diff --git a/xiaomusic/file_watcher.py b/xiaomusic/file_watcher.py index 7d36919..f6dc1a1 100644 --- a/xiaomusic/file_watcher.py +++ b/xiaomusic/file_watcher.py @@ -107,11 +107,17 @@ class FileWatcherManager: self._file_watch_handler = None def start(self, loop): - """启动文件监控 + """启动文件监控(支持重入) + + 如果监控器已在运行,会先停止再重新启动。 Args: loop: asyncio 事件循环对象 """ + # 如果已经在运行,先停止 + if self._observer: + self.stop() + if not self.config.enable_file_watch: self.log.info("目录监控功能已关闭") return @@ -136,7 +142,10 @@ class FileWatcherManager: self.log.info(f"已启动对 {self.music_path} 的目录监控。") def stop(self): - """停止文件监控""" + """停止文件监控(支持重入) + + 如果监控器未运行,不做任何操作。 + """ if self._observer: self._observer.stop() self._observer.join() diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index 7607ce3..59d0df6 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -952,9 +952,8 @@ class XiaoMusic: def update_config_from_setting(self, data): """从设置更新配置""" - # 委托给 config_manager 更新配置,并获取更新前的快照 - snapshot = self._config_manager.update_config(data) - pre_efw = snapshot["enable_file_watch"] + # 委托给 config_manager 更新配置 + self._config_manager.update_config(data) # 重新初始化配置相关的属性 self.init_config() @@ -966,14 +965,13 @@ class XiaoMusic: self.log.info(f"语音控制已启动, 用【{joined_keywords}】开头来控制") self.log.debug(f"key_word_dict: {self.config.key_word_dict}") - # 检查 enable_file_watch 配置是否发生变化 - now_efw = self.config.enable_file_watch - if pre_efw != now_efw: - self.log.info("配置更新:{}目录监控".format("开启" if now_efw else "关闭")) - if now_efw: - self.start_file_watch() - else: - self.stop_file_watch() + # 根据新配置控制文件监控 + if self.config.enable_file_watch: + self.log.info("配置更新:开启目录监控") + self.start_file_watch() + else: + self.log.info("配置更新:关闭目录监控") + self.stop_file_watch() # 重新加载计划任务 self.crontab.reload_config(self)