smartbus.ipc module

面向对象样式的 IPC 客户端 API 的 Python 封装

class smartbus.ipc.Client(client_id, client_type, user=None, password=None, info=None, lib_path='', event_executor=None)

基类:smartbus.utils.LoggerMixin

IPC 客户端

注解

使用 create() 或者 get_or_create() 建立实例,不要直接使用构造函数!

参数:
  • client_id (int) –
  • client_type (int) –
  • user (str) – 用户名
  • password (str) – 密码
  • info (str) – 附加信息
  • lib_path (str) – SO/DLL 文件路径名。 默认值是 None : 将按照 DLL_NAME 查找库文件,需确保库文件在 Python 运行时的搜索路径中。
  • event_executor (ThreadPoolExecutor) – 事件执行器。在此执行器中回调事件执行函数。 默认执行器的线程池数量是 1
引发:

SmartBusError – 如果建立连接的尝试失败

加载 Smartbus IPC 客户端 so/dll 到这个 Python 包, 并进行初始化。

activate()

激活客户端

将建立连接。连接一旦建立,就可以接受/发送数据,

注意

该函数立即返回,无论连接成功与否。 如该函数没有直接返回失败,客户端会自动尝试连接 smartbus 服务器,并在连接断开/失败时自动尝试重连

client_id
client_type
classmethod create(*args, **kwargs)

创建 Singleton 实例

返回:新建的实例
返回类型:Client
classmethod destroy()
dispose()

必须 调用该方法方可释放

classmethod get()

获取 Singleton 实例

返回:存在的实例
返回类型:Client
classmethod get_or_create(*args, **kwargs)

创建或者返回 Singleton 实例

返回:存在的或者新建的实例
返回类型:Client
  • 如果实例存在,相当于 get()
  • 如果实例不存在,相当于 create()
info
classmethod instance()

获取 Singleton 实例

返回:存在的实例
返回类型:Client
launch_flow(server_unit_id, process_index, project_id, flow_id, mode, timeout, params)

调用流程

参数:
  • server_unit_id (int) – 目标 IPSC 服务器 smartbus 单元ID
  • process_index (int) – IPSC 进程 ID ,同时也是该 IPSC 进程的 smartbus Client ID
  • project_id (str) – 流程项目ID
  • flow_id (str) – 流程ID
  • mode (int) – 调用模式:0 有流程返回、1 无流程返回
  • timeout (float) – 有流程返回时的等待超时值(秒)
  • params (list) – 流程输入参数里表。简单数据类型JSON数组。 子流程开始节点的传人参数自动变换为list类型数据。 对应的字符串内容最大长度不超过32K字节。
返回:

invoke_id,调用ID,用于流程结果返回匹配用途。

返回类型:

int

notify(server_unit_id, process_index, project_id, title, mode, expires, txt)

发送通知消息

参数:
  • server_unit_id (int) – 目标IPSC服务器 smartbus 单元ID
  • process_index (int) – IPSC 进程ID,同时也是该IPSC进程的 smartbus client-id
  • project_id (str) – 流程项目ID
  • title (str) – 通知的标题
  • mode (int) – 调用模式。目前无意义,一律使用0
  • expires (float) – 消息有效期。单位秒
  • txt (str) – 消息文本(对于 Python2 bytes string, 必须 使用 utf-8 编码)
返回:

调用任务的ID。

返回类型:

int

Except:

API返回错误

on_connect()

连接成功

on_connect_fail(error_code)

连接失败

on_data(head, data)

接收到了数据

参数:
on_disconnect()

连接断开

on_flow_ack(head, project_id, invoke_id, status_code, msg)

流程启动确认

参数:
  • head (Head) – 消息头
  • project_id (str) – 流程项目ID
  • invoke_id (int) – 流程调用ID
  • status_code (int) – 状态码. 1表示正确.
  • msg (str) – 错误信息
on_flow_error(head, project_id, invoke_id, error_code)

流程执行错误

参数:
  • head (Head) – 消息头
  • project_id (str) – 流程项目ID
  • invoke_id (int) – 流程调用ID
  • error_code (int) – 错误码
on_flow_resp(head, project_id, invoke_id, params)

调用流程结果返回

参数:
  • head (Head) – 消息头
  • project_id (str) – 流程项目ID
  • invoke_id (int) – 流程调用ID
  • params (list) – 流程返回值结果串, 采用 JSON Array 格式,对应于被调用流程的“子项目结束”节点的返回值列表
on_flow_timeout(head, project_id, invoke_id)

流程执行超时

参数:
  • head (Head) – 消息头
  • project_id (str) – 流程项目ID
  • invoke_id (int) – 流程调用ID
on_global_connect_state_changed(unit_id, client_id, client_type, access_unit_id, status_code, info)

全局节点客户端连接、断开事件

参数:
  • unit_id (int) – 发生连接或断开事件的Smartbus节点单元ID
  • client_id (int) – 发生连接或断开事件的Smartbus节点单元中的客户端ID
  • client_type (int) – 发生连接或断开事件的Smartbus节点单元中的客户端类型
  • access_unit_id (int) – 连接点的UnitID
  • status_code (int) – 连接状态码: 0 断开连接、1 新建连接、2 已有的连接
  • info (str) – 连接附加信息

当smartbus上某个节点发生连接或者断开时触发。

password
ping(dst_unit_id, dst_client_id, dst_client_type, data=None)

发送PING命令

参数:
  • dst_unit_id (int) – 目标的smartbus单元ID
  • dst_client_id (int) – 目标的smartbus客户端ID
  • dst_client_type (int) – 目标的smartbus客户端类型
  • data (bytes) – 待发送数据,类型必须是 bytes
send_data(cmd, cmd_type, dst_unit_id, dst_client_id, dst_client_type, data)

发送数据

参数:
  • cmd (int) – 命令
  • cmd_type (int) – 命令类型
  • dst_unit_id (int) – 目标节点ID
  • dst_client_id (int) – 目标客户端ID
  • dst_client_type (int) – 目标客户端类型
  • data (bytes) – 待发送数据,类型必须是 bytes
user