SmartBus client Python wrapper

内容目录:

smartbus

smartbus Package

smartbus Package

smartbus 的Python封装

包括 net 与 ipc 客户端的Python类型封装

date:2013-7-14
author:lxy@hesong.ent

_c_smartbus Module

smartbus.h 的Python封装

该模块大致实现了与 smartbus.h 一对一的 ctyps 封装。 请参考 smartbus.h

date:2013-6-1
author:lxy@hesong.ent
smartbus._c_smartbus.MAX_GLOBAL_SMART_NODE = 16

全局起始单元(节点)编号

smartbus._c_smartbus.MAX_SMARTBUS_NETCLI_UNITID_NUM = 32

net客户端值范围是16-47,全局最多32个。

smartbus._c_smartbus.MAX_SMARTBUS_NODE_CLI_NUM = 8

最大节点内的客户端数

smartbus._c_smartbus.MAX_SMARTBUS_NODE_NUM = 48

最大节点数

16 + 32 = 48 最大节点数

smartbus._c_smartbus.MIN_SMARTBUS_NETCLI_UNITID = 16

最小net客户端unitid值为16

Net客户端的unitid不能小于16

class smartbus._c_smartbus.PackInfo(lp_head_struct)

基类:builtins.object

对应 SMARTBUS_PACKET_HEAD 结构体的 ctypes 数据类型 _PACKET_HEAD 的再次封装

cmd

命令

一条 SmartBus 数据的命令关键字

cmdType

命令类型

一条 SmartBus 数据的命令类型

dataLen

正文数据长度

dstUnitClientId

接收者客户端ID

dstUnitClientType

接收者客户端类型

dstUnitId

接收者节点ID

packetSize

包长度

srcUnitClientId

发送者客户端ID

srcUnitClientType

发送者客户端类型

srcUnitId

发送者节点ID

errors Module

错误信息定义 :date: 2013-6-8 :author: lxy@hesong.ent

exception smartbus.errors.AlreadyExistsError

基类:builtins.Exception

对象已经存在,无法再次新建

exception smartbus.errors.AlreadyInitializedError

基类:builtins.Exception

已经初始化,无法重复的初始化

exception smartbus.errors.InvokeFlowIdError

基类:builtins.Exception

收到调用流程的返回结果时,ID无法匹配

exception smartbus.errors.NotInitializedError

基类:builtins.Exception

尚未初始化,无法使用

exception smartbus.errors.SmartBusError(code, message)

基类:builtins.Exception

SmartBus 通信错误

code

SmartBus错误码

message

错误信息

smartbus.errors.check_restval(code, raise_if_err=True)

检查 SmartBus 客户端 C-API 的返回结果是否正确

参数:
  • code (int) – 要检查的返回结果编码
  • raise_if_err (bool) – 是否在发现错误时抛出异常。默认为真。
返回:

raise_if_err 为 True 时,无错误则返回 None,有错误则抛出错误异常; 当 raise_if_err 为 False 时,无错误则返回 None,有错误则返回错误异常实例。

utils Module

辅助功能 :date: 2013-3-4 :author: tanbro

smartbus.utils.default_encoding = 'utf-8'

默认编码

sys.getdefaultencoding() 的返回值

smartbus.utils.ifnone(a, b)

如果第一个参数为None,则返回第二个参数,否则返回第一个参数 :param a: 第一个参数 :param b: 第二个参数 :return: 如果 a 为 None,则返回 b,否则返回 a。

相当于:

def ifnone(a, b):
   if a is None:
       return b
   else:
       return a

例如:

v1 = 1
v2 = 2
ifnone(v1, v2)

那么,返回值为1。

如果:

v1 = None
v2 = 2
ifnone(v1, v2)

那么,返回值为2。

smartbus.utils.unicode

str 的别名

smartbus.utils.to_bytes(s, encoding='utf-8')

str 转为 bytes

参数:
  • s (str, unicode, bytes, None) – 要转换的字符串
  • encoding – 编码,默认为系统编码。
smartbus.utils.to_unicode(s, encoding='utf-8')

str 转为 unicode

参数:
  • s (str, unicode, bytes, None) – 要转换的字符串
  • encoding – 编码,默认为系统编码。
smartbus.utils.to_str(s, encoding='utf-8')

unicode 或者 bytes 转为系统 str

Subpackages

ipcclient Package

ipcclient Package
_c_smartbus_ipccli_interface Module

‘smartbus 进程通信客户端C-API ctypes 对照翻译

使用ctypes将C-API的函数与基本数据结构做一对一的翻译,没有进行更进一步的包装。

date:2013-1-28
author:lxy@heosng.net
smartbus.ipcclient._c_smartbus_ipccli_interface.lib_filename = 'libbusipccli.so'

smartbus IPC 客户端默认的共享/动态库文件名。在WINNT系统下,默认是 busipccli.dll。

smartbus.ipcclient._c_smartbus_ipccli_interface.load_lib(filepath='libbusipccli.so')

加载共享/动态库

参数:filepath – 动态/共享库文件名
client Module

smartbus 进程间通信客户端的Python接口类客户端类型 :author: lxy@hesong.net :date: 2013-6-8

class smartbus.ipcclient.client.Client(username=None, password=None, extInfo=None, encoding='utf-8')

基类:builtins.object

SmartBus IPC 客户端类

这个类封装了 SmartBus IPC 客户端的一系列方法与事件

addr_expr
clientid
clienttype
connect(username=None, password=None)
classmethod finalize()
get_addr_expr()
get_clientid()
get_clienttype()
get_unitid()
classmethod initialize(clientid, clienttype, onglobalconnect=None, libraryfile='libbusipccli.so', logging_option=(True, 10, 40))

初始化

这是一个类方法。调用其他方法前,必须首先使用这个方法初始化库。 :param clientid: 客户端ID。在同一个节点中,ID必须唯一。不得小于等于16 :param clienttype: 客户端类型。 :param onglobalconnect: :param libraryfile:

classmethod instance(username=None, password=None, extInfo=None)
invokeFlow(server, process, project, flow, parameters=, []isNeedReturn=True, timeout=30)
classmethod isInitialized()
library
onConnectFail(unitId, errno)
onConnectSuccess(unitId)
onDisconnect()
onInvokeFlowAcknowledge(packInfo, project, invokeId, ack, msg)
onInvokeFlowError(packInfo, project, invokeId, errno)
onInvokeFlowRespond(packInfo, project, invokeId, result)
onInvokeFlowTimeout(packInfo, project, invokeId)
onReceiveText(packInfo, txt)
ping(dstUnitId, dstClientId, dstClientType, data, encoding=None)
send(cmd, cmdType, dstUnitId, dstClientId, dstClientType, data, encoding=None)
unitid

netclient Package

netclient Package
_c_smartbus_netcli_interface Module

smartbus 网络通信客户端C-API ctypes 对照翻译

使用ctypes将C-API的函数与基本数据结构做一对一的翻译,没有进行更进一步的包装。

date:2013-2-3
author:lxy@heosng.net
smartbus.netclient._c_smartbus_netcli_interface.load_lib(filepath='libbusnetcli.so')

加载共享/动态库

参数:filepath – 动态/共享库文件名
client Module
class smartbus.netclient.client.Client(localClientId, localClientType, masterHost, masterPort, slaverHost=None, slaverPort=65535, authorUsr=None, authorPwd=None, extInfo=None, encoding='utf-8')

基类:builtins.object

addr_expr
authorPwd
authorUsr
connect()
dispose()
encoding
extInfo
classmethod finalize()
classmethod getUnitId()
classmethod initialize(unitid, onglobalconnect=None, libraryfile='libbusnetcli.so', logging_option=(True, 10, 40))
invokeFlow(server, process, project, flow, parameters=, []isNeedReturn=True, timeout=30)
classmethod isInitialized()
localClientId
localClientType
masterHost
masterPort
onConnectFail(unitId, errno)
onConnectSuccess(unitId)
onDisconnect()
onInvokeFlowAcknowledge(packInfo, project, invokeId, ack, msg)
onInvokeFlowError(packInfo, project, invokeId, errno)
onInvokeFlowRespond(packInfo, project, invokeId, result)
onInvokeFlowTimeout(packInfo, project, invokeId)
onReceiveText(packInfo, txt)
ping(dstUnitId, dstClientId, dstClientType, data, encoding=None)
send(cmd, cmdType, dstUnitId, dstClientId, dstClientType, data, encoding=None)
sendNotify(server, process, project, title, mode, expires, param)
slaverHost
slaverPort
unitid