def handle_push_notification(self, namespace, payload, from_myself=False): # Handle the ONLINE push notification # Leave the rest to the specific implementation if namespace == ONLINE: old_online_status = self.online status = payload['online']['status'] if status == 2: with self._state_lock: self.online = False elif status == 1: with self._state_lock: self.online = True else: l.error( "Unknown online status has been reported from the device: %d" % status) # If the online status changed, fire the corresponding event if old_online_status != self.online: evt = DeviceOnlineStatusEvent(self, self.online) self.fire_event(evt) else: self._handle_push_notification(namespace, payload, from_myself=from_myself)
def handle_push_notification(self, namespace, payload, from_myself=False): # Handle the ONLINE push notification # Leave the rest to the specific implementation if namespace == ONLINE: old_online_status = self.online status = int(payload['online']['status']) if status == 2: with self._state_lock: self.online = False elif status == 1: with self._state_lock: self.online = True else: l.error( "Unknown online status has been reported from the device: %d" % status) # If the online status changed, fire the corresponding event if old_online_status != self.online: evt = DeviceOnlineStatusEvent(self, self.online) self.fire_event(evt) elif namespace == BIND: data = payload['bind'] evt = DeviceBindEvent(device=self, bind_data=data) self.fire_event(evt) elif namespace == UNBIND: # Let everybody know we are going down before doing anything evt = DeviceUnbindEvent(device=self) self.fire_event(evt) # Let's handle stat clearing and resource release self._handle_unbound() else: self._handle_push_notification(namespace, payload, from_myself=from_myself)
def _handle_device_discovered(self, dev): d_type = dev['deviceType'] d_uuid = dev['uuid'] device = build_wrapper(device_type=d_type, device_uuid=d_uuid, cloud_client=self._cloud_client, device_specs=dev) if device is not None: # Check if the discovered device is already in the list of handled devices. # If not, add it right away. Otherwise, ignore it. is_new = False new_dev = None with self._devices_lock: if d_uuid not in self._devices: is_new = True new_dev = build_wrapper(cloud_client=self._cloud_client, device_type=d_type, device_uuid=d_uuid, device_specs=dev) self._devices[d_uuid] = new_dev # If this is new device, register the event handler for it and fire the ONLINE event. if is_new: with self._event_callbacks_lock: for c in self._event_callbacks: new_dev.register_event_callback(c) evt = DeviceOnlineStatusEvent(new_dev, new_dev.online) self._fire_event(evt)
def _handle_push_notification(self, namespace, payload, from_myself=False): # The only "common" push notification for all sub-devices seems to be the HUB_ONLINE. # We handle this one only for generic sub devices. if namespace == HUB_ONLINE: online = self._raw_state.get('online') if online is None: online = {} self._raw_state['online'] = online online.update(payload) online_status = online.get('status')==1 evt = DeviceOnlineStatusEvent(dev=self, current_status=online_status) self.fire_event(evt) return True
def _handle_device_discovered(self, dev, parent_hub=None): device = None # Check whether we are dealing with a full device or with a subdevice if 'deviceType' in dev and 'uuid' in dev: # FULL DEVICE case d_type = dev['deviceType'] d_id = dev['uuid'] device_id = d_id device = build_wrapper(device_type=d_type, device_uuid=d_id, cloud_client=self._cloud_client, device_specs=dev) elif 'subDeviceType' in dev and 'subDeviceId' in dev: # SUB DEVICE case d_type = dev['subDeviceType'] d_id = dev['subDeviceId'] device_id = "%s:%s" % (parent_hub.uuid, d_id) device = build_subdevice_wrapper(device_type=d_type, device_id=d_id, parent_hub=parent_hub, cloud_client=self._cloud_client, device_specs=dev) else: l.warn("Discovered device does not seem to be either a full device nor a subdevice.") return if device is not None: # Check if the discovered device is already in the list of handled devices. # If not, add it right away. Otherwise, ignore it. is_new = False with self._devices_lock: if d_id not in self._devices: is_new = True self._devices[device_id] = device # If this is new device, register the event handler for it and fire the ONLINE event. if is_new: with self._event_callbacks_lock: for c in self._event_callbacks: device.register_event_callback(c) evt = DeviceOnlineStatusEvent(device, device.online) self._fire_event(evt) return device