import sys import _thread import datetime import time import schedule import echo_controller.controller as Controller import echo_db.database as EchoDB class EchoScheduler: @staticmethod def init(): print('Starting Echo Scheduler Thread...') _thread.start_new_thread(EchoScheduler.scheduler, ("Main Scheduler Thread", 2, )) @staticmethod def scheduler(threadname, delay): print('Echo Scheduler Thread started!') # Schedule Tasks schedule.every(2.1).minutes.do(EchoScheduler.getClimateData) schedule.every(2.2).minutes.do(EchoScheduler.getLightData) schedule.every(1).minute.do(EchoScheduler.checkAutomaticShutdown) schedule.every(1).minute.do(EchoScheduler.checkAutomaticBlinds) # collection_delete = EchoDB.EchoDB.database['ClimateLogs'] # collection_delete.remove({}) while True: schedule.run_pending() time.sleep(1) @staticmethod def getClimateData(): # Get Device collection = EchoDB.EchoDB.database['Devices'] device = collection.find_one({'uuid': 'aa50-48fd'}) # Call Action Controller.Controller.handle_action(device, "gather_climate_data") print('Scheduler Gathered Climate Data') @staticmethod def getLightData(): # Get Device collection = EchoDB.EchoDB.database['Devices'] device = collection.find_one({'uuid': 'be0c-4bc7'}) # Call Action Controller.Controller.handle_action(device, "gather_light_data") print('Scheduler Gathered Light Data') @staticmethod def checkAutomaticShutdown(): collection = EchoDB.EchoDB.database['Devices'] # # Main Light # # Get Device device_main_light = collection.find_one({'uuid': '9472-4f60'}) # Check if light is on if device_main_light['status'] == "off": return; # Check if automatic movement is enabled if device_main_light['automatic_movement'] == False: return; # Check if date is current time is past device automatic shutdown time automatic_shutdown_time = device_main_light['automatic_shutdown'] current_time = datetime.datetime.now() if current_time > automatic_shutdown_time: Controller.Controller.handle_action(device_main_light, "off") print('Scheduler Turned off main light') # # LED Light # # Get Device device_bed_led = collection.find_one({'uuid': '34gr-54jf'}) # Check if light is on if device_bed_led['status'] == "off": return; # Check if automatic movement is enabled if device_bed_led['automatic_movement'] == False: return; # Check if date is current time is past device automatic shutdown time automatic_shutdown_time = device_bed_led['automatic_shutdown'] current_time = datetime.datetime.now() if current_time > automatic_shutdown_time: Controller.Controller.handle_action(device_bed_led, "off") print('Scheduler Turned off main bed led light') @staticmethod def checkAutomaticBlinds(): # Get current time now_time = datetime.datetime.now() now_hours = now_time.hour now_minutes = now_time.minute check_time = str(now_hours).zfill(2) + ":" + str(now_minutes).zfill(2) # Get Device collection = EchoDB.EchoDB.database['Devices'] device = collection.find_one({'uuid': 'b039-471a'}) # Check if automatic open is enabled if device['automatic_open'] == False: return; automatic_open_time = device['automatic_open_time'] if automatic_open_time == check_time: Controller.Controller.handle_action(device, "preset", "open") print('Scheduler Opened Sunblinds')