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)
Beispiel #5
0
def storeValidProcessedDataInDB(data, product_id):
    """ Helper Processed Measurement function to insert Valid Processed Data """
    installation_id = same_helper.getInstallationIdBaseName(product_id)
    if(installation_id!=None):
      valid_data = {'timestamp': data['timestamp'],'CO': data['CO'],'CO_ug_m3': data['CO_ug_m3'], 'H2S': data['H2S'],
                    'H2S_ug_m3': data['H2S_ug_m3'],'SO2': data['SO2'],'SO2_ug_m3': data['SO2_ug_m3'],'NO2': data['NO2'],
                    'NO2_ug_m3': data['NO2_ug_m3'],'O3': data['O3'],'O3_ug_m3': data['O3_ug_m3'],'PM25': data['PM25'],
                    'lat':data['lat'],'lon':data['lon'],'PM1': data['PM1'],'PM10': data['PM10'], 'UV': data['UV'],
                    'UVA': data['UVA'],'UVB': data['UVB'],'SPL': data['spl'],'humidity': data['humidity'], 'CO2':data['CO2'],
                    'pressure': data['pressure'],'temperature': data['temperature'],'timestamp_zone': data['timestamp_zone'],
                    'I_temperature':data['I_temperature'],'VOC':data['VOC']}
      valid_processed_measurement = ValidProcessedMeasurement(**valid_data, qhawax_installation_id=installation_id)
      session.add(valid_processed_measurement)
      session.commit()
      data = util_helper.setNoneStringElements(data)
      socketio.emit(data['ID'], data)
def storeValidProcessedDataInDBMobile(data, product_id):
    """Helper Processed Measurement function to insert Valid Processed Data"""
    installation_id = same_helper.getInstallationIdBaseName(product_id)
    if installation_id != None:
        valid_data = {
            "timestamp": data["timestamp"],
            "CO": data["CO"],
            "CO_ug_m3": data["CO_ug_m3"],
            "H2S": data["H2S"],
            "H2S_ug_m3": data["H2S_ug_m3"],
            "SO2": data["SO2"],
            "SO2_ug_m3": data["SO2_ug_m3"],
            "NO2": data["NO2"],
            "NO2_ug_m3": data["NO2_ug_m3"],
            "O3": data["O3"],
            "O3_ug_m3": data["O3_ug_m3"],
            "PM25": data["PM25"],
            "lat": data["lat"],
            "lon": data["lon"],
            "PM1": data["PM1"],
            "PM10": data["PM10"],
            "UV": data["UV"],
            "UVA": data["UVA"],
            "UVB": data["UVB"],
            "SPL": data["spl"],
            "humidity": data["humidity"],
            "CO2": data["CO2"],
            "pressure": data["pressure"],
            "temperature": data["temperature"],
            "timestamp_zone": data["timestamp_zone"],
            "I_temperature": data["I_temperature"],
            "VOC": data["VOC"],
        }
        valid_processed_measurement = ValidProcessedMeasurement(
            **valid_data, qhawax_installation_id=installation_id
        )
        session.add(valid_processed_measurement)
        session.commit()
        data = util_helper.setNoneStringElements(data)
        socketio.emit(data["ID"] + "_mobile", data)
 def test_set_none_string_elements_valid(self):
     data = {
         "ID": 1,
         "CO": 1,
         "CO_ug_m3": "string",
         "H2S": 1,
         "H2S_ug_m3": 1.39,
         "NO2": "string",
         "NO2_ug_m3": 1.88,
         "O3": 1,
         "O3_ug_m3": 1.96,
         "PM1": 1,
         "PM10": 1,
         "PM25": 1,
         "SO2": 1,
         "SO2_ug_m3": 2.62,
         "spl": 1,
         "UV": 1,
         "UVA": 1,
         "UVB": 1,
         "humidity": 1,
         "lat": -12.000000,
         "lon": -77.00000,
         "pressure": 100,
         "temperature": 20,
         "timestamp": "2020-01-01 00:00:00",
         "timestamp_zone": "2020-01-01 00:00:00",
         "VOC": None,
         "CO2": None,
         "I_temperature": None,
     }
     processed = {
         "ID": 1,
         "CO": 1,
         "CO_ug_m3": None,
         "H2S": 1,
         "H2S_ug_m3": 1.39,
         "NO2": None,
         "NO2_ug_m3": 1.88,
         "O3": 1,
         "O3_ug_m3": 1.96,
         "PM1": 1,
         "PM10": 1,
         "PM25": 1,
         "SO2": 1,
         "SO2_ug_m3": 2.62,
         "spl": 1,
         "UV": 1,
         "UVA": 1,
         "UVB": 1,
         "humidity": 1,
         "lat": -12.000000,
         "lon": -77.00000,
         "pressure": 100,
         "temperature": 20,
         "timestamp": "2020-01-01 00:00:00",
         "timestamp_zone": "2020-01-01 00:00:00",
         "VOC": None,
         "CO2": None,
         "I_temperature": None,
     }
     self.assertAlmostEqual(
         util_helper.setNoneStringElements(data), processed
     )