def storeLogs(telemetry, drone_name): global drone_elapsed_time, drone_telemetry, drone_storage telemetry = exceptions.checkVariable_helper(telemetry, dict) drone_name = exceptions.checkVariable_helper(drone_name, str) if drone_elapsed_time is None: drone_elapsed_time = time.time() if drone_name not in drone_storage: qhawax_id = ( same_helper.getQhawaxID(drone_name) if same_helper.getQhawaxID(drone_name) is not None else "qH001" ) drone_storage[drone_name] = qhawax_id if time.time() - drone_elapsed_time > MAX_SECONDS_DATA_STORAGE: drone_telemetry = formatTelemetryForStorage(telemetry) drone_telemetry["timestamp"] = datetime.datetime.now( dateutil.tz.tzutc() ) drone_telemetry["qhawax_id"] = drone_storage[drone_name] drone_telemetry = DroneTelemetry(**drone_telemetry) session.add(drone_telemetry) session.commit() drone_elapsed_time = time.time()
def recordFirstTimeLoop(qhawax_name, timestamp): """qHAWAX function to record first time loop""" qhawax_id = same_helper.getQhawaxID(qhawax_name) if qhawax_id is not None: json_first_time_loop = {"first_time_loop": timestamp} same_helper.qhawaxQueryUpdateFilterByQhawaxId(json_first_time_loop, qhawax_id)
def getValidProcessedData(): """ To list all measurement of valid processed measurement table record the last N minutes """ qhawax_name = request.args.get('name') \ if request.args.get('name') is not None else "" interval_minutes = int(request.args.get('interval_minutes')) \ if request.args.get('interval_minutes') is not None else 60 try: qhawax_id = same_helper.getQhawaxID(qhawax_name) if (qhawax_id is not None): final_timestamp = datetime.datetime.now(dateutil.tz.tzutc()) initial_timestamp = final_timestamp - datetime.timedelta( minutes=interval_minutes) date_format = '%Y-%m-%d %H:%M:%S.%f%z' valid_processed_measurements = get_data_helper.queryDBValidProcessedByQhawaxScript(qhawax_id, \ str(initial_timestamp), str(final_timestamp),\ date_format) if valid_processed_measurements is not None: return make_response(jsonify(valid_processed_measurements), 200) return make_response(jsonify('Valid Measurements not found'), 200) return make_response(jsonify('qHAWAX ID does not exist'), 200) except TypeError as e: json_message = jsonify({'error': '\'%s\'' % (e)}) return make_response(json_message, 400)
def queryDBTelemetry(qhawax_name, initial_timestamp, final_timestamp): """Helper function to get Telemetry filter by qHAWAX between timestamp""" qhawax_id = same_helper.getQhawaxID(qhawax_name) if qhawax_id is not None: sensors = ( DroneTelemetry.airspeed, DroneTelemetry.alt, DroneTelemetry.battery_perc, DroneTelemetry.dist_home, DroneTelemetry.flight_mode, DroneTelemetry.lat, DroneTelemetry.lon, DroneTelemetry.num_gps, DroneTelemetry.voltage, DroneTelemetry.velocity_variance, DroneTelemetry.timestamp, DroneTelemetry.current, DroneTelemetry.fix_type, ) telemetry = (session.query(*sensors).filter( DroneTelemetry.qhawax_id == qhawax_id).filter( DroneTelemetry.timestamp >= initial_timestamp).filter( DroneTelemetry.timestamp <= final_timestamp).order_by( DroneTelemetry.timestamp).all()) return [t._asdict() for t in telemetry] return None
def recordDroneLanding(flight_end, qhawax_name, flight_detail): qhawax_id = same_helper.getQhawaxID(qhawax_name) landing_json = {"flight_end": flight_end, "flight_detail": flight_detail} session.query(DroneFlightLog).filter_by( qhawax_id=qhawax_id, flight_end=None ).update(values=landing_json) session.commit()
def updateLastestLatLonMobile(qhawax_name, json_val): qhawax_id = same_helper.getQhawaxID(qhawax_name) session.query(QhawaxInstallationHistory).filter_by( qhawax_id=qhawax_id, end_date_zone=None ).update(values=json_val) # json_val should have {"lat": 12.123, "lon": 12.123} session.commit()
def queryDBGasAverageMeasurement(qhawax_name, gas_name): """ Helper function to get gas average measurement based on qHAWAX name and sensor name""" gas_name = exceptions.checkStringVariable(gas_name) if(gas_name not in sensor_array): raise ValueError("Sensor name "+str(gas_name)+" should be CO, H2S, NO2, O3, PM25, PM10 or SO2") qhawax_id = same_helper.getQhawaxID(qhawax_name) if(qhawax_id!= None): initial_timestamp = datetime.datetime.now() last_timestamp = datetime.datetime.now() - datetime.timedelta(hours=24) column_array = [AirQualityMeasurement.CO.label('sensor'), AirQualityMeasurement.H2S.label('sensor'), AirQualityMeasurement.NO2.label('sensor'), AirQualityMeasurement.O3.label('sensor'), AirQualityMeasurement.PM25.label('sensor'), AirQualityMeasurement.PM10.label('sensor'), AirQualityMeasurement.SO2.label('sensor')] for i in range(len(sensor_array)): if(gas_name==sensor_array[i]): sensors = (AirQualityMeasurement.timestamp_zone, column_array[i]) return session.query(*sensors).filter(AirQualityMeasurement.qhawax_id == qhawax_id). \ filter(AirQualityMeasurement.timestamp_zone >= last_timestamp). \ filter(AirQualityMeasurement.timestamp_zone <= initial_timestamp). \ order_by(AirQualityMeasurement.timestamp_zone.asc()).all() return None
def queryLastTimeOffDueLackEnergy(qhawax_name): """Helper qHAWAX function to get last time off due to lack energy""" if ( same_helper.getInstallationIdBaseName(qhawax_name) is not None ): # Enter if qHAWAX is in field qhawax_id = same_helper.getQhawaxID(qhawax_name) list_last_turn_off = ( session.query(Bitacora.timestamp_zone) .filter_by(qhawax_id=qhawax_id) .filter_by(description="qHAWAX off") .order_by(Bitacora.timestamp_zone.desc()) .limit(1) .all() ) if list_last_turn_off != []: return list_last_turn_off[0][0] else: list_last_turn_on = ( session.query( QhawaxInstallationHistory.last_time_physically_turn_on_zone ) .filter_by(qhawax_id=qhawax_id) .filter_by(end_date_zone=None) .limit(1) .all() ) return list_last_turn_on[0] return None
def recordEndTrip(qhawax_name, details): qhawax_id = same_helper.getQhawaxID(qhawax_name) name = qhawax_name.strip() if qhawax_id != None: finish_date = ( session.query(TripLog.trip_end) .filter(TripLog.qhawax_id == qhawax_id) .order_by(TripLog.id.desc()) .first() ) if finish_date[0] == None: installation_id = same_helper.getInstallationIdBaseName( qhawax_name ) if installation_id is not None: value = ( session.query(ValidProcessedMeasurement.timestamp_zone) .filter_by(qhawax_installation_id=installation_id) .order_by(ValidProcessedMeasurement.id.desc()) .first() .timestamp_zone ) socketio.emit(name + "_finishTrip", str(value)) finish_json = {"trip_end": value, "details": details} session.query(TripLog).filter_by( qhawax_id=qhawax_id, trip_end=None ).update(values=finish_json) session.commit()
def recordDroneTakeoff(flight_start, qhawax_name): qhawax_id = same_helper.getQhawaxID(qhawax_name) gas_inca_processed = DroneFlightLog( flight_start=flight_start, qhawax_id=qhawax_id ) session.add(gas_inca_processed) session.commit()
def queryDBAirQuality(qhawax_name, initial_timestamp, final_timestamp, date_format): sensors = (AirQualityMeasurement.CO, AirQualityMeasurement.H2S, AirQualityMeasurement.NO2, AirQualityMeasurement.O3, AirQualityMeasurement.PM25, AirQualityMeasurement.PM10, AirQualityMeasurement.SO2, AirQualityMeasurement.lat, AirQualityMeasurement.lon, AirQualityMeasurement.alt, AirQualityMeasurement.timestamp_zone) initial_timestamp_utc = datetime.datetime.strptime(initial_timestamp, date_format) final_timestamp_utc = datetime.datetime.strptime(final_timestamp, date_format) qhawax_id = same_helper.getQhawaxID(qhawax_name) if (qhawax_id != None): air_quality_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() if (air_quality_measurements != []): return [ measurement._asdict() for measurement in air_quality_measurements ] return [] return None
def insertDefaultOffsets(qhawax_name): """ To insert a Default Offset """ last_gas_sensor_id = 0 last_gas_sensor = get_business_helper.queryGetLastGasSensor() if (last_gas_sensor != None): last_gas_sensor_id = last_gas_sensor[0] if (same_helper.qhawaxExistBasedOnName(qhawax_name)): qhawax_id = int(same_helper.getQhawaxID(qhawax_name)) initial_serial_number = qhawax_id * 100 start = 1 for index in range(len(var_gases)): sensor_data = { 'id': last_gas_sensor_id + start, 'qhawax_id': qhawax_id, 'serial_number': initial_serial_number + start, 'type': var_gases[index], 'WE': 0.0, 'AE': 0.0, 'sensitivity': 0.0, 'sensitivity_2': 0.0, 'C0': 0.0, 'C1': 0.0, 'C2': 0.0, 'NC0': 0.0, 'NC1': 0.0 } sensor_data_var = GasSensor(**sensor_data) session.add(sensor_data_var) session.commit() start += 1
def storeAirQualityDataInDB(data): """Helper function to record Air Quality measurement""" data = exceptions.checkVariable_helper(data, dict) qhawax_name = data.pop("ID", None) qhawax_id = same_helper.getQhawaxID(qhawax_name) if qhawax_id != None: air_quality_data = { "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_ug_m3": data["O3_ug_m3"], "PM25": data["PM25"], "PM10": data["PM10"], "O3": data["O3"], "lat": data["lat"], "lon": data["lon"], "alt": data["alt"], "uv": data["UV"], "spl": data["SPL"], "temperature": data["temperature"], "timestamp_zone": data["timestamp_zone"], "I_temperature": data["I_temperature"], "humidity": data["humidity"], "pressure": data["pressure"], } air_quality_measurement = AirQualityMeasurement( **air_quality_data, qhawax_id=qhawax_id ) session.add(air_quality_measurement) session.commit()
def queryDBProcessed(qhawax_name, initial_timestamp, final_timestamp): """ Helper function to get Processed Measurement filter by qHAWAX between timestamp""" qhawax_id = same_helper.getQhawaxID(qhawax_name) if(qhawax_id is not None): sensors = (ProcessedMeasurement.CO, ProcessedMeasurement.CO2, ProcessedMeasurement.H2S,ProcessedMeasurement.NO, ProcessedMeasurement.NO2, ProcessedMeasurement.O3, ProcessedMeasurement.PM1,ProcessedMeasurement.PM25, ProcessedMeasurement.PM10,ProcessedMeasurement.SO2, ProcessedMeasurement.VOC, ProcessedMeasurement.UV, ProcessedMeasurement.UVA, ProcessedMeasurement.UVB, ProcessedMeasurement.spl, ProcessedMeasurement.humidity, ProcessedMeasurement.pressure, ProcessedMeasurement.temperature, ProcessedMeasurement.lat, ProcessedMeasurement.lon, ProcessedMeasurement.alt, ProcessedMeasurement.timestamp_zone, ProcessedMeasurement.CO_ug_m3, ProcessedMeasurement.H2S_ug_m3, ProcessedMeasurement.NO2_ug_m3, ProcessedMeasurement.O3_ug_m3, ProcessedMeasurement.SO2_ug_m3, ProcessedMeasurement.I_temperature) processed_measurements = session.query(*sensors).filter(ProcessedMeasurement.qhawax_id == qhawax_id). \ filter(ProcessedMeasurement.timestamp_zone >= initial_timestamp). \ filter(ProcessedMeasurement.timestamp_zone <= final_timestamp). \ order_by(ProcessedMeasurement.timestamp_zone).all() all_measurement = [] for measurement in processed_measurements: measurement = measurement._asdict() for key, value in measurement.items(): if((type(value) is float) and math.isnan(value)): measurement[key] = None all_measurement.append(measurement) return all_measurement return None
def queryDBProcessedByPollutant(qhawax_name, initial_timestamp, final_timestamp, pollutant): """Helper function to get Processed Measurement filter by qHAWAX between timestamp""" qhawax_id = same_helper.getQhawaxID(qhawax_name) if qhawax_id is not None: column_array = [ ProcessedMeasurement.CO.label("pollutant"), ProcessedMeasurement.H2S.label("pollutant"), ProcessedMeasurement.NO2.label("pollutant"), ProcessedMeasurement.O3.label("pollutant"), ProcessedMeasurement.PM25.label("pollutant"), ProcessedMeasurement.PM10.label("pollutant"), ProcessedMeasurement.SO2.label("pollutant"), ProcessedMeasurement.CO2.label("pollutant"), ProcessedMeasurement.VOC.label("pollutant"), ] # sensor_array = ['CO','H2S','NO2','O3','PM25','PM10','SO2'] for i in range(len(mobile_sensor_array)): if pollutant == mobile_sensor_array[i]: sensors = ( ProcessedMeasurement.timestamp_zone, column_array[i], ProcessedMeasurement.lat, ProcessedMeasurement.lon, ) measurements = (session.query(*sensors).filter( ProcessedMeasurement.qhawax_id == qhawax_id).filter( ProcessedMeasurement.timestamp_zone >= initial_timestamp ).filter(ProcessedMeasurement.timestamp_zone <= final_timestamp). order_by( ProcessedMeasurement.timestamp_zone.asc()).all()) return [t._asdict() for t in measurements] return None
def qHAWAXIsInFlight(qhawax_name): qhawax_id = same_helper.getQhawaxID(qhawax_name) if(qhawax_id is not None): flight = session.query(DroneFlightLog.flight_start).\ filter(DroneFlightLog.qhawax_id==qhawax_id,DroneFlightLog.flight_end == None).order_by(DroneFlightLog.id).all() if(flight!=[]): return flight[0][0] return None
def storeProcessedDataInDB(data): """Helper Processed Measurement function to store Processed Data""" data = exceptions.checkVariable_helper(data, dict) qhawax_name = data.pop("ID", None) qhawax_id = same_helper.getQhawaxID(qhawax_name) processed_measurement = ProcessedMeasurement(**data, qhawax_id=qhawax_id) session.add(processed_measurement) session.commit()
def queryMobileQhawaxColor(name): """Helper qHAWAX function to get main inca value""" qhawax_id = same_helper.getQhawaxID(name) if qhawax_id is not None: main_inca = getMainIncaQhawaxTable(qhawax_id) if main_inca is not None: return util_helper.getColorBaseOnGasValuesMobile(main_inca) return None
def queryLastMainInca(qhawax_name): """Helper function to get last main inca based on qHAWAX ID """ qhawax_id = same_helper.getQhawaxID(qhawax_name) if(qhawax_id is not None): inca = session.query(GasInca.main_inca).filter(GasInca.qhawax_id == qhawax_id).order_by(GasInca.id).all() if(inca==[]): return None return session.query(GasInca.main_inca).filter(GasInca.qhawax_id == qhawax_id).order_by(GasInca.id.desc()).first()[0] return None
def qHAWAXIsInTrip(qhawax_name): qhawax_id = same_helper.getQhawaxID(qhawax_name) if qhawax_id is not None: trip = (session.query(TripLog.trip_start).filter( TripLog.qhawax_id == qhawax_id, TripLog.trip_end == None).order_by(TripLog.id).all()) if trip != []: return trip[0][0] return None
def queryIncaQhawax(name): """ Helper qHAWAX function to get main inca value """ qhawax_id = same_helper.getQhawaxID(name) if (qhawax_id is not None): qhawax_inca = int(same_helper.getMainIncaQhawaxTable(qhawax_id)) return util_helper.getColorBaseOnIncaValue(qhawax_inca) return None
def storeGasIncaInDB(data): """ Helper function to record GAS INCA measurement""" data = exceptions.checkDictionaryVariable(data) gas_inca_data = {'CO': data['CO'],'H2S': data['H2S'],'SO2': data['SO2'],'NO2': data['NO2'],'timestamp_zone':data['timestamp_zone'], 'O3': data['O3'],'PM25': data['PM25'],'PM10': data['PM10'],'main_inca':data['main_inca']} qhawax_name = data.pop('ID', None) qhawax_id = same_helper.getQhawaxID(qhawax_name) gas_inca_processed = GasInca(**gas_inca_data, qhawax_id=qhawax_id) session.add(gas_inca_processed) session.commit()
def getQhawaxLatestTimestampProcessedMeasurement(qhawax_name): """ Helper qHAWAX function to get latest timestamp in UTC 00 from Processed Measurement """ qhawax_id =same_helper.getQhawaxID(qhawax_name) if(qhawax_id is not None): qhawax_time = session.query(ProcessedMeasurement.timestamp_zone).filter_by(qhawax_id=qhawax_id).first() processed_measurement_timestamp="" if(qhawax_time!=None): processed_measurement_timestamp = session.query(ProcessedMeasurement.timestamp_zone).filter_by(qhawax_id=qhawax_id) \ .order_by(ProcessedMeasurement.id.desc()).first().timestamp_zone return processed_measurement_timestamp return None
def queryDBProcessedByPollutantMobile(qhawax_name, initial_timestamp, final_timestamp, pollutant): """Helper function to get Processed Measurement filter by qHAWAX between timestamp""" qhawax_id = same_helper.getQhawaxID(qhawax_name) if qhawax_id is not None: column_array = [ ProcessedMeasurement.CO.label("pollutant"), ProcessedMeasurement.H2S.label("pollutant"), ProcessedMeasurement.NO2.label("pollutant"), ProcessedMeasurement.O3.label("pollutant"), ProcessedMeasurement.PM25.label("pollutant"), ProcessedMeasurement.PM10.label("pollutant"), ProcessedMeasurement.SO2.label("pollutant"), ProcessedMeasurement.CO2.label("pollutant"), ProcessedMeasurement.VOC.label("pollutant"), ] # sensor_array = ['CO','H2S','NO2','O3','PM25','PM10','SO2'] for i in range(len(mobile_sensor_array)): if pollutant == mobile_sensor_array[i]: sensors = ( ProcessedMeasurement.timestamp_zone, column_array[i], ProcessedMeasurement.lat, ProcessedMeasurement.lon, ) measurements = (session.query(*sensors).filter( ProcessedMeasurement.qhawax_id == qhawax_id).filter( ProcessedMeasurement.timestamp_zone >= initial_timestamp ).filter(ProcessedMeasurement.timestamp_zone <= final_timestamp). order_by( ProcessedMeasurement.timestamp_zone.asc()).all()) factor_final_json = { "CO": 100 / 10000, "NO2": 100 / 200, "PM10": 100 / 150, "PM25": 100 / 25, "SO2": 100 / 20, "O3": 100 / 120, "H2S": 100 / 150, } values = [] if pollutant in factor_final_json: for t in measurements: dictValue = t._asdict() dictValue["pollutant"] = round( dictValue["pollutant"] * factor_final_json[pollutant], 3) values.append(dictValue) return values else: return [t._asdict() for t in measurements] return None
def queryLastTimePhysicallyTurnOnZone(qhawax_name): qhawax_id = same_helper.getQhawaxID(qhawax_name) if qhawax_id is not None: last_turn_on = ( session.query( QhawaxInstallationHistory.last_time_physically_turn_on_zone ) .filter(QhawaxInstallationHistory.qhawax_id == qhawax_id) .first() ) return last_turn_on[0] return None
def queryLastRegistrationTimezone(name): qhawax_id = same_helper.getQhawaxID(name) if qhawax_id is not None: last_turn_on = ( session.query( QhawaxInstallationHistory.last_registration_time_zone ) .filter(QhawaxInstallationHistory.qhawax_id == qhawax_id) .first() ) return last_turn_on[0] return None
def recordStartTrip(qhawax_name): qhawax_id = same_helper.getQhawaxID(qhawax_name) name = qhawax_name.strip() if qhawax_id != None: start_trip = datetime.datetime.now(dateutil.tz.tzutc()) start = TripLog( trip_start=datetime.datetime.now(dateutil.tz.tzutc()), qhawax_id=qhawax_id, ) session.add(start) session.commit() socketio.emit(name + "_startTrip", str(start_trip))
def storeProcessedDataInDB(data): """ Helper Processed Measurement function to store Processed Data """ if(isinstance(data, dict) is not True): raise TypeError("Processed variable "+str(data)+" should be Json") qhawax_name = data.pop('ID', None) qhawax_id = same_helper.getQhawaxID(qhawax_name) processed_measurement = ProcessedMeasurement(**data, qhawax_id=qhawax_id) session.add(processed_measurement) session.commit()
def storeAirQualityDataInDB(data): if(isinstance(data, dict) is not True): raise TypeError("Air Quality variable "+str(data)+" should be Json") qhawax_name = data.pop('ID', None) qhawax_id = same_helper.getQhawaxID(qhawax_name) data['uv'] = data['UV'] data['spl'] = data['SPL'] data.pop('SPL', None) data.pop('UV', None) air_quality_measurement = AirQualityMeasurement(**data, qhawax_id=qhawax_id) session.add(air_quality_measurement) session.commit()
def storeGasIncaInDB(data): """ Helper function to record GAS INCA measurement """ if(isinstance(data, dict) is not True): raise TypeError("Gas Inca variable "+str(data)+" should be Json") qhawax_name = data.pop('ID', None) qhawax_id = same_helper.getQhawaxID(qhawax_name) gas_inca_processed = GasInca(**data, qhawax_id=qhawax_id) session.add(gas_inca_processed) session.commit()