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

class ModBusContext(ContextModelInterface):
    __slots__ = (
        'Addr'            ,
        'Port'            ,
        'IsOpen'          ,
        'TimeOut'         ,
        'ReconnectedTimes',
        'ReConnected'     ,
        'API'             ,
    )
    __types__ = (
        str   ,
        int   ,
        bool  ,
        float ,
        float ,
        int   ,
        str   ,
    )
    Addr             : str        #
    Port             : int        #
    IsOpen           : bool       #
    TimeOut          : float      #
    ReconnectedTimes : float      #
    ReConnected      : int        #
    API              : str        #

    def __init__(self, Addr            : str  = "192.168.10.208", Port            : int  = 502,
                 TimeOut         : float = 1.0, ReconnectedTimes: float = 500.0):
        super(ModBusContext, self).__init__()
        self.initialization("PLC上下文")
        self.initParameter(**{
            'Addr'            : Addr            ,
            'Port'            : Port            ,
            'IsOpen'          : False           ,
            'TimeOut'         : TimeOut         ,
            'ReconnectedTimes': ReconnectedTimes
        }
        )

    @classmethod
    def instance(cls, Addr            : str  = "192.168.10.208", Port            : int  = 502,
                 TimeOut         : float = 1.0, ReconnectedTimes: float = 500.0) -> 'ModBusContext':
        result: cls = cls.__new__(cls)
        result.__init__(Addr, Port, TimeOut, ReconnectedTimes)
        return result

    @classmethod
    def Instance(cls, **args) -> 'ModBusContext':
        result: cls = cls.__new__(cls)
        result.__init__()
        result.initParameter(**args)
        return result

    def initParameter(self, **args):
        super(ModBusContext, 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(ModBusContext, self).initValue()
        self.Addr             = "192.168.10.208"      # 连接地址
        self.Port             = 502                   # 连接端口
        self.IsOpen           = False                 # 连接状态
        self.TimeOut          = 0.5                   # 超时时长
        self.ReconnectedTimes = 5                     # 重联时长
        self.ReConnected      = 5                     # 重联时长
        self.API              = 'Library/ModBusAPI.dll'

    @property
    def absoluteFilePath(self) -> str:
        if not self.hasAPI:
            return AppUtils.toSourceFile("Library/ModBusAPI.dll")
        return AppUtils.toSourceFile(self.API)

    @property
    def hasAddr             (self) -> bool:
        return hasattr(self, 'Addr'            ) and isinstance(self.Addr             , str               )

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

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

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

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

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

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





























































