def _evaluate(self, unknown_devices, init):
     missing_devices, new_devices, changed_devices = self._diff(__class__._known_devices, unknown_devices)
     if missing_devices:
         for missing_device_id in missing_devices:
             logger.info("can't find '{}' with id '{}'".format(__class__._known_devices[missing_device_id].get('name'), missing_device_id))
             del __class__.bridge_map[missing_device_id]
             if init:
                 DevicePool.remove(missing_device_id)
             else:
                 Client.disconnect(missing_device_id)
     if new_devices:
         for new_device_id in new_devices:
             name = unknown_devices[new_device_id].get('name')
             logger.info("found '{}' with id '{}'".format(name, new_device_id))
             __class__.bridge_map[new_device_id] = (unknown_devices[new_device_id].get('LIGHT_KEY'), Converter(get_light_gamut(unknown_devices[new_device_id].get('modelid'))))
             device = Device(new_device_id, SEPL_DEVICE_TYPE, name)
             device.addTag('type', unknown_devices[new_device_id].get('type'))
             device.addTag('manufacturer', unknown_devices[new_device_id].get('manufacturername'))
             if init:
                 DevicePool.add(device)
             else:
                 Client.add(device)
     if changed_devices:
         for changed_device_id in changed_devices:
             device = DevicePool.get(changed_device_id)
             name = unknown_devices[changed_device_id].get('name')
             if not name == device.name:
                 device.name = name
                 if init:
                     DevicePool.update(device)
                 else:
                     Client.update(device)
                 logger.info("name of '{}' changed to {}".format(changed_device_id, name))
     __class__._known_devices = unknown_devices
Exemplo n.º 2
0
 def ping(self, device_id):
     response = self.openhab_api_manager.get_thing(device_id)
     status = response.get("statusInfo")
     if status:
         if status.get("status") == "OFFLINE":
             Client.disconnect(device_id)
         elif status == "ONLINE":
             device = device_pool.DevicePool.get(device_id)
             Client.add(device)
Exemplo n.º 3
0
 def _evaluate(self, unknown_devices):
     missing_devices, new_devices, changed_devices = self._diff(__class__._known_devices, unknown_devices)
     if missing_devices:
         for missing_device_id in missing_devices:
             logger.info("can't find '{}' with id '{}'".format(__class__._known_devices[missing_device_id].get('label'), missing_device_id))
             try:
                 Client.delete(missing_device_id)
             except AttributeError:
                 DevicePool.remove(missing_device_id)
     if new_devices:
         for new_device_id in new_devices:
             name = unknown_devices[new_device_id].get('label')
             logger.info("found '{}' with id '{}'".format(name, new_device_id))
             device = Device(new_device_id, SEPL_DEVICE_TYPE, name)
             product = unknown_devices[new_device_id].get('product')
             device.addTag('type', 'Extended color light')
             device.addTag('product', product.get('name'))
             device.addTag('manufacturer', product.get('company'))
             try:
                 Client.add(device)
             except AttributeError:
                 DevicePool.add(device)
     if changed_devices:
         for changed_device_id in changed_devices:
             seconds_since_seen = unknown_devices[changed_device_id].get('seconds_since_seen')
             if seconds_since_seen >= 60:
                 try:
                     Client.disconnect(changed_device_id)
                 except AttributeError:
                     DevicePool.remove(changed_device_id)
                 del unknown_devices[changed_device_id]
             else:
                 device = DevicePool.get(changed_device_id)
                 name = unknown_devices[changed_device_id].get('label')
                 if not name == device.name:
                     device.name = name
                     try:
                         Client.update(device)
                     except AttributeError:
                         DevicePool.update(device)
                     logger.info("name of '{}' changed to {}".format(changed_device_id, name))
     __class__._known_devices = unknown_devices
Exemplo n.º 4
0
 def run(self):
     logger.debug("starting serial controller for device '{}'".format(
         self._id))
     self._writeSerialLog('serial connection open')
     self._writeSerialLog(self._greeting, 'D')
     if self._waitFor('RDY'):
         self._writeSerialLog('RDY', 'D')
         logger.info("started serial controller for device '{}'".format(
             self._id))
         logger.debug(self._serial_con)
         if self._configureDevice(init=True):
             try:
                 Client.add(self._device)
             except AttributeError:
                 DevicePool.add(self._device)
             if self._strt:
                 self.startDetection()
             while True:
                 try:
                     command, callbk, kwargs = self._commands.get(timeout=1)
                     if command != self._stopAction:
                         if kwargs:
                             command(callbk, **kwargs)
                         else:
                             command(callbk)
                     else:
                         callbk(409)
                 except Empty:
                     pass
                 except __class__.Interrupt:
                     break
             try:
                 Client.disconnect(self._device)
             except AttributeError:
                 DevicePool.remove(self._device)
     else:
         logger.error("device '{}' not ready".format(self._id))
     self._closeConnection()
     logger.info("serial controller for device '{}' exited".format(
         self._id))
Exemplo n.º 5
0
 def _evaluate(self, unknown_devices, init):
     missing_devices, new_devices, changed_devices = self._diff(
         __class__._known_devices, unknown_devices)
     if missing_devices:
         for missing_device_id in missing_devices:
             logger.info("can't find '{}' with id '{}'".format(
                 __class__._known_devices[missing_device_id].get(
                     'deviceName'), missing_device_id))
             if init:
                 DevicePool.remove(missing_device_id)
             else:
                 Client.disconnect(missing_device_id)
     if new_devices:
         for new_device_id in new_devices:
             name = unknown_devices[new_device_id].get('deviceName')
             logger.info("found '{}' with id '{}'".format(
                 name, new_device_id))
             device = BleboxDevice(new_device_id, SEPL_DEVICE_TYPE, name,
                                   unknown_devices[new_device_id].get('ip'))
             device.addTag('type',
                           unknown_devices[new_device_id].get('type'))
             if init:
                 DevicePool.add(device)
             else:
                 Client.add(device)
     if changed_devices:
         for changed_device_id in changed_devices:
             device = DevicePool.get(changed_device_id)
             name = unknown_devices[changed_device_id].get('deviceName')
             if not name == device.name:
                 device.name = name
                 if init:
                     DevicePool.update(device)
                 else:
                     Client.update(device)
                 logger.info("name of '{}' changed to {}".format(
                     changed_device_id, name))
     __class__._known_devices = unknown_devices
 def run(self):
     logger.info("starting session for '{}'".format(self.device.id))
     try:
         self.mqtt_c.connect(self.ip_address, self.port, keepalive=5)
         self.mqtt_c.loop_start()
         self.init_state.wait(timeout=10)
         if self.device.state:
             while not self.stop:
                 try:
                     command = self.command_queue.get(timeout=0.5)
                     state = self.device.state
                     for key, value in command.items():
                         if key in DysonDevice.state_map and value in DysonDevice.state_map[key]:
                             state[key] = value
                     payload = {
                         "msg": "STATE-SET",
                         "time": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
                         "mode-reason": "LAPP",
                         "data": state
                     }
                     self.mqtt_c.publish('{}/{}/command'.format(self.device.product_type, self.device.id), json.dumps(payload), 1)
                 except Empty:
                     pass
                 except Exception as ex:
                     logger.error("error handling command - '{}'".format(ex))
             try:
                 Client.disconnect(self.device)
             except AttributeError:
                 DevicePool.remove(self.device)
         else:
             self.mqtt_c.disconnect()
             logger.error("could not get device state for '{}'".format(self.device.id))
     except TimeoutError as ex:
         logger.error("could not connect to broker '{}' on '{}' - reason '{}'".format(self.ip_address, self.port, ex))
     self.mqtt_c.loop_stop()
     if self.device_sensor_request.is_alive():
         self.device_sensor_request.join()
     SessionManager.cleanSession(self.device.id)
        logger.info('------ remove tag on existing device ------')
        device = device_manager.get(id_2)
        device.removeTag('type')
        Client.update(device)

    if 4 in tests:
        time.sleep(0.5)
        logger.info('------ change name of existing device ------')
        device = device_manager.get(id_3)
        device.name = 'Dummy Smart Bulb'
        Client.update(device)

    if 5 in tests:
        time.sleep(0.5)
        logger.info('------ disconnect existing device ------')
        Client.disconnect(id_1)

    if 6 in tests:
        time.sleep(0.5)
        logger.info('------ delete existing device ------')
        Client.delete(id_3)

    if 7 in tests:
        time.sleep(0.5)
        logger.info('------ add new device ------')
        new_device = Device(id_4, 'iot#1740e97f-1ae1-4547-a757-a62018083d3f',
                            'Dummy Device 4')
        Client.add(new_device)

    if 8 in tests:
        time.sleep(0.5)
Exemplo n.º 8
0
    def run(self):
        while True:
            #todo die disconnected auf connected umstellen wenn wieder da,
            # allles auf connected stellen wenn nicht eh schon auf connected

            time.sleep(10)
            local_coap_client = HelperClient(server=("127.0.0.1", 5683))
            response = local_coap_client.get("/rd-lookup/ep")

            if response.payload:
                known_devices = core_link_format_helper.parse_links(
                    response.payload)
                # ping unicast instead of multicast, dont know if the device listens on multicast

                missing_devices = []
                reconnected_devices = []
                for device in known_devices:
                    url = request_helper.parse_url(device.get("path"))
                    device_was_connected = None
                    print(device)

                    if device.get("status") == '1':
                        device_was_connected = True
                    elif device.get("status") == '0':
                        device_was_connected = False

                    is_online = self.ping(url.hostname, url.port)
                    if not is_online and device_was_connected:
                        missing_devices.append(device)

                    if is_online and not device_was_connected:
                        reconnected_devices.append(device)

                for device in missing_devices:
                    id = device.get("id")
                    logger.info(
                        "PINGER: device {id} is not online -> disconnect".
                        format(id=id))
                    device = DB.DB.get(id)
                    try:
                        Client.disconnect(device)
                        logger.info(
                            "PINGER: device {id} was disconnected from RD and platform"
                            .format(id=id))
                    except Exception as e:
                        logger.error(
                            "PINGER: device {id} was not successful disconnected"
                            .format(id=id))
                        logger.error(e)

                for device in reconnected_devices:
                    id = device.get("id")
                    logger.info(
                        "PINGER: device {id} is online -> reconnect".format(
                            id=id))
                    device = DB.DB.get(id)
                    try:
                        # For reconnect, use add() - maybe connect() better ?
                        Client.add(device)
                        logger.info(
                            "PINGER: device {id} was added to platform".format(
                                id=id))
                    except Exception as e:
                        logger.error(
                            "PINGER: device {id} was not successful added to platform"
                            .format(id=id))