#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
硬件管理器模块
提供硬件对象的统一管理和访问
"""
from maix import time
import config
from at_client import ATClient


class HardwareManager:
    """硬件管理器（单例）"""
    _instance = None
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(HardwareManager, cls).__new__(cls)
            cls._instance._initialized = False
        return cls._instance
    
    def __init__(self):
        if self._initialized:
            return
        
        # 私有硬件对象
        self._uart4g = None              # 4G模块UART
        self._bus = None                 # I2C总线
        self._adc_obj = None             # ADC对象
        self._at_client = None            # AT客户端

        self._last_active_time = 0       # 用于记录用户的最后一次活跃的时间
        self._stop_timer = False         # 用于停止定时器的标志

        self._initialized = True


    
    # ==================== 硬件访问（只读属性）====================
    
    @property
    def uart4g(self):
        """4G模块UART（只读）"""
        return self._uart4g
    
    @property
    def bus(self):
        """I2C总线（只读）"""
        return self._bus
    
    @property
    def adc_obj(self):
        """ADC对象（只读）"""
        return self._adc_obj
    
    @property
    def at_client(self):
        """AT客户端（只读）"""
        return self._at_client
    
    # ==================== 初始化方法 ====================
    
    def init_uart4g(self, device=None, baudrate=None):
        """初始化4G模块UART"""
        from maix import uart
        if device is None:
            device = config.UART4G_DEVICE
        if baudrate is None:
            baudrate = config.UART4G_BAUDRATE
        self._uart4g = uart.UART(device, baudrate)
        return self._uart4g
    
    def init_bus(self, bus_num=None):
        """初始化I2C总线"""
        from maix import i2c
        if bus_num is None:
            bus_num = config.I2C_BUS_NUM
        self._bus = i2c.I2C(bus_num, i2c.Mode.MASTER)
        return self._bus
    
    def init_adc(self, channel=None, res_bit=None):
        """初始化ADC"""
        from maix.peripheral import adc
        if channel is None:
            channel = config.ADC_CHANNEL
        if res_bit is None:
            res_bit = adc.RES_BIT_12
        self._adc_obj = adc.ADC(channel, res_bit)
        return self._adc_obj
    
    def init_at_client(self, uart_obj=None):
        """初始化AT客户端"""
        if uart_obj is None:
            if self._uart4g is None:
                raise ValueError("uart4g must be initialized before at_client")
            uart_obj = self._uart4g
        self._at_client = ATClient(uart_obj)
        self._at_client.start()
        return self._at_client

    def power_off(self):
        """关闭电源板"""
        try:
            # 物理引脚是 A24，对应 GPIO 功能是 GPIOA24
            # 注意：这里需要先在 config.PIN_MAPPINGS 中配置好 "A24": "GPIOA24"
            from maix import gpio
            # 输出高电平关闭
            gpio.GPIO("GPIOA24", gpio.Mode.OUT).value(1)
        except Exception as e:
            print(f"关机失败: {e}")

    def start_idle_timer(self):
        self._stop_timer = False
        self._last_active_time = time.time()

    def stop_idle_timer(self):
        self._stop_timer = True

    def get_idle_time_in_sec(self):
        if self._stop_timer:
            return 0
        diff = time.time() - self._last_active_time
        if diff < 0:
            # 时间可能被重置了，重新计时
            self._last_active_time = time.time()
            return 0
        return diff


# 创建全局单例实例
hardware_manager = HardwareManager()


