hesong.ipsc.busnetcli.client module

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

class hesong.ipsc.busnetcli.client.Client(client_id, client_type, master_ip, master_port, slave_ip='', slave_port=None, user='', password='', info='')

基类:hesong.ipsc.busnetcli.utils.LoggerMixin

NET 客户端

注解

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

警告

实例一旦建立,无法释放!

参数:
  • client_id (int) – 本地 client id, >= 0 and <= 255
  • client_type (int) – 本地 client type
  • master_ip (str) – 主服务器IP地址
  • master_port (int) – 主服务器端口
  • slave_ip (str) – 从服务器IP地址。没有从地址的,填写0,或者”“
  • slave_port (int) – 从服务器端口。没有从端口的,填写0xFFFF
  • user (str) – 用户名
  • password (int) – 密码
  • info (str) – 附加信息
access_unit_id

连接点的UnitID

返回类型:int
activate()

激活客户端

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

注意

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

client_id

本地 client id

返回类型:int

>= 0 and <= 255

client_type

本地 client type

返回类型:int
classmethod create(*args, **kwargs)

建立实例

参数见构造函数

classmethod find(client_id, default=None)

查找并返回实例

参数:
  • client_id – 要查找的实例的 client_id
  • default – 如果找不到,返回这个值
返回:

找到的实例

返回类型:

Client

info

附加信息

返回类型:str
classmethod initialize(unit_id, global_connect_callback=None, lib_path='')

初始化

参数:
  • unit_id (int) – 在连接到 Smartbus 后,本节点的单元ID。
  • global_connect_callback (callable) – 全局连接事件回调函数
  • lib_path (str) – SO/DLL 文件路径名。 默认值是 None : 将按照 DLL_NAME 查找库文件,需确保库文件在 Python 运行时的搜索路径中。

警告

unit_id >= 16 ,且全局唯一,不得重复

全局节点客户端连接、断开事件,当smartbus上某个节点发生连接或者断开时触发。 其函数形如:

global_connect_callback(
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) – 连接附加信息
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

master_ip

主服务器IP地址

返回类型:str
master_port

主服务器端口

返回类型: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
password

密码

返回类型:str
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
classmethod release()

释放 Library

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
slave_ip

从服务器IP地址

返回类型:str
slave_port

从服务器端口

返回类型:int
unit_id

发生连接或断开事件的Smartbus节点单元ID

返回类型:int
user

用户名该

返回类型:str