#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
测试设备处理器批量注册功能
验证新的register_and_run接口是否正常工作
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from services.online_script.device_handler_service import DeviceHandlerService
from data.enum.call_device_enum import DeviceType

def test_batch_registration():
    """测试批量设备注册功能"""
    
    print("=== 测试设备处理器批量注册功能 ===")
    
    # 创建设备处理服务
    handler_service = DeviceHandlerService()
    
    def test_handler(message):
        """测试处理器函数"""
        print(f"收到消息: {message.topic}")
        print(f"载荷: {message.payload}")
        return {"forward": True, "payload": message.payload}
    
    try:
        # 测试1: 批量注册华瑞小车
        print("\n1. 测试批量注册华瑞小车")
        device_ids = handler_service.register_and_run(
            device_ids=["AGV001", "AGV002", "AGV003"],
            device_type=DeviceType.VEHICLE,
            brand_name="huarui",
            handler=test_handler,
            script_id="test_script_001",
            description="测试华瑞小车批量注册"
        )
        print(f"华瑞小车批量注册成功: {device_ids}")
        
        # 测试2: 批量注册仙工小车
        print("\n2. 测试批量注册仙工小车")
        device_ids_seer = handler_service.register_and_run(
            device_ids=["SEER001", "SEER002"],
            device_type=DeviceType.VEHICLE,
            brand_name="seer",
            command_types=["order", "state"],  # 只处理特定指令
            handler=test_handler,
            script_id="test_script_002",
            description="测试仙工小车批量注册"
        )
        print(f"仙工小车批量注册成功: {device_ids_seer}")
        
        # 测试3: 批量注册门设备
        print("\n3. 测试批量注册门设备")
        door_ids = handler_service.register_and_run(
            device_ids=["DOOR001", "DOOR002"],
            device_type=DeviceType.DOOR,
            brand_name="huarui",  # 门设备也可以使用华瑞品牌
            handler=test_handler,
            script_id="test_script_003",
            description="测试门设备批量注册"
        )
        print(f"门设备批量注册成功: {door_ids}")
        
        # 测试4: 获取运行状态
        print("\n4. 获取设备处理器运行状态")
        status = handler_service.get_running_handlers()
        print(f"运行状态: {status}")
        
        print("\n=== 所有测试通过 ===")
        
    except Exception as e:
        print(f"测试失败: {e}")
        import traceback
        traceback.print_exc()
    
    finally:
        # 清理资源
        try:
            handler_service.stop()
            print("设备处理服务已停止")
        except:
            pass

def test_topic_generation():
    """测试topic自动生成功能"""
    
    print("\n=== 测试Topic自动生成功能 ===")
    
    from services.online_script.device_handler_service import TopicManager
    
    topic_manager = TopicManager()
    
    # 测试华瑞小车topics生成
    print("\n1. 测试华瑞小车topics生成")
    listen_topics = topic_manager.generate_listen_topics(
        device_ids=["AGV001", "AGV002"], 
        brand_name="huarui",
        device_type=DeviceType.VEHICLE
    )
    forward_topics = topic_manager.generate_forward_topics(
        device_ids=["AGV001", "AGV002"], 
        brand_name="huarui",
        device_type=DeviceType.VEHICLE
    )
    print(f"监听topics: {listen_topics}")
    print(f"转发topics: {forward_topics}")
    
    # 测试仙工小车topics生成
    print("\n2. 测试仙工小车topics生成")
    listen_topics_seer = topic_manager.generate_listen_topics(
        device_ids=["SEER001"], 
        brand_name="seer",
        command_types=["order", "state"],
        device_type=DeviceType.VEHICLE
    )
    forward_topics_seer = topic_manager.generate_forward_topics(
        device_ids=["SEER001"], 
        brand_name="seer",
        command_types=["order", "state"],
        device_type=DeviceType.VEHICLE
    )
    print(f"仙工监听topics: {listen_topics_seer}")
    print(f"仙工转发topics: {forward_topics_seer}")
    
    # 测试品牌验证
    print("\n3. 测试品牌验证")
    print(f"支持的品牌: {topic_manager.get_supported_brands()}")
    print(f"华瑞品牌有效性: {topic_manager.validate_brand('huarui')}")
    print(f"仙工品牌有效性: {topic_manager.validate_brand('seer')}")
    print(f"未知品牌有效性: {topic_manager.validate_brand('unknown')}")

if __name__ == "__main__":
    test_topic_generation()
    test_batch_registration()