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

    def initValue(self):
        super(HookImpl, self).initValue()
        self.initContext()

    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 = HookContext()
            from AppSettings.AppSetting import AppConfig
            self.Parameter.asRegisterContext(AppConfig.Manipulator.AxisH.copy())

    
    def set_h_clear_zero(self, enable: (int, bool)= True) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: int = 1 if isinstance(enable, (int, bool)) and enable else 0
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.HookClearZero, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        return isWrong

    
    def set_h_force_quit(self, enable: (int, bool)= True) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: int = 1 if isinstance(enable, (int, bool)) and enable else 0
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.ForceQuitRegister, _enable)
        AppTimeLoop.processEvents(500)
        return isWrong

    def wait_force_quit(self, waittime: int = 500, **args) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: int = 1
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.ForceQuitRegister, _enable)
        if isinstance(waittime, (float, int)):
            AppTimeLoop.processEvents(min(1000, max(100, int(waittime))))
        return isWrong

    def pick_tray(self, layer: int) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(layer, (int, float)):
            return isWrong
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendUInt16(handler, self.Parameter.SetLayerCountRegister, int(layer))
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.PickTrayCompleteRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.PickTrayRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        return isWrong

    
    def wait_pick_tray(self, layer: int) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(layer, (int, float)):
            return isWrong
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendUInt16(handler, self.Parameter.SetLayerCountRegister, int(layer))
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong

        HardWareUnit.WaitSendBool(handler, self.Parameter.PickTrayCompleteRegister, 0)

        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.PickTrayRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong

        isWrong = self.waitForPickTrayComplete()
        return isWrong

    def waitForPickTrayComplete(self) -> int:
        maxwaittimes: int = 1200000
        waittimes   : int = 1000
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                status = HardWareUnit.ReadBool(handler, self.Parameter.PickTrayCompleteRegister)
                if status:
                    isWrong = RECODE_SUCCEED
                    break
                """如果发生故障 挂钩电机复位功能"""
                ForwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.ForwardMalfunction)
                BackwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction)
                if ForwardMalfunction or BackwardMalfunction:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                    AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
                AppTimeLoop.processEvents(waittimes)
        return isWrong
    
    def drop_tray(self, layer: int) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(layer, (int, float)):
            return isWrong
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendUInt16(handler, self.Parameter.SetLayerCountRegister, int(layer))
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.DropTrayCompleteRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.DropTrayRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        return isWrong

    
    def wait_drop_tray(self, layer: int) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(layer, (int, float)):
            return isWrong
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendUInt16(handler, self.Parameter.SetLayerCountRegister, int(layer))
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.DropTrayCompleteRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.DropTrayRegister, _enable)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        isWrong = self.waitForDropTrayComplete()
        return isWrong

    def reset_hook(self) -> int:
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        return isWrong

    def waitForDropTrayComplete(self) -> int:
        maxwaittimes: int = 1200000
        waittimes: int = 100
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                status = HardWareUnit.ReadBool(handler, self.Parameter.DropTrayCompleteRegister)
                # print(f'M{self.Parameter.DropTrayCompleteRegister.Addr}状态: {status}')
                if status:
                    isWrong = RECODE_SUCCEED
                    break
                """如果发生故障 挂钩电机复位功能"""
                ForwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.ForwardMalfunction)
                BackwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction)
                if ForwardMalfunction or BackwardMalfunction:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                    AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
                AppTimeLoop.processEvents(waittimes)
        return isWrong

    
    def wait_check_tray(self, layer: int) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(layer, (int, float)):
            return isWrong
        # if RECODE_SUCCEED != isWrong:
        #     return isWrong
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendUInt16(handler, self.Parameter.SetLayerCountRegister, int(layer))
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.DetectorTrayCompleteRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.DetectorTrayRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        isWrong = self.waitForDetectorTrayComplete()
        return isWrong

    def waitForDetectorTrayComplete(self) -> int:
        maxwaittimes: int = 1200000
        waittimes: int = 100
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                status = HardWareUnit.ReadBool(handler, self.Parameter.DetectorTrayCompleteRegister)
                # print(f'M{self.Parameter.DetectorTrayCompleteRegister.Addr}状态: {status}')
                if status:
                    isWrong = RECODE_SUCCEED
                    break
                AppTimeLoop.processEvents(waittimes)
        return isWrong

    def wait_check_chamber(self) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: bool = True

        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.DetectorChamberCompleteRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.DetectorChamberRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        isWrong = self.waitForDetectorChamberComplete()
        return isWrong

    def wait_check_chamber_box(self, **args) -> int:

        isWrong: int = RECODE_FAILURE
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.ChamberStatusRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.CheckChamberRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        count: int = 0
        while RECODE_SUCCEED != isWrong:
            isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.CheckChamberRegister, _enable)
            AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
            count += 1
            if count > 5:
                break
        if RECODE_SUCCEED != isWrong:
            return isWrong
        isWrong = self.waitForDetectorChamberBoxComplete()
        return isWrong


    def waitForDetectorChamberBoxComplete(self, **args) -> int:
        maxwaittimes: int = min(1200000, args.get('maxwaittimes', 10000))
        isWrong: int = RECODE_FAILURE
        waittimes: int = 100
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                status          : bool = HardWareUnit.ReadBool(handler, self.Parameter.ChamberStatusRegister)
                AxisXMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.AxisXMalfunction)
                AxisYMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.AxisYMalfunction)
                AxisZMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.AxisZMalfunction)
                if status:
                    isWrong = RECODE_SUCCEED
                    break
                if AxisXMalfunction or AxisYMalfunction or AxisZMalfunction:
                    break
                AppTimeLoop.processEvents(waittimes)
        return isWrong

    def waitForDetectorChamberComplete(self) -> int:
        maxwaittimes: int = 1200000
        waittimes: int = 100
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                status = HardWareUnit.ReadBool(handler, self.Parameter.DetectorChamberCompleteRegister)
                # print(f'M{self.Parameter.DetectorChamberCompleteRegister.Addr}状态: {status}')
                if status:
                    isWrong = RECODE_SUCCEED
                    break
                AppTimeLoop.processEvents(waittimes)
        return isWrong

    def move_scan_code_position(self) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodePlaceRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodePickRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        return isWrong

    
    def waitmove_scan_code_position(self, layer: int) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(layer, (int, float)):
            return isWrong
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        _enable: bool = True
        isWrong = HardWareUnit.WaitSendUInt16(handler, self.Parameter.SetLayerCountRegister, int(layer))
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodePlaceRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodePickTrayCompleteRegister, 0)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        HardWareUnit.WaitSendBool(handler, self.Parameter.BackLimit, 0)

        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        count: int = 0
        while count < 3:
            isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodePickRegister, _enable)
            AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
            if RECODE_SUCCEED == isWrong:
                break
        if RECODE_SUCCEED == isWrong:
            isWrong = self._waitForScanCodePlaceRegister()
        return isWrong


    def _waitForScanCodePlaceRegister(self) -> int:
        maxwaittimes: int = 1200000
        waittimes: int = 100
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                tray_status         : bool = HardWareUnit.ReadBool(handler, self.Parameter.ScanCodePlaceRegister)

                if tray_status:
                    isWrong = RECODE_SUCCEED
                    break
                ForwardMalfunction  : bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction   )
                if ForwardMalfunction:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                    AppTimeLoop.processEvents(HOOT_WAIT_TIMES)

                AppTimeLoop.processEvents(waittimes)
        return isWrong


    def move_scan_code_complete(self) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodePickTrayCompleteRegister, 0)
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodeCompleteMoveRegister, _enable)
        AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
        return isWrong

    def waitmove_scan_code_complete(self) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: bool = True
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        count: int = 0
        while count < 3:
            isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.ScanCodeCompleteMoveRegister, True)
            AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
            count += 1
            if RECODE_SUCCEED == isWrong:
                break
        if RECODE_SUCCEED == isWrong:
            isWrong = self.waitForScanCodePlaceRegister()
        return isWrong


    def waitForScanCodePlaceRegister(self) -> int:
        maxwaittimes: int = 1200000
        waittimes: int = 100
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        count = 0
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                tray_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.ScanCodePlaceRegister)
                back_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackLimit)
                if count >= 10 and back_status:
                    isWrong = RECODE_SUCCEED
                    break
                if tray_status:

                    isWrong = RECODE_SUCCEED
                    break
                # ForwardMalfunction      #
                # BackwardMalfunction     #
                # HookResetRegister       #
                ForwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction)
                if ForwardMalfunction:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                    AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
                AppTimeLoop.processEvents(waittimes)
                count += 1
        return isWrong

    def waitForScanCodePickComplete(self) -> int:
        maxwaittimes: int = 1200000
        waittimes: int = 100
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            count: int = 0
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                tray_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.ScanCodePickTrayCompleteRegister)
                # print(f'M{self.Parameter.ScanCodePlaceRegister.Addr}状态: {status}')
                if tray_status:
                    isWrong = RECODE_SUCCEED
                    break
                place_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.PlaceRegister)
                if place_status:
                    isWrong = RECODE_SUCCEED
                    break
                back_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackLimit)
                if count >= 10 and back_status:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.ForceQuitRegister, True)
                    AppTimeLoop.processEvents(500)
                    isWrong = RECODE_SUCCEED
                    break
                ForwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction)
                if ForwardMalfunction:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                    AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
                AppTimeLoop.processEvents(waittimes)
                count += 1
        return isWrong



    def wait_empty_hook_return(self) -> int:
        isWrong: int = RECODE_FAILURE
        AppTimeLoop.processEvents(100)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        _enable: bool = True
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.EmptyTrayReturnRegister, _enable)
        if RECODE_SUCCEED != isWrong:
            HardWareUnit.WaitSendBool(handler, self.Parameter.EmptyTrayReturnRegister, _enable)
        AppTimeLoop.processEvents(100)
        isWrong = self.waitForEmptyTrayReturnComplete()
        return isWrong

    def waitForEmptyTrayReturnComplete(self) -> int:
        maxwaittimes: int = 1200000
        waittimes: int = 100
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            endtimes: Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            while True:
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                status = HardWareUnit.ReadBool(handler, self.Parameter.EmptyTrayReturnStatusRegister)
                # print(f'M{self.Parameter.EmptyTrayReturnStatusRegister.Addr}状态: {status}')
                if status:
                    isWrong = RECODE_SUCCEED
                    break
                # ForwardMalfunction      #
                # BackwardMalfunction     #
                # HookResetRegister       #
                """如果发生故障 挂钩电机复位功能"""
                ForwardMalfunction  : bool = HardWareUnit.ReadBool(handler, self.Parameter.ForwardMalfunction)
                BackwardMalfunction : bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction)
                if ForwardMalfunction or BackwardMalfunction:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                    AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
                AppTimeLoop.processEvents(waittimes)
        return isWrong
    
    def forward(self, enable: (int, bool)) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: int = 1 if isinstance(enable, (int, bool)) and enable else 0
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.ForwardRegister, _enable)
        return isWrong

    
    def backward(self, enable: (int, bool)) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: int = 1 if isinstance(enable, (int, bool)) and enable else 0
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendBool(handler, self.Parameter.BackwardRegister, _enable)
        return isWrong


    def get_forward_status(self) -> int:
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.ReadBool(handler, self.Parameter.ForwardRegister)
        return isWrong


    def get_backward_status(self) -> int:
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.ReadBool(handler, self.Parameter.BackwardRegister)
        return isWrong


    def get_back_limit_status(self) -> int:
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.ReadBool(handler, self.Parameter.BackLimit)
        return isWrong


    def get_front_limit_status(self) -> int:
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.ReadBool(handler, self.Parameter.FrontLimit)
        return isWrong

    def Reset(self, enable: (int, bool) = True) -> int:
        isWrong: int = RECODE_FAILURE
        _enable: int = 1 if isinstance(enable, (int, bool)) and enable else 0
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.ReadBool(handler, self.Parameter.HookResetRegister, _enable)
        return isWrong

    def getPosition(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.Position)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position

    def get_h_speed(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.Velocity)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def get_layer_hover_height(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetLayerHoverHeightRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_hover_height(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler,self.Parameter.SetLayerHoverHeightRegister, position)
        return isWrong


    def get_layer_drag_height(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetLayerDragHoverHeightRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_drag_height(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetLayerDragHoverHeightRegister, position)
        return isWrong


    def get_layer_spacing_height(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetLayerSpacingHeightRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_spacing_height(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetLayerSpacingHeightRegister, position)
        return isWrong


    def get_layer_axis_x_position(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetAxisXPickPositionRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_axis_x_position(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetAxisXPickPositionRegister, position)
        return isWrong


    def get_layer_axis_y_position(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetAxisYPickPositionRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_axis_y_position(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetAxisYPickPositionRegister, position)
        return isWrong


    def get_layer_axis_x_detector_position(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetAxisXDetectorPositionRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_axis_x_detector_position(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetAxisXDetectorPositionRegister, position)
        return isWrong


    def get_layer_axis_y_detector_position(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetAxisYDetectorPositionRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_axis_y_detector_position(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetAxisYDetectorPositionRegister, position)
        return isWrong


    def get_layer_scancode_position(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetScanCodePositionRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position


    def set_layer_scancode_position(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetScanCodePositionRegister, position)
        return isWrong


    def get_layer_scancode_spacing(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetScanCodeSpacingRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, float) else RECODE_FAILURE
            position = _position if isinstance(_position, float) else position
        return isWrong, position

    def set_layer_scancode_spacing(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: float = float(value)
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetScanCodeSpacingRegister, position)
        return isWrong


    def get_layer_index(self) -> (int, int):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadUInt16(handler, self.Parameter.SetLayerCountRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, int) else RECODE_FAILURE
            position = _position if isinstance(_position, int) else position
        return isWrong, position


    def set_layer_index(self, index: int) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(index, (int, float)):
            return isWrong
        position: int = int(abs(index))
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendUInt16(handler, self.Parameter.SetLayerCountRegister, position)
        return isWrong


    def get_layer_detector_position(self) -> (int, float):
        isWrong: int = RECODE_FAILURE
        position: float = None
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            _position = HardWareUnit.ReadFloat(handler, self.Parameter.SetDetectorLayerPositionRegister)
            isWrong = RECODE_SUCCEED if isinstance(_position, (int, float)) else RECODE_FAILURE
            position = _position if isinstance(_position, (int, float)) else position
        return isWrong, position


    def set_layer_detector_position(self, value: (int, float)) -> int:
        isWrong: int = RECODE_FAILURE
        if not isinstance(value, (int, float)):
            return isWrong
        position: int = int(abs(index))
        handler: ModBusImplInterface = self.hook_handler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        isWrong = HardWareUnit.WaitSendFloat(handler, self.Parameter.SetDetectorLayerPositionRegister, position)
        return isWrong


    def get_lower_limit_status      (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.ForwardRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_upper_limit_status      (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.BackwardRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_place_status            (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.PlaceRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_detector_status         (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.DetectorRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False



    def get_pick_tray_status        (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.PickTrayCompleteRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_drop_tray_status        (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.DropTrayCompleteRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_tray_detector_status    (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.DetectorTrayCompleteRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_chamber_detector_status (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.DetectorChamberCompleteRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_pick_scan_code_status   (self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.ScanCodePickTrayCompleteRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_empty_tray_return_status(self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.EmptyTrayReturnStatusRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_forward_warning_status(self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.ForwardMalfunction)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_backward_warning_status(self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.ForwardMalfunction)
            return False if not isinstance(status, (int, bool)) else status
        return False

    def get_layer_detector_status   (self, layer: int) -> int:
        from AppSettings.SystemSetting import SystemConfig
        MaxChamberNumber: int = SystemConfig.MaxChamberNumber
        MinChamberNumber: int = SystemConfig.MinChamberNumber
        if not isinstance(layer, int) or layer < MinChamberNumber or layer > MaxChamberNumber:
            return False
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            register: ModBusRegister = self.Parameter.LayerRegister.copy()
            register.Addr = self.Parameter.LayerRegister.Addr + layer - 1
            status: int = HardWareUnit.ReadBool(handler, register)
            return status
        return False

    def get_layer_status(self) -> list:
        from AppSettings.SystemSetting import SystemConfig
        MaxChamberNumber: int = SystemConfig.MaxChamberNumber
        status: list[int] = [0] * MaxChamberNumber
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            for index, _ in enumerate(status):
                register: ModBusRegister = self.Parameter.LayerRegister.copy()
                register.Addr = self.Parameter.LayerRegister.Addr + index
                state: int = HardWareUnit.ReadBool(handler, register)
                status[index] = 1 if isinstance(state, (int, bool)) and state else 0


        return status

    def get_layer_status_(self) -> list:
        from AppSettings.SystemSetting import SystemConfig
        MaxChamberNumber: int = SystemConfig.MaxChamberNumber
        status: list[int] = [0] * MaxChamberNumber
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            for index, _ in enumerate(status):
                register: ModBusRegister = self.Parameter.LayerRegister.copy()
                register.Addr = self.Parameter.LayerRegister.Addr + index
                state: int = HardWareUnit.ReadBool(handler, register)
                status[index] = 1 if isinstance(state, (int, bool)) and state else 0


        return status


    def get_hook_back_limit_status(self) -> int:
        """获取挂钩后限位状态"""
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.BackLimit)
            return False if not isinstance(status, (int, bool)) else status
        return False


    def get_hook_front_limit_status(self) -> int:
        """获取挂钩前限位状态"""
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.FrontLimit)
            return False if not isinstance(status, (int, bool)) else status
        return False

    def get_tray_place_status(self) -> int:
        """获取载玻台托盘状态"""
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.PlaceRegister)
            return False if not isinstance(status, (int, bool)) else status
        return False

    def isTrayAvailable(self) -> int:
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        state: bool = False
        if isinstance(handler, ModBusImplInterface) and handler.isAvailable:
            status: int = HardWareUnit.ReadBool(handler, self.Parameter.PlaceRegister)
            state =  False if not isinstance(status, (int, bool)) else status
            if not state:
                status = HardWareUnit.ReadBool(handler, self.Parameter.BackLimit)
                state = False if not isinstance(status, (int, bool)) else status
        return state

    def waitmove_to_back(self) -> int:
        """移动到后限位"""
        isWrong: int = self.backward(True)
        AppTimeLoop.processEvents(5)
        self.waitForBackComplete()
        self.backward(False)
        AppTimeLoop.processEvents(5)
        return isWrong



    def waitmove_to_front(self) -> int:
        """移动到前限位"""
        isWrong: int = self.forward(True)
        if RECODE_SUCCEED == isWrong:
            isWrong = self.waitForFrontComplete()
        self.forward(False)
        return isWrong

    def waitForBackComplete(self) -> int:
        """移动到后限位"""
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        if handler.isAvailable:
            """移动到限位最大限时3分钟"""
            maxwaittimes: int = 360000
            waittime    : int = 10
            endtimes    : Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
            place_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.PlaceRegister)
            limit_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackLimit    )
            count       : int  = 0
            while True:
                if place_status or limit_status:
                    break
                if Qt.QDateTime.currentDateTime() > endtimes:
                    break
                """如果发生故障 挂钩电机复位功能"""
                ForwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.ForwardMalfunction)
                BackwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction)
                if ForwardMalfunction or BackwardMalfunction:
                    HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                    AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
                AppTimeLoop.processEvents(waittime)
                place_status = HardWareUnit.ReadBool(handler, self.Parameter.PlaceRegister)
                limit_status = HardWareUnit.ReadBool(handler, self.Parameter.BackLimit    )
                count += 1
            isWrong = RECODE_SUCCEED if place_status or limit_status else RECODE_FAILURE
        return isWrong


    def waitForFrontComplete(self) -> int:
        """移动到前限位"""
        isWrong: int = RECODE_FAILURE
        handler: ModBusImplInterface = self.readhandler
        if not isinstance(handler, ModBusImplInterface):
            handler = self.new_handler
        if not isinstance(handler, ModBusImplInterface):
            return isWrong
        if not handler.isAvailable:
            handler.ReConnected()
        if not handler.isAvailable:
            return isWrong
        """移动到限位最大限时3分钟"""
        maxwaittimes: int = 360000
        waittime    : int = 500
        endtimes    : Qt.QDateTime = Qt.QDateTime.currentDateTime().addMSecs(maxwaittimes)
        limit_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.FrontLimit    )
        while True:
            if limit_status:
                break
            if Qt.QDateTime.currentDateTime() > endtimes:
                break
            """如果发生故障 挂钩电机复位功能"""
            ForwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.ForwardMalfunction)
            BackwardMalfunction: bool = HardWareUnit.ReadBool(handler, self.Parameter.BackwardMalfunction)
            if ForwardMalfunction or BackwardMalfunction:
                HardWareUnit.WaitSendBool(handler, self.Parameter.HookResetRegister, True)
                AppTimeLoop.processEvents(HOOT_WAIT_TIMES)
            AppTimeLoop.processEvents(waittime)
            limit_status: bool = HardWareUnit.ReadBool(handler, self.Parameter.FrontLimit    )
        isWrong = RECODE_SUCCEED if limit_status else RECODE_FAILURE
        return isWrong


    def waitHome(self) -> int:
        return self.waitmove_to_back()



    @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

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