def job(h, m, normal_day, holiday_day): # are we on holidays? global holidays_flag if holidays_flag: day_config = holiday_day else: day_config = normal_day now = datetime.datetime.today() now = now.replace(hour=int(h), minute=int(m)) res = False for _slice in day_config: beg = now.replace(hour=int(_slice[0:2]), minute=int(_slice[3:5])) end = now.replace(hour=int(_slice[6:8]), minute=int(_slice[9:11])) if ((now >= beg) and (now < end)): res = True break if res is True: print() # choose an announcement m_announce = one_of(day_config[_slice]) if (m == "00"): m_time = h+"h" else: m_time = h+"h"+m tools.log.debug("announcing time: "+m_announce.format(m_time)) try: requests.get(interphone_url, params={'service': tools.service_name, 'announce': m_announce.format(m_time)}, timeout=interphone_timeout) except Exception as e: tools.log.error(e.__str__()) tools.log.error("*** ERROR reaching interphone on "+str(interphone_url)+" ***") tools.notify("ERROR", "*** ERROR reaching interphone on "+str(interphone_url)+" ***") else: print("(not inside any timeframe)") return
def alive_check(): # tools.log.info("*** performing alive check() ***") t = Timer(tools.alive_frequency, alive_check) t.start() try: requests.get(tools.alive_url, params={'service': tools.service_name, 'version': tools.service_version}, timeout=tools.alive_timeout) except Exception as e: tools.log.error(e.__str__()) tools.log.error("*** ERROR reaching alive_url on "+str(tools.alive_url)+" ***") tools.notify("ERROR", "*** ERROR reaching alive_url on "+str(tools.alive_url)+" ***") return
def scan_alias(alias): """ bla bla bla """ try: res = check_output(["sudo hcitool cc "+BT_aliases[alias]+" && sudo hcitool rssi "+BT_aliases[alias]], shell=True) result = res.splitlines()[-1].decode('ascii') rssi = -75 except Exception as e: tools.notify("ERROR", "Error trying to scan alias "+alias+": "+e.__str__()) result = "NOT FOUND" rssi = -99 return(result, rssi)
def update_holidays_flag(): # are we on holidays? global holidays_flag try: r = requests.get(status_url, timeout=20) except Exception as e: tools.log.error(e.__str__()) tools.log.error("*** ERROR reaching status_url on "+str(status_url)+" ***") tools.notify("ERROR", "*** ERROR reaching status_url on "+str(status_url)+" ***") if r.json()[status_field] == status_value: holidays_flag = True else: holidays_flag = False
def query(query): """ query the db using a query string """ if client is None: print("Error: db.client has not been initialized!") exit(1) tools.log.debug("requesting from influxDB: "+str(query)) try: client.query(query) except Exception as e: tools.log.error(e.__str__()) tools.notify("ERROR", "while querying influxDB with query={}".format(query)) # TODO: we're not returning anything here !?! return
def review_alive_checks(): """ alive_check timestamp regular check """ tools.log.info("*** alive check() review ***") t = Timer(alive_review_frequency, review_alive_checks) t.start() now = datetime.datetime.now() for service in services: if (now - service_TS[service]).total_seconds() > max_delay: tools.notify("ALARM", "Service '"+service+"'' has not checked alive for "+tools.slang(now - service_TS[service])) # bc.notify("INFO", "Will now try to reboot machine '"+service_machine[service]+"'.") # TODO: try to reboot the remote machine? # !!! do not reboot the same machine multiple times, establish a list of rebooting machines or reset timers for services on the same machine return
def update_holidays_flag(): # are we on holidays? global holidays_flag try: r = requests.get(status_url, timeout=20) except Exception as e: tools.log.error(e.__str__()) tools.log.error("*** ERROR reaching status_url on " + str(status_url) + " ***") tools.notify( "ERROR", "*** ERROR reaching status_url on " + str(status_url) + " ***") if r.json()[status_field] == status_value: holidays_flag = True else: holidays_flag = False
def query(query): """ query the db using a query string """ if client is None: print("Error: db.client has not been initialized!") exit(1) tools.log.debug("requesting from influxDB: " + str(query)) try: client.query(query) except Exception as e: tools.log.error(e.__str__()) tools.notify("ERROR", "while querying influxDB with query={}".format(query)) # TODO: we're not returning anything here !?! return
def write_points(influx_json_body): """ write to the db using the json body returns: True if success """ # TODO: fill in the 'time field' if a bool option is True (default) if client is None: print("Error: db.client has not been initialized!") exit(1) influx_json_body[0]['time'] = datetime.datetime.utcnow().isoformat() tools.log.debug("writing to influxDB: "+str(influx_json_body)) try: client.write_points(influx_json_body) except Exception as e: tools.log.error(e.__str__()) tools.notify("ERROR", "while trying to write to influxDB with json_body={}".format(str(influx_json_body))) return False return True
def job(h, m, normal_day, holiday_day): # are we on holidays? global holidays_flag if holidays_flag: day_config = holiday_day else: day_config = normal_day now = datetime.datetime.today() now = now.replace(hour=int(h), minute=int(m)) res = False for _slice in day_config: beg = now.replace(hour=int(_slice[0:2]), minute=int(_slice[3:5])) end = now.replace(hour=int(_slice[6:8]), minute=int(_slice[9:11])) if ((now >= beg) and (now < end)): res = True break if res is True: print() # choose an announcement m_announce = one_of(day_config[_slice]) if (m == "00"): m_time = h + "h" else: m_time = h + "h" + m tools.log.debug("announcing time: " + m_announce.format(m_time)) try: requests.get(interphone_url, params={ 'service': tools.service_name, 'announce': m_announce.format(m_time) }, timeout=interphone_timeout) except Exception as e: tools.log.error(e.__str__()) tools.log.error("*** ERROR reaching interphone on " + str(interphone_url) + " ***") tools.notify( "ERROR", "*** ERROR reaching interphone on " + str(interphone_url) + " ***") else: print("(not inside any timeframe)") return
def alive_check(): # tools.log.info("*** performing alive check() ***") t = Timer(tools.alive_frequency, alive_check) t.start() try: requests.get(tools.alive_url, params={ 'service': tools.service_name, 'version': tools.service_version }, timeout=tools.alive_timeout) except Exception as e: tools.log.error(e.__str__()) tools.log.error("*** ERROR reaching alive_url on " + str(tools.alive_url) + " ***") tools.notify( "ERROR", "*** ERROR reaching alive_url on " + str(tools.alive_url) + " ***") return
def write_points(influx_json_body): """ write to the db using the json body returns: True if success """ # TODO: fill in the 'time field' if a bool option is True (default) if client is None: print("Error: db.client has not been initialized!") exit(1) influx_json_body[0]['time'] = datetime.datetime.utcnow().isoformat() tools.log.debug("writing to influxDB: " + str(influx_json_body)) try: client.write_points(influx_json_body) except Exception as e: tools.log.error(e.__str__()) tools.notify( "ERROR", "while trying to write to influxDB with json_body={}".format( str(influx_json_body))) return False return True
(result, rssi) = scan_alias(request.params.alias) return({'alias': request.params.alias, 'result': result, 'rssi': str(rssi)}) else: return("ERROR: alias unknown!") else: return("ERROR: alias field not found!") # ======================================================= # main loop - load config, init logs, wait and run if __name__ == "__main__": # initialize config/logs tools.load_config() tools.init_logs() # .ini startup_wait = tools.config.getint('startup', 'wait') # also: getfloat, getint, getboolean # read the aliases & BT_addresses BT_aliases = {} for key in tools.config['BT']: BT_aliases[key] = tools.config['BT'][key] # startup sync & notification tools.log.info("--= Restarting =--") tools.log.info("sleeping {} seconds for startup sync between services...".format(startup_wait)) time.sleep(startup_wait) tools.notify("WARNING", tools.service_version+" - (re)started!") # run baby, run! regular_check() run(host=tools.hostname, port=tools.port, server='gevent')
@get('/alive') def do_alive(): return(tools.service_version) @get('/turn_mellotron_on') def turn_mellotron_on(): print("YES!!! :)") return("OK") # ======================================================= # main loop if __name__ == "__main__": # initialize config/logs tools.load_config(optional_service_name="alexa_interface") # .ini startup_wait = tools.config.getint('startup', 'wait') # also: getfloat, getint, getboolean tools.init_logs() influx_json_body = db.init() # startup sync & notification tools.log.info("--= Restarting =--") tools.log.info("sleeping {} seconds for startup sync between services...".format(startup_wait)) time.sleep(startup_wait) tools.notify("WARNING", tools.service_version+" - (re)started!") # alive_check() run(host=tools.hostname, port=tools.port, server='gevent')