예제 #1
0
def add_activity(version=app.config['ACTUAL_API']):
    """
    Adds a new activity into the system


    # TODO Instrumental and Basic activities are present HERE!!!! There are two type of Activities
    # Copanion? YES NO boolean

    An example in JSON could be:

    {
        "activity_name": "LeaveHouse",
        "activity_description": "Some description of the activity",          # Optional
        "instrumental": True,                                               # Optional
        "house_number": 0,              # Optional
        "indoor": false,                # Optional
        "location":"eu:c4a:Bus:39",     # Optional
        "pilot": "LCC"                  # Optional
    }

    :param version: Api version
    :return:
    """
    if Utilities.check_connection(app, version):
        data = _convert_to_dict(request.json)
        res, msg = Utilities.check_add_activity_data(AR_DATABASE, data)
        if data and res and USER:
            # User and data are OK. save data into DB
            res_ar = AR_DATABASE.add_activity(data)
            res_sr = SR_DATABASE.add_activity(data)
            if res_ar and res_sr:
                Utilities.write_log_info(app, (
                    "add_activity: the username: %s adds new activity into database"
                    % USER.username))
                return Response('Data stored in database OK\n'), 200
            else:
                Utilities.write_log_error(
                    app, ("add_activity: the username: %s failed to store "
                          "data into database. 500" % USER.username))
                return Response("There is an error in DB"), 500
        else:
            return Response(msg), 400
예제 #2
0
def add_activity(version=app.config['ACTUAL_API']):
    """
    Adds a new activity into the system


    An example in JSON could be:


    {
        "activity": "LeaveHouse",
        "user": "******",
        "pilot": "LCC",
        "start_time": "2018-04-20T07:08:41.013+03:00",
        "end_time": "2018-05-20T07:08:41.013+03:00",
        "duration": 23232323,
        "payload": [{
            "action": "eu:c4a:POI_EXIT",
            "location": "eu:c4a:Pharmacy:Vanilla123",
            "timestamp": "2015-05-20T07:08:41.013+03:00",
            "position": "38.976908 22.724375"
        }, {
            "action": "eu:c4a:POI_ENTER",
            "location": "eu:c4a:Shop:Ermou96",
            "timestamp": "2014-05-20T07:08:41.013+03:00",
            "position": "37.976908 23.724375"
        }, {
            "action": "eu:c4a:APPLIANCE_ON",
            "location": "eu:c4a:Room:number23",
            "timestamp": "2012-05-20T07:08:41.013+03:00",
            "position": "42.986908 33.724375"
        }
                   ],
        "data_source_type": ["sensors", "external_dataset"]
    }

    :param basestring version: Api version

    :return: Response of the request
    """
    if Utilities.check_connection(app, version):
        data = _convert_to_dict(request.json)
        msg = Utilities.check_add_activity_data(AR_DATABASE, data)
        if data and not msg and USER:
            # User and data are OK. save data into DB
            res_ar = AR_DATABASE.add_activity(data)
            res_sr = SR_DATABASE.add_activity(data)
            if res_ar and res_sr:
                logging.info(
                    "add_activity: the username: %s adds new activity into database"
                    % USER.username)
                return Response('Data stored in database OK\n'), 200
            else:
                logging.error(
                    "add_activity: %s failed to store data into database. 500"
                    % USER.username)
                return Response("There is an error in DB"), 500
        else:
            logging.error("add_activity: there is a problem with entered data")
            # Data is not valid, check the problem
            if msg and "duplicated" in msg:
                # Data sent is duplicated.
                return Response(msg), 409
            else:
                # Standard Error
                return Response(msg), 400