修改传送带电机、舵机、达妙电机等相关接口
This commit is contained in:
@ -116,6 +116,15 @@ class RelayController:
|
||||
self.sock = None
|
||||
self.is_connected = False # 长连接状态标记
|
||||
|
||||
# 初始化时自动尝试建立长连接
|
||||
print(f"正在初始化网络继电器并建立长连接")
|
||||
connect_success = self.connect()
|
||||
|
||||
if connect_success:
|
||||
print(f"网络继电器长连接建立成功:{HOST}:{PORT}")
|
||||
else:
|
||||
print(f"网络继电器长连接建立失败")
|
||||
|
||||
def connect(self) -> bool:
|
||||
"""
|
||||
建立长连接
|
||||
@ -241,27 +250,8 @@ class RelayController:
|
||||
|
||||
|
||||
# 全局变量
|
||||
GLOBAL_RELAY = None
|
||||
|
||||
# --------对外接口--------
|
||||
def init_relay():
|
||||
"""初始化网络继电器实例+建立长连接"""
|
||||
global GLOBAL_RELAY
|
||||
|
||||
try:
|
||||
GLOBAL_RELAY = RelayController()
|
||||
GLOBAL_RELAY.connect()
|
||||
|
||||
except Exception as e:
|
||||
raise ValueError(f"初始化失败{e}")
|
||||
|
||||
def deinit_relay():
|
||||
"""断开长连接"""
|
||||
global GLOBAL_RELAY
|
||||
|
||||
if GLOBAL_RELAY is not None:
|
||||
GLOBAL_RELAY.disconnect()
|
||||
GLOBAL_RELAY = None
|
||||
# private
|
||||
_GLOBAL_RELAY = RelayController()
|
||||
|
||||
def ng_push():
|
||||
"""NG推料流程"""
|
||||
@ -288,8 +278,7 @@ def write_do(device_name: str, state: bool):
|
||||
:param device_name: 设备名称
|
||||
:param state: True:打开 False:关闭
|
||||
"""
|
||||
global GLOBAL_RELAY
|
||||
if GLOBAL_RELAY is None:
|
||||
if _GLOBAL_RELAY is None:
|
||||
raise ValueError("未初始化实例")
|
||||
|
||||
# 验证设备是否存在
|
||||
@ -298,12 +287,12 @@ def write_do(device_name: str, state: bool):
|
||||
raise ValueError(f"无效的设备名 '{device_name}'。有效设备: {valid_devices}")
|
||||
|
||||
# 确保已连接
|
||||
if not GLOBAL_RELAY.is_connected:
|
||||
if not GLOBAL_RELAY.connect():
|
||||
if not _GLOBAL_RELAY.is_connected:
|
||||
if not _GLOBAL_RELAY.connect():
|
||||
raise RuntimeError("无法连接到网络继电器")
|
||||
|
||||
try:
|
||||
GLOBAL_RELAY.set_device(device_name, state)
|
||||
_GLOBAL_RELAY.set_device(device_name, state)
|
||||
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"控制设备 '{device_name}' 失败: {e}")
|
||||
@ -313,13 +302,12 @@ def read_all_io() -> dict[str, dict[str, bool]]:
|
||||
读取所有DI(传感器)和DO(设备)状态
|
||||
:return: {'devices': {...}, 'sensors': {...}}
|
||||
"""
|
||||
global GLOBAL_RELAY
|
||||
if GLOBAL_RELAY is None:
|
||||
if _GLOBAL_RELAY is None:
|
||||
raise ValueError("未初始化")
|
||||
|
||||
try:
|
||||
devices = GLOBAL_RELAY.get_all_device_status('devices')
|
||||
sensors = GLOBAL_RELAY.get_all_device_status('sensors')
|
||||
devices = _GLOBAL_RELAY.get_all_device_status('devices')
|
||||
sensors = _GLOBAL_RELAY.get_all_device_status('sensors')
|
||||
return {'devices': devices, 'sensors': sensors}
|
||||
|
||||
except Exception as e:
|
||||
@ -328,7 +316,6 @@ def read_all_io() -> dict[str, dict[str, bool]]:
|
||||
|
||||
# ------------测试接口-------------
|
||||
if __name__ == '__main__':
|
||||
init_relay()
|
||||
|
||||
write_do(SOLENOID_VALVE1, True)
|
||||
|
||||
@ -339,7 +326,6 @@ if __name__ == '__main__':
|
||||
status_str = "开启" if status else "关闭"
|
||||
print(f"{device_name_map.get(name, name)}: {status_str}")
|
||||
|
||||
deinit_relay()
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user