Example #1
0
def glasses_request() -> Response:
    """ This function can register new glasses in the OGC server.
    :return: An HTTP Response.
    """
    logging.debug(glasses_request.__name__ + " method called from: " +
                  request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    glasses_id = request.json[TAG_ID_KEY]

    if request.method == "POST":  # POST
        # -> ### DATASTREAM REGISTRATION ###
        rc = scral_module.get_resource_catalog()
        if glasses_id in rc:
            logging.error("Device already registered!")
            return make_response(
                jsonify({ERROR_RETURN_STRING: DUPLICATE_REQUEST}), 422)

        logging.info("Glasses: '" + str(glasses_id) + "' registration.")
        ok = scral_module.ogc_datastream_registration(glasses_id)
        if not ok:
            return make_response(
                jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)
        else:
            return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)

    elif request.method == "DELETE":  # DELETE
        response = scral_module.delete_device(glasses_id)
        return response
Example #2
0
def wristband_request() -> Response:
    logging.debug(wristband_request.__name__ + " method called from: " +
                  request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    wristband_id = request.json[TAG_ID_KEY]

    if request.method == "POST":  # Device Registration
        rc = scral_module.get_resource_catalog()
        if wristband_id not in rc:
            logging.info("Wristband: '" + str(wristband_id) +
                         "' registration.")
        else:
            logging.warning(
                "Device '" + str(wristband_id) +
                "' already registered... It will be overwritten on RC!")

        ok = scral_module.ogc_datastream_registration(wristband_id,
                                                      request.json)
        if not ok:
            return make_response(
                jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)
        else:
            return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)

    elif request.method == "DELETE":  # Remove Device
        response = scral_module.delete_device(wristband_id)
        return response
Example #3
0
def put_observation(observed_property: str, payload: dict) -> Response:
    """ This function is used to store a new OBSERVATION in the OGC Server.

    :param observed_property: The type of property
    :param payload: The payload of the observation
    :return: An HTTP request.
    """
    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    glasses_id = payload[TAG_ID_KEY]
    result = scral_module.ogc_observation_registration(observed_property,
                                                       payload)
    if result is True:
        return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)
    elif result is None:
        logging.error("Glasses: '" + str(glasses_id) + "' was not registered.")
        return make_response(
            jsonify({ERROR_RETURN_STRING: DEVICE_NOT_REGISTERED}), 400)
    else:
        logging.error("Impossible to publish on MQTT server.")
        return make_response(
            jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)
Example #4
0
def new_device_localization() -> Response:
    """ This function is used to store a new OBSERVATION in the OGC Server.

    :return: An HTTP request.
    """
    logging.debug(new_device_localization.__name__ + " method called from: " +
                  request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    payload = request.json
    device_id = payload[DEVICE_ID_KEY]

    # you should use the same property_name used in the ogc_config.conf file
    result = scral_module.ogc_observation_registration("Property1", payload)
    if result is True:
        return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)
    elif result is None:
        logging.error("Device: '" + str(device_id) + "' was not registered.")
        return make_response(
            jsonify({ERROR_RETURN_STRING: DEVICE_NOT_REGISTERED}), 400)
    else:
        logging.error("Impossible to publish on MQTT server.")
        return make_response(
            jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)
def remove_gps_tag() -> Response:
    """ This function delete all the DATASTREAM associated with a particular device
        on the resource catalog and on the OGC server.

    :return: An HTTP Response.
    """
    logging.debug(remove_gps_tag.__name__ + ", " + request.method + " method called from: " + request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module, request)
    if not ok:
        return status
    else:
        response = scral_module.delete_device(request.json[TAG_ID_KEY])
        return response
Example #6
0
def delete_device() -> Response:
    """ This function can be used to register a new device in the OGC server.
        :return: An HTTP Response.
    """
    logging.debug(delete_device.__name__ + " method called from: " +
                  request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    device_id = request.json[DEVICE_ID_KEY]
    response = scral_module.delete_device(device_id)
    return response
def new_sound_event() -> Response:
    """ This function can register an OBSERVATION in the OGC server.
        It is able to accept any new OBSERVED PROPERTY according to the content of "type" field.
        This new property is run-time generated.

    :return: An HTTP Response.
    """
    logging.debug(new_sound_event.__name__ + " method called from: " +
                  request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    payload = request.json
    lower_payload = {k.lower(): v
                     for k, v in request.json.items()
                     }  # this payload has all the keys in lower case
    property_name = lower_payload[TYPE_KEY.lower(
    )]  # accessing to lower_payload fields require to lower constants too
    try:
        property_description = lower_payload[DESCRIPTION_KEY.lower()]
    except KeyError:
        property_description = property_name + " description"
    try:
        property_definition = lower_payload[DEFINITION_KEY.lower()]
    except KeyError:
        property_definition = property_name + " definition"

    obs_prop = OGCObservedProperty(property_name, property_description,
                                   property_definition)
    device_id = lower_payload[DEVICE_ID_KEY.lower()]

    datastream_id = scral_module.new_datastream(device_id, obs_prop)
    if datastream_id is False:
        return make_response(
            jsonify({ERROR_RETURN_STRING: DEVICE_NOT_REGISTERED}), 400)
    elif datastream_id is None:
        return make_response(
            jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)
    else:
        scral_module.ogc_observation_registration(
            datastream_id, lower_payload[START_TIME_KEY.lower()], payload)
        return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)
Example #8
0
def register_device() -> Response:
    """ This function can be used to register a new device in the OGC server.
    :return: An HTTP Response.
    """
    logging.debug(register_device.__name__ + " method called from: " +
                  request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    device_id = request.json[DEVICE_ID_KEY]
    logging.info("Device: '" + str(device_id) + "' registration.")
    ok = scral_module.ogc_datastream_registration(device_id)
    if not ok:
        return make_response(
            jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)
    else:
        return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)
def new_gps_tag_request() -> Response:
    logging.debug(new_gps_tag_request.__name__ + " method called from: "+request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module, request)
    if not ok:
        return status

    gps_tag_id = request.json[TAG_ID_KEY]

    # -> ### DATASTREAM REGISTRATION ###
    rc = scral_module.get_resource_catalog()
    if gps_tag_id not in rc:
        logging.info("GPS tag: '" + str(gps_tag_id) + "' registration.")
        ok = scral_module.new_datastream(request.json)
        if not ok:
            return make_response(jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)

        return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)

    else:
        logging.error("Device already registered!")
        return make_response(jsonify({ERROR_RETURN_STRING: DUPLICATE_REQUEST}), 422)
Example #10
0
def new_wristband_association_request() -> Response:
    logging.debug(new_wristband_association_request.__name__ +
                  " method called from: " + request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    wristband_id_1 = request.json[ID1_ASSOCIATION_KEY]
    wristband_id_2 = request.json[ID2_ASSOCIATION_KEY]

    rc = scral_module.get_resource_catalog()
    if wristband_id_1 not in rc or wristband_id_2 not in rc:
        logging.error("One of the wristbands is not registered.")
        return make_response(
            jsonify({
                ERROR_RETURN_STRING:
                "One of the wristbands is not registered!"
            }), 400)

    vds = scral_module.get_ogc_config().get_virtual_datastream(
        SENSOR_ASSOCIATION_NAME)
    if not vds:
        logging.critical(
            'No Virtual DATASTREAM registered for Virtual SENSOR: "' +
            SENSOR_ASSOCIATION_NAME + '"')
        return make_response(
            jsonify({ERROR_RETURN_STRING: INTERNAL_SERVER_ERROR}), 500)

    result = scral_module.ogc_service_observation_registration(
        vds, request.json)
    if result is True:
        return make_response(jsonify({SUCCESS_RETURN_STRING: "Ok"}), 201)
    else:
        logging.error("Impossible to publish on MQTT server.")
        return make_response(
            jsonify({ERROR_RETURN_STRING: NO_MQTT_PUBLICATION}), 502)
def camera_request() -> Response:
    """ This function can register new camera in the OGC server or store new OGC OBSERVATIONs.

    :return: An HTTP Response.
    """
    logging.debug(camera_request.__name__ + ", " + request.method +
                  " method called from: " + request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    if request.method == "POST":  # POST
        camera_id = request.json[CAMERA_ID_KEY]
        rc = scral_module.get_resource_catalog()

        if camera_id not in rc:
            logging.info('Camera: "' + str(camera_id) + '" registration.')
            response = scral_module.ogc_datastream_registration(
                camera_id, CAMERA_SENSOR_TYPE, request.json)
        else:
            logging.error("Camera already registered!")
            response = make_response(
                jsonify({ERROR_RETURN_STRING: DUPLICATE_REQUEST}), 422)
        return response

    elif request.method == "PUT":  # PUT
        camera_id = str(request.json[CAMERA_IDS_KEY][0])
        logging.info("New OBSERVATION from camera: '" + str(camera_id) + "'.")
        property_type = request.json[TYPE_MODULE_KEY]

        if property_type == FIGHT_KEY:
            observed_property = "FD-Estimation"
        elif property_type == CROWD_KEY:
            observed_property = "CDL-Estimation"
        elif property_type == FLOW_KEY:
            observed_property = "FA-Estimation"
        elif property_type == OBJECT_KEY:
            observed_property = "OD-Estimation"
        elif property_type == GATE_COUNT_KEY:
            observed_property = "GC-Estimation"
        else:
            logging.error("Unknown property: <" + property_type + ">.")
            return make_response(
                jsonify({
                    ERROR_RETURN_STRING:
                    "Unknown property <" + property_type + ">."
                }), 400)

        response = put_observation(camera_id, observed_property, request.json)
        return response

    elif request.method == "DELETE":
        try:
            camera_id = request.json[CAMERA_ID_KEY]
            logging.info('Try to delete camera: "' + camera_id + '"')
            response = scral_module.delete_device(camera_id)
        except KeyError:
            logging.error("Missing " + CAMERA_ID_KEY + ". Delete aborted.")
            response = make_response(
                jsonify({ERROR_RETURN_STRING: WRONG_PAYLOAD_REQUEST}), 400)
        return response
    else:
        logging.critical("Unauthorized method!")
        return make_response(jsonify({ERROR_RETURN_STRING: WRONG_REQUEST}),
                             405)
def cdg_request() -> Response:
    """ This function can register new Crowd Density Global in the OGC server or store new OGC OBSERVATIONs.

    :return: An HTTP Response.
    """
    logging.debug(cdg_request.__name__ + ", " + request.method +
                  " method called from: " + request.remote_addr)

    ok, status = rest_util.tests_and_checks(DOC[MODULE_NAME_KEY], scral_module,
                                            request)
    if not ok:
        return status

    cdg_module_id = None
    try:
        cdg_module_id = request.json[
            MODULE_ID_KEY]  # NB: module here is Crowd Density Global module
    except KeyError:
        logging.error("Missing " + MODULE_ID_KEY + ". Request aborted.")
        return make_response(
            jsonify({ERROR_RETURN_STRING: WRONG_PAYLOAD_REQUEST}), 400)

    if request.method == "POST":  # POST
        rc = scral_module.get_resource_catalog()
        if cdg_module_id not in rc:
            logging.info("CDG: '" + str(cdg_module_id) + "' registration.")
            response = scral_module.ogc_datastream_registration(
                cdg_module_id, CDG_SENSOR_TYPE, request.json)
        else:
            logging.error("CDG module already registered!")
            response = make_response(
                jsonify({ERROR_RETURN_STRING: DUPLICATE_REQUEST}), 422)
        return response

    elif request.method == "PUT":  # PUT
        logging.info("New OBSERVATION from CDG: '" + str(cdg_module_id))

        property_type = request.json[TYPE_MODULE_KEY]
        request.json[TIMESTAMP_KEY] = request.json.pop(
            "timestamp_1")  # renaming "timestamp_1" to "timestamp"

        if property_type == CDG_KEY:
            observed_property = CDG_PROPERTY
        else:
            logging.error("Unknown property.")
            return make_response(
                jsonify({ERROR_RETURN_STRING: UNKNOWN_PROPERTY}), 400)

        response = put_observation(cdg_module_id, observed_property,
                                   request.json)
        return response

    elif request.method == "DELETE":  # DELETE
        logging.info('Try to delete CDG: "' + cdg_module_id + '"')
        response = scral_module.delete_device(cdg_module_id)
        return response

    else:
        logging.critical("Unauthorized method!")
        return make_response(jsonify({ERROR_RETURN_STRING: WRONG_REQUEST}),
                             405)