import requests
import json
from celery import shared_task
from celery.utils.log import get_task_logger
from influxdb.exceptions import InfluxDBClientError

from endpoints.utils import get_influxdb_client

logger = get_task_logger(__name__)


@shared_task
def save_to_influxdb(dbname, measurements):
    """
    Save valid `measurements` dictionary into InfluxDB database `dbname`.
    Log errors.
    :param dbname: Database name
    :param measurements: a valid InfluxDB dictionary.
    """
    iclient = get_influxdb_client(database=dbname)
    try:
        iclient.create_database(dbname)
        iclient.write_points(measurements)
        logger.info('Successfully saved InfluxDB object to database {}'.format(dbname))  # Goes to celery log
    except InfluxDBClientError as err:
        err_msg = '[InfluxDB] {}'.format(err)
        logger.error(err_msg)
        logger.error(json.dumps(measurements))


@@ -902,8 +902,786 @@
 ents))%0A%0A
+%0A@shared_task%0Adef push_ngsi_orion(data, url_root, username, password):%0A    # device_id = data%5B'id'%5D%0A    resp = None%0A    try:  # ...to update the entity...%0A        resp = requests.patch('%7B%7D/entities/%7B%7D/attrs/'.format(url_root, data%5B'id'%5D), auth=(username, password),%0A                              json=%7B'NoiseLevelObserved': data%5B'NoiseLevelObserved'%5D%7D)%0A    except Exception as err:%0A        logger.error('Something went wrong! Exception: %7B%7D'.format(e))%0A        print('Something went wrong PATCHing to Orion! Exception: %7B%7D'.format(err))%0A%0A    # ...if updating failed, the entity probably doesn't exist yet so create it%0A    if not resp or (resp.status_code != 204):%0A        resp = requests.post('%7B%7D/entities/'.format(url_root), auth=(username, password), json=data)%0A    return resp%0A

Add push_ngsi_orion task