def init_start(self): session = self.get_session() sample = Samples(temperature=0, pressure=0, humidity=0, windspeed=0) session.add(sample) session.commit() sample_id = int(sample.id) session.close() return sample_id
def init_sample(self, dict_sample): session = self.get_session() sample = Samples(temperature=dict_sample["temperatura"], humidity=dict_sample["humedad"], pressure=dict_sample["presion"], windspeed=dict_sample["viento"]) session.add(sample) session.commit() sample_id = int(sample.id) session.close() return sample_id
def _get_metrics(): pro.start_process() average=Samples() last_meas=Samples() db=Database() last_meas = db.get_last_sample() average = db.get_average_sample() ### #COMENTAR O BORRAR DE ACA ''' average.temperature = randint(0, 100) average.humidity = randint(0, 100) average.pressure = randint(900, 1024) average.windspeed = randint(25, 500) last_meas.temperature = randint(0, 100) last_meas.humidity = randint(0, 100) last_meas.pressure = randint(900, 1024) last_meas.windspeed = randint(25, 500) ''' ###* #HASTA ACA ###* #Se crea un conjunto clave,valor para luego crear el json test = [ { "temp" : last_meas.temperature,"hum" : last_meas.humidity,"pre" : last_meas.pressure, "wind": last_meas.windspeed}, { "temp" : average.temperature,"hum" : average.humidity,"pre" : average.pressure, "wind": average.windspeed} ] ###* #Creamos el json test = json.dumps(test) return test
def save_values(self, values): session = self.get_session() samp = Samples(temperature=values['measuredtemp'], humidity=values['measuredhum'], pressure=values['measuredpres'], windspeed=values['measuredwsp']) session.add(samp) session.commit() samp_id = int(samp.id) session.close() return samp_id
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 init_start( self ): #Inicializa los valores de un sample que luego seran modificados en el py y los guarda en la base de datos. Además devuelve el Id. session = self.get_session() sample = Samples(temperature=22, pressure=1013, humidity=50, windspeed=10) session.add(sample) session.commit() sample_id = int(sample.id) session.close() return sample_id
def add_sample(self, sample): session = self.get_session() samples = {} samples["temperatura"] = sample.temperature + random.randint(-1,1) samples["humedad"] = sample.humidity + random.randint(-1,1) samples["presion"] = sample.pressure + random.randint(-1,1) samples["viento"] = sample.windspeed + random.randint(-1,1) newSample = Samples(temperature=samples["temperatura"], humidity=samples["humedad"], pressure=samples["presion"], windspeed=samples["viento"]) session.add(newSample) session.commit() session.close() return newSample
def new_samples(self, temperatura, humedad, h_pascales, viento): """Generate samples of temperature, humidity, pressure and windspeed in the database. Returns: nothing """ session = self.get_session() samples = Samples(temperature=temperatura, humidity=humedad, pressure=h_pascales, windspeed=viento) session.add(samples) session.commit() session.close()
def main(session): killer = GracefulKiller() while(1): #asignacion de parametros a los valores r = Samples(temperature=random.randint(10,13),humidity=random.randint(50,55),pressure=random.randint(1019,1022), windspeed=random.randint(20,40)) #agregarlos a la sesion obtenida en el main principal session.add(r) #llevarlos a la db session.commit() time.sleep(1) if killer.kill_now: session.close() break
def set_sample(self, temperature, humidity, pressure, windspeed): """Persiste una muestra a la DB Returns: [state] -- [True si la muestra fue guardada correctamente] """ session = self.get_session() sample = Samples(temperature=temperature, humidity=humidity, pressure=pressure, windspeed=windspeed) session.add(sample) session.commit() session.close() return True
def main(session,t): killer = GracefulKiller() while(1): t.temperature += random.randint(-1,1) #Modifica el valor de la ultima muestra tomada t.humidity += random.randint(-1,1) t.pressure += random.randint(-1,1) t.windspeed += random.randint(-1,1) #cant = session.query(func.count(Samples.id)) #Busca la ultima muestra tomada sample = Samples(temperature=t.temperature, pressure=t.pressure, humidity=t.humidity, windspeed=t.windspeed) #Crea una nueva fila en la base de datos con la nueva muestra session.add(sample) session.commit() #print(" Temperatura: %s | Humedad: %s | Presion: %s | Velocidad del viento: %s" % (t.temperature, t.humidity, t.pressure, t.windspeed)) time.sleep(2) #Lo puse cada 10 para que no genere tantos valores y poder debugearlo mejor if killer.kill_now: session.close() break
def main(session): killer = GracefulKiller() while (True): t, h, p, w = getrand() sample = Samples(temperature=round(t, 2), humidity=round(h, 2), pressure=round(p, 2), windspeed=round(w, 2)) session.add(sample) session.commit() print("Muestra guardada ") time.sleep(1) # print x if killer.kill_now: session.close() break
def main(session): killer = GracefulKiller() while (True): temperature = random.randint(-30, 40) # Crea valores aleatorios humidity = random.randint(0, 101) pressure = random.randint(1011, 1014) windspeed = random.randint(0, 200) # Crea una muestra a partir de ellos sample = Samples(temperature=temperature, humidity=humidity, pressure=pressure, windspeed=windspeed) session.add(sample) session.commit() # Persiste en la db sleep(1) # Delay de un segundo if killer.kill_now: session.close() break
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)
# Imports from flask import Flask from flask import render_template from flask import jsonify from flask import request from flask import redirect from models import Samples from aux_pro import Process from database import Database import time samples = Samples() app = Flask(__name__) db = Database() proc = Process() @app.route('/') def index(): proc.start_process() samples = db.get_last_ten() promedios = db.get_promedio() return render_template('index.html', samples=samples, promedios=promedios, refresco=5) @app.route('/stop') def stop():
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_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
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)