#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：DetectorImpl.py
@Description：
@Author：XSH
@Time：2025/11/14 13:15
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from AppSettings.Settings           import *
from AppInterfaces.ImplInterfaces   import ModBusImplInterface, ModBusPoolInterface, DetectionImplInterface
from AppContexts.Contexts           import DetectionContext
from AppCore.Levels                 import AxisLevel
from AppUtils.HardWareUnit import HardWareUnit
from AppInterfaces.AppTimeLoop import AppTimeLoop
class DetectorImpl(Qt.QObject, DetectionImplInterface):
    def __init__(self, context: DetectionContext,pool: ModBusPoolInterface = None,parent: Qt.QObject = None, **args):
        super(DetectorImpl, self).__init__(parent=parent)
        self.Parameter = context if isinstance(context, DetectionContext) else None
        self.Manager   =    pool    if isinstance(pool      , ModBusPoolInterface   ) else None
        self.Initialization('DetectorImpl', self, parent)
        self.setLevel(AxisLevel.Detection)

    def setLevel(self, level: int, **args):
        self.initContext()



    def initContext(self):
        if not self.hasLevel:
            self.Level = AxisLevel.Detection
        if not self.hasParameter:
            self.Parameter = DetectionContext()
            from AppSettings.AppSetting import AppConfig
            self.Parameter.asRegisterContext(AppConfig.Manipulator.Detection.copy())

    @property
    def handler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        if self.isValid:
            handler = self.manager.handle
        return handler

    @property
    def readhandler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        if self.isValid:
            handler = self.manager.readhandler
        return handler

    @property
    def detectorhandler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        if self.isValid:
            handler = self.manager.detectorhandler
        return handler

    @property
    def slow_handler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        if self.isValid:
            handler = self.manager.slow_handler
        return handler

    @property
    def scan_handler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        if self.isValid:
            handler = self.manager.scan_handler
        return handler

    @property
    def drop_handler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        if self.isValid:
            handler = self.manager.drop_handler
        return handler

    @property
    def hook_handler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        if self.isValid:
            handler = self.manager.hook_handler
        return handler

    
    def oil_switch(self, enable: (bool, int)) -> int:
        """打开油泵"""
        isWrong: int = RECODE_FAILURE
        status: bool = 1 if isinstance(enable, (bool, int)) and enable else 0
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return isWrong
            isWrong = HardWareUnit.SendBool(handler, self.Parameter.OilPumpRegister, status)
        return isWrong
    
    def set_oil_speed(self, speed: int) -> int:
        """设置油泵速度"""
        status: bool = False
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return status
            _status: bool = HardWareUnit.ReadBool(handler, self.Parameter.DoorStatusRegister)
            status = _status if isinstance(_status, bool) else False
        return status

    
    def get_oil_speed(self) -> (int, int):
        """获取油泵速度"""
        status: bool = False
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return status
            _status: bool = HardWareUnit.ReadBool(handler, self.Parameter.DoorStatusRegister)
            status = _status if isinstance(_status, bool) else False
        return status

    
    def get_oil_status(self) -> bool:
        """获取油泵状态"""
        status: bool = False
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return status
            _status: bool = HardWareUnit.ReadBool(handler, self.Parameter.OilStatusRegister)
            status = _status if isinstance(_status, bool) else False
        return status

    def get_oil_switch(self) -> bool:
        """获取油泵状态"""
        status: bool = False
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return status
            _status: bool = HardWareUnit.ReadBool(handler, self.Parameter.OilPumpRegister)
            status = _status if isinstance(_status, bool) else False
        return status

    
    def get_door_status(self) -> bool:
        isWrong: int = RECODE_FAILURE
        # isWrong: int = self.oil_switch(True)
        # if RECODE_SUCCEED == isWrong:
        #     AppTimeLoop.processEvents(1000 if not isinstance(timeout, (int, float)) else int(timeout))
        # isWrong = self.oil_switch(False)
        return isWrong

    
    def is_close_the_door(self) -> bool:
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return isWrong
            _status: bool = True if isinstance(enable, (bool, int)) and enable else False
            isWrong = HardWareUnit.SendBool(handler, self.Parameter.BuzzerRegister, _status)
        return isWrong

    
    def get_chamber_status(self) -> bool:
        """获取片仓状态"""
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return isWrong
            isWrong = HardWareUnit.ReadBool(handler, self.Parameter.BuzzerRegister)
        return isWrong


    
    def buzzer_switch(self, enable: (int, bool)) -> int:
        """打开蜂鸣器"""
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return isWrong
            _status: bool = True if isinstance(enable, (bool, int)) and enable else False
            isWrong = HardWareUnit.SendBool(handler, self.Parameter.BuzzerRegister, _status)
        return isWrong

    
    def get_buzzer_switch(self) -> int:
        """打开蜂鸣器"""
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.detectorhandler
        if isinstance(handler, ModBusImplInterface):
            if not handler.isAvailable:
                handler.ReConnected()
            if not handler.isAvailable:
                return isWrong
            isWrong = HardWareUnit.ReadBool(handler, self.Parameter.BuzzerRegister)
        return isWrong

    @property
    def new_handler(self) -> ModBusImplInterface:
        handler: ModBusImplInterface = None
        from AppCore.AppImpls.ModBusImpl import ModBusImpl
        if self.isValid:
            handler = self.manager.new_handler
        else:
            handler = ModBusImpl(self.parameter)
        return handler