def execute_task(self, task): coap_client = HelperClient("127.0.0.1", 5683) response = coap_client.get("/rd-lookup/ep?id={id}".format(id=task)) links = core_link_format_helper.parse_links(response) device = links[0] uri = request_helper.parse_url(device.get("path")) coap_client = HelperClient((uri.hostname, uri.port)) try: logger.info( "EXECUTER: Send command to {host}:{port} with id: {id}".format( host=uri.hostname, port=uri.port, id=id)) config = task.get("metadata") service = task.get("data") if config == "GET": coap_client.get(device.get("path") + service) elif config == "POST": coap_client.post(device.get("path") + service, task) elif config == "PUT": coap_client.put(device.get("path") + service, task) elif config == "DELETE": coap_client.delete(device.get("path") + service) logger.info("EXECUTER: Send command was sucsessful") except Exception as e: logger.error("EXECUTER: Send command was not successful") try: logger.info("EXECUTER: Send response to platform") Client.response(task, task) except Exception as e: logger.error( "OBSERVER: Send response to platform was not successful")
def run(self): logger.info("starting executor ...") while True: message = Client.receive() logger.debug(message) response = self.get_command(message) Client.response(message, response, metadata=None, timeout=10, callback=None, block=True)
def router(): while True: task = Client.receive() try: for part in task.payload.get('protocol_parts'): if part.get('name') == 'data': command = json.loads(part.get('value')) session = SessionManager.sessions.get(task.payload.get('device_url')) session.command_queue.put(command) Client.response(task, '200') except Exception as ex: Client.response(task, '500') logger.error("could not route command '{}' for '{}'".format(task.payload.get('protocol_parts'), task.payload.get('device_url'))) logger.error(ex)
def router(): while True: task = Client.receive() try: for part in task.payload.get('protocol_parts'): if part.get('name') == 'data': command = part.get('value') http_resp = http.put( '{}/lights/id:{}/state'.format( LIFX_CLOUD_URL, task.payload.get('device_url') ), command, headers={ 'Authorization': 'Bearer {}'.format(LIFX_API_KEY), 'Content-Type': 'application/json' } ) if http_resp.status not in (200, 207): logger.error("could not route message to LIFX API - '{}'".format(http_resp.status)) response = str(http_resp.status) except Exception as ex: logger.error("error handling task - '{}'".format(ex)) response = '500' Client.response(task, response)
data = json.dumps({'str_field': 'dummy event', 'int_field': event}) response = Client.event(id_4, 'dummy-event', data, 'dummy metadata {}'.format(event)) logger.info("event response '{}'".format(response.payload)) if 9 in tests: time.sleep(0.5) logger.info('------ receive command and respond ------') msg_obj = Client.receive() device_id = msg_obj.payload.get('device_url') service = msg_obj.payload.get('service_url') command = msg_obj.payload.get('protocol_parts') logger.info("received command for device '{}' on service '{}':".format( device_id, service)) logger.info(command) Client.response(msg_obj, '200', 'status') logger.info('sent response') if 10 in tests: time.sleep(0.5) logger.info('------ add existing device ------') new_device = Device(id_1, 'iot#1740e97f-1ae1-4547-a757-a62018083d3f', 'Dummy Device 1') Client.add(new_device) if 11 in tests: time.sleep(0.5) logger.info('------ disconnect unknown device ------') Client.disconnect('0okm9ijn') if 12 in tests: