#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：ModBusPoolInterface.py
@Description：
@Author：XSH
@Time：2025/11/11 12:52
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from abc import abstractmethod
from AppInterfaces.AppImplInterfaces.ModBusImplInterface import ModBusImplInterface
from AppContexts.Contexts import ModBusContext, ModBusRegister
from AppInterfaces.ImplInterface import ImplInterface, ServerImplInterface, PoolInterface


class ModBusPoolInterface(PoolInterface):
    Parameter: ModBusContext

    @property
    def parameter(self) -> ModBusContext:
        return self.Parameter if self.hasParameter else None

    def setParameter(self, value):
        self.Parameter = value if isinstance(value, ModBusContext) else None

    @property
    def hasParameter(self) -> bool:
        return hasattr(self, 'Parameter') and isinstance(self.Parameter, ModBusContext)

    @abstractmethod
    def initContext(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def initPools(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def delePools(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def initManager(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def deleManager(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def initMaxPoolLength(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def initManagers(self, **args):
        raise Exception('Interface Instance error')

    @abstractmethod
    def deleManagers(self):
        raise Exception('Interface Instance error')

    @property
    def isAvailable(self) -> bool:
        return self.isValid and self.IsOpen


    @property
    def isValid(self) -> bool:
        return self.hasManagers and self.length() == self.MaxPoolLength

    def IsOpen(self) -> bool:
        manager: ImplInterface
        for manager in self.Managers:
            if not manager:
                continue
            if not manager.IsOpen:
                return False
        return True

    def Open(self, timeOut: int = 1000, **args) -> int:
        isWrong: int = 0
        if not self.hasManagers:
            self.initManagers()
        if not self.hasManagers:
            return isWrong
        if not self.hasPool():
            self.initPools()
        if not self.hasPool():
            return isWrong
        manager: ModBusImplInterface
        for manager in self.Managers:
            if manager:
                if not manager.IsOpen:
                    manager.Open(timeOut=timeOut, **args)
        isWrong = 1
        return isWrong


    def Close(self, timeOut: int = 1000, **args) -> int:
        isWrong: int = 0
        if not self.hasManagers:
            return isWrong
        manager: ModBusImplInterface
        for manager in self.Managers:
            if manager:
                manager.Close(timeOut=timeOut, **args)
        isWrong = 1
        return isWrong



