def add(device):
     if not _isDevice(device):
         raise TypeError(
             "device must be Device or subclass of Device but got '{}'".
             format(type(device)))
     if device.id not in __class__.__pool:
         __class__.__pool[device.id] = device
     else:
         logger.warning("device '{}' already in pool".format(device.id))
 def update(device):
     if not _isDevice(device):
         raise TypeError(
             "device must be Device or subclass of Device but got '{}'".
             format(type(device)))
     if device.id in __class__.__pool:
         __class__.__pool[device.id] = device
     else:
         logger.error("can't update device '{}' please add it first".format(
             device.id))
 def remove(device):
     if _isDevice(device):
         device = device.id
     elif type(device) is not str:
         raise TypeError(
             "device must be Device, subclass of Device or string (if ID only) but got '{}'"
             .format(type(device)))
     try:
         del __class__.__pool[device]
     except KeyError:
         logger.error(
             "device '{}' does not exist in device pool".format(device))
Ejemplo n.º 4
0
 def update(device) -> bool:
     """
     User method for updating devices.
     :param device: Device (or subclass of Device) object.
     :param kwargs: timeout=10, callback=None, block=True.
     :return: Boolean.
     """
     if not _isDevice(device):
         raise TypeError("device must be Device or subclass of Device but got '{}'".format(type(device)))
     __class__.__device_manager.update(device)
     if __class__.__ready:
         local_hash = _hashDevices(__class__.__device_manager.devices())
         if __class__.__put(device):
             if __class__.__commit(local_hash):
                 logger.info("updated device '{}'".format(device.name))
                 return True
     logger.warning("could not update device '{}'".format(device.name))
     return False
Ejemplo n.º 5
0
 def disconnect(device) -> bool:
     """
     User method for disconnecting devices.
     :param device: Device ID or a Device (or subclass of Device) object.
     :param kwargs: timeout=10, callback=None, block=True.
     :return: Boolean.
     """
     if _isDevice(device):
         device = device.id
     elif type(device) is not str:
         raise TypeError("device must be Device, subclass of Device or string (if ID only) but got '{}'".format(type(device)))
     __class__.__device_manager.remove(device)
     if __class__.__ready:
         local_hash = _hashDevices(__class__.__device_manager.devices())
         if __class__.__disconnect(device):
             if __class__.__commit(local_hash):
                 logger.info("disconnected device '{}'".format(device))
                 return True
     logger.warning("could not disconnect device '{}'".format(device))
     return False
Ejemplo n.º 6
0
 def event(device, service, data, metadata=None, **kwargs) -> Message:
     """
     User method for pushing events to the SEPL platform.
     :param device: Device ID or a Device (or subclass of Device) object.
     :param service: SEPL service.
     :param data: Event data as string.
     :param metadata: Event metadata.
     :param kwargs: timeout=10, callback=None, block=True.
     :return: Message object.
     """
     if _isDevice(device):
         d_id = device.id
     elif type(device) is str:
         d_id = device
     else:
         raise TypeError("device must be string, Device or subclass of Device but got '{}'".format(type(device)))
     if type(service) is not str:
         raise TypeError("service must be string but got '{}'".format(type(service)))
     event_msg = Message(handlers['event_handler'])
     event_msg.payload = {
         'device_uri': d_id,
         'service_uri': service,
         'value': [
             {
                 'name': 'metadata',
                 'value': metadata
             },
             {
                 'name': 'data',
                 'value': data
             }
         ]
     }
     if __class__.__ready:
         return __class__.__send(event_msg, **kwargs)
     logger.warning("could not send event: {}".format(event_msg.payload))