def run(self, handle_connect, handle_command_insert=None, handle_command_update=None, handle_notification=None, timeout=5): handler_kwargs = {'handle_connect': handle_connect, 'handle_command_insert': handle_command_insert, 'handle_command_update': handle_command_update, 'handle_notification': handle_notification} device_hive = DeviceHive(TestHandler, **handler_kwargs) device_hive.connect(self._transport_url, refresh_token=self._refresh_token) device_hive.join(timeout) exception_info = device_hive.exception_info() if not exception_info: return six.reraise(*exception_info)
def _dh_loop(self): self.dh_status.set_connecting() self.deviceHive = DeviceHive(DeviceHiveHandler, self.dh_cfg.data['deviceid']) error = '' try: self.dh_status.set_connected() url = self.dh_cfg.data['url'] refresh_token = self.dh_cfg.data['token'] self.deviceHive.connect(url, refresh_token=refresh_token) except TransportError as e: logger.exception(e) error = str(e) finally: self.dh_status.set_disconnected(error) logger.info('Stop devicehive')
def _dh_loop(self): self.deviceHive = DeviceHive(self._dh_handler_class, device_id=self.dh_cfg.data['device_id'], connect_cb=self._dh_connect, *self._dh_handler_args, **self._dh_handler_kwargs) error = '' try: self.dh_status.set_connecting() url = self.dh_cfg.data['url'] access_token = self.dh_cfg.data['a_token'] refresh_token = self.dh_cfg.data['r_token'] self.deviceHive.connect(url, access_token=access_token, refresh_token=refresh_token) except TransportError as e: logger.exception(e) error = str(e) finally: self.dh_status.set_disconnected(error) logger.info('Stop devicehive')
def run(self, handle_connect, handle_command_insert=None, handle_command_update=None, handle_notification=None, handle_timeout=60): handler_kwargs = {'handle_connect': handle_connect, 'handle_command_insert': handle_command_insert, 'handle_command_update': handle_command_update, 'handle_notification': handle_notification} device_hive = DeviceHive(TestHandler, **handler_kwargs) device_hive.connect(self._transport_url, transport_keep_alive=False, **self._credentials) start_time = time.time() while time.time() - handle_timeout < start_time: exception_info = device_hive.transport.exception_info if exception_info: six.reraise(*exception_info) if not device_hive.handler.api.connected: return time.sleep(self._timeout_sleep_time) raise TimeoutError('Waited too long for handle.')
self._device = None def handle_connect(self): self._device = self.api.put_device(self._device_id) self._device.subscribe_insert_commands( names=self._accept_command_names) self._device.subscribe_notifications() def handle_command_insert(self, command): print('Accept commands "%s"' % self._accept_command_names) command.status = 'accepted' command.save() def handle_notification(self, notification): print('Notification "%s" received' % notification.notification) if notification.notification[5:7] == 'on': self._device.data = {'TV': 'ON'} print("Your very smart TV turned ON!") self._device.save() else: if (notification.notification[5:8] == 'off'): self._device.data = {'TV': 'OFF'} print("Your very smart TV turned OFF") self._device.save() url = 'ws://playground.devicehive.com/api/websocket' refresh_token = 'your refresh token' dh = DeviceHive(ReceiverHandler) dh.connect(url, refresh_token=refresh_token)