#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: SerialContext.py
 @DateTime: 2025/4/8 19:49
 @SoftWare: PyCharm
 @Author  ：xsh
"""
import sys
from PyQt5                              import Qt
from AppInterfaces.ModelInterface       import ContextModelInterface
from AppCore.Levels                     import *
from AppUtils.utils                     import *
from AppCore.Requests                   import *


class SerialContext(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):

        super(SerialContext, self).__init__()
        self.initialization("串口连接上下文")
        self.initParameter(**args)
    def initParameter(self, **args):
        super(SerialContext, 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(SerialContext, self).initValue()
        self.COM          =  "COM5"       #  端口名称
        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/ScanCodeAPI.dll"

    def initScanning(self):
        self.COM          =  "COM5"       #  端口名称
        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/ScanCodeAPI.dll"


    @property
    def absoluteFilePath(self) -> str:
        return AppUtils.toSourceFile(f"{self.API}")
    @property
    def hasAPI(self) -> bool:
        return self.has_field('API', str)
    @property

    def hasCOM(self) -> bool:
        return self.has_field('COM', str)
    @property
    def hasBaudRate(self) -> bool:
        return self.has_field('BaudRate', int)
    @property
    def hasDataBit(self) -> bool:
        return self.has_field('DataBit', int)
    @property
    def hasStopBit(self) -> bool:
        return self.has_field('StopBit', int)


    @property
    def hasParityBit(self) -> bool:
        return self.has_field('ParityBit', int)

    @property
    def hasIsOpen(self) -> bool:
        return self.has_field('IsOpen', bool)

































