用于eclipseiofog开发的pythonsdk。
iofog的Python项目详细描述
ioFog Python SDK
此SDK提供以下模块:
- microservices:微服务与ioFog对话的客户端(例如ioMessage和websocket客户端)
- 在休息控制器:控制器REST API的客户端
安装
安装python包:
sudo python3 -m pip install iofog
微服务
这个模块允许您轻松地构建ioElement。通过ioFog提供与本地API交互的所有功能。它还包含IoMessage转换所需的所有方法。在
- 向ioFog发送新消息(post_消息)
- 从下一个fog获取未读邮件
- 获取时间段的消息和可访问的发布者列表(在时间范围内从发布者获取下一条消息)
- 获取配置选项(获取配置)
- 创建IoMessage,将原始字节编码(解码)到(从)原始字节,将marshall(解组)转换为(来自)JSON对象(IoMessage类方法)
- 通过WebSocket连接到ioFog控制通道(建立连接)
- 通过WebSocket连接到ioFog消息通道(建立连接)并通过这个通道发布新消息(通过套接字发布消息)
代码片段:
将iofog客户端和其他类导入项目:
^{pr2}$使用默认设置创建IoFog客户端:
try:client=IoFogClient()exceptIoFogExceptionase:# client creation failed, e contains description
或显式指定主机、端口、ssl和容器id:
try:client=IoFogClient(id='container_id',host='iofog_host',port=6666)exceptIoFogExceptionase:# client creation failed, e contains description
休息通话
获取下一个未读邮件的列表:
try:messages=client.get_next_messages()exceptIoFogExceptionase:# some error occurred, e contains description
通过REST call向ioFog发布新的IoMessage:
msg=IoMessage()msg.infotype="infotype"msg.infoformat="infoformat"msg.contentdata="sdkjhwrtiy8wrtgSDFOiuhsrgowh4touwsdhsDFDSKJhsdkljasjklweklfjwhefiauhw98p328946982weiusfhsdkufhaskldjfslkjdhfalsjdf=serg4towhr"msg.contextdata=""msg.tag="tag"msg.groupid="groupid"msg.authid="authid"msg.authgroup="authgroup"msg.hash="hash"msg.previoushash="previoushash"msg.nonce="nonce"try:receipt=client.post_message(msg)exceptIoFogException,e:# some error occurred, e contains description
在给定时间范围内从指定的发布者获取IoMessages数组:
query={'timeframestart':1234567890123,'timeframeend':1234567890123,'publishers':['sefhuiw4984twefsdoiuhsdf','d895y459rwdsifuhSDFKukuewf','SESD984wtsdidsiusidsufgsdfkh']}try:query_response=client.get_next_messages_from_publishers_within_timeframe(query)exceptIoFogException,e:# some error occurred, e contains description
获取容器的配置:
try:config=client.get_config()exceptIoFogException,ex:# some error occurred, ex contains description
WebSocket调用
要使用websocket连接,应按如下方式实现侦听器:
classMyControlListener(IoFogControlWsListener):defon_control_signal(self):# do smth on control signalclassMyMessageListener(IoFogMessageWsListener):defon_receipt(self,message_id,timestamp):# do smth with message receiptdefon_message(self,io_msg):# do smth with new message
之后,您可以建立websocket连接:
client.establish_message_ws_connection(MyMessageListener())client.establish_control_ws_connection(MyControlListener())
每个连接都将在一个单独的线程中进行管理。在
成功连接到消息websocket后,您可以发送到它:
client.post_message_via_socket(io_msg_instance)
消息实用程序
从JSON构造IoMessage(JSON字符串和python字典都可以接受):
msg=IoMessage.from_json(json_msg)
IoMessage到JSON:
json_str=io_msg_instance.to_json()
从原始字节构造IoMessage:
msg=IoMessage.from_bytearray([0,4,...])
将IoMessage打包到bytearray中:
msg_bytes=io_msg_instance.to_bytearray()
记录
fromiofog.microservices.logimportLoggerlog=Logger("first")log.debug("hello")log.info("world")log.warn("good")log.error("bye")
免责声明
这些模块正在进行中。它最初是作为一组helper函数编写的,python脚本使用它来部署一组使用yaml文件配置的微服务。在
强烈建议将我们的golang SDK作为控制器restapi客户端。在
- 项目
标签: