def handleProcessedDataByDrone():
    """
    Records processed and valid processed measurements every second by drone
    qHAWAX: Record new measurement
    """
    flag_email = False
    data_json = request.get_json()
    try:
        product_id = data_json['ID']
        data_json = util_helper.validTimeJsonProcessed(data_json)
        data_json = util_helper.validAndBeautyJsonProcessed(data_json)
        post_data_helper.storeProcessedDataInDB(data_json)
        data_json['ID'] = product_id
        data_json = util_helper.setNoneStringElements(data_json)
        for i in range(len(pollutants)):
            socket_name = data_json['ID'] + '_' + str(
                pollutants[i]) + '_processed'
            pollutant = str(pollutants[i]) + "_ug_m3" if (pollutants[i] in [
                'CO', 'NO2', 'O3', 'H2S', 'SO2'
            ]) else str(pollutants[i])
            new_data_json = {
                "sensor": pollutants[i],
                "center": {
                    "lat": data_json["lat"],
                    "lng": data_json["lon"]
                }
            }
            new_data_json[pollutants[i]] = data_json[pollutant]
            socketio.emit(socket_name, new_data_json)  #qH006_CO_proccessed
        return make_response('OK', 200)
    except TypeError as e:
        json_message = jsonify({'error': '\'%s\'' % (e)})
        return make_response(json_message, 400)
def handleProcessedDataByQhawax():
    """
    Records processed and valid processed measurements every five seconds
    qHAWAX: Record new measurement
    """
    flag_email = False
    data_json = request.get_json()
    try:
        product_id = data_json['ID']
        data_json = util_helper.validTimeJsonProcessed(data_json)
        data_json = util_helper.validAndBeautyJsonProcessed(data_json)
        post_data_helper.storeProcessedDataInDB(data_json)
        data_json['ID'] = product_id
        data_json['zone'] = "Undefined Zone"
        mode = same_helper.getQhawaxMode(product_id)
        inca_value = same_helper.getMainIncaQhawaxTable(product_id)
        if (mode == "Customer" and inca_value != None):
            data_json['zone'] = get_business_helper.getNoiseData(product_id)
            minutes_difference, last_time_turn_on = get_business_helper.getHoursDifference(
                product_id)
            if (minutes_difference != None):
                if (minutes_difference < 5):
                    post_data_helper.validTimeOfValidProcessed(
                        10, "minute", last_time_turn_on, data_json, product_id,
                        inca_value)
                elif (minutes_difference >= 5):
                    post_data_helper.validTimeOfValidProcessed(
                        2, "hour", last_time_turn_on, data_json, product_id,
                        inca_value)
        data_json = util_helper.setNoneStringElements(data_json)
        socketio.emit(data_json['ID'] + '_processed', data_json)
        return make_response('OK', 200)
    except TypeError as e:
        json_message = jsonify({'error': '\'%s\'' % (e)})
        return make_response(json_message, 400)
def handleProcessedDataByDrone():
    """
    Records processed and valid processed measurements every second by drone
    qHAWAX: Record new measurement
    """
    flag_email = False
    data_json = request.get_json()
    try:
        product_id = data_json["ID"]
        data_json = util_helper.validTimeJsonProcessed(data_json)
        data_json = util_helper.validAndBeautyJsonProcessed(data_json)
        post_data_helper.storeProcessedDataInDB(data_json)
        data_json["ID"] = product_id
        data_json = util_helper.setNoneStringElements(data_json)
        for i in range(len(pollutants)):
            socket_name = (data_json["ID"] + "_" + str(pollutants[i]) +
                           "_processed")
            pollutant = (str(pollutants[i]) + "_ug_m3" if
                         (pollutants[i] in ["CO", "NO2", "O3", "H2S", "SO2"
                                            ]) else str(pollutants[i]))
            new_data_json = {
                "sensor": pollutants[i],
                "center": {
                    "lat": data_json["lat"],
                    "lng": data_json["lon"]
                },
            }
            new_data_json[pollutants[i]] = data_json[pollutant]
            socketio.emit(socket_name, new_data_json)  # qH006_CO_proccessed
        return make_response("OK", 200)
    except TypeError as e:
        json_message = jsonify({"error": "'%s'" % (e)})
        return make_response(json_message, 400)
def handleProcessedDataByQhawax():
    """
    Records processed and valid processed measurements every five seconds
    qHAWAX: Record new measurement
    """
    flag_email = False
    data_json = request.get_json()
    try:
        product_id = data_json["ID"]
        data_json = util_helper.validTimeJsonProcessed(data_json)
        data_json = util_helper.validAndBeautyJsonProcessed(data_json)
        post_data_helper.storeProcessedDataInDB(data_json)
        data_json["ID"] = product_id
        data_json["zone"] = "Undefined Zone"
        mode = same_helper.getQhawaxMode(product_id)
        inca_value = same_helper.getMainIncaQhawaxTable(product_id)
        # same endpoint for every qHAWAX but logic different per type of qHAWAX
        if mode == "Customer" and inca_value != None:
            data_json["zone"] = get_business_helper.getNoiseData(product_id)
            (
                minutes_difference,
                last_time_turn_on,
            ) = get_business_helper.getHoursDifference(product_id)
            if minutes_difference != None:
                if minutes_difference < 5:
                    post_data_helper.validTimeOfValidProcessed(
                        10,
                        "minute",
                        last_time_turn_on,
                        data_json,
                        product_id,
                        inca_value,
                    )
                elif minutes_difference >= 5:
                    post_data_helper.validTimeOfValidProcessed(
                        2,
                        "hour",
                        last_time_turn_on,
                        data_json,
                        product_id,
                        inca_value,
                    )
        data_json = util_helper.setNoneStringElements(data_json)
        socketio.emit(data_json["ID"] + "_processed", data_json)
        return make_response("OK", 200)
    except TypeError as e:
        json_message = jsonify({"error": "'%s'" % (e)})
        return make_response(json_message, 400)
def handleProcessedDataByMobileQhawax():
    data_json = request.get_json()
    try:
        product_id = data_json["ID"]
        if data_json is not None:
            if "zone" in data_json:
                data_json.pop("zone")
            post_data_helper.storeProcessedDataInDB(data_json)
            data_json["ID"] = product_id
            state = get_business_helper.queryQhawaxStatus(product_id)
            mode = same_helper.getQhawaxMode(product_id)
            if state == "OFF":  # if API turned it off
                post_business_helper.saveTurnOnLastTimeProcessedMobile(
                    product_id)
                post_business_helper.saveStatusQhawaxTable(
                    product_id, "ON", 1)  # state = ON - qhawax
                post_business_helper.writeBinnacle(product_id, "Reconnection",
                                                   "API")

            if mode == "Customer":
                (
                    minutes_difference,
                    last_time_turn_on,
                ) = get_business_helper.getHoursDifference(product_id)
                if minutes_difference != None:
                    if minutes_difference < 30:
                        if last_time_turn_on + datetime.timedelta(
                                minutes=1) < datetime.datetime.now(
                                    dateutil.tz.tzutc()):
                            post_data_helper.validAndBeautyJsonValidProcessedMobile(
                                data_json, product_id)

                    elif minutes_difference >= 30:
                        if last_time_turn_on + datetime.timedelta(
                                hours=2) < datetime.datetime.now(
                                    dateutil.tz.tzutc()):
                            post_data_helper.validAndBeautyJsonValidProcessedMobile(
                                data_json, product_id)

            return make_response("OK", 200)
        return make_response("ID not found", 400)
    except TypeError as e:
        json_message = jsonify({"error": "'%s'" % (e)})
        return make_response(json_message, 400)
def handleProcessedData():
    """
    To record processed measurement and valid processed measurement every five seconds  

    """
    try:
        flag_email = False
        data_json = request.get_json()
        product_id = data_json['ID']
        data_json = util_helper.validTimeJsonProcessed(data_json)
        data_json = util_helper.validAndBeautyJsonProcessed(data_json)
        post_data_helper.storeProcessedDataInDB(data_json)
        data_json['ID'] = product_id
        data_json['zone'] = "Zona No Definida"
        qhawax_id = same_helper.getQhawaxID(product_id)
        mode = same_helper.getQhawaxMode(product_id)
        if (mode == "Cliente"):
            qhawax_zone = get_data_helper.getNoiseData(product_id)
            data_json['zone'] = qhawax_zone
            minutes_difference, last_time_turn_on = get_data_helper.getHoursDifference(
                qhawax_id)
            if (minutes_difference != None):
                if (minutes_difference < 5):
                    if (last_time_turn_on + datetime.timedelta(minutes=10) <
                            datetime.datetime.now(dateutil.tz.tzutc())):
                        post_data_helper.storeValidProcessedDataInDB(
                            data_json, qhawax_id)
                        socketio.emit('new_data_summary_valid', data_json)
                elif (minutes_difference >= 5):
                    if (last_time_turn_on + datetime.timedelta(hours=2) <
                            datetime.datetime.now(dateutil.tz.tzutc())):
                        post_data_helper.storeValidProcessedDataInDB(
                            data_json, qhawax_id)
                        socketio.emit('new_data_summary_valid', data_json)
        socketio.emit('new_data_summary_processed', data_json)
        return make_response('OK', 200)
    except TypeError as e:
        json_message = jsonify({'error': '\'%s\'' % (e)})
        return make_response(json_message, 400)
 def test_store_processed_data_valid(self):
     processed_measurement_json = {
         "CO": 1986.208,
         "CO_ug_m3": 1986.208,
         "H2S_ug_m3": 43.404,
         "H2S": 43.404,
         "NO2": 19.78,
         "NO2_ug_m3": 19.78,
         "O3": 3.126,
         "O3_ug_m3": 3.126,
         "VOC": 0,
         "CO2": 1,
         "SO2": 4.388,
         "SO2_ug_m3": 4.388,
         "PM10": 35.349,
         "PM25": 11.678,
         "UVA": 1,
         "UVB": 1,
         "alt": 0.0,
         "lat": -12.0402780000002,
         "lon": -77.0436090000003,
         "PM1": 1,
         "timestamp_zone": "Mon, 04 Jan 2021 00:00:00 GMT",
         "ID": "qH057",
         "pressure": 10,
         "humidity": 25,
         "I_temperature": 25,
         "temperature": 21,
         "spl": 1,
         "UV": 1
     }
     post_data_helper.storeProcessedDataInDB(processed_measurement_json)
     processed_measurement_json_nan = {
         "CO": "nan",
         "CO_ug_m3": "nan",
         "H2S_ug_m3": 43.404,
         "H2S": 43.404,
         "NO2": 19.78,
         "NO2_ug_m3": 19.78,
         "O3": 3.126,
         "O3_ug_m3": 3.126,
         "VOC": 0,
         "CO2": 1,
         "SO2": 4.388,
         "SO2_ug_m3": 4.388,
         "PM10": 35.349,
         "PM25": 11.678,
         "UVA": 1,
         "UVB": 1,
         "alt": 0.0,
         "lat": -12.0402780000002,
         "lon": -77.0436090000003,
         "PM1": 1,
         "timestamp_zone": "Mon, 04 Jan 2021 00:00:00 GMT",
         "ID": "qH057",
         "pressure": 10,
         "humidity": 25,
         "I_temperature": 25,
         "temperature": 21,
         "spl": 1,
         "UV": 1
     }
     post_data_helper.storeProcessedDataInDB(processed_measurement_json_nan)