diff --git a/test_all_audio_received.py b/test_all_audio_received.py deleted file mode 100644 index b571963..0000000 --- a/test_all_audio_received.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -""" -测试 all_audio_received 修改后的逻辑 -""" - -import sys -import os -sys.path.append(os.path.dirname(__file__)) - -def test_all_audio_received_logic(): - """测试 all_audio_received 的设置逻辑""" - print("🧪 测试 all_audio_received 修改后的逻辑") - print("=" * 50) - - # 模拟播放工作线程的逻辑 - class MockAudioProcess: - def __init__(self): - self.currently_playing = False - self.all_audio_received = False - self.last_audio_chunk_time = 0 - - def simulate_audio_playback(self): - """模拟音频播放过程""" - print("🔊 模拟音频播放开始...") - - # 模拟第875行的逻辑 - if not self.currently_playing: - self.currently_playing = True - import time - self.last_audio_chunk_time = time.time() - - # 新增:只要有音频播放就设置all_audio_received为True - if not self.all_audio_received: - self.all_audio_received = True - print(f"🎵 音频开始播放,设置all_audio_received=True") - - print(f"🎵 播放状态变化: currently_playing = True (开始播放)") - print(f"🎵 设置last_audio_chunk_time = {self.last_audio_chunk_time}") - - # 测试1:正常播放场景 - print("\n📋 测试1:正常播放场景") - mock1 = MockAudioProcess() - print(f"初始状态: currently_playing={mock1.currently_playing}, all_audio_received={mock1.all_audio_received}") - mock1.simulate_audio_playback() - print(f"播放后状态: currently_playing={mock1.currently_playing}, all_audio_received={mock1.all_audio_received}") - - # 测试2:重复播放场景(不应该重复设置) - print("\n📋 测试2:重复播放场景") - mock2 = MockAudioProcess() - mock2.all_audio_received = True # 已经设置为True - print(f"初始状态: currently_playing={mock2.currently_playing}, all_audio_received={mock2.all_audio_received}") - mock2.simulate_audio_playback() - print(f"播放后状态: currently_playing={mock2.currently_playing}, all_audio_received={mock2.all_audio_received}") - - print("\n✅ 所有测试完成") - print("\n📊 修改总结:") - print(" • 在播放工作线程中添加了 all_audio_received = True 的设置") - print(" • 移除了对 end_signal_received 的依赖") - print(" • 保留了原有逻辑作为备用机制") - print(" • 确保只要有音频播放就会设置 all_audio_received = True") - -if __name__ == "__main__": - test_all_audio_received_logic() \ No newline at end of file diff --git a/test_nfc_functionality.py b/test_nfc_functionality.py deleted file mode 100644 index 99ab333..0000000 --- a/test_nfc_functionality.py +++ /dev/null @@ -1,141 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -""" -测试NFC角色切换功能 -""" - -import json -import os -import sys -from nfc_manager import NFCManager - -def test_character_loading(): - """测试角色配置加载""" - print("🔍 测试角色配置加载...") - - # 创建测试NFC管理器 - nfc_manager = NFCManager() - - # 检查映射是否正确加载 - expected_mappings = { - '1DC6C90D0D1080': 'libai', - '1DC7C90D0D1080': 'zhubajie' - } - - for uid, character in expected_mappings.items(): - if uid in nfc_manager.uid_to_character: - actual_character = nfc_manager.uid_to_character[uid] - if actual_character == character: - print(f"✅ {character} -> {uid}") - else: - print(f"❌ {character} -> {uid} (实际: {actual_character})") - else: - print(f"❌ 未找到UID: {uid}") - - return len(nfc_manager.uid_to_character) == len(expected_mappings) - -def test_uid_reading(): - """测试UID读取功能(需要真实NFC设备)""" - print("\n🔍 测试UID读取功能...") - - nfc_manager = NFCManager() - - try: - uid = nfc_manager._read_uid() - if uid: - print(f"✅ 读取到UID: {uid}") - return True - else: - print("⚠️ 未读取到UID(可能没有NFC设备或卡片)") - return False - except Exception as e: - print(f"❌ UID读取失败: {e}") - return False - -def test_character_switch_callback(): - """测试角色切换回调""" - print("\n🔍 测试角色切换回调...") - - nfc_manager = NFCManager() - - def callback(character_name): - print(f"🎭 角色切换回调被调用: {character_name}") - - nfc_manager.set_character_switch_callback(callback) - - # 模拟角色切换 - test_uid = '1DC6C90D0D1080' - if test_uid in nfc_manager.uid_to_character: - character_name = nfc_manager.uid_to_character[test_uid] - print(f"📡 模拟检测到UID: {test_uid}") - print(f"🎭 应该切换到角色: {character_name}") - - # 手动调用回调函数 - if nfc_manager.character_switch_callback: - nfc_manager.character_switch_callback(character_name) - return True - - return False - -def test_character_configs(): - """测试角色配置文件""" - print("\n🔍 测试角色配置文件...") - - characters_dir = "characters" - test_files = ['libai.json', 'zhubajie.json'] - - for filename in test_files: - filepath = os.path.join(characters_dir, filename) - if os.path.exists(filepath): - try: - with open(filepath, 'r', encoding='utf-8') as f: - config = json.load(f) - - required_fields = ['name', 'nfc_uid', 'greeting'] - missing_fields = [field for field in required_fields if field not in config] - - if not missing_fields: - print(f"✅ {filename}: {config['name']} (NFC: {config['nfc_uid']})") - else: - print(f"❌ {filename}: 缺少字段 {missing_fields}") - - except Exception as e: - print(f"❌ {filename}: 读取失败 - {e}") - else: - print(f"❌ {filename}: 文件不存在") - -def main(): - """主测试函数""" - print("🧪 NFC角色切换功能测试") - print("=" * 50) - - # 测试角色配置文件 - test_character_configs() - - # 测试角色加载 - loading_success = test_character_loading() - - # 测试UID读取 - reading_success = test_uid_reading() - - # 测试角色切换回调 - callback_success = test_character_switch_callback() - - print("\n" + "=" * 50) - print("📊 测试结果:") - print(f" 角色配置加载: {'✅ 通过' if loading_success else '❌ 失败'}") - print(f" UID读取功能: {'✅ 通过' if reading_success else '⚠️ 需要NFC设备'}") - print(f" 角色切换回调: {'✅ 通过' if callback_success else '❌ 失败'}") - - if loading_success and callback_success: - print("\n🎉 核心功能测试通过!") - print("💡 提示: 要测试完整的NFC功能,请:") - print(" 1. 安装libnfc工具") - print(" 2. 连接NFC读取器") - print(" 3. 运行: python multiprocess_recorder.py --enable-nfc") - else: - print("\n❌ 部分测试失败,请检查配置") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/test_raspberry_nfc.py b/test_raspberry_nfc.py deleted file mode 100644 index 17d0eb8..0000000 --- a/test_raspberry_nfc.py +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -""" -测试树莓派上的NFC功能 -""" - -import subprocess -import os -import re -import sys -import time - -def test_nfc_connection(): - """测试NFC连接""" - print("🔍 测试NFC连接...") - - # 设置环境变量 - os.environ['LIBNFC_DRIVER'] = 'pn532_i2c' - os.environ['LIBNFC_DEVICE'] = 'pn532_i2c:/dev/i2c-1' - - try: - result = subprocess.run(['nfc-list'], capture_output=True, text=True, timeout=10) - print(f"NFC输出: {result.stdout}") - print(f"返回码: {result.returncode}") - - if result.returncode == 0: - if "NFC device: user defined device opened" in result.stdout: - print("✅ NFC设备连接成功") - return True - else: - print("⚠️ NFC设备未正确初始化") - return False - else: - print(f"❌ NFC设备连接失败: {result.stderr}") - return False - except Exception as e: - print(f"❌ NFC测试异常: {e}") - return False - -def test_nfc_reading(): - """测试NFC读取""" - print("\n🔍 测试NFC读取...") - - # 设置环境变量 - os.environ['LIBNFC_DRIVER'] = 'pn532_i2c' - os.environ['LIBNFC_DEVICE'] = 'pn532_i2c:/dev/i2c-1' - - try: - result = subprocess.run(['nfc-list'], capture_output=True, text=True, timeout=10) - if result.returncode == 0: - match = re.search(r'UID\s*\(NFCID1\):\s*([0-9A-Fa-f\s]+)', result.stdout) - if match: - uid = match.group(1).strip().replace(' ', '').upper() - print(f"✅ 读取到UID: {uid}") - return uid - else: - print("⚠️ 未检测到NFC卡片") - return None - else: - print(f"❌ NFC读取失败: {result.stderr}") - return None - except Exception as e: - print(f"❌ NFC读取异常: {e}") - return None - -def test_nfc_manager(): - """测试NFC管理器""" - print("\n🔍 测试NFC管理器...") - - try: - # 导入NFC管理器 - sys.path.append('/home/zhuchaowe/Local-Voice') - from nfc_manager import get_nfc_manager - - # 创建NFC管理器 - nfc_manager = get_nfc_manager() - - # 检查映射 - print(f"角色映射: {nfc_manager.uid_to_character}") - - # 测试读取 - uid = nfc_manager._read_uid() - if uid: - print(f"✅ NFC管理器读取到UID: {uid}") - - # 检查映射 - if uid in nfc_manager.uid_to_character: - character = nfc_manager.uid_to_character[uid] - print(f"🎭 对应角色: {character}") - else: - print("⚠️ UID未映射到角色") - else: - print("⚠️ 未读取到UID") - - return True - except Exception as e: - print(f"❌ NFC管理器测试失败: {e}") - import traceback - traceback.print_exc() - return False - -def main(): - """主测试函数""" - print("🧪 树莓派NFC功能测试") - print("=" * 50) - - # 测试NFC连接 - connection_ok = test_nfc_connection() - - # 测试NFC读取 - uid = test_nfc_reading() - - # 测试NFC管理器 - manager_ok = test_nfc_manager() - - print("\n" + "=" * 50) - print("📊 测试结果:") - print(f" NFC连接: {'✅ 通过' if connection_ok else '❌ 失败'}") - print(f" UID读取: {'✅ 通过' if uid else '⚠️ 无卡片'}") - print(f" NFC管理器: {'✅ 通过' if manager_ok else '❌ 失败'}") - - if uid: - print(f"\n🎯 当前检测到的UID: {uid}") - print("💡 提示: 您现在可以使用以下命令测试完整的NFC角色切换功能:") - print(" cd Local-Voice && python multiprocess_recorder.py --enable-nfc") - else: - print(f"\n💡 提示: 请确保NFC读取器已连接并放置了NFC卡片") - -if __name__ == "__main__": - main() \ No newline at end of file