def __init__(self, lgt, lat, use_cron, myxpl, log): """ Init the dawndusk API @param lgt : longitude of the observer @param lat : latitude of the observer """ self.use_cron = use_cron self.log = log self.myxpl = myxpl if self.use_cron == False: self._scheduler = Scheduler() self._scheduler.start() else: self._cronquery = CronQuery(self.myxpl, self.log) self.mycity = ephem.Observer() self.mycity.lat, self.mycity.lon = lat, lgt self.mycity.horizon = '-6' self.job = None self.job_test_dawn = None self.job_test_dusk = None
def setUp(self): global sendplugin self.__myxpl = sendplugin.myxpl self.cronquery = CronQuery(self.__myxpl)
def __init__(self): """ Create the cron class """ XplPlugin.__init__(self, name = 'earth') self.log.debug("__init__ : Start ...") self.config = Query(self.myxpl, self.log) try: delay_cron = int(self.config.query('earth', 'delay-cron')) except: delay_cron = 30 error = "Can't get configuration from XPL : %s" % (traceback.format_exc()) self.log.warning("__init__ : " + error) self.log.warning("Continue with default values.") cron = CronQuery(self.myxpl, self.log) cont = 3 cron_started = False while not self.get_stop().isSet() and cont>=0: try : res = cron.is_running_server() except : pass if res : cron_started = True cont = -1 else: self.log.debug("Can't talk to cron plugin. Retries=%s" % cont) self.get_stop().wait(delay_cron) cont -= 1 if not cron_started : self.force_leave() error = "Can't talk to cron plugin. Exiting ..." self.log.error("__init__ : "+error) return else : self.log.info("Communication with the cron plugin established.") self.log.debug("__init__ : Try to start the earth API") try: self._earth = EarthAPI(self.log, self.config, self.myxpl, \ self.get_data_files_directory(), \ self.get_stop(), self.get_sanitized_hostname()) except: self.force_leave() error = "Something went wrong during EarthAPI init : %s" % \ (traceback.format_exc()) self.log.error("__init__ : "+error) return self.log.debug("__init__ : Try to create listeners") Listener(self.request_cb, self.myxpl, {'schema': 'earth.request', 'xpltype': 'xpl-cmnd'}) Listener(self.fired_cb, self.myxpl, {'schema': 'earth.basic', 'xpltype': 'xpl-trig', 'current': 'fired'}) Listener(self.basic_cb, self.myxpl, {'schema': 'earth.basic', 'xpltype': 'xpl-cmnd'}) self.add_stop_cb(self._earth.stop_all) self.log.debug("__init__ : Enable the heartbeat") self.enable_hbeat() self._earth.plugin_enabled(True) self.log.info("Plugin earth correctly started.")
class DawnduskAPI: """ dawndusk API """ def __init__(self, lgt, lat, use_cron, myxpl, log): """ Init the dawndusk API @param lgt : longitude of the observer @param lat : latitude of the observer """ self.use_cron = use_cron self.log = log self.myxpl = myxpl if self.use_cron == False: self._scheduler = Scheduler() self._scheduler.start() else: self._cronquery = CronQuery(self.myxpl, self.log) self.mycity = ephem.Observer() self.mycity.lat, self.mycity.lon = lat, lgt self.mycity.horizon = '-6' self.job = None self.job_test_dawn = None self.job_test_dusk = None def __del__(self): """ Kill the dawndusk API @param lgt : longitude of the observer @param lat : latitude of the observer """ if self.use_cron == True: self._cronquery.halt_job("dawndusk") self._cronquery.halt_job("dawn-test") self._cronquery.halt_job("dusk-test") else: self._scheduler.shutdown() def sched_add(self, sdate, cb_function, label): """ Add an event in the schedulered tasks @param sdate : the date of the event @param cb_function : the callback function to call @param : the label of the event """ self.log.debug("dawndusk.schedAdd : Start ... %s" % label) if self.use_cron == False: if label == "dawn" or label == "dusk": self.job = self._scheduler.add_date_job(cb_function, \ sdate, args = [label]) self.log.debug("dawndusk.schedAdd : Use internal cron \ for %s" % label) elif label == "dawn-test": self.job_test_dawn = self._scheduler.add_date_job\ (cb_function, sdate, args = ["dawn"]) self.log.debug("dawndusk.schedAdd : Use internal cron \ for %s" % "dawn") elif label == "dusk-test": self.job_test_dusk = self._scheduler.add_date_job\ (cb_function, sdate, args = ["dusk"]) self.log.debug("dawndusk.schedAdd : Use internal cron \ for %s" % "dusk") for i in self._scheduler.get_jobs(): self.log.debug("APScheduler : %-10s | %8s" % \ (str(i.trigger), i.runs)) else: self.log.debug("dawndusk.schedAdd : Use external cron ...") if label == "dawn" or label == "dusk": device = "dawndusk" elif label == "dawn-test": device = "dawn-test" elif label == "dusk-test": device = "dusk-test" if self._cronquery.status_job(device, extkey = "current") \ != "halted": self._cronquery.halt_job(device) self.log.debug("dawndusk.schedAdd : Halt old device") nstmess = XplMessage() nstmess.set_type("xpl-trig") nstmess.set_schema("dawndusk.basic") nstmess.add_data({"type": "dawndusk"}) if label == "dawn": nstmess.add_data({"status": "dawn"}) elif label == "dusk": nstmess.add_data({"status": "dusk"}) elif label == "dawn-test": nstmess.add_data({"status": "dawn"}) elif label == "dusk-test": nstmess.add_data({"status": "dusk"}) if self._cronquery.start_date_job(device, nstmess, sdate): self.log.debug("dawndusk.schedAdd : External cron activated") self.log.debug("dawndusk.schedAdd : Done :)") else: self.log.error("dawndusk.schedAdd : Can't activate \ external cron") self.log.debug("dawndusk.schedAdd : Done :(") return False self.log.info("Add a new event of type %s at %s" % (label, sdate)) return True def get_next_dawn(self): """ Return the date and time of the next dawn @return : the next dawn daytime """ self.mycity.date = datetime.datetime.today() dawn = ephem.localtime(self.mycity.next_rising(ephem.Sun(), \ use_center = True)) return dawn def get_next_dusk(self): """ Return the date and time of the dusk @return : the next dusk daytime """ self.mycity.date = datetime.datetime.today() dusk = ephem.localtime(self.mycity.next_setting(ephem.Sun(), \ use_center = True)) return dusk def get_next_fullmoon_dawn(self): """ Return the date and time of the next dawn and dusk of the next fullmoon @return : the next dawn daytime """ self.mycity.date = self._get_next_fullmoon() dawn = ephem.localtime(self.mycity.next_rising(ephem.Moon(), \ use_center = True)) dusk = ephem.localtime(self.mycity.next_setting(ephem.Moon(), \ use_center = True)) if dawn > dusk: dawn = ephem.localtime(self.mycity.previous_rising(ephem.Moon(), \ use_center = True)) return dawn def get_next_fullmoon_dusk(self): """ Return the date and time of the dusk of the next fullmoon @return : the next dusk daytime """ self.mycity.date = self._get_next_fullmoon() dusk = ephem.localtime(self.mycity.next_setting(ephem.Moon(), \ use_center = True)) return dusk def get_next_fullmoon(self): """ Return the date and time of the next fullmoon @return : the next full moon daytime """ dusk = ephem.localtime(self._get_next_fullmoon()) return dusk def _get_next_fullmoon(self): """ Return the date and time of the next full moon @return : the next full moon daytime """ now = datetime.datetime.today() nextfullmoon = ephem.next_full_moon(now) return nextfullmoon
class DawnduskAPI: """ dawndusk API """ def __init__(self, lgt, lat, use_cron, myxpl, log): """ Init the dawndusk API @param lgt : longitude of the observer @param lat : latitude of the observer """ self.use_cron = use_cron self.log = log self.myxpl = myxpl if self.use_cron == False: self._scheduler = Scheduler() self._scheduler.start() else: self._cronquery = CronQuery(self.myxpl, self.log) self.mycity = ephem.Observer() self.mycity.lat, self.mycity.lon = lat, lgt self.mycity.horizon = '-6' self.job = None self.job_test_dawn = None self.job_test_dusk = None def __del__(self): """ Kill the dawndusk API @param lgt : longitude of the observer @param lat : latitude of the observer """ if self.use_cron == True: self._cronquery.halt_job("dawndusk") self._cronquery.halt_job("dawn-test") self._cronquery.halt_job("dusk-test") else : self._scheduler.shutdown() def sched_add(self, sdate, cb_function, label): """ Add an event in the schedulered tasks @param sdate : the date of the event @param cb_function : the callback function to call @param : the label of the event """ self.log.debug("dawndusk.schedAdd : Start ... %s" % label) if self.use_cron == False: if label == "dawn" or label == "dusk": self.job = self._scheduler.add_date_job(cb_function, \ sdate, args = [label]) self.log.debug("dawndusk.schedAdd : Use internal cron \ for %s" % label) elif label == "dawn-test": self.job_test_dawn = self._scheduler.add_date_job\ (cb_function, sdate, args = ["dawn"]) self.log.debug("dawndusk.schedAdd : Use internal cron \ for %s" % "dawn") elif label == "dusk-test": self.job_test_dusk = self._scheduler.add_date_job\ (cb_function, sdate, args = ["dusk"]) self.log.debug("dawndusk.schedAdd : Use internal cron \ for %s" % "dusk") for i in self._scheduler.get_jobs(): self.log.debug("APScheduler : %-10s | %8s" % \ (str(i.trigger), i.runs)) else : self.log.debug("dawndusk.schedAdd : Use external cron ...") if label == "dawn" or label == "dusk": device = "dawndusk" elif label == "dawn-test": device = "dawn-test" elif label == "dusk-test": device = "dusk-test" if self._cronquery.status_job(device, extkey = "current") \ != "halted": self._cronquery.halt_job(device) self.log.debug("dawndusk.schedAdd : Halt old device") nstmess = XplMessage() nstmess.set_type("xpl-trig") nstmess.set_schema("dawndusk.basic") nstmess.add_data({"type" : "dawndusk"}) if label == "dawn": nstmess.add_data({"status" : "dawn"}) elif label == "dusk": nstmess.add_data({"status" : "dusk"}) elif label == "dawn-test": nstmess.add_data({"status" : "dawn"}) elif label == "dusk-test": nstmess.add_data({"status" : "dusk"}) if self._cronquery.start_date_job(device, nstmess, sdate): self.log.debug("dawndusk.schedAdd : External cron activated") self.log.debug("dawndusk.schedAdd : Done :)") else: self.log.error("dawndusk.schedAdd : Can't activate \ external cron") self.log.debug("dawndusk.schedAdd : Done :(") return False self.log.info("Add a new event of type %s at %s" % (label, sdate)) return True def get_next_dawn(self): """ Return the date and time of the next dawn @return : the next dawn daytime """ self.mycity.date = datetime.datetime.today() dawn = ephem.localtime(self.mycity.next_rising(ephem.Sun(), \ use_center = True)) return dawn def get_next_dusk(self): """ Return the date and time of the dusk @return : the next dusk daytime """ self.mycity.date = datetime.datetime.today() dusk = ephem.localtime(self.mycity.next_setting(ephem.Sun(), \ use_center = True)) return dusk def get_next_fullmoon_dawn(self): """ Return the date and time of the next dawn and dusk of the next fullmoon @return : the next dawn daytime """ self.mycity.date = self._get_next_fullmoon() dawn = ephem.localtime(self.mycity.next_rising(ephem.Moon(), \ use_center = True)) dusk = ephem.localtime(self.mycity.next_setting(ephem.Moon(), \ use_center = True)) if dawn > dusk: dawn = ephem.localtime(self.mycity.previous_rising(ephem.Moon(), \ use_center = True)) return dawn def get_next_fullmoon_dusk(self): """ Return the date and time of the dusk of the next fullmoon @return : the next dusk daytime """ self.mycity.date = self._get_next_fullmoon() dusk = ephem.localtime(self.mycity.next_setting(ephem.Moon(), \ use_center = True)) return dusk def get_next_fullmoon(self): """ Return the date and time of the next fullmoon @return : the next full moon daytime """ dusk = ephem.localtime(self._get_next_fullmoon()) return dusk def _get_next_fullmoon(self): """ Return the date and time of the next full moon @return : the next full moon daytime """ now = datetime.datetime.today() nextfullmoon = ephem.next_full_moon(now) return nextfullmoon