def test_grow_session(self): self.make_data_grow_session() self.assertEqual(len(GrowSession.get_active()), 1, "Number of active grow sessions is wrong") grow_session = GrowSession.get_active()[0] self.assertTrue(grow_session.is_active()) grow_session2 = GrowSession.get_inactive()[0] self.assertFalse(grow_session2.is_active())
def make_data_subscriber(self): self.db.session.add(GrowSession(name="grow_session", start_date=datetime.now())) grow_session = GrowSession.get_active()[0] self.db.session.add(Subscriber(name="test_subscriber", grow_session_id=grow_session.id)) self.db.session.add(GrowSession(name="grow_session2")) grow_session2 = GrowSession.get_inactive()[0] self.db.session.add(Subscriber(name="test_subscriber2", grow_session_id=grow_session2.id)) self.db.session.commit()
def update_subscribers(): """ Updates all Subscribers of active GrowSessions Returns: """ for active_grow_session in GrowSession.get_active(): subscribers = active_grow_session.subscribers if len(subscribers) == 0: continue newest_data_point = appbuilder.session.query(FlowerData) \ .filter(FlowerData.grow_session_id == active_grow_session.id) \ .order_by(FlowerData.timestamp.desc()).first() if newest_data_point is None: # If no data in GrowSession just skip. continue data = newest_data_point.get_data_dict() for subscriber in subscribers: receiver = subscriber.receiver_email subject = "Flower Data " \ + newest_data_point.grow_session.name \ + " " + data['TimeStamp'] + " Water: " \ + data['Water'] body = "" for attribute in data: body += attribute + " " + data[attribute] + "\n" hub.send_email(receiver, subject, body) new_event("Send email to " + subscriber.name)
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")