Files
web2.0-backend/coffee_scale/views.py
T
2017-09-19 21:04:48 +03:00

47 lines
1.0 KiB
Python

from django.shortcuts import render
from django.http import JsonResponse
import datetime
from .mqtt import get_latest
import coffee_scale.mqtt # somehow this is needed
import logging
from django.conf import settings
def get_cups_from_weight(weight):
if not weight:
return 0
EMPTY = 820 # grams
FULL = 2000 # grams
cups = 10 * (weight - EMPTY) / (FULL - EMPTY)
cups = round(cups, 1)
if cups < 0.5:
cups = 0
if cups > 10:
cups = 10
# logging.debug("Coffee cups: {}, weight: {}".format(cups, weight))
return cups
def coffee_view(request):
logging.info('User navigated to coffee page!')
return render(request, 'coffee.html')
def cups_view(request):
now = datetime.datetime.now()
latest = get_latest()
cups = get_cups_from_weight(latest.get('weight'))
data = {
'date': now,
'cups': cups,
'temp': latest.get('temp'),
'last_brew': latest.get('last_brew'),
'brewing': latest.get('brewing')
}
return JsonResponse(data)