#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：LeiCaContext.py
@Description：
@Author：XSH
@Time：2026/1/12 17:49
@Department：研发部
@Copyright：©2026 乐辰生物
"""
from PyQt5                          import Qt
from AppInterfaces.ModelInterface   import ContextModelInterface
from AppUtils.AppUtils              import AppUtils
from AppUtils.AppConvert            import AppConvert

class LeiCaContext(ContextModelInterface):
    __slots__ = (
        'COM'         ,
        'BaudRate'    ,
        'DataBit'     ,
        'StopBit'     ,
        'ParityBit'   ,
        'FlowControl' ,
        'Direction'   ,
        'IsOpen'      ,
        'ReadWaitTime',
        'SpaceTime'   ,
        'API'         ,
    )
    __types__ = (
        str    ,
        int    ,
        int    ,
        int    ,
        int    ,
        int    ,
        int    ,
        bool   ,
        int    ,
        int    ,
        str    ,
    )
    COM          : str  #
    BaudRate     : int  #
    DataBit      : int  #
    StopBit      : int  #
    ParityBit    : int  #
    FlowControl  : int  #
    Direction    : int  #
    IsOpen       : bool #
    ReadWaitTime : int  #
    SpaceTime    : int  #
    API          : str  #
    def __init__(self, *args, **kwargs):
        super(LeiCaContext, self).__init__(*args, **kwargs)
        self.initialization("显微镜上下文")


    def initParameter(self, **args):
        super(LeiCaContext, self).initParameter(**args)
        if not isinstance(args, dict) or len(args) <= 0:
            return
        for index, _property in enumerate(self.__slots__):
            setattr(self, _property, AppConvert.ObjectConvert(args.get(_property, getattr(self, _property, None)),
                                                              self.__types__[index]))

    def initValue(self):
        super(LeiCaContext, self).initValue()
        self.COM          = "COM6"        #  端口名称
        self.BaudRate     = 9600          #  波特率　
        self.DataBit      = 8             #  数据位　
        self.StopBit      = 1             #  停止位　
        self.ParityBit    = 0             #  校验位　
        self.FlowControl  = 0             #  流控模式
        self.Direction    = 3             #  打开模式
        self.IsOpen       = False         #  打开状态
        self.ReadWaitTime = 500           #  读写时长
        self.SpaceTime    = 1000          #  读取间隔
        self.API          = "Library/LeiCaMicroscopeAPI.dll"

    def initMicroscope(self):
        self.COM          = "COM6"        #  端口名称
        self.BaudRate     = 9600          #  波特率　
        self.DataBit      = 8             #  数据位　
        self.StopBit      = 1             #  停止位　
        self.ParityBit    = 0             #  校验位　
        self.FlowControl  = 0             #  流控模式
        self.Direction    = 3             #  打开模式
        self.IsOpen       = False         #  打开状态
        self.ReadWaitTime = 500           #  读写时长
        self.SpaceTime    = 1000          #  读取间隔
        self.API          = "Library/LeiCaMicroscopeAPI.dll"

    @property
    def absoluteFilePath(self) -> str:
        return AppUtils.toSourceFile(self.API) if self.hasAPI else AppUtils.toSourceFile("Library/LeiCaMicroscopeAPI.dll")
    @property
    def hasCOM          (self) -> bool:
        return hasattr(self, 'COM'         ) and isinstance(self.COM          , str           )

    @property
    def hasBaudRate     (self) -> bool:
        return hasattr(self, 'BaudRate'    ) and isinstance(self.BaudRate     , (int, float)  )

    @property
    def hasDataBit      (self) -> bool:
        return hasattr(self, 'DataBit'     ) and isinstance(self.DataBit      , (int, float)  )

    @property
    def hasStopBit      (self) -> bool:
        return hasattr(self, 'StopBit'     ) and isinstance(self.StopBit      , (int, float)  )

    @property
    def hasParityBit    (self) -> bool:
        return hasattr(self, 'ParityBit'   ) and isinstance(self.ParityBit    , (int, float)  )

    @property
    def hasFlowControl  (self) -> bool:
        return hasattr(self, 'FlowControl' ) and isinstance(self.FlowControl  , (int, float)  )

    @property
    def hasDirection    (self) -> bool:
        return hasattr(self, 'Direction'   ) and isinstance(self.Direction    , (int, float)  )

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

    @property
    def hasReadWaitTime (self) -> bool:
        return hasattr(self, 'ReadWaitTime') and isinstance(self.ReadWaitTime , (int, float)  )

    @property
    def hasSpaceTime    (self) -> bool:
        return hasattr(self, 'SpaceTime'   ) and isinstance(self.SpaceTime    , (int, float)  )

    @property
    def hasAPI          (self) -> bool:
        return hasattr(self, 'API'         ) and isinstance(self.API          , str           ) and Qt.QLibrary.isLibrary(self.API)
