def connector(): try: connection = RDB.connect(host=RDB_HOST, port=RDB_PORT) except RqlDriverError: logger.error( "No database connection could be established by address: %s:%s", RDB_HOST, RDB_PORT) exit() return connection
def get_lifecycle(item): lifecycle = [] connection = rdb_connector() logger.info("Loading lifecycle for `%s` from storage", item) cursor = RDB.db(DB_NAME).table(item).order_by('order').run(connection) for phase in cursor: lifecycle.append(phase) logger.debug("Loaded lifecycle: %s", lifecycle) return lifecycle
def collector(): while True: logger.info("Calculate current phase and time of the day...") config.CURRENT_PHASE = current_phase(LIFECYCLE, startup_date_time) logger.info("Collecting current metrics...") all_metrics() logger.info("Metrics Collected. Sleep for %s sec...", config.READ_EVERY_SEC) time.sleep(config.READ_EVERY_SEC)
'Growbox Current Phase duration') PHASE_FROM_START = Gauge('growbox_phase_from_start_seconds', 'Growbox Current Phase seconds from start') PHASE_BEFORE_FINISH = Gauge('growbox_phase_before_finish_seconds', 'Growbox Current Phase seconds before finish') LIGHT_DARK_MODE = Enum('growbox_light_dark_mode', 'Current Growbox Light/Dark mode', states=LIGHT_DARK_MODES) LIGHT_DARK_MODE_DURATION = Gauge( 'growbox_light_dark_mode_duration_seconds', 'Growbox Current Light/Dark mode duration') LIGHT_DARK_MODE_FROM_START = Gauge( 'growbox_light_dark_mode_from_start_seconds', 'Growbox Current Light/Dark mode seconds from start') LIGHT_DARK_MODE_BEFORE_FINISH = Gauge( 'growbox_light_dark_mode_before_finish_seconds', 'Growbox Current Light/Dark mode seconds before finish') FAN = Gauge('growbox_fan_enabled', 'Growbox FANNING enabled') LIGHT = Gauge('growbox_light_enabled', 'Growbox LIGHTENING enabled') WATER = Gauge('growbox_water_enabled', 'Growbox WATERING enabled') TEMPERATURE = Gauge('growbox_temperature_celsius', 'Growbox Temperature') HUMIDITY = Gauge('growbox_humidity_percents', 'Growbox Humidity') MOISTURE = Gauge('growbox_moisture_percents', 'Growbox Moisture') except ValueError as e: logger.error("Prometheus Metrics are not properly initialized") logger.debug(e) exit()
def controller(): logger.info("Starting Flask HTTP server...") app.run(host=config.FLASK_HOST, port=config.FLASK_PORT, debug=True, use_reloader=False)
def collector(): while True: logger.info("Calculate current phase and time of the day...") config.CURRENT_PHASE = current_phase(LIFECYCLE, startup_date_time) logger.info("Collecting current metrics...") all_metrics() logger.info("Metrics Collected. Sleep for %s sec...", config.READ_EVERY_SEC) time.sleep(config.READ_EVERY_SEC) if __name__ == "__main__": # Moment when the growcycle was started startup_date_time = datetime.datetime.now() logger.info("====================================") logger.info("Startup Date/Time: %s", startup_date_time.strftime("%y-%m-%d %H:%M:%S")) logger.info("====================================") logger.info("Controller starting in separate thread...") logger.info("====================================") threading.Thread(target=controller).start() logger.info("====================================") logger.info("Collector starting in separate thread...") logger.info("====================================") threading.Thread(target=collector).start()
def read_yaml(file): with open(file, "r") as f: logger.debug("Loading file: %s", file) yaml_file = yaml.full_load(f) logger.debug("Loaded file: %s", yaml_file) return yaml_file
from common import config from common.logs import MAIN as logger from pigpio_dht import DHT11 from gpiozero import MCP3008, DigitalOutputDevice try: TEMP_HUMD_SENSOR = DHT11(config.DHT11_PIN) MOISTURE_SENSOR = MCP3008(channel=config.MCP3008_CHANNEL) MOISTURE_SENSOR_POWER = DigitalOutputDevice(config.MCP3008_POWER_PIN) FAN_RELAY = DigitalOutputDevice(config.FAN_RELAY_PIN, active_high=False, initial_value=False) LIGHT_RELAY = DigitalOutputDevice(config.LIGHT_RELAY_PIN, active_high=False, initial_value=False) WATER_RELAY = DigitalOutputDevice(config.WATER_RELAY_PIN, active_high=False, initial_value=False) except Exception as e: logger.error("Hardware doesn't start properly: %s", e)
def import_db_from_yaml(): data_tree = [] for index, tree in enumerate(os.walk(DATA_PATH)): if index > 0: data_tree.append(tree) for data in data_tree: database = data[0].split('/')[-1] connection = rdb_connector() try: RDB.db_create(database).run(connection) logger.info("Database `%s` setup completed", database) except RqlRuntimeError: logger.error("Database `%s` already exists", database) except RqlDriverError as e: logger.error(e) exit() finally: connection.close() tables = data[2] for table in tables: table_name = os.path.splitext(table)[0] table_data = read_yaml(DATA_PATH + '/' + database + '/' + table) connection = rdb_connector() try: RDB.db(database).table_create(table_name).run(connection) RDB.db(database).table(table_name).insert(table_data).run( connection) logger.info("Table `%s` setup in database `%s` completed", table_name, database) except RqlRuntimeError: logger.error("Table `%s` in database `%s` already exists", table_name, database) except RqlDriverError as e: logger.error(e) exit() finally: connection.close()