def get_last_ten(self): promedio = Samples() # asigno la tabla de muestras #inicializo las variables para acumular promedio.temperature = 0 promedio.humidity = 0 promedio.pressure = 0 promedio.windspeed = 0 session = self.get_session() #optengo la sesion diez = session.query(Samples).order_by(Samples.id.desc()).limit( 10) #obtengo las ultimas 10 muestras session.close() #cierro sesion #acumulando las ultimas 10 temperaturas for d in diez: promedio.temperature = (promedio.temperature + d.temperature) promedio.humidity = (promedio.humidity + d.humidity) promedio.pressure = (promedio.pressure + d.pressure) promedio.windspeed = (promedio.windspeed + d.windspeed) #calculando el promedio para cada campo promedio.temperature = (promedio.temperature / 10) promedio.humidity = (promedio.humidity / 10) promedio.pressure = (promedio.pressure / 10) promedio.windspeed = (promedio.windspeed / 10) return promedio
def get_average_sample(self): temp = 0 humedad = 0 presion = 0 viento = 0 #obtiene la sesion session = self._get_session() #obtiene 10 medidas ordenadas por id en forma descendente query = session.query(Samples).order_by(desc(Samples.id)).limit(10) samples = query.all() #suma los valores de las ultimas 10 medidas for s in samples: temp = temp + s.temperature humedad = humedad + s.humidity presion = presion + s.pressure viento = viento + s.windspeed average = Samples() #calcula el promedio average.temperature = float(temp) / 10 average.humidity = float(humedad) / 10 average.pressure = float(presion) / 10 average.windspeed = float(viento) / 10 session.close() #retorna el objeto return average
def get_last_ten(self): promedio = Samples() promedio.temperature = 0 promedio.humidity = 0 promedio.pressure = 0 promedio.windspeed = 0 session = self.get_session() diez = session.query(Samples).order_by(Samples.id.desc()).limit(10) session.close() for d in diez: promedio.temperature = (promedio.temperature + d.temperature) promedio.humidity = (promedio.humidity + d.humidity) promedio.pressure = (promedio.pressure + d.pressure) promedio.windspeed = (promedio.windspeed + d.windspeed) promedio.temperature = (promedio.temperature/10) promedio.humidity = (promedio.humidity/10) promedio.pressure = (promedio.pressure/10) promedio.windspeed = (promedio.windspeed/10) return promedio
def getAverageSamples(self): session = self.getSession() result = session.query(Samples).order_by(Samples.id.desc()).limit(10) session.close() auxTemperature = 0 auxHumidity = 0 auxPressure = 0 auxWindspeed = 0 for sample in result: auxTemperature = auxTemperature + sample.temperature auxHumidity = auxHumidity + sample.humidity auxPressure = auxPressure + sample.pressure auxWindspeed = auxWindspeed + sample.windspeed tenLastSampleAverage = Samples() tenLastSampleAverage.temperature = int(auxTemperature)/10 tenLastSampleAverage.humidity = int(auxHumidity)/10 tenLastSampleAverage.pressure = int(auxPressure)/10 tenLastSampleAverage.windspeed = int(auxWindspeed)/10 return tenLastSampleAverage.serialize()
from database import Database from models import Samples import time from random import randint #crea una instancia del objeto Database db=Database() while True: #crea una instancia de una medida sample=Samples() #crea las medidas en forma aleatoria sample.temperature= randint(0, 100) sample.humidity=randint(0, 100) sample.pressure=randint(900, 1024) sample.windspeed=randint(25, 500) #inserta la nueva muestra en la base de datos db.insert_sample(sample) time.sleep(1)
from random import randint from database import Database from models import Samples import time db = Database() while True: ### Generate value random and start insert data to DB ### toInsert = Samples() #toInsert.id = idValue++ This value is autoincremental. toInsert.temperature = randint(0,100) toInsert.humidity = randint(0,100) toInsert.pressure = randint(1020,1030) toInsert.windspeed = randint(5,300) ### End generate and insert to DB ### db.insertSample(toInsert) time.sleep(1)