def meassure(): """ Measures the FlowerDevices of all active GrowSessions. Waters if their water is under the WaterDevice threshhol. Returns: """ if app.config['HARDWARE']: from app.hardware import flower_power result = [] for grow_session in GrowSession.get_active(): result += grow_session.flower_devices if len(result) == 0: print("Nothing to meassure") return for flower_device in result: if app.config['HARDWARE']: data = flower_power.get_flower_data(flower_device.mac) else: # Mock data for development and testing. data = dict(Temperature=10.23, Light=100, Water=50.0, Battery=90, Ecb=0.5, EcPorus=0.6, DLI=0.7, Ea=0.8, ) flower_data = FlowerData.new_flower_data(data, flower_device.id, flower_device.grow_session.id) db.session.add(flower_data) db.session.commit() hub.new_data_point(flower_data) new_event("Meassure Flower_device " + flower_device.mac) # Check if we have to water. for water_device in flower_device.grow_session.water_devices: if data['Water'] < water_device.water_threshhold: # Have to water. start_water(water_device.id) logging.getLogger().warning("Meassure completed")
def make_data_flower_data(self): self.db.session.add(GrowSession(name="grow_session", start_date=datetime.now())) grow_session = self.db.session.query(GrowSession).first() self.db.session.add(FlowerDevice(name="flower", mac="A0:14:3D:08:B4:90", grow_session_id=grow_session.id)) flower_device = self.db.session.query(FlowerDevice).first() data = dict(Temperature=10.23, Light=100, Water=50.0, Battery=90, Ecb=0.5, EcPorus=0.6, DLI=0.7, Ea=0.8, ) self.db.session.add(FlowerData.new_flower_data(data, flower_device.id, flower_device.grow_session.id)) self.db.session.commit()
def reset(): """ Make test data for dev mode. """ db.drop_all() db.create_all() from app.models import GrowSessions from datetime import datetime, timedelta session = GrowSessions(name='test_session', start_date=datetime.utcnow(), days_grow_stage=20) db.session.add(session) db.session.commit() from app.models import FlowerDevices device_a = FlowerDevices(name='flower_device_a', mac='amac', grow_session_id=session.id) db.session.add(device_a) device_b = FlowerDevices(name='flower_device_b', mac='bmac', grow_session_id=session.id) db.session.add(device_b) db.session.commit() for i in range(50): from app.models import FlowerData datapoint = FlowerData( timestamp=datetime.utcnow() - timedelta(i), temperature=10.23 + i * 0.1, light=100, water=40.5 - i * 0.5, battery=90 - i, ecb=0.5, ec_porus=0.6, dli=0.7, ea=0.8, flower_device_id=device_a.id, ) db.session.add(datapoint) user = Users(username='******', password='******') db.session.add(user) db.session.commit() print('Fake data made')