#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：HitBotViews.py\n
 @DateTime: 2025/7/25 18:24\n
 @Author  ：xsh
"""
import sys
from PyQt5 import Qt
from AppCore.DBModels import *
from AppCore.Levels import *
from AppCore.Readers import *
from AppInterfaces.utils import *
from utils import *
from AppSettings.utils import *
from AppInterfaces.UiInterface import UiInterface, AppUiProperty
from AppHeadEnd.UiCommons import *
from AppCore.Requests               import *
from AppCore.Contexts               import HitBotData
from AppUtils.UiUtils               import UiUtils
from AppSettings.AppGlobalStatus    import AppGlobalStatus, AppStatus
from AppBackEnd.AppBackEndBusiness  import *
from AppBackEnd.AppScanningFlow     import AppScanningFlow
# from AppBusiness.AppScanningFlow import AppScanningFlow
from AppCore.Managers import *

class HitBotViews(Qt.QWidget, UiInterface, SingletonInterface):
    Locker              : Qt.QMutex = Qt.QMutex()
    DrawData                    : DrawHitBotDataView
    Devices                     : HardWareManager
    Business                    : AppScanningFlow
    Stacked                     : Qt.QStackedLayout      #
    HitBotControl               : HitBotControlView      #
    Operation                   : HitBotOperationView    #
    CheckChamberAction          : HitBotModelView        #
    CheckChamberLSlideAction    : HitBotModelView        #
    CheckChamberRSlideAction    : HitBotModelView        #
    CodeChamberSlideAction      : HitBotModelView        #
    PickChamberSlideAction      : HitBotModelView        #
    DropChamberSlideAction      : HitBotModelView        #
    DropTraySlideAction         : HitBotModelView        #
    PickTraySlideAction         : HitBotModelView        #
    SafeHitBotAction            : HitBotModelView        #
    BrokenDropAction            : HitBotModelView        #

    @classmethod
    def GetInstance(cls, *args, **kwargs) -> 'HitBotViews':
        return super(HitBotViews, cls).GetInstance(*args, **kwargs)
    def __init__(self, parent: Qt.QWidget = None):
        super(HitBotViews, self).__init__(parent=parent)
        self.Initialization('HitBotViews', self, parent)
        self.initWidgetStyle()
        self.initWindowIcon()
        self.setWindowTitle("机械臂调试")

    def initWidgetStyle(self):
        self.setStyleSheet(UiUtils.getWidgetStyle())

    def initValue(self):
        super(HitBotViews, self).initValue()
        self.initWindows    ()
        self.initDevices    ()
        self.initBusiness   ()

    def initConnect(self):
        super(HitBotViews, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.HitBotControl.valueChanged.connect(slot=self.SlotValueChanged, type=TYPE)
        self.Operation.CheckChamberMoveButton          .clicked.connect(slot=self.SlotCheckChamberMoveButton                    , type=TYPE)
        self.HitBotControl.gripper_on_button           .clicked.connect(slot=self.SlotGripperONButton                           , type=TYPE)
        self.HitBotControl.gripper_off_button          .clicked.connect(slot=self.SlotGripperOFFButton                          , type=TYPE)
        self.Operation.InitHitBotButton                .clicked.connect(slot=self.SlotInitHitBotButton                          , type=TYPE)
        self.Operation.SafeHitBotButton                .clicked.connect(slot=self.SlotSafeHitBotButton                          , type=TYPE)
        self.Operation.QuitHitBotButton                .clicked.connect(slot=self.SlotQuitHitBotButton                          , type=TYPE)
        self.Operation.CheckChamberButton              .clicked.connect(slot=self.SlotCheckChamberButton                        , type=TYPE)
        self.Operation.SelectChamber.CheckChamberButton.clicked.connect(slot=self.SlotCheckChamberSlideButton                   , type=TYPE)
        self.Operation.SelectChamber.CheckGroupButton  .clicked.connect(slot=self.SlotCheckGroupButton                          , type=TYPE)
        self.Operation.CodeChamberSlideButton          .clicked.connect(slot=self.SlotCodeChamberSlideButton                    , type=TYPE)
        self.Operation.PickChamberSlideButton          .clicked.connect(slot=self.SlotPickChamberSlideButton                    , type=TYPE)
        self.Operation.DropChamberSlideButton          .clicked.connect(slot=self.SlotDropChamberSlideButton                    , type=TYPE)
        self.Operation.DropTraySlideButton             .clicked.connect(slot=self.SlotDropTraySlideButton                       , type=TYPE)
        self.Operation.PickTraySlideButton             .clicked.connect(slot=self.SlotPickTraySlideButton                       , type=TYPE)
        self.Operation.CheckChamberActionButton        .clicked.connect(slot=self.SlotCheckChamberActionButton                  , type=TYPE)
        self.Operation.CheckChamberLSlideActionButton  .clicked.connect(slot=self.SlotCheckChamberLSlideActionButton            , type=TYPE)
        self.Operation.CheckChamberRSlideActionButton  .clicked.connect(slot=self.SlotCheckChamberRSlideActionButton            , type=TYPE)
        self.Operation.CodeChamberSlideActionButton    .clicked.connect(slot=self.SlotCodeChamberSlideActionButton              , type=TYPE)
        self.Operation.PickChamberSlideActionButton    .clicked.connect(slot=self.SlotPickChamberSlideActionButton              , type=TYPE)
        self.Operation.DropChamberSlideActionButton    .clicked.connect(slot=self.SlotDropChamberSlideActionButton              , type=TYPE)
        self.Operation.DropTraySlideActionButton       .clicked.connect(slot=self.SlotDropTraySlideActionButton                 , type=TYPE)
        self.Operation.PickTraySlideActionButton       .clicked.connect(slot=self.SlotPickTraySlideActionButton                 , type=TYPE)
        self.Operation.SafeHitBotActionButton          .clicked.connect(slot=self.SlotSafeHitBotActionButton                    , type=TYPE)
        self.Operation.BrokenDropActionButton          .clicked.connect(slot=self.SlotBrokenDropActionButton                    , type=TYPE)
        self.Operation.ChamberToTrayActionButton       .clicked.connect(slot=self.SlotChamberToTrayActionButton                 , type=TYPE)
        self.Operation.TrayToChamberActionButton       .clicked.connect(slot=self.SlotTrayToChamberActionButton                 , type=TYPE)
        self.Operation.DrawCheckStatusButton           .clicked.connect(slot=self.SlotDrawCheckStatusButton                     , type=TYPE)
        self.Operation.GetHitBotPositionButton         .clicked.connect(slot=self.SlotGetHitBotPositionButton                   , type=TYPE)
        self.Operation.SetHitBotPositionButton         .clicked.connect(slot=self.SlotSetHitBotPositionButton                   , type=TYPE)
        self.Operation.EmptyHitBotButton               .clicked.connect(slot=self.SlotEmptyHitBotButton                         , type=TYPE)
        self.Operation.InitHitBotXButton               .clicked.connect(slot=self.SlotInitHitBotXButton                         , type=TYPE)
        self.Operation.InitHitBotYButton               .clicked.connect(slot=self.SlotInitHitBotYButton                         , type=TYPE)
        self.Operation.InitHitBotZButton               .clicked.connect(slot=self.SlotInitHitBotZButton                         , type=TYPE)
        self.Operation.InitHitBotRButton               .clicked.connect(slot=self.SlotInitHitBotRButton                         , type=TYPE)
        self.Operation.GripperGrabButton               .clicked.connect(slot=self.SlotGripperGrabButton                         , type=TYPE)

        self.Operation.GroupSlideMoveButton            .clicked.connect(slot=self.SlotGroupSlideMoveButton                      , type=TYPE)
        self.Operation.ChamberSlideMoveButton          .clicked.connect(slot=self.SlotChamberSlideMoveButton                    , type=TYPE)
        self.Operation.GroupSlideTableMoveButton       .clicked.connect(slot=self.SlotGroupSlideTableMoveButton                 , type=TYPE)
        self.Operation.ChamberSlideTableMoveButton     .clicked.connect(slot=self.SlotChamberSlideTableMoveButton               , type=TYPE)
        self.Operation.GroupSlideScanningButton        .clicked.connect(slot=self.SlotGroupSlideScanningButton                  , type=TYPE)
        self.Operation.ChamberSlideScanningButton      .clicked.connect(slot=self.SlotChamberSlideScanningButton                , type=TYPE)
        self.Operation.AsyncChamberSlideScanningButton .clicked.connect(slot=self.SlotAsyncChamberSlideScanningButton           , type=TYPE)

    def isAvailable(self) -> bool:
        self.initBusiness   ()
        self.initDevices    ()
        if not self.hasHitBot:
            self.business.initHardWare()
        if not AppStatus.isHitBotAvailable():
            self.business.waitForHitBotHome()
        if not AppStatus.isHitBotAvailable():
            return False
        return True

    @property
    def IsHitBotOpen(self):
        self.initBusiness   ()
        self.initDevices    ()
        if not self.hasHitBot:
            self.business.initHardWare()
        if not self.hasHitBot:
            return False
        if not self.hitbot.IsOpen():
            self.hitbot.ReOpen()
        return self.hitbot.IsOpen()


    def SlotUpdateHitBotStatus(self, data: HitBotData, model: HitBotModelInterface):
        if self.hasOperation:
            self.Operation.HitBotPosition.setModel(model)



    def SlotActionMovesSignal(self, model: HitBotModelInterface):
        self.SlotValueChanged(model)

    def SlotValueChanged(self, model: HitBotModelInterface):
        if isinstance(model, HitBotModelInterface):
            if not self.isAvailable():
                return
            self.HitBotControl.blockSignals(True)
            self.hitbot.move(model)
            AppTimeLoop.processEvents(500)
            self.HitBotControl.blockSignals(False)
            model: HitBotModelInterface = self.hitbot.GetHitModel()
            self.HitBotControl.valueUpdated.emit(model)

    def SlotGripperONButton  (self):
        if self.isAvailable():
            self.hitbot.SetGripperON()
    def SlotGripperOFFButton (self):
        if self.isAvailable():
            self.hitbot.SetGripperOFF()
            self.hitbot.IsGripperOFF()
    def SlotInitHitBotButton                (self):
        self.initBusiness()
        self.devices.waitForHitBotHome()

    def SlotSafeHitBotButton                (self):
        if not self.isAvailable():
            return
        self.hitbot.moveToSafePosition()

    def SlotQuitHitBotButton                (self):
        if not self.hasHitBot:
            return
        self.hitbot.Close()


    def SlotCheckChamberMoveButton(self):
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        isWrong = self.business.checkbusiness.WaitDone(ChamberNumber=ChamberNumber, isSafe=False)
        if 1 != isWrong:
            self.business.status.initChamberSlideStatus(ChamberNumber)
        self.business.status.setRunStatus(False)
        pass

    def SlotCheckChamberButton(self):

        ChamberNumber: int
        RowNumber    : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        isWrong = self.business.checkbusiness.CheckChamber(chamberNumber=ChamberNumber, isSafe=False)
        if 1 != isWrong:
            self.business.status.initChamberSlideStatus(ChamberNumber)

        self.business.status.setRunStatus(False)

    def SlotCheckChamberSlideButton             (self):
        ChamberNumber   : int
        RowNumber       : int
        if not self.isAvailable():
            return
        AppStatus.setRunning(True)
        ChamberStatusModel.InitAllCheckStatus()
        ChamberStatusView.GetInstance().flushAllGroup()
        self.business.status.setRunStatus(True)
        isWrong = self.business.checkbusiness.WaitDone(ChamberNumber=None, isSafe=True)
        self.business.status.setRunStatus(False)
        AppStatus.waitForRunning()
        pass
    def SlotCheckGroupButton        (self):
        ChamberNumber: int
        RowNumber    : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        ChamberStatusModel.InitCheckStatus(ChamberNumber)
        ChamberStatusView.GetInstance().flushGroup(ChamberNumber)
        isWrong = self.business.CheckChamberSlide(chamber_number=ChamberNumber, isSafe=True)
        self.business.status.setRunStatus(False)
    def SlotCodeChamberSlideButton(self):
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        if not self.isAvailable():
            return
        if not self.hitbot.IsGrab():
            return
        self.business.status.setRunStatus(True)
        self.business.SlideScanCode(chamber_number=ChamberNumber, row_number = RowNumber, isSafe=True)
        self.business.status.setRunStatus(False)

    def SlotPickChamberSlideButton          (self):
        ChamberNumber   : int
        RowNumber       : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber   : int = self.Operation.SelectChamber.getTrayNumber()
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        self.business.ChamberToReadyScanning(chamber_number=ChamberNumber, row_number=RowNumber, tray_number=TrayNumber, isSafe=True)
        self.business.status.setRunStatus(False)

    def SlotDropChamberSlideButton          (self):
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber: int = self.Operation.SelectChamber.getTrayNumber()
        if not self.isAvailable():
            return

        self.business.status.setRunStatus(True)
        self.business.SlideToChamber(chamber_number=ChamberNumber, row_number=RowNumber, tray_number=TrayNumber,isSafe=True)
        self.business.status.setRunStatus(False)

    def SlotDropTraySlideButton             (self):
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber: int = ModuleLevel.getTrayNumber(self.Operation.SelectChamber.getTrayNumber())
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        self.business.status.setTrayStatus(TrayNumber, False)
        self.business.traydropbusiness.WaitDone(ChamberNumber=ChamberNumber, RowNumber=RowNumber, TrayNumber=TrayNumber, isSafe=True)
        self.business.status.setRunStatus(False)

    def SlotPickTraySlideButton             (self):
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber: int = ModuleLevel.getTrayNumber(self.Operation.SelectChamber.getTrayNumber())
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        self.business.status.setTrayStatus(TrayNumber, True)
        self.business.traypickbusiness.WaitDone(ChamberNumber=ChamberNumber, RowNumber=RowNumber, TrayNumber=TrayNumber, isSafe=True)
        self.business.status.setRunStatus(False)

    def SlotCheckChamberActionButton        (self):
        self.Stacked.setCurrentWidget(self.CheckChamberAction       )
    def SlotCheckChamberLSlideActionButton  (self):
        self.Stacked.setCurrentWidget(self.CheckChamberLSlideAction )
    def SlotCheckChamberRSlideActionButton  (self):
        self.Stacked.setCurrentWidget(self.CheckChamberRSlideAction )
    def SlotCodeChamberSlideActionButton    (self):
        self.Stacked.setCurrentWidget(self.CodeChamberSlideAction   )
    def SlotPickChamberSlideActionButton    (self):
        self.Stacked.setCurrentWidget(self.PickChamberSlideAction   )
    def SlotDropChamberSlideActionButton    (self):
        self.Stacked.setCurrentWidget(self.DropChamberSlideAction    )
    def SlotDropTraySlideActionButton       (self):
        self.Stacked.setCurrentWidget(self.DropTraySlideAction      )
    def SlotPickTraySlideActionButton       (self):
        self.Stacked.setCurrentWidget(self.PickTraySlideAction      )
    def SlotSafeHitBotActionButton          (self):
        self.Stacked.setCurrentWidget(self.SafeHitBotAction         )
    def SlotBrokenDropActionButton          (self):
        pass


        # self.Stacked.setCurrentWidget(self.BrokenDropAction         )
    def SlotChamberToTrayActionButton       (self):
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber()
        if not self.isAvailable():
            return
        """ 判断当前托盘是否可以放片"""
        if OperatingStatusModel.IsTrayAvailable(TrayNumber):
            """托盘有片还要放片的错误"""
            return
        self.business.status.setRunStatus(True)
        isWrong: int = self.business.ChamberPick.WaitDone(ChamberNumber=ChamberNumber, RowNumber=RowNumber, isSafe=False)
        if RECODE_SUCCEED != isWrong:
            return
        if not self.business.hitbot.IsGrab():
            return

        isWrong = self.business.CodeScan.WaitDone(ChamberNumber=ChamberNumber, RowNumber=RowNumber, isSafe=False)
        if RECODE_SUCCEED != isWrong:
            return

        self.business.TrayDrop.WaitDone(ChamberNumber=ChamberNumber, RowNumber=RowNumber, TrayNumber=TrayNumber)
        self.business.status.setRunStatus(False)


    def SlotTrayToChamberActionButton       (self):
        ChamberNumber   : int
        RowNumber       : int
        TrayNumber      : int
        isWrong         : int
        ChamberNumber, RowNumber    = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber                  = self.Operation.SelectChamber.getTrayNumber()
        self.business.status.setRunStatus(True)
        if not self.isAvailable():
            return
        """ 判断当前托盘是否可以放片"""
        if not OperatingStatusModel.IsTrayAvailable(TrayNumber):
            """托盘有片还要放片的错误"""
            return
        isWrong = self.business.TrayPick.WaitDone(ChamberNumber=ChamberNumber, RowNumber=RowNumber, TrayNumber=TrayNumber)
        OperatingStatusModel.setTrayAvailable(TrayNumber, False)
        if RECODE_SUCCEED != isWrong:
            return
        if not self.business.hitbot.IsGrab():
            return
        isWrong = self.business.ChamberDrop.WaitDone(ChamberNumber=ChamberNumber, RowNumber=RowNumber, isSafe=False)
        self.business.status.setRunStatus(False)

    def SlotDrawCheckStatusButton           (self):
        isVisible: bool = ChamberStatusView.GetInstance().isVisible()
        isVisible = not isVisible
        ChamberStatusView.GetInstance().setVisible(isVisible)
        if isVisible:
            ChamberStatusView.GetInstance().flushFrame()


        pass
    def SlotGetHitBotPositionButton         (self):
        self.initBusiness()
        if not self.hasHitBot:
            return
        model: HitBotModelInterface = self.hitbot.GetHitModel()
        self.HitBotControl.valueUpdated.emit(model)
        self.Operation.HitBotPosition.setModel(model)

    def SlotSetHitBotPositionButton         (self):
        self.initBusiness()
        self.business.hitbot.Close()
        self.business.hitbot.Open()
        self.business.hitbot.initHitBot()
        AppStatus.setHitBot(1 if self.business.hitbot.IsInitialize() else 0)



    def SlotInitHitBotXButton(self):
        if not self.IsHitBotOpen:
            return
        self.hitbot.AxisXHome()
    def SlotInitHitBotYButton(self):
        if not self.IsHitBotOpen:
            return
        self.hitbot.AxisYHome()
    def SlotInitHitBotZButton(self):
        if not self.IsHitBotOpen:
            return
        self.hitbot.AxisZHome()
    def SlotInitHitBotRButton(self):
        if not self.IsHitBotOpen:
            return
        self.hitbot.AxisRHome()
    def SlotGripperGrabButton(self):
        if not self.isAvailable():
            return
        print(self.hitbot.IsGrab())
        pass

    def SlotEmptyHitBotButton(self):
        isVisible: bool = HitBotChamberPositionView.GetInstance().isVisible()
        HitBotChamberPositionView.GetInstance().setVisible(not isVisible)

    def SlotGroupSlideMoveButton            (self):
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber   ()
        if not self.hasBusiness:
            return
        self.business.status.setRunStatus(True)
        self.business.status.waitForRunning()
        from AppBackEnd.BackEndEventBus.HitBotControllerPool import HitBotControllerPool
        Thread: HitBotControllerPool = HitBotControllerPool.GetInstance()
        try:
            Thread.Start(target=self.business.ChamberGroupSlideMoveFlow, args=(ChamberNumber, True,))
            Thread.waitForFinishing()
            print(Thread.result, Thread.args, Thread.kwargs)
            Thread.Stop()
        except Exception as e:
            AppUtils.print_Exception(e)

        # self.business.ChamberGroupSlideMoveFlow(chamber_number=ChamberNumber)
        self.business.status.setRunStatus(False)

    def SlotChamberSlideMoveButton          (self):
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber   ()
        if not self.hasBusiness:
            return
        self.business.status.setRunStatus(True)
        self.business.status.waitForRunning()
        self.business.ChamberSlideMoveFlow()
        self.business.status.setRunStatus(False)

    def SlotGroupSlideTableMoveButton       (self):
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber   ()
        if not self.hasBusiness:
            return
        self.business.status.setRunStatus(True)
        self.business.status.waitForRunning()
        from AppBackEnd.BackEndEventBus.HitBotControllerPool import HitBotControllerPool
        Thread: HitBotControllerPool = HitBotControllerPool.GetInstance()
        try:
            ProcessObject = self.business
            Thread.Start(target=self.business.GroupToSlideTableMoveFlow, args=(ChamberNumber, True,), kwargs=None)
            Thread.waitForFinishing()
            print(Thread.result, Thread.args, Thread.kwargs)
            Thread.Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        # self.business.GroupToSlideTableMoveFlow(chamber_number=ChamberNumber)
        self.business.status.setRunStatus(False)
    def SlotChamberSlideTableMoveButton     (self):
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber   ()
        if not self.hasBusiness:
            return
        self.business.status.setRunStatus(True)
        self.business.status.waitForRunning()
        self.business.ChamberToSlideTableMoveFlow()
        self.business.status.setRunStatus(False)

    def SlotGroupSlideScanningButton        (self):
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber   ()
        if not self.hasBusiness:
            return
        self.business.status.setRunStatus(True)
        self.business.status.waitForRunning()
        self.business.GroupSlideScanningFlow(chamber_number=ChamberNumber)
        self.business.status.setRunStatus(False)

    def SlotChamberSlideScanningButton      (self):
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber   ()
        if not self.hasBusiness:
            return
        self.business.status.waitForRunning()
        self.business.status.setRunStatus(True)
        self.business.ChamberSlideScanningFlow()
        self.business.status.setRunStatus(False)

    def SlotAsyncChamberSlideScanningButton (self):
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber    : int
        TrayNumber   : int
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber   ()
        if not self.hasBusiness:
            return

    def SlotPickDropGroupSlideButton         (self):
        """测试片仓取放动作"""
        if not self.isAvailable():
            return
        warning_string  : str
        ChamberNumber   : int
        RowNumber       : int
        TrayNumber      : int
        self.business.status.setRunStatus(True)
        MinRowNumber, MaxRowNumber = self.business.system.getRowRange()
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber               = self.Operation.SelectChamber.getTrayNumber()
        self.business.SlideScanCodeFlow(ChamberNumber=ChamberNumber, isSafe=True)
        self.business.status.setRunStatus(False)



    def SlotPickDropChamberSlideButton         (self):
        self.initBusiness()
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        self.business.WaitChamberOnlyScanCodeFlow()
        self.business.status.setRunStatus(False)

    def SlotPickDropChamberToTrayButton         (self):
        self.initBusiness()
        if not self.isAvailable():
            return
        ChamberNumber, RowNumber    = self.Operation.SelectChamber.getChamberNumber()
        TrayNumber                  = self.Operation.SelectChamber.getTrayNumber()
        self.business.status.setRunStatus(True)
        self.business.SlideTableMoveSlide(chamber_number=ChamberNumber, isSafe=True)
        self.business.status.setRunStatus(False)

    def SlotOperationControlSlideButton              (self):
        self.initBusiness()
        if not self.isAvailable():
            return
        self.business.status.setRunStatus(True)
        self.business.WaitMoveScanningFlow()
        self.business.status.setRunStatus(False)

    def SlotOperationControlTraySlideButton          (self):
        self.initBusiness()
        if not self.isAvailable():
            return
        ChamberNumber: int
        RowNumber: int
        self.business.status.setRunStatus(True)
        ChamberNumber, RowNumber = self.Operation.SelectChamber.getChamberNumber()
        self.business.ChamberRowWaitScanningFlow(ChamberNumber=ChamberNumber, RowNumber=RowNumber)
        self.business.status.setRunStatus(False)


    def SlotOperationControlTrayChamberButton        (self):
        self.initBusiness()
        if not self.isAvailable():
            return
        ChamberNumber: int
        self.business.status.setRunStatus(True)
        ChamberNumber, _ = self.Operation.SelectChamber.getChamberNumber()
        self.business.ChamberWaitScanningFlow(ChamberNumber=ChamberNumber)
        self.business.status.setRunStatus(False)

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        layout.setSpacing(1)
        layout.setContentsMargins(5, 1, 5, 1)

        l_layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        l_layout.setSpacing(1)
        l_layout.setContentsMargins(5, 1, 5, 1)
        top_layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        top_layout.setSpacing(1)
        top_layout.setContentsMargins(5, 1, 5, 1)

        top_layout.addWidget(self.HitBotControl)
        top_layout.addWidget(self.DrawData)
        l_layout.addLayout(top_layout)
        l_layout.addWidget(self.Operation)

        self.Stacked.addWidget(self.CheckChamberAction          )
        self.Stacked.addWidget(self.CheckChamberLSlideAction    )
        self.Stacked.addWidget(self.CheckChamberRSlideAction    )
        self.Stacked.addWidget(self.CodeChamberSlideAction      )
        self.Stacked.addWidget(self.PickChamberSlideAction      )
        self.Stacked.addWidget(self.DropChamberSlideAction      )
        self.Stacked.addWidget(self.DropTraySlideAction         )
        self.Stacked.addWidget(self.PickTraySlideAction         )
        self.Stacked.addWidget(self.SafeHitBotAction            )
        self.Stacked.addWidget(self.BrokenDropAction            )

        layout.addLayout(l_layout)
        layout.addLayout(self.Stacked)
        self.setLayout(layout)

    def initProperty(self):
        super(HitBotViews, self).initProperty()
        ChamberStatusView.GetInstance().setVisible(False)

    def initWindows(self):
        self.initStacked                     ()
        self.initHitBotControl               ()
        self.initOperation                   ()
        self.initCheckChamberAction          ()
        self.initCheckChamberLSlideAction    ()
        self.initCheckChamberRSlideAction    ()
        self.initCodeChamberSlideAction      ()
        self.initPickChamberSlideAction      ()
        self.initDropChamberSlideAction      ()
        self.initDropTraySlideAction         ()
        self.initPickTraySlideAction         ()
        self.initSafeHitBotAction            ()
        self.initBrokenDropAction            ()
        self.initDrawData                    ()

    def initStacked                     (self):
        if not self.hasStacked                     :
            self.Stacked                      = Qt.QStackedLayout   (                               )
    def initHitBotControl                     (self):
        if not self.hasHitBotControl                     :
            self.HitBotControl                      = HitBotControlView   (                           self)
    def initOperation                   (self):
        if not self.hasOperation                   :
            self.Operation                    = HitBotOperationView (self                           )


    def initCheckChamberAction          (self):
        if not self.hasCheckChamberAction          :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.CheckChamberAction           = HitBotModelView     (HitBotCheckChamberAction(), self)
            self.CheckChamberAction          .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)

    def initCheckChamberLSlideAction    (self):
        if not self.hasCheckChamberLSlideAction    :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.CheckChamberLSlideAction     = HitBotModelView     (HitBotCheckSlideLAction(), self)
            self.CheckChamberLSlideAction    .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)

    def initCheckChamberRSlideAction    (self):
        if not self.hasCheckChamberRSlideAction    :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.CheckChamberRSlideAction     = HitBotModelView     (HitBotCheckSlideRAction(), self)
            self.CheckChamberRSlideAction    .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)
    def initCodeChamberSlideAction      (self):
        if not self.hasCodeChamberSlideAction      :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.CodeChamberSlideAction       = HitBotModelView     (HitBotCodeScanAction   (), self)
            self.CodeChamberSlideAction      .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)

    def initPickChamberSlideAction      (self):
        if not self.hasPickChamberSlideAction      :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.PickChamberSlideAction       = HitBotModelView     (HitBotChamberPickAction(), self)
            self.PickChamberSlideAction      .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)
    def initDropChamberSlideAction      (self):
        if not self.hasDropChamberSlideAction      :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.DropChamberSlideAction       = HitBotModelView     (HitBotChamberDropAction(), self)
            self.DropChamberSlideAction      .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)
    def initDropTraySlideAction         (self):
        if not self.hasDropTraySlideAction         :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.DropTraySlideAction          = HitBotModelView     (HitBotTrayDropAction   (), self)
            self.DropTraySlideAction         .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)
    def initPickTraySlideAction         (self):
        if not self.hasPickTraySlideAction         :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.PickTraySlideAction          = HitBotModelView     (HitBotTrayPickAction   (), self)
            self.PickTraySlideAction         .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)
    def initSafeHitBotAction            (self):
        if not self.hasSafeHitBotAction            :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.SafeHitBotAction             = HitBotModelView     (HitBotSafeAction       (), self)
            self.SafeHitBotAction            .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)
    def initBrokenDropAction            (self):
        if not self.hasBrokenDropAction            :
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.BrokenDropAction             = HitBotModelView     (HitBotBrokenDropAction (), self)
            self.BrokenDropAction            .ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)

    @property
    def business(self) -> AppScanningFlow:
        return None if not self.hasBusiness else self.Business

    @property
    def devices(self) -> HardWareManager:
        return None if not self.hasDevices else self.Devices

    def initBusiness(self):
        if not self.hasBusiness:
            self.Business = AppScanningFlow.GetInstance()
    def initDevices(self):
        if not self.hasDevices:
            self.Devices = HardWareManager.GetInstance()


    @property
    def hasDevices(self):
        return hasattr(self, 'Devices') and isinstance(self.Devices, HardWareManager)

    @property
    def hasBusiness(self):
        return hasattr(self, 'Business') and isinstance(self.Business, AppScanningFlow)

    def initDrawData(self):
        if not self.hasDrawData:
            self.DrawData = DrawHitBotDataView(self)


    @property
    def hasStacked                     (self) -> bool:
        return hasattr(self, 'Stacked'                  ) and isinstance(self.Stacked                     , Qt.QStackedLayout   )

    @property
    def hasHitBotControl                     (self) -> bool:
        return hasattr(self, 'HitBotControl'                  ) and isinstance(self.HitBotControl         , HitBotControlView   )

    @property
    def hasOperation                   (self) -> bool:
        return hasattr(self, 'Operation'                ) and isinstance(self.Operation                   , HitBotOperationView )

    @property
    def hasCheckChamberAction          (self) -> bool:
        return hasattr(self, 'CheckChamberAction'       ) and isinstance(self.CheckChamberAction          , HitBotModelView     )

    @property
    def hasCheckChamberLSlideAction    (self) -> bool:
        return hasattr(self, 'CheckChamberLSlideAction' ) and isinstance(self.CheckChamberLSlideAction    , HitBotModelView     )

    @property
    def hasCheckChamberRSlideAction    (self) -> bool:
        return hasattr(self, 'CheckChamberRSlideAction' ) and isinstance(self.CheckChamberRSlideAction    , HitBotModelView     )

    @property
    def hasCodeChamberSlideAction      (self) -> bool:
        return hasattr(self, 'CodeChamberSlideAction'   ) and isinstance(self.CodeChamberSlideAction      , HitBotModelView     )

    @property
    def hasPickChamberSlideAction      (self) -> bool:
        return hasattr(self, 'PickChamberSlideAction'   ) and isinstance(self.PickChamberSlideAction      , HitBotModelView     )

    @property
    def hasDropChamberSlideAction      (self) -> bool:
        return hasattr(self, 'DropChamberSlideAction'   ) and isinstance(self.DropChamberSlideAction      , HitBotModelView     )

    @property
    def hasDropTraySlideAction         (self) -> bool:
        return hasattr(self, 'DropTraySlideAction'      ) and isinstance(self.DropTraySlideAction         , HitBotModelView     )

    @property
    def hasPickTraySlideAction         (self) -> bool:
        return hasattr(self, 'PickTraySlideAction'      ) and isinstance(self.PickTraySlideAction         , HitBotModelView     )

    @property
    def hasSafeHitBotAction            (self) -> bool:
        return hasattr(self, 'SafeHitBotAction'         ) and isinstance(self.SafeHitBotAction            , HitBotModelView     )

    @property
    def hasBrokenDropAction            (self) -> bool:
        return hasattr(self, 'BrokenDropAction'         ) and isinstance(self.BrokenDropAction            , HitBotModelView     )

    @property
    def hasDrawData(self) -> bool:
        return hasattr(self, 'DrawData') and isinstance(self.DrawData, DrawHitBotDataView)



    # @property
    # def business(self) -> AppScanningFlow:
    #     return None if not self.hasBusiness else self.Business


    @property
    def hasHitBot(self) -> bool:
        return self.hasDevices and self.Devices.hasHitBot

    @property
    def hasMotion(self) -> bool:
        return self.hasDevices and self.Devices.hasMotion

    @property
    def hasCamera(self) -> bool:
        return self.hasDevices and self.Devices.hasCamera

    @property
    def hitbot(self) -> HitBotReader:
        return None if not self.hasHitBot else self.Devices.hitbot

    @property
    def motion(self) -> MotionReader:
        return None if not self.hasMotion else self.Devices.motion

    @property
    def camera(self) -> CameraReader:
        return None if not self.hasCamera else self.Devices.camera