44 lines
929 B
Python
44 lines
929 B
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
|
|
|
|
|
|
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:
|
|
cups = 0
|
|
if cups > 10:
|
|
cups = 10
|
|
|
|
print("cups: ", cups, "weight: ", weight)
|
|
return cups
|
|
|
|
|
|
def coffee_view(request):
|
|
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)
|