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")
def switch_off(self, light_devices): from app.tasks import switch_light_off if isinstance(light_devices, list): light_devices = light_devices else: light_devices = [light_devices] self.update_redirect() for light_device in light_devices: new_event("Switch Light Action " + light_device.name + " off") switch_light_off(light_device.id) return redirect(self.get_redirect())
def webcam(): if app.config["WEBCAM"]: from app.hardware import webcam as webcam_module for webcam_item in Webcam.get_active(): if webcam_item.grow_session.is_day(): filename = webcam_module.webcam_make_screenshot() # Upload screenshot hub.new_webcam_screenshot(filename) new_event("Made screenshot " + webcam_item.name) # Save screenshot in DB. image = Image.open(filename) db.session.add(WebcamScreenshot.new_webcam_screenshot(image, webcam_item)) db.session.commit()
def time_lapse(): if app.config["WEBCAM"]: from app.hardware import webcam as webcam_module for webcam_item in Webcam.get_active(): screenshots = db.session.query(WebcamScreenshot).order_by(WebcamScreenshot.timestamp).all() images = [] for screenshot in screenshots: image = Image.frombytes('RGB', screenshot.get_size(), buffer(screenshot.file)) images.append(image) time_lapse_filename = 'time_lapse.gif' writeGif(time_lapse_filename, images, duration=0.2) hub.new_webcam_gif(time_lapse_filename) new_event("Made Time Lapse")
def stop_water(water_id): """ Stops the WaterDevice. Args: water_id (str): The id of the WaterDevice. Returns: """ water_device = appbuilder.session.query(WaterDevice).filter(WaterDevice.id == water_id).first() switch_water_to(water_device, False) water_device.switch_off_time = None db.session.commit() new_event("Switch Water " + water_device.name + " to False")
def switch(self, water_devices): from app.tasks import scheduler, switch_water if isinstance(water_devices, list): water_devices = water_devices else: water_devices = [water_devices] self.update_redirect() for water_device in water_devices: job = scheduler.get_job('water_off_' + water_device.name) if job: job.remove() new_event("Switch Water Action " + water_device.name) switch_water(water_device.id) return redirect(self.get_redirect())
def start(self, grow_sessions): if isinstance(grow_sessions, list): grow_session = grow_sessions[0] else: grow_session = grow_sessions grow_session.start_date = datetime.now() new_event("GrowSession started " + grow_session.name) self.update_redirect() from tasks import start_light_tasks for light_device in grow_session.light_devices: start_light_tasks(light_device) return redirect(self.get_redirect())
def start_water(water_id): """ Starts the WaterDevice with water_id and starts the task to turn it off. Args: water_id (str): The id of the WaterDevice. Returns: """ water_device = appbuilder.session.query(WaterDevice).filter(WaterDevice.id == water_id).first() switch_water_to(water_device, True) water_device.switch_off_time = datetime.now() + timedelta(minutes=water_device.watering_duration_minutes) db.session.commit() scheduler.add_job(stop_water, 'date', run_date=water_device.switch_off_time, args=[water_device.id], misfire_grace_time=10000000, id='water_off_' + water_device.name) new_event("Switch Water " + water_device.name + " to True")