Example #1
0
def queryDBValidAirQuality(qhawax_id, initial_timestamp, final_timestamp,
                           date_format):
    """
    Helper function to get Air Quality measurement

    """
    sensors = (AirQualityMeasurement.CO_ug_m3, AirQualityMeasurement.H2S_ug_m3,
               AirQualityMeasurement.NO2_ug_m3, AirQualityMeasurement.O3_ug_m3,
               AirQualityMeasurement.PM25, AirQualityMeasurement.PM10,
               AirQualityMeasurement.SO2_ug_m3, AirQualityMeasurement.uv,
               AirQualityMeasurement.uva, AirQualityMeasurement.uvb,
               AirQualityMeasurement.spl, AirQualityMeasurement.humidity,
               AirQualityMeasurement.pressure,
               AirQualityMeasurement.temperature, AirQualityMeasurement.lat,
               AirQualityMeasurement.lon, AirQualityMeasurement.timestamp_zone)

    initial_timestamp = datetime.datetime.strptime(initial_timestamp,
                                                   '%d-%m-%Y %H:%M:%S')
    final_timestamp = datetime.datetime.strptime(final_timestamp,
                                                 '%d-%m-%Y %H:%M:%S')

    if (same_helper.qhawaxExistBasedOnID(qhawax_id)):
        average_valid_processed= session.query(*sensors).filter(AirQualityMeasurement.qhawax_id == qhawax_id). \
                                        filter(AirQualityMeasurement.timestamp_zone >= initial_timestamp). \
                                        filter(AirQualityMeasurement.timestamp_zone <= final_timestamp). \
                                        order_by(AirQualityMeasurement.timestamp).all()
        if (average_valid_processed != []):
            return [
                measurement._asdict()
                for measurement in average_valid_processed
            ]
        return []
    return None
def queryDBValidAirQuality(qhawax_id, initial_timestamp,
                           final_timestamp):  # validar con sabri
    """Helper function to get Air Quality measurement"""
    sensors = (
        AirQualityMeasurement.CO_ug_m3,
        AirQualityMeasurement.H2S_ug_m3,
        AirQualityMeasurement.NO2_ug_m3,
        AirQualityMeasurement.PM25,
        AirQualityMeasurement.PM10,
        AirQualityMeasurement.SO2_ug_m3,
        AirQualityMeasurement.uv.label("UV"),
        AirQualityMeasurement.spl.label("SPL"),
        AirQualityMeasurement.humidity,
        AirQualityMeasurement.pressure,
        AirQualityMeasurement.O3_ug_m3,
        AirQualityMeasurement.temperature,
        AirQualityMeasurement.lat,
        AirQualityMeasurement.lon,
        AirQualityMeasurement.timestamp_zone,
    )

    if same_helper.qhawaxExistBasedOnID(qhawax_id):
        valid_processed_measurements = (session.query(*sensors).filter(
            AirQualityMeasurement.qhawax_id == qhawax_id).filter(
                AirQualityMeasurement.timestamp_zone >= initial_timestamp
            ).filter(AirQualityMeasurement.timestamp_zone <= final_timestamp
                     ).order_by(AirQualityMeasurement.timestamp_zone).all())
        return [
            measurement._asdict()
            for measurement in valid_processed_measurements
        ]
    return None
Example #3
0
def setAvailableQhawax(qhawax_id):
    """
    Update qhawax installation state in qHAWAX table

    """
    if (same_helper.qhawaxExistBasedOnID(qhawax_id)):
        session.query(Qhawax).filter_by(id=qhawax_id).update(
            values={'availability': 'Available'})
        session.commit()
Example #4
0
def setModeCustomer(qhawax_id):
    """
    Update qHAWAX mode to Customer

    """
    if (same_helper.qhawaxExistBasedOnID(qhawax_id)):
        session.query(Qhawax).filter_by(id=qhawax_id).update(
            values={'mode': "Cliente"})
        session.commit()
Example #5
0
def setOccupiedQhawax(qhawax_id):
    """
    Update qHAWAX Availability to Occupied

    """
    if (same_helper.qhawaxExistBasedOnID(qhawax_id)):
        session.query(Qhawax).filter_by(id=qhawax_id).update(
            values={'availability': 'Occupied'})
        session.commit()
Example #6
0
def saveEndWorkFieldDate(qhawax_id, end_date, date_format):
    """
    Save End Work in Field

    """
    util_helper.check_valid_date(end_date, date_format)

    if (same_helper.qhawaxExistBasedOnID(qhawax_id)):
        installation_id = same_helper.getInstallationId(qhawax_id)
        session.query(QhawaxInstallationHistory).filter_by(id=installation_id).\
                                                 update(values={'end_date_zone': end_date})
        session.commit()
Example #7
0
def getHoursDifference(qhawax_id):
    """
    Helper Processed Measurement function to get 
    minutes difference between last_registration_time and last_time_physically_turn_on

    """
    if (same_helper.qhawaxExistBasedOnID(qhawax_id)):
        values = session.query(QhawaxInstallationHistory.last_time_physically_turn_on_zone, \
                               QhawaxInstallationHistory.last_registration_time_zone).\
                         filter(QhawaxInstallationHistory.qhawax_id == qhawax_id).first()
        if (values[0] != None and values[1] != None):
            minutes_difference = int(
                (values[0] - values[1]).total_seconds() / 60)
            return minutes_difference, values[0]
    return None, None
Example #8
0
def queryDBAirDailyQuality(qhawax_id, init_week, init_year, end_week,
                           end_year):
    """
    Air Daily Measurement function helper to get daily average measurement based on week number and year 

    """

    sensors = (AirDailyMeasurement.CO, AirDailyMeasurement.H2S,
               AirDailyMeasurement.NO2, AirDailyMeasurement.O3,
               AirDailyMeasurement.PM25, AirDailyMeasurement.PM10,
               AirDailyMeasurement.SO2, AirDailyMeasurement.timestamp_zone)

    if (type(init_week) not in [int]):
        raise TypeError("Initial week number should be int")

    if (type(init_year) not in [int]):
        raise TypeError("Initial year number should be int")

    if (type(end_week) not in [int]):
        raise TypeError("Last week number should be int")

    if (type(end_year) not in [int]):
        raise TypeError("Last year number should be int")

    if (end_week < init_week):
        raise ValueError("End week must be higher than init week")

    if (end_year < init_year):
        raise ValueError("End year must be higher or equal than init year")

    if (same_helper.qhawaxExistBasedOnID(qhawax_id)):

        init_firstdate, init_lastdate = util_helper.getDateRangeFromWeek(
            init_year, init_week)
        end_firstdate, end_lastdate = util_helper.getDateRangeFromWeek(
            end_year, end_week)

        air_daily_measurements = session.query(*sensors).filter(AirDailyMeasurement.qhawax_id == qhawax_id). \
                                         filter(AirDailyMeasurement.timestamp_zone >= init_firstdate). \
                                         filter(AirDailyMeasurement.timestamp_zone <= end_lastdate). \
                                         order_by(AirDailyMeasurement.timestamp_zone).all()
        if (air_daily_measurements != []):
            return [
                measurement._asdict() for measurement in air_daily_measurements
            ]
        return []
    return None
 def test_qhawax_exist_based_on_id_valid(self):
     self.assertAlmostEqual(same_helper.qhawaxExistBasedOnID(1), True)
     self.assertAlmostEqual(same_helper.qhawaxExistBasedOnID(999), False)