def listen(self): self.app = tornado.web.Application( [ (r"/", IndexHandler), (r"/(favicon.ico)", tornado.web.StaticFileHandler, {"path": static_dir}), (r"/json", JsonHandler), (r"/stats", StatsHandler), (r"/api/beer/(.*)", APIBeerDetails), (r"/api/search", APISearch), (r"/update", UpdateHandler), (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": static_dir}), (r"/auth", AuthHandler), (r"/admin/(.*)", AdminHandler), (r"/admin", AdminIndexHandler), ], cookie_secret=Config.get("cookie_secret"), google_oauth={ "url": Config.get("google_oauth_url"), "key": Config.get("google_oauth_key"), "secret": Config.get("google_oauth_secret"), }, ) self.app.listen(Config.get("web_port")) self.ioloop = tornado.ioloop.IOLoop.instance() self.ioloop.start()
def get_taps(cls): taps = [] logging.debug("Getting taps from database") cursor = cls.connect().cursor() cursor.execute( "select tap_id, coalesce(beer_id, ''), last_updated, last_updated_by, amount_poured from taps order by tap_id" ) for row in cursor: taps.append({ "tap_id": row[0], "beer_id": row[1], "last_updated": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(row[2])), "last_updated_by": row[3], "amount_poured": row[4] * Config.get("units_per_pulse")[str(row[0])], "pct_full": 1 - (row[4] * Config.get("units_per_pulse")[str(row[0])] / Config.get("total_keg_units")), }) cursor.close() return taps
def initialize_window(self): self.window = Tkinter.Tk() self.window.attributes("-fullscreen", True) self.window.tk_setPalette(background="White") self.window.rowconfigure(1, weight=1) self.title = Tkinter.Label(text="On Tap", font=("PT Sans", 32, "bold"), background="#cfcfcf", borderwidth=1, relief=Tkinter.GROOVE) self.title.pack(fill=Tkinter.X) # Taps self.tap_container = Tkinter.Frame(background="#bfbfc7", padx=10) self.tap_container.pack(expand=True, fill=Tkinter.BOTH) self.taps = dict() for i, tap in enumerate(DBClient.get_taps()): self.taps[tap["tap_id"]] = TapDisplay(tap["tap_id"], self.tap_container) # Checkins self.checkin_container = Tkinter.Frame(background="#dfe7ef", borderwidth=1, relief="sunken") self.checkin_container.pack(fill=Tkinter.X) self.checkin_displays = [] for i in range(Config.get("num_checkins")): self.checkin_displays.append(CheckinDisplay(self.checkin_container)) self.powered_image_pil = Image.open(pbu_file) self.powered_image = ImageTk.PhotoImage(self.powered_image_pil) self.powered_image_container = Tkinter.Label(self.checkin_container, height=40, width=166, image=self.powered_image, background="#dfe7ef") self.powered_image_container.pack(side=Tkinter.RIGHT, padx=10)
def post(self): if self.get_argument("update_secret") != Config.get("update_secret"): raise tornado.web.HTTPError(httplib.UNAUTHORIZED) if self.get_argument("tap_id", False) and self.get_argument("pulses", False): DB.update_amount_poured(self.get_argument("tap_id"), self.get_argument("pulses")) if self.get_argument("sensor_id", False) and self.get_argument("deg_c", False): DB.update_temperature(self.get_argument("sensor_id"), self.get_argument("deg_c"))
def get_taps(cls): taps = [] logging.debug("Getting taps from database") cursor = cls.connect().cursor() cursor.execute("select tap_id, coalesce(beer_id, ''), last_updated, last_updated_by, amount_poured from taps order by tap_id") for row in cursor: taps.append({ "tap_id": row[0], "beer_id": row[1], "last_updated": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(row[2])), "last_updated_by": row[3], "amount_poured": row[4] * Config.get("units_per_pulse")[str(row[0])], "pct_full": 1 - (row[4] * Config.get("units_per_pulse")[str(row[0])] / Config.get("total_keg_units")), }) cursor.close() return taps
def update_amount_poured(cls, tap_id, pulses): logging.debug("Sending update for tap {}, {} pulses".format(tap_id, pulses)) url = urlparse.urljoin(cls.web_host(), "update") try: requests.post(url, params={ "update_secret": Config.get("update_secret"), "tap_id": tap_id, "pulses": pulses, }) except requests.exceptions.ConnectionError as e: logging.error("Couldn't update amount poured ({} on tap {}): {}".format(pulses, tap_id, e))
def update_temperature(cls, sensor_id, deg_c): logging.debug("Sending update for temperature sensor {}, {} degrees".format(sensor_id, deg_c)) url = urlparse.urljoin(cls.web_host(), "update") try: requests.post(url, params={ "update_secret": Config.get("update_secret"), "sensor_id": sensor_id, "deg_c": deg_c, }) except requests.exceptions.ConnectionError as e: logging.error("Couldn't update temperature: {}".format(e))
def update_active_tap(self, tap): self.amount_poured = tap.pulses * Config.get("units_per_pulse")[str(tap.tap_id)] self.amount_poured_number.config(text="{:.2f}".format(self.amount_poured)) if self.active: return logging.debug("making tap {} active".format(self.tap_id)) self.active = True self.beer_description.pack_forget() self.pack("amount_poured_frame") self.set_background(highlight_color)
def get(self): if self.get_argument("code", False): user = yield self.get_authenticated_user( redirect_uri=Config.get("google_oauth_url"), code=self.get_argument("code"), ) jwt = oauth2client.client.verify_id_token(user["id_token"], Config.get("google_oauth_key")) if jwt and jwt["email"] and jwt["email"].endswith("@" + Config.get("admin_email_domain")): self.set_secure_cookie("email", jwt["email"]) self.redirect("/admin") else: self.set_status(403) self.finish() else: yield self.authorize_redirect( redirect_uri=Config.get("google_oauth_url"), client_id=Config.get("google_oauth_key"), scope=["email", "profile"], response_type="code", extra_params={"approval_prompt": "auto"}, )
def update_active_tap(self, tap): self.amount_poured = tap.pulses * Config.get("units_per_pulse")[str( tap.tap_id)] self.amount_poured_number.config( text="{:.2f}".format(self.amount_poured)) if self.active: return logging.debug("making tap {} active".format(self.tap_id)) self.active = True self.beer_description.pack_forget() self.pack("amount_poured_frame") self.set_background(highlight_color)
def update_amount_poured(cls, tap_id, pulses): logging.debug("Sending update for tap {}, {} pulses".format( tap_id, pulses)) url = urlparse.urljoin(cls.web_host(), "update") try: requests.post(url, params={ "update_secret": Config.get("update_secret"), "tap_id": tap_id, "pulses": pulses, }) except requests.exceptions.ConnectionError as e: logging.error( "Couldn't update amount poured ({} on tap {}): {}".format( pulses, tap_id, e))
def update_temperature(cls, sensor_id, deg_c): logging.debug( "Sending update for temperature sensor {}, {} degrees".format( sensor_id, deg_c)) url = urlparse.urljoin(cls.web_host(), "update") try: requests.post(url, params={ "update_secret": Config.get("update_secret"), "sensor_id": sensor_id, "deg_c": deg_c, }) except requests.exceptions.ConnectionError as e: logging.error("Couldn't update temperature: {}".format(e))
def post(self, action): user = self.get_secure_cookie("email") if not user and not Config.get("debug_admin"): self.redirect("/auth") return if action == "update": db = DB.connect() tap_id = self.get_argument("tap_id"); beer_id = self.get_argument("beer_id"); cursor = db.cursor() cursor.execute("update taps set beer_id = ?, last_updated = strftime('%s', 'now'), last_updated_by = ?, amount_poured = 0 where tap_id = ?", [beer_id, user, tap_id]) cursor.close() db.commit() self.write(simplejson.dumps({"tap_id": tap_id, "beer_id": beer_id}))
def initialize_window(self): self.window = Tkinter.Tk() self.window.attributes("-fullscreen", True) self.window.tk_setPalette(background="White") self.window.rowconfigure(1, weight=1) self.title = Tkinter.Label(text="On Tap", font=("PT Sans", 32, "bold"), background="#cfcfcf", borderwidth=1, relief=Tkinter.GROOVE) self.title.pack(fill=Tkinter.X) # Taps self.tap_container = Tkinter.Frame(background="#bfbfc7", padx=10) self.tap_container.pack(expand=True, fill=Tkinter.BOTH) self.taps = dict() for i, tap in enumerate(DBClient.get_taps()): self.taps[tap["tap_id"]] = TapDisplay(tap["tap_id"], self.tap_container) # Checkins self.checkin_container = Tkinter.Frame(background="#dfe7ef", borderwidth=1, relief="sunken") self.checkin_container.pack(fill=Tkinter.X) self.checkin_displays = [] for i in range(Config.get("num_checkins")): self.checkin_displays.append(CheckinDisplay( self.checkin_container)) self.powered_image_pil = Image.open(pbu_file) self.powered_image = ImageTk.PhotoImage(self.powered_image_pil) self.powered_image_container = Tkinter.Label(self.checkin_container, height=40, width=166, image=self.powered_image, background="#dfe7ef") self.powered_image_container.pack(side=Tkinter.RIGHT, padx=10)
def __init__(self, kegmeter_status): self.kegmeter_status = kegmeter_status self.hardware_id = Config.get("hardware_id") self.statuses = dict() self.errors = 0
def web_host(cls): return "{}:{}".format(Config.get("web_remote_host"), Config.get("web_remote_port"))
def db_file(cls): return Config.get("db_file")
def get(self): if not self.get_secure_cookie("email") and not Config.get("debug_admin"): self.redirect("/auth") return self.write(self.loader.load("admin.html").generate(taps=DB.get_taps()))