Ejemplo n.º 1
0
def consumer_callback(channel, method, properties, body, options=None):
    serialised_request = data_unpack(body)
    ok, body = decode_json_body(serialised_request['request.body'])
    if body.get('type') == 'uplink':
        parse_everynet_request(serialised_request, body)
    # TODO: create function to normalise everynet json
    channel.basic_ack(method.delivery_tag)
 def handle_request(self, request):
     if request.method != 'POST':
         return HttpResponse('Only POST with JSON body is allowed',
                             status=405)
     serialised_request = serialize_django_request(request)
     ok, body = decode_json_body(serialised_request['request.body'])
     if ok is False:
         return HttpResponse(f'JSON ERROR: {body}',
                             status=400,
                             content_type='text/plain')
     try:
         devid = body['deviceId']
     except KeyError as err:
         return HttpResponse(
             f'Invalid request payload, no devid found: {body}',
             status=400,
             content_type='text/plain')
     serialised_request['devid'] = devid
     serialised_request['time'] = datetime.datetime.utcnow().isoformat(
     ) + 'Z'
     message = data_pack(serialised_request)
     key = create_routing_key('ruuvistation', devid)
     send_message(settings.RAW_HTTP_EXCHANGE, key, message)
     if devid is not None:
         datalogger, created = get_datalogger(devid=devid,
                                              update_activity=True)
     return HttpResponse('OK', content_type='text/plain')
 def handle_request(self, request):
     """
     Decode json body and send serialised request to RabbitMQ exchange
     :param request: Django HttpRequest
     :return: HttpResponse
     """
     if request.method != 'POST':
         return HttpResponse('Only POST with JSON body is allowed',
                             status=405)
     serialised_request = serialize_django_request(request)
     ok, body = decode_json_body(serialised_request['request.body'])
     if ok is False:
         err_msg = f'JSON ERROR: {body}'
         logging.error(err_msg)  # Error or warning?
         return HttpResponse(err_msg, status=400, content_type='text/plain')
     # TODO: this will fail if json is malformed
     devid = body['meta'].get('device', 'unknown')
     serialised_request['devid'] = devid
     serialised_request['time'] = datetime.datetime.utcnow().isoformat(
     ) + 'Z'
     logging.debug(json.dumps(body))
     message = data_pack(serialised_request)
     key = create_routing_key('everynet', devid)
     send_message(settings.RAW_HTTP_EXCHANGE, key, message)
     if body.get('type') == 'uplink':
         datalogger, created = get_datalogger(devid=devid,
                                              update_activity=True)
         if created:
             logging.info(f'Created new Datalogger {devid}')
     return HttpResponse('OK', content_type='text/plain')
Ejemplo n.º 4
0
def consumer_callback(channel, method, properties, body, options=None):
    serialised_request = data_unpack(body)
    ok, data = decode_json_body(serialised_request['request.body'])
    if ok:
        parse_sentilo_request(serialised_request, data)
        # logger.debug(json.dumps(data, indent=2))
    else:
        logger.warning(f'Failed to parse senstilo data.')
    channel.basic_ack(method.delivery_tag)
Ejemplo n.º 5
0
def consumer_callback(channel, method, properties, body, options=None):
    serialised_request = data_unpack(body)
    ok, data = decode_json_body(serialised_request['request.body'])
    if ok and 'DevEUI_uplink' in data:
        parse_thingpark_request(serialised_request, data)
        logger.debug(json.dumps(data, indent=2))
    else:
        logger.warning(f'DevEUI_uplink was not found in data.')
    channel.basic_ack(method.delivery_tag)
Ejemplo n.º 6
0
 def handle_request(self, request):
     serialised_request = serialize_django_request(request)
     ok, body = decode_json_body(serialised_request['request.body'])
     if ok is False:
         return HttpResponse(f'JSON ERROR: {body}',
                             status=400,
                             content_type='text/plain')
     key = body.get('key', '')
     key_splitted = key.split('/')
     if len(key_splitted) == 3:
         devid = '/'.join(key_splitted[:2])
     else:
         return HttpResponse(
             f'Key error: key "{key}" is not correctly formed',
             status=400,
             content_type='text/plain')
     datalogger, created = get_datalogger(devid=devid,
                                          update_activity=False,
                                          create=False)
     if datalogger is None:
         return HttpResponse(f'Datalogger "{devid}" does not exist',
                             status=400,
                             content_type='text/plain')
     data = body['data']
     location = data.pop('location')
     data.update(location)
     epoch = data.pop('timestamp') / 1000
     timestamp = epoch2datetime(epoch)
     dataline = create_dataline(timestamp, data)
     datalines = [dataline]
     parsed_data = create_parsed_data_message(devid, datalines)
     config = datalogger_get_config(datalogger, parsed_data)
     db_name = config.get('influxdb_database')
     measurement_name = config.get('influxdb_measurement')
     if db_name is not None and measurement_name is not None:
         save_parsed_data2influxdb(db_name, measurement_name, parsed_data)
         return HttpResponse('OK', content_type='text/plain')
     else:
         return HttpResponse(
             f'InfluxDB database and measurement are not defined for Datalogger "{devid}"',
             status=400,
             content_type='text/plain')