def dashboard(request): '''Creates dashboard (gauges and table) page for the active beer''' active_config = do.getActiveConfig() active_beer = do.getActiveBeer() cur_reading = do.getLastReading(active_beer) cur_temp_amb = cur_reading.get_temp_amb() cur_temp_beer = cur_reading.get_temp_beer() cur_light_amb = cur_reading.get_light_amb() cur_pres_beer = cur_reading.get_pres_beer() if active_config.temp_amb_base != None and active_config.temp_amb_dev != None: temp_amb_rng = [active_config.temp_amb_base - active_config.temp_amb_dev, active_config.temp_amb_base + active_config.temp_amb_dev] else: temp_amb_rng = (0,0) if active_config.temp_beer_base != None and active_config.temp_beer_dev != None: temp_beer_rng = [active_config.temp_beer_base - active_config.temp_beer_dev, active_config.temp_beer_base + active_config.temp_beer_dev] else: temp_beer_rng = (0,0) s, alert_res = sendCommand("?code=a&var=get") alert_var = None alert_rng = None if s == "Success" and "off" not in alert_res: re_alert = re.search("(.*)\[(\d+), (\d+)\]", alert_res.split(":")[1], re.IGNORECASE) alert_var = re_alert.group(1) alert_rng = [int(re_alert.group(2)), int(re_alert.group(3))] val = locals()["cur_"+alert_var] if not(val < alert_rng[0] or val > alert_rng[1]): alert_var = None data = { "vals": { "temp_amb": cur_temp_amb, "temp_beer": cur_temp_beer, "light_amb": cur_light_amb, "pres_beer": cur_pres_beer }, "alert_var": alert_var, "alert_rng": alert_rng, "bgcols" : { "temp_amb": do.get_paint_cols(cur_temp_amb, temp_amb_rng)[0], "temp_beer": do.get_paint_cols(cur_temp_beer, temp_beer_rng)[0], "light_amb": do.get_paint_cols(cur_light_amb)[0], "pres_beer": do.get_paint_cols(cur_pres_beer)[0] }, "greenrng": { "temp_amb": temp_amb_rng, "temp_beer": temp_beer_rng }, "last_log_date": cur_reading.instant_actual.strftime("%Y-%m-%d"), "last_log_time": cur_reading.instant_actual.strftime("%H:%M:%S"), "last_log_ago": do.get_date_diff(cur_reading.instant_actual, do.nowInUtc()), "next_log": do.next_log_estimate(), "all_beers": Beer.objects.all(), "active_beer": active_beer, "beer_date": active_beer.brew_date, } #Alert flash color based on BG, unless white, make it orange. col = data["bgcols"][alert_var] if alert_var != None else "#FFFFFF" data["alert_col"] = col if col != "#FFFFFF" else "#FFA500" return render_to_response('dashboard.html',data)
def getStatus(command, request = None, key = None): out = None if key != None: out = chkCookie(request, key) if out == None: s, status = sendCommand(command) if s != "Success": out = "?" else: if "on." in status: out = "on" else: out = "off" return(out)
def dashboard_update(request): '''Forces a log then returns to dashboard page''' active_beer = do.getActiveBeer() old_reading = do.getLastReading(active_beer) command_status = sendCommand("?code=F") if command_status[0] == "Success": '''Wait until the forced log actually shows up in django''' for i in range(49): if do.getLastReading(active_beer) != old_reading: break sleep(.1) return HttpResponseRedirect(reverse('dashboard'))
def next_log_estimate(): '''Estimates the next reading time based on log freq and last logged time''' last_reading = getLastReading(getActiveBeer()).instant_actual log_freq = None r, msg = sendCommand("?code=M") if r == "Success": log_freq = int(msg.split("=")[1]) out = "unknown amount of time" if log_freq != None: next = last_reading + timedelta(minutes = log_freq) now = nowInUtc() if next >= now: out = get_date_diff(now, next, append = None) elif next >= now - timedelta(minutes = 5): out = "less than a minute" return(out)
def handle(self, *args, **options): time = datetime.utcnow().timestamp() command = "?code=s&time=" + str(time) s, msg = sendCommand(command) return (s)
def commands(request): blank = str('') command_status = blank error = blank details = blank command = request.GET.copy() #List vars: [Current Value, Alert, Cell Color (for future use)] varlist = { "temp_amb":["?", "?", "#FFFFFF"], "temp_beer":["?", "?", "#FFFFFF"], "light_amb":["?", "?", "#FFFFFF"], "pres_beer":["?", "?", "#FFFFFF"] } cookielist = [var for var in varlist] + ["collection_status", "logging_status", "log_freq", "alert_res"] if command: if "cookie" in command: cookie = command["cookie"] if cookie: if cookie.lower() == "all": for c in cookielist: if c in request.COOKIES: request.COOKIES[c] = None elif cookie.lower() == "sensors": for c in varlist: if c in request.COOKIES: request.COOKIES[c] = None elif cookie.lower() == "alertvars": for c in [v for v in varlist] + ["alert_res"]: if c in request.COOKIES: request.COOKIES[c] = None elif cookie in request.COOKIES: request.COOKIES[cookie] = None code = command["code"].lower() if code == "s": command["time"] = datetime.utcnow().timestamp() if code != "": command_status = sendCommand(command.urlencode()) error = command_status[0] details = command_status[1] active_beer = do.getActiveBeer() all_beers = do.getAllBeer() beer_name = active_beer beer_date = active_beer.brew_date log_freq = do.chkCookie(request, "log_freq") if log_freq == None: s, log_freq = sendCommand("?code=m") if s != "Success": log_freq = "?" else: log_freq = log_freq.split("=")[1] collection_status = do.getStatus("?code=c&dir=get", request, "collection_status") logging_status = do.getStatus("?code=L&dir=get", request, "logging_status") alert_res = do.chkCookie(request, "alert_res") if alert_res == None: s, alert_res = sendCommand("?code=A&var=get") if s == "Success": if "off" not in alert_res: re_alert = re.search("(.*)(\[\d+, \d+\])", alert_res.split(":")[1], re.IGNORECASE) alert_var = re_alert.group(1) alert_rng = re_alert.group(2) else: alert_var = None else: alert_var = "?" else: if "off" not in alert_res: re_alert = re.search("(.*)(\[\d+, \d+\])", alert_res.split(":")[1], re.IGNORECASE) alert_var = re_alert.group(1) alert_rng = re_alert.group(2) else: alert_var = None for var in varlist: temp = do.chkCookie(request, var) if temp == None: s, val = sendCommand("?code=r&var=" + var) if s == "Success": varlist[var][0] = val.split(":")[1] if alert_var != "?": if alert_var == var: varlist[var][1] = str(alert_rng) else: varlist[var][1] = "Set alert" else: varlist[var] = temp.split("|") data = { 'all_beers': all_beers, 'beer_name': beer_name, 'beer_date': beer_date, 'active_beer': do.getActiveBeer(), 'command_status': command_status, 'varlist': varlist, 'error': error, 'details': details, 'log_freq': log_freq, 'collection_status': collection_status, 'logging_status': logging_status, } max_age = 300 expires = datetime.utcnow() + timedelta(seconds=max_age) response = render_to_response('commands.html', data, context_instance=RequestContext(request)) for var in cookielist: if var in varlist: val = "|".join(varlist[var]) else: val = locals()[var] if val != "?": response.set_cookie(var, val, max_age=max_age, expires=expires) return(response)