def print_taps(): """ :return: """ db = beer_db.BeerDB(db_filepath=DB_FILEPATH) for i in range(1,5): print "\tTap %s | %s remaining" % (i, db.get_percentage100(i))
def update_mqtt(): NUM_TAPS = 4 for tap_id in range(1, (NUM_TAPS + 1)): db = beer_db.BeerDB(db_filepath=DB_FILEPATH) mqtt_client = bar_mqtt.BoozerMqtt(config.get("Mqtt", "broker")) percent = db.get_percentage100(tap_id) topic = "bar/tap%s" % str(tap_id) mqtt_client.pub_mqtt(topic, str(percent)) print "[MQTT] updated tap %i" % tap_id
def reset_tap(tap_id): """ :param tap_id: :return: """ db = beer_db.BeerDB(db_filepath="../db/db.sqlite") print "current [Tap %s ] %s remaining" % (str(tap_id), str(db.get_percentage(tap_id))) msg = "Are you sure that you reset tapid: " + str(tap_id) if yes_or_no(msg): db.reset_tap_val(tap_id) print "updated! [Tap %s ] %s remaining" % ( str(results.reset_tap_id), str(db.get_percentage(tap_id))) else: print "bailing"
class Boozer: db = None config = None CONFIG_FILEPATH = os.getenv("CONFIG_FILEPATH", "./config.ini") DB_FILEPATH = os.getenv("DB_FILEPATTH", "./db.sqlite") MQTT_ENABLED = False INFLUXDB_ENABLED = False TWITTER_ENABLED = False SCROLLPHAT_ENABLED = False SLACK_ENABLED = False TEMPERATURE_ENABLED = False scrollphat_cleared = True ## TODO: decouple this taps = [] mqtt_client = None twitter_client = None boozer_display = None slack_client = None temperature_client = None INFLUXDB_LBL = "Influxdb" def __init__(self): # Setup the configuration self.config = ConfigParser.ConfigParser() self.config.read(self.CONFIG_FILEPATH) # Set the logger logger = logging.getLogger() handler = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) logger.setLevel(logging.INFO) # Check to see if we need to override the logging level try: level = self.config.get("Boozer", "logging_level") if level == "INFO" or level == "info": logger.setLevel(logging.INFO) if level == "WARN" or level == "warn": logger.setLevel(logging.WARN) if level == "DEBUG" or level == "debug": logger.setLevel(logging.DEBUG) if level == "ERROR" or level == "error": logger.setLevel(logging.ERROR) except Exception, e: logger.debug("not overrding the logging level. error: " + str(e)) self.db = beer_db.BeerDB( self.DB_FILEPATH) # TODO: replace this with configuration value current_path = os.path.dirname(os.path.realpath(__file__)) if not os.path.isfile(self.DB_FILEPATH): logger.fatal("[fatal] cannot load db from " % self.DB_FILEPATH) sys.exit(1) if not os.path.isfile(self.CONFIG_FILEPATH): logger.fatal("[fatal] cannot load config from " % self.CONFIG_FILEPATH) sys.exit(1) # setup twitter client if self.config.getboolean("Twitter", "enabled"): self.TWITTER_ENABLED = True try: consumer_key = self.config.get("Twitter", "consumer_key").strip('"') consumer_secret = self.config.get("Twitter", "consumer_secret").strip('"') access_token = self.config.get("Twitter", "access_token").strip('"') access_token_secret = self.config.get( "Twitter", "access_token_secret").strip('"') self.twitter_client = twitter_notify.TwitterNotify( consumer_key, consumer_secret, access_token, access_token_secret) logger.debug("TWITTER_ENABLED=True") except Exception, e: logger.error( "Unable to pull twitter credentials from configuration file. setting TWITTER_ENABLED=False; error: " + str(e)) self.TWITTER_ENABLED = False logger.debug("TWITTER_ENABLED=False")
import beer_db import twitter_notify import slack_notify import requests import ConfigParser import logging import bar_mqtt scrollphat_cleared = True # Setup the configuration CONFIG_FILE = "./config.ini" config = ConfigParser.ConfigParser() config.read(CONFIG_FILE) db = beer_db.BeerDB("./db.sqlite") # TODO: replace this with configuration value MQTT_ENABLED = False TWITTER_ENABLED = False SCROLLPHAT_ENABLED = False SLACK_ENABLED = False if config.get("Scrollphat", 'enabled') == "True": scrollphat.set_brightness(7) # Set the logger logger = logging.getLogger() handler = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter)
def __init__(self): # Setup the configuration self.config = ConfigParser.ConfigParser() self.config.read(self.CONFIG_FILEPATH) self.db = beer_db.BeerDB(self.DB_FILEPATH) # TODO: replace this with configuration value #if config.get("Scrollphat", 'enabled') == "True": # scrollphat.set_brightness(7) # Set the logger logger = logging.getLogger() handler = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) logger.setLevel(logging.INFO) current_path = os.path.dirname(os.path.realpath(__file__)) if not os.path.isfile(self.DB_FILEPATH): logger.fatal("[fatal] cannot load db from " % self.DB_FILEPATH) sys.exit(1) if not os.path.isfile(self.CONFIG_FILEPATH): logger.fatal("[fatal] cannot load config from " % self.CONFIG_FILEPATH) sys.exit(1) # setup twitter client try: if self.config.getboolean("Twitter", "enabled"): self.TWITTER_ENABLED = True try: consumer_key = self.config.get("Twitter", "consumer_key").strip('"') consumer_secret = self.config.get("Twitter", "consumer_secret").strip('"') access_token = self.config.get("Twitter", "access_token").strip('"') access_token_secret = self.config.get("Twitter", "access_token_secret").strip('"') except: logger.error("Unable to pull twitter credentials from configuration file.") self.twitter_client = twitter_notify.TwitterNotify(self.config) except: logger.info("Twitter Entry not found in %s, setting TWITTER_ENABLED to False", sys.exc_info()[0] ) self.TWITTER_ENABLED = False # Check to see if we need to override the logging level try: level = self.config.get("Boozer", "logging_level") if level == "INFO" or level == "info": logger.setLevel(logging.INFO) if level == "WARN" or level == "warn": logger.setLevel(logging.WARN) if level == "DEBUG" or level == "debug": logger.setLevel(logging.DEBUG) if level == "ERROR" or level == "error": logger.setLevel(logging.ERROR) except: logger.debug("not overrding the logging level") # setup mqtt client try: if self.config.getboolean("Mqtt", "enabled"): logger.info("config: MQTT enabled") self.MQTT_ENABLED = True # Read in username and password username = None password = None try: username = self.config.get("Mqtt", "username") password = self.config.get("Mqtt", "password") except: logger.debug("skipping mqtt username/password because at least one was missing.") self.mqtt_client = bar_mqtt.BoozerMqtt(self.config.get("Mqtt", "broker"), port=self.config.get("Mqtt", "port"), username=username, password=password) except: logger.info("MQTT Entry not found in %s, setting MQTT_ENABLED to False" % self.CONFIG_FILEPATH) self.MQTT_ENABLED = False # INFLUXDB plugin try: if self.config.getboolean("Influxdb", "enabled"): logger.info("config: INFLUXDB enabled") self.INFLUXDB_ENABLED = True # Read in username and password username = None host = None password = None database = None port = 8086 database = "boozer" logger.info("about to configure influx") try: # read in the mandatory values host = self.config.get(self.INFLUXDB_LBL, "host") except: logger.error("config error. unable to get host from %s." % self.INFLUXDB_LBL) self.INFLUXDB_ENABLED = False return try: # read in optional values, if they don't exist, that's fine. continue. port = self.config.getint(self.INFLUXDB_LBL, "port") username = self.config.get(self.INFLUXDB_LBL, "username") password = self.config.get(self.INFLUXDB_LBL, "password") database = self.config.get(self.INFLUXDB_LBL, "database") except: logger.debug("missing an optional key in influx config. moving on.") self.influxdb_client = influxdb_client.InfluxdbBoozerClient(host=host, port=port, username=username, password=password, database=database) logger.info("influxdb component initialized") else: logger.info("INFLUXDB_ENABLED=False") except: logger.info("%s Entry not found in %s, setting INFLUXBD_ENABLED to False" % (self.INFLUXDB_LBL, self.CONFIG_FILEPATH)) self.INFLUXDB_ENABLED = False # setup temperature client try: try: if self.config.getboolean("Temperature", "enabled"): self.TEMPERATURE_ENABLED = True logger.info("TEMPERATURE_ENABLED = True") try: sensor_url = self.config.get("Temperature", "endpoint") logger.info("Temperature. setting up endpoint based temp client") self.temperature_client = beer_temps.BeerTemps(sensor_protocol=beer_temps.SENSOR_HTTP, sensor_url=sensor_url) logger.warn("Temperature endpoint is deprecated. use url instead.") except: temperature_url = None sensor_protocol = self.config.get("Temperature", "sensor_protocol") if sensor_protocol == beer_temps.SENSOR_DS18B20: logger.info("setting up ds18b20 sensor ") self.temperature_client = beer_temps.BeerTemps(sensor_protocol="ds18b20") elif sensor_protocol == beer_temps.SENSOR_HTTP: sensor_url = self.config.get("Temperature", "sensor_url") self.temperature_client = beer_temps.BeerTemps(sensor_protocol=beer_temps.SENSOR_HTTP, sensor_url=sensor_url) except: logger.error("Configuration error setting up temperature sensor.") logger.error(sys.exc_info()[0]) logger.info("TEMPERATURE_ENABLED = True") except: logger.info("Temperature Entry not found in %s, setting TEMPERATURE_ENABLED to False") self.TEMPERATURE_ENABLED = False # setup slack client try: if self.config.getboolean("Slack", "enabled"): self.SLACK_ENABLED = True self.slack_client = slack_notify.SlackNotify(self.config.get("Slack", "webhookurl")) except: logger.info("Slack Entry not found in %s, setting SLACK_ENABLED to False") self.SLACK_ENABLED = False # setup scrollphat client try: if self.config.getboolean("Scrollphat", "enabled"): self.SCROLLPHAT_ENABLED = True self.boozer_display = boozer_display.BoozerDisplay() except: logger.info("Scrollphat Entry not found in %s, setting SCROLLPHAT_ENABLED to False") self.SCROLLPHAT_ENABLED = False try: if self.config.getboolean("Twitter", "enabled"): self.TWITTER_ENABLED = True except: logger.info("Twitter Entry not found in %s, setting TWITTER_ENABLED to False") self.TWITTER_ENABLED = False # Boozer overrides minimum_pour_vol = None try: self.config.get("Boozer", FlowMeter.MINIMUM_POUR_VOL_LBL) except: logger.debug("%s not set" % FlowMeter.MINIMUM_POUR_VOL_LBL) # set up the flow meters # _ #| |_ __ _ _ __ ___ #| __/ _` | '_ \/ __| #| || (_| | |_) \__ \ # \__\__,_| .__/|___/ # |_| for tap in range(1,10): # limit of 10 taps str_tap = "tap%i" % tap str_tapN_gpio_pin = "%s_gpio_pin" % str_tap str_tapN_beer_name = "%s_beer_name" % str_tap str_tapN_reset_database = "%s_reset_database" % str_tap capacity_gallons = 5 # default is 5 gallons # see if the user set the capacity try: capacity_gallons = self.config.getint("Taps", str_tapN_gallon_capacity) logger.info("Tap %i Config Override: Setting capacity to %i" % (tap, capacity_gallons)) except: logger.info("Tap %i: Setting capacity to %i" % (tap, capacity_gallons)) try: this_tap_gpio_pin = self.config.getint("Taps", str_tapN_gpio_pin) # this looks for the tap gpio pin such as "tap1_gpio_pin" this_tap_beer_name = [self.config.get("Taps", str_tapN_beer_name)] new_tap = FlowMeter("not metric", this_tap_beer_name, tap_id=tap, pin=this_tap_gpio_pin, config=self.config, capacity=capacity_gallons, minimum_pour_vol=minimum_pour_vol) # Create the tap object self.taps.append(new_tap) # Add the new tap object to the array except: break # If mqtt is enabled, we need to push the new value. This is because mqtt does not always persist and that's a good thing to do. if self.MQTT_ENABLED: self.update_mqtt(tap) # Check to see if we need to reset the database value try: if self.config.getboolean('Taps', str_tapN_reset_database): self.db.reset_tap_val(tap) logger.info("Detected %s. Successfully reset the database entry to 100 percent volume remaining." % str_tapN_reset_database) except: continue if len(self.taps) < 1: # if there were no taps read in, there's no point living anymore. go fatal logger.fatal("FATAL - No taps were read in from the config file. Are they formatted correctly?") sys.exit(1) ## TODO This needs to be pulled into the init script for tap in self.taps: # setup all the taps. add event triggers to the opening of the taps. GPIO.add_event_detect(tap.get_pin(), GPIO.RISING, callback=lambda *a: self.register_tap(tap), bouncetime=20) #if MQTT_ENABLED: update_mqtt(tap.get_tap_id()) # do a prelim mqtt update in case it's been awhile zope.event.subscribers.append(self.register_pour_event) # Attach the event