107 lines
2.8 KiB
Python
107 lines
2.8 KiB
Python
|
|
#!/opt/homebrew/bin/python3
|
||
|
|
# -*- coding:utf-8 -*-
|
||
|
|
|
||
|
|
from PyQt6 import *
|
||
|
|
from PyQt6.QtCore import *
|
||
|
|
from logs import log
|
||
|
|
|
||
|
|
class SessionAbstract(QObject):
|
||
|
|
connectSuccess = pyqtSignal(str)
|
||
|
|
connectClosed = pyqtSignal(str)
|
||
|
|
connectFailed = pyqtSignal(str)
|
||
|
|
__supportedType = {
|
||
|
|
"general" : "通用接口",
|
||
|
|
"serial" : "串口",
|
||
|
|
"tbus" : "RS-TBUS",
|
||
|
|
"tcp" : "TCP",
|
||
|
|
"tbus_ns" : "非标IFB_旋转测试台专用接口",
|
||
|
|
"tbus_tcp" : "TCP-TBUS",
|
||
|
|
# "udp" : "UDP",
|
||
|
|
"http": "HTTP",
|
||
|
|
}
|
||
|
|
|
||
|
|
def __init__(self,id, name, type, attrs = {}, parent=None):
|
||
|
|
super().__init__()
|
||
|
|
self.__lock = QMutex() # 私有成员,不可继承,不可外部使用
|
||
|
|
self.__openState = False # 私有成员,不可继承,不可外部使用
|
||
|
|
self.__lockState = False # 私有成员,不可继承,不可外部使用
|
||
|
|
self.__usingCount = 0 # 私有成员,不可继承,不可外部使用
|
||
|
|
if type not in SessionAbstract.__supportedType.keys():
|
||
|
|
log.error("unsupported interface type:", type)
|
||
|
|
self._PROTECTED__type = ""
|
||
|
|
return False
|
||
|
|
self.id = id
|
||
|
|
self.name = name
|
||
|
|
self._PROTECTED__type = type # 保护成员,可继承,不建议外部使用
|
||
|
|
self._PROTECTED__attrs = {} # 保护成员,可继承,不建议外部使用
|
||
|
|
self._PROTECTED__attrs.update(attrs)
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
def getSupportedType():
|
||
|
|
return SessionAbstract.__supportedType
|
||
|
|
|
||
|
|
def setAttrs(self, attrs):
|
||
|
|
self._PROTECTED__attrs.update(attrs)
|
||
|
|
|
||
|
|
def getAttrs(self):
|
||
|
|
return self._PROTECTED__attrs
|
||
|
|
|
||
|
|
def getType(self):
|
||
|
|
return self._PROTECTED__type
|
||
|
|
|
||
|
|
def open(self):
|
||
|
|
self.__openState = True
|
||
|
|
return True
|
||
|
|
|
||
|
|
def startAutoReconnect(self):
|
||
|
|
return True
|
||
|
|
|
||
|
|
def stopAutoReconnect(self):
|
||
|
|
return True
|
||
|
|
|
||
|
|
def isOpen(self):
|
||
|
|
return self.__openState
|
||
|
|
|
||
|
|
def isConnect(self):
|
||
|
|
return True
|
||
|
|
|
||
|
|
@pyqtSlot(dict)
|
||
|
|
def ioctrl(self, data: dict):
|
||
|
|
pass
|
||
|
|
|
||
|
|
def close(self):
|
||
|
|
self.__openState = False
|
||
|
|
|
||
|
|
def getLockState(self):
|
||
|
|
return self.__lockState
|
||
|
|
|
||
|
|
def lock(self):
|
||
|
|
self.__lockState = self.__lock.tryLock()
|
||
|
|
return self.__lockState
|
||
|
|
|
||
|
|
def unlock(self):
|
||
|
|
if self.__lockState:
|
||
|
|
self.__lockState = False
|
||
|
|
self.__lock.unlock()
|
||
|
|
|
||
|
|
# 声明使用此接口
|
||
|
|
def declearUsing(self):
|
||
|
|
self.__usingCount += 1
|
||
|
|
|
||
|
|
# 声明不再使用此接口
|
||
|
|
def declearUnusing(self):
|
||
|
|
if self.__usingCount > 0:
|
||
|
|
self.__usingCount -= 1
|
||
|
|
|
||
|
|
# 查询此接口的使用状态
|
||
|
|
def getUsingState(self):
|
||
|
|
if self.__usingCount > 0:
|
||
|
|
return True
|
||
|
|
else:
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|