Package: devicehive-plugin
Devicehive pluginThis library provides wrapper for DeviceHive plugin APIInstallationTo install this package run:pipinstalldevicehive-pluginCreating a client using Plugin classFirst of all you need to create customHandlerclass.Handlerclass provides severalhandle_*methods: *handle_connect(self)will be called after successful connection *handle_event(self, event)will be called after event of any type is
received. TakesApiEventobject. *handle_command_insert(self, command)will be called aftercommand/insertevent is received. TakesCommandobject. *handle_command_update(self, command)will be called aftercommand/updateevent is received. TakesCommandobject. *handle_notification(self, notification)will be called afternotification/insertevent is received. TakesNotificationobject.handle_eventwill be called before type-specialhandlemethods.Example: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)The second step is to usePluginclass for creating connection to
the server.Example: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)Custom handler argsIf you need to initialize your handler you can do it the next way: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')AuthenticationThere are several ways of initial authentication:Using plugin’s access tokenUsing plugin’s refresh tokenUsing user’s access tokenUsing user’s refresh tokenUsing user’s login and passwordIf you want to use anything but plugin’s access token you need to
provideauth_urlparameter.Examples: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 referenceApiEvent objectProperties (read only):is_command_insert_eventis_command_update_eventis_command_eventis_notification_eventactionraw_datadataCommand objectProperties (read only):iduser_idcommandparameterslifetimetimestamplast_updatedstatusresultNotification objectProperties (read only):device_ididnotificationparameterstimestampDocker testsBuild imagedocker build -f Dockerfile -t devicehive-plugin-tests .Run testsYou can run tests with refresh_token by settingADMIN_REFRESH_TOKENand/orCLIENT_REFRESH_TOKENvariable:docker run -it -e ADMIN_REFRESH_TOKEN='SOME_ADMIN_REFRESH_TOKEN' devicehive-plugin-testsOr with access_token by settingADMIN_ACCESS_TOKENand/orCLIENT_ACCESS_TOKENvariable:docker run -it -e ADMIN_ACCESS_TOKEN='SOME_ADMIN_ACCESS_TOKEN' devicehive-plugin-testsOr with user login and password by settingADMIN_LOGINandADMIN_PASSWORDfor admin account and/orCLIENT_LOGINandCLIENT_PASSWORDfor client account.docker run -it -e ADMIN_LOGIN='SOME_ADMIN_LOGIN' -e ADMIN_PASSWORD='SOME_ADMIN_PASSWORD' devicehive-plugin-testsTo run tests with enabled requests logging you need to changeLOG_LEVELvariable:docker run -it -e ADMIN_REFRESH_TOKEN='SOME_ADMIN_REFRESH_TOKEN' -e LOG_LEVEL='DEBUG' devicehive-plugin-testsTo run the specific test you need to setTESTvariable:docker run -it -e TEST=test_api.py::test_get_info -e ADMIN_REFRESH_TOKEN='SOME_ADMIN_REFRESH_TOKEN' devicehive-plugin-tests