设备配置单元python插件连接库
devicehive-plugin的Python项目详细描述
设备配置单元插件
此库为设备配置单元插件API提供包装
安装
要安装此软件包,请运行:
pip install devicehive-plugin
使用plugin类创建客户机
首先需要创建自定义的Handler类。
Handler类提供了几个handle_*方法:* handle_connect(self)将在成功连接后调用* handle_event(self, event)将在任何类型的事件 收到。接受ApiEvent对象。* handle_command_insert(self, command)将在之后调用 command/insert事件已收到。拿 Command对象。* handle_command_update(self, command)将在之后调用 command/update事件已收到。拿 Command对象。* handle_notification(self, notification)将在之后调用 notification/insert事件已收到。拿 Notification对象。
handle_event将在特殊类型handle方法之前调用。
示例:
fromdevicehive_pluginimportHandlerclassSimpleHandler(Handler):defhandle_connect(self):print('Successfully connected')defhandle_event(self,event):print(event.action)print(type(event.data))defhandle_command_insert(self,command):print(command.command)defhandle_command_update(self,command):print(command.command)defhandle_notification(self,notification):print(notification.notification)
第二步是使用Plugin类创建到 服务器。
示例:
fromdevicehive_pluginimportHandlerfromdevicehive_pluginimportPluginclassSimpleHandler(Handler):defhandle_connect(self):print('Successfully connected')defhandle_event(self,event):print(event.action)print(type(event.data))defhandle_command_insert(self,command):print(command.command)defhandle_command_update(self,command):print(command.command)defhandle_notification(self,notification):print(notification.notification)url='ws://playground-dev.devicehive.com/plugin/proxy/'topic_name='PLUGIN_TOPIC_NAME'plugin_access_token='PLUGIN_ACCESS_TOKEN'plugin=Plugin(SimpleHandler)plugin.connect(url,topic_name,plugin_access_token=plugin_access_token)
自定义处理程序参数
如果需要初始化处理程序,您可以按以下方式进行:
fromdevicehive_pluginimportHandlerfromdevicehive_pluginimportPluginclassSimpleHandler(Handler):def__init__(self,api,some_arg,some_kwarg):super(SimpleHandler,self).__init__(api)self._some_arg=some_argself._some_kwarg=some_kwargplugin=Plugin(SimpleHandler,'some_arg',some_kwarg='some_kwarg')
身份验证
初始身份验证有几种方法:
- 使用插件的访问令牌
- 使用插件的刷新令牌
- 使用用户的访问令牌
- 使用用户的刷新令牌
- 使用用户的登录名和密码
如果你不想使用插件的访问令牌,你需要 提供auth_url参数。
示例:
url='ws://playground-dev.devicehive.com/plugin/proxy/'topic_name='PLUGIN_TOPIC_NAME'plugin.connect(url,topic_name,plugin_access_token='SOME_PLUGIN_ACCESS_TOKEN')
url='ws://playground-dev.devicehive.com/plugin/proxy/'topic_name='PLUGIN_TOPIC_NAME'auth_url='http://playground-dev.devicehive.com/api/rest'plugin.connect(url,topic_name,auth_url=auth_url,plugin_refresh_token='SOME_PLUGIN_REFRESH_TOKEN')
url='ws://playground-dev.devicehive.com/plugin/proxy/'topic_name='PLUGIN_TOPIC_NAME'auth_url='http://playground-dev.devicehive.com/api/rest'plugin.connect(url,topic_name,auth_url=auth_url,access_token='SOME_USER_ACCESS_TOKEN')
url='ws://playground-dev.devicehive.com/plugin/proxy/'topic_name='PLUGIN_TOPIC_NAME'auth_url='http://playground-dev.devicehive.com/api/rest'plugin.connect(url,topic_name,auth_url=auth_url,refresh_token='SOME_USER_REFRESH_TOKEN')
url='ws://playground-dev.devicehive.com/plugin/proxy/'topic_name='PLUGIN_TOPIC_NAME'auth_url='http://playground-dev.devicehive.com/api/rest'plugin.connect(url,topic_name,auth_url=auth_url,login='SOME_USER_LOGIN',password='SOME_USER_PASSWORD')
API参考
apievent对象
属性(只读):
- is_command_insert_event
- is_command_update_event
- is_command_event
- is_notification_event
- action
- raw_data
- data
命令对象
属性(只读):
- id
- user_id
- command
- parameters
- lifetime
- timestamp
- last_updated
- status
- result
通知对象
属性(只读):
- device_id
- id
- notification
- parameters
- timestamp
Docker测试
生成图像
docker build -f Dockerfile -t devicehive-plugin-tests .
运行测试
通过设置^{tt37},可以使用refresh_令牌运行测试$ 和/或CLIENT_REFRESH_TOKEN变量:
docker run -it -e ADMIN_REFRESH_TOKEN='SOME_ADMIN_REFRESH_TOKEN' devicehive-plugin-tests
或者通过设置ADMIN_ACCESS_TOKEN和/或使用访问令牌 CLIENT_ACCESS_TOKEN变量:
docker run -it -e ADMIN_ACCESS_TOKEN='SOME_ADMIN_ACCESS_TOKEN' devicehive-plugin-tests
或者通过设置ADMIN_LOGIN和 ADMIN_PASSWORD用于管理帐户和/或CLIENT_LOGIN和 CLIENT_PASSWORD用于客户端帐户。
docker run -it -e ADMIN_LOGIN='SOME_ADMIN_LOGIN' -e ADMIN_PASSWORD='SOME_ADMIN_PASSWORD' devicehive-plugin-tests
要使用已启用的请求日志运行测试,您需要更改 LOG_LEVEL变量:
docker run -it -e ADMIN_REFRESH_TOKEN='SOME_ADMIN_REFRESH_TOKEN' -e LOG_LEVEL='DEBUG' devicehive-plugin-tests
要运行特定的测试,您需要设置TEST变量:
docker run -it -e TEST=test_api.py::test_get_info -e ADMIN_REFRESH_TOKEN='SOME_ADMIN_REFRESH_TOKEN' devicehive-plugin-tests