#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: HardWareStatusModel.py
 @DateTime: 2025/6/29 11:26
 @SoftWare: PyCharm
 @Author  ：xsh
"""
from PyQt5 import Qt
from datetime import datetime, date
import copy
from AppCore.AppDBModels.CoreModel import (CoreModel, DBManager, dbController)
from sqlalchemy.orm import declarative_base, mapper, sessionmaker, Session
from sqlalchemy.orm import (declarative_base, Mapped, mapped_column, relationship, mapper, sessionmaker)
from sqlalchemy import Table, Engine, create_engine, Column, ForeignKey, and_, or_, insert, delete, update, select, func, ColumnElement,distinct
from sqlalchemy.types import (Text, VARCHAR, REAL, Float, Integer, String, SmallInteger, DateTime, Date, ARRAY,Boolean, JSON)
from sqlalchemy.orm.decl_api import DeclarativeBase, MetaData
from AppUtils.utils import *
from AppCore.Levels import *
class OperatingStatusModel(CoreModel):
    __tablename__ = 't_OperatingStatusModel'
    ID              : Mapped[int                ] = mapped_column(primary_key=True                  , nullable=False)
    Microscope      : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　显微镜状态　　', comment='　显微镜状态　　')
    WebSocket       : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　网络服务状态　', comment='　网络服务状态　')
    HitBot          : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　机械臂状态　　', comment='　机械臂状态　　')
    AxisX           : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='载玻台Ｘ轴状态　', comment='载玻台Ｘ轴状态　')
    AxisY           : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='载玻台Ｙ轴状态　', comment='载玻台Ｙ轴状态　')
    TrayZ           : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='载玻台Ｚ轴状态　', comment='载玻台Ｚ轴状态　')
    TrayH           : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='载玻台Ｈ轴状态　', comment='载玻台Ｈ轴状态　')
    Camera          : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　相机状态　　　', comment='　相机状态　　　')
    ColorCamera     : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　彩色相机状态　', comment='　彩色相机状态　')
    ScanCode        : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　扫描器状态　　', comment='　扫描器状态　　')
    Oil             : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　镜油管状态　　', comment='　镜油管状态　　')
    Chamber         : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　载玻片片仓状态', comment='　载玻片片仓状态')
    Buzzer          : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　蜂鸣器器状态　', comment='　蜂鸣器器状态　')
    Door            : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='仪器门传感器状态', comment='仪器门传感器状态')
    TrayStatus      : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='当前载玻台号状态', comment='当前载玻台号状态')
    AutoScanMode    : Mapped[int                ] = mapped_column(default=1                         , nullable=True , doc='玻片自动扫描模式', comment='玻片自动扫描模式')
    JumpScanning    : Mapped[int                ] = mapped_column(default=0                         , nullable=True , doc='　　跳过扫描　　', comment='　　跳过扫描　　')
    def __init__(self, **args):
        super(OperatingStatusModel, self).__init__()
        self.initialize()
        self.initParameter(**args)


    def initValue(self):
        super(OperatingStatusModel, self).initValue()
        self.ID              = 1
        self.Microscope      = 0
        self.WebSocket       = 0
        self.HitBot          = 0
        self.AxisX           = 0
        self.AxisY           = 0
        self.TrayZ           = 0
        self.TrayH           = 0
        self.Camera          = 0
        self.ColorCamera     = 0
        self.ScanCode        = 0
        self.Oil             = 0
        self.Chamber         = 0
        self.Buzzer          = 0
        self.Door            = 0
        self.TrayStatus      = 0
        self.AutoScanMode    = 1
        self.JumpScanning    = 0




    @classmethod
    def initModels(cls):
        return [cls.instance(ID              = 1,Microscope      = 0,WebSocket       = 0,HitBot          = 0,AxisX           = 0,AxisY           = 0,TrayZ           = 0,TrayH           = 0,Camera          = 0,ColorCamera     = 0,ScanCode        = 0,Oil             = 0,Chamber         = 0,Buzzer          = 0,Door            = 0,TrayStatus      = 0,AutoScanMode    = 1,JumpScanning    = 0)]

    @classmethod
    def changed(cls, data: dict):
        item: cls
        try:
            dbController.initEngine()
            with Session(dbController.Engine) as session:
                session.query(cls).update(data)
                session.commit()
                session.close()
        except Exception as e:
            from AppLog.logHelper import logDB
            logDB << AppUtils.toString(e)
        return


    @classmethod
    def isManipulatorAvailable(cls) -> (int, int):
        item: cls = cls.first()
        return item.AxisX, item.AxisY

    @classmethod
    def isTrayAvailable(cls) -> (int, int):
        item: cls = cls.first()
        return item.TrayZ, item.TrayH

    @classmethod
    def isManipulatorAvailable(cls) -> int:
        item: cls = cls.first()
        if item is None:
            return False
        if not item.GetAxisX or not item.GetAxisY:
            return False
        if not item.GetTrayZ or not item.GetTrayH:
            return False
        return True

    @classmethod
    def setManipulatorAvailable(cls, enable: (bool, int)):
        _enable: int = 1 if isinstance(enable, (bool, int)) and enable else 0
        cls.changed({cls.AxisX: _enable,
                     cls.AxisY: _enable,
                     cls.TrayZ: _enable,
                     cls.TrayH: _enable,
                     })



    @classmethod
    def setTrayAvailable(cls, z_enable: (int, bool), h_enable: (int, bool)):
        cls.changed({cls.TrayZ: 1 if isinstance(z_enable, (bool, int)) and z_enable else 0,
                     cls.TrayH: 1 if isinstance(h_enable, (bool, int)) and h_enable else 0,})

    @classmethod
    def setChamberStatus(cls, state: (bool, int)):
        status: int = 1 if isinstance(state, (int, bool)) and state else 0
        cls.changed({cls.Chamber: status})

    @classmethod
    def isRunAvailable(cls) -> int:
        item: cls = cls.first()
        if not item.isMicroscope        :
            return False
        if not item.isWebSocket         :
            return False
        if not item.isAxisX             :
            return False
        if not item.isAxisY             :
            return False
        if not item.isTrayZ             :
            return False
        if not item.isTrayH             :
            return False
        if not item.isCamera            and not item.isColorCamera:
            return False
        if not item.isScanCode          :
            return False
        if not item.isChamber           :
            return False
        if item.isBuzzer                :
            return False
        return True

    @classmethod
    def isAvailable(cls) -> int:
        item: cls = cls.first()
        if not item.GetMicroscope        :
            return False
        if not item.GetWebSocket         :
            return False
        if not item.GetAxisX             :
            return False
        if not item.GetAxisY             :
            return False
        if not item.GetTrayZ             :
            return False
        if not item.GetTrayH             :
            return False
        if not item.GetCamera            and not item.GetColorCamera:
            return False
        if not item.GetScanCode          :
            return False
        # if not item.GetChamber           :
        #     return False
        if item.GetOil                   :
            return False
        if item.GetBuzzer                :
            return False
        return True

    @classmethod
    def setAvailable(cls, enable: (bool, int)):
        _enable: int = 1 if isinstance(enable, (bool, int)) and enable else 0
        cls.changed(
            {
                cls.Microscope      : _enable,
                cls.WebSocket       : _enable,
                cls.AxisX           : _enable,
                cls.AxisY           : _enable,
                cls.TrayZ           : _enable,
                cls.TrayH           : _enable,
                cls.ScanCode        : _enable,
                cls.Chamber         : _enable,
            }
        )

    @classmethod
    def getHardWareInitStatus(cls) -> (int, int, int, int):
        item: cls
        try:
            dbController.initEngine()
            args_string: str = "cls.{1}".format(cls, cls.get_primary_name(cls))
            with Session(dbController.Engine) as session:
                Primary = session.query(func.max(eval(args_string))).scalar()
                item = session.query(cls).filter_by(**{cls.get_primary_name(cls): Primary}).first()
                session.close()
        except Exception as e:
            from AppLog.logHelper import logDB
            logDB << AppUtils.toString(e)
            item = cls.instance()
        return item.HitBotInit, item.MicroscopeInit, item.AxisXInit, item.AxisYInit



    @classmethod
    def SetBuzzerWarning  (cls):
        item: cls
        try:
            dbController.initEngine()
            args_string: str = "cls.{1}".format(cls, cls.get_primary_name(cls))
            with Session(dbController.Engine) as session:
                Primary = session.query(func.max(eval(args_string))).scalar()
                session.query(cls).filter_by(**{cls.get_primary_name(cls): Primary}).update({cls.Buzzer: RunStatusLevel.BuzzerWarning})
                session.commit()
                session.close()
        except Exception as e:
            from AppLog.logHelper import logDB
            logDB << AppUtils.toString(e)

    @classmethod
    def setMicroscopeAvailable      (cls, enable: (int, bool)):
        cls.changed({cls.Microscope      : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setWebSocketAvailable       (cls, enable: (int, bool)):
        cls.changed({cls.WebSocket       : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setHitBotAvailable          (cls, enable: (int, bool)):
        cls.changed({cls.HitBot          : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setAxisXAvailable           (cls, enable: (int, bool)):
        cls.changed({cls.AxisX           : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setAxisYAvailable           (cls, enable: (int, bool)):
        cls.changed({cls.AxisY           : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setTrayZAvailable           (cls, enable: (int, bool)):
        cls.changed({cls.TrayZ           : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setTrayHAvailable           (cls, enable: (int, bool)):
        cls.changed({cls.TrayH           : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setCameraAvailable          (cls, enable: (int, bool)):
        cls.changed({cls.Camera          : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setColorCameraAvailable     (cls, enable: (int, bool)):
        cls.changed({cls.ColorCamera     : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setScanCodeAvailable        (cls, enable: (int, bool)):
        cls.changed({cls.ScanCode        : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setOilAvailable             (cls, enable: (int, bool)):
        cls.changed({cls.Oil             : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setChamberAvailable         (cls, enable: (int, bool)):
        cls.changed({cls.Chamber         : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setBuzzerAvailable          (cls, enable: (int, bool)):
        cls.changed({cls.Buzzer          : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setDoorAvailable            (cls, enable: (int, bool)):
        cls.changed({cls.Door            : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setTrayStatus               (cls, enable: (int, bool)):
        cls.changed({cls.TrayStatus      : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setAutoScanMode             (cls, enable: (int, bool)):
        cls.changed({cls.AutoScanMode    : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def setJumpScanning             (cls, enable: (int, bool)):
        cls.changed({cls.JumpScanning    : 1 if isinstance(enable, (int, bool)) and enable else 0})

    @classmethod
    def isMicroscopeAvailable      (cls) -> int:
        item: cls = cls.first()
        return item.GetMicroscope                  #

    @classmethod
    def isWebSocketAvailable       (cls) -> int:
        item: cls = cls.first()
        return item.GetWebSocket                   #

    @classmethod
    def isHitBotAvailable          (cls) -> int:
        item: cls = cls.first()
        return item.GetHitBot                      #

    @classmethod
    def isAxisXAvailable           (cls) -> int:
        item: cls = cls.first()
        return item.GetAxisX                       #

    @classmethod
    def isAxisYAvailable           (cls) -> int:
        item: cls = cls.first()
        return item.GetAxisY                       #

    @classmethod
    def isTrayZAvailable           (cls) -> int:
        item: cls = cls.first()
        return item.GetTrayZ                       #

    @classmethod
    def isTrayHAvailable           (cls) -> int:
        item: cls = cls.first()
        return item.GetTrayH                       #

    @classmethod
    def isCameraAvailable          (cls) -> int:
        item: cls = cls.first()
        return item.GetCamera                      #

    @classmethod
    def isColorCameraAvailable     (cls) -> int:
        item: cls = cls.first()
        return item.GetColorCamera                 #

    @classmethod
    def isScanCodeAvailable        (cls) -> int:
        item: cls = cls.first()
        return item.GetScanCode                    #

    @classmethod
    def isOilAvailable             (cls) -> int:
        item: cls = cls.first()
        return item.GetOil                         #

    @classmethod
    def isChamberAvailable         (cls) -> int:
        item: cls = cls.first()
        return item.GetChamber                     #

    @classmethod
    def isBuzzerAvailable          (cls) -> int:
        item: cls = cls.first()
        return item.GetBuzzer                      #

    @classmethod
    def isDoorAvailable            (cls) -> int:
        item: cls = cls.first()
        return item.GetDoor                        #

    @classmethod
    def isTrayStatus               (cls) -> int:
        item: cls = cls.first()
        return item.GetTrayStatus                  #

    @classmethod
    def isAutoScanMode             (cls) -> int:
        item: cls = cls.first()
        return item.GetAutoScanMode                #

    @classmethod
    def isJumpScanning             (cls) -> int:
        item: cls = cls.first()
        return item.GetJumpScanning                #



    def SetID              (self, enable: (int, bool)):
        self.ID               = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetMicroscope      (self, enable: (int, bool)):
        self.Microscope       = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetWebSocket       (self, enable: (int, bool)):
        self.WebSocket        = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetHitBot          (self, enable: (int, bool)):
        self.HitBot           = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetAxisX           (self, enable: (int, bool)):
        self.AxisX            = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetAxisY           (self, enable: (int, bool)):
        self.AxisY            = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetTrayZ           (self, enable: (int, bool)):
        self.TrayZ            = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetTrayH           (self, enable: (int, bool)):
        self.TrayH            = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetCamera          (self, enable: (int, bool)):
        self.Camera           = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetColorCamera     (self, enable: (int, bool)):
        self.ColorCamera      = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetScanCode        (self, enable: (int, bool)):
        self.ScanCode         = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetOil             (self, enable: (int, bool)):
        self.Oil              = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetChamber         (self, enable: (int, bool)):
        self.Chamber          = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetBuzzer          (self, enable: (int, bool)):
        self.Buzzer           = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetDoor            (self, enable: (int, bool)):
        self.Door             = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetTrayStatus      (self, enable: (int, bool)):
        self.TrayStatus       = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetAutoScanMode    (self, enable: (int, bool)):
        self.AutoScanMode     = 1 if isinstance(enable, (int, bool)) and enable else 0

    def SetJumpScanning    (self, enable: (int, bool)):
        self.JumpScanning     = 1 if isinstance(enable, (int, bool)) and enable else 0

    @property
    def GetID              (self) -> int:
        return None if not self.hasID               else self.ID

    @property
    def GetMicroscope      (self) -> int:
        return None if not self.hasMicroscope       else self.Microscope

    @property
    def GetWebSocket       (self) -> int:
        return None if not self.hasWebSocket        else self.WebSocket

    @property
    def GetHitBot          (self) -> int:
        return None if not self.hasHitBot           else self.HitBot

    @property
    def GetAxisX           (self) -> int:
        return None if not self.hasAxisX            else self.AxisX

    @property
    def GetAxisY           (self) -> int:
        return None if not self.hasAxisY            else self.AxisY

    @property
    def GetTrayZ           (self) -> int:
        return None if not self.hasTrayZ            else self.TrayZ

    @property
    def GetTrayH           (self) -> int:
        return None if not self.hasTrayH            else self.TrayH

    @property
    def GetCamera          (self) -> int:
        return None if not self.hasCamera           else self.Camera

    @property
    def GetColorCamera     (self) -> int:
        return None if not self.hasColorCamera      else self.ColorCamera

    @property
    def GetScanCode        (self) -> int:
        return None if not self.hasScanCode         else self.ScanCode

    @property
    def GetOil             (self) -> int:
        return None if not self.hasOil              else self.Oil

    @property
    def GetChamber         (self) -> int:
        return None if not self.hasChamber          else self.Chamber

    @property
    def GetBuzzer          (self) -> int:
        return None if not self.hasBuzzer           else self.Buzzer

    @property
    def GetDoor            (self) -> int:
        return None if not self.hasDoor             else self.Door

    @property
    def GetTrayStatus      (self) -> int:
        return None if not self.hasTrayStatus       else self.TrayStatus

    @property
    def GetAutoScanMode    (self) -> int:
        return None if not self.hasAutoScanMode     else self.AutoScanMode

    @property
    def GetJumpScanning    (self) -> int:
        return None if not self.hasJumpScanning     else self.JumpScanning

    @property
    def hasID              (self) -> bool:
        return hasattr(self, 'ID'            ) and isinstance(self.ID              , int)

    @property
    def hasMicroscope      (self) -> bool:
        return hasattr(self, 'Microscope'    ) and isinstance(self.Microscope      , int)

    @property
    def hasWebSocket       (self) -> bool:
        return hasattr(self, 'WebSocket'     ) and isinstance(self.WebSocket       , int)

    @property
    def hasHitBot          (self) -> bool:
        return hasattr(self, 'HitBot'        ) and isinstance(self.HitBot          , int)

    @property
    def hasAxisX           (self) -> bool:
        return hasattr(self, 'AxisX'         ) and isinstance(self.AxisX           , int)

    @property
    def hasAxisY           (self) -> bool:
        return hasattr(self, 'AxisY'         ) and isinstance(self.AxisY           , int)

    @property
    def hasTrayZ           (self) -> bool:
        return hasattr(self, 'TrayZ'         ) and isinstance(self.TrayZ           , int)

    @property
    def hasTrayH           (self) -> bool:
        return hasattr(self, 'TrayH'         ) and isinstance(self.TrayH           , int)

    @property
    def hasCamera          (self) -> bool:
        return hasattr(self, 'Camera'        ) and isinstance(self.Camera          , int)

    @property
    def hasColorCamera     (self) -> bool:
        return hasattr(self, 'ColorCamera'   ) and isinstance(self.ColorCamera     , int)

    @property
    def hasScanCode        (self) -> bool:
        return hasattr(self, 'ScanCode'      ) and isinstance(self.ScanCode        , int)

    @property
    def hasOil             (self) -> bool:
        return hasattr(self, 'Oil'           ) and isinstance(self.Oil             , int)

    @property
    def hasChamber         (self) -> bool:
        return hasattr(self, 'Chamber'       ) and isinstance(self.Chamber         , int)

    @property
    def hasBuzzer          (self) -> bool:
        return hasattr(self, 'Buzzer'        ) and isinstance(self.Buzzer          , int)

    @property
    def hasDoor            (self) -> bool:
        return hasattr(self, 'Door'          ) and isinstance(self.Door            , int)

    @property
    def hasTrayStatus      (self) -> bool:
        return hasattr(self, 'TrayStatus'    ) and isinstance(self.TrayStatus      , int)

    @property
    def hasAutoScanMode    (self) -> bool:
        return hasattr(self, 'AutoScanMode'  ) and isinstance(self.AutoScanMode    , int)

    @property
    def hasJumpScanning    (self) -> bool:
        return hasattr(self, 'JumpScanning'  ) and isinstance(self.JumpScanning    , int)



















    __field__ = (
        'ID'            ,
        'Microscope'    ,
        'WebSocket'     ,
        'HitBot'        ,
        'AxisX'         ,
        'AxisY'         ,
        'TrayZ'         ,
        'TrayH'         ,
        'Camera'        ,
        'ColorCamera'   ,
        'ScanCode'      ,
        'Oil'           ,
        'Chamber'       ,
        'Buzzer'        ,
        'Door'          ,
        'TrayStatus'    ,
        'AutoScanMode'  ,
        'JumpScanning'  ,
    )

    __types__ = (
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
        int             ,
    )