Ejemplo n.º 1
0
def device(request, device_id):
    '''
    Display a device
    '''
    dev = get_object_or_404(Device, pk=device_id)
    last = History.objects.filter(device=device_id).latest()
    his = History.objects.filter(device=device_id).exclude(user='******')[0:10]
    power = Device.objects.get(pk=device_id).watt
    today = datetime.today()
    kwh_today = History.objects.filter(device=device_id, action=0, user="******", result=0, date=today).aggregate(Count('result'))["result__count"] * power / 1000.0
    yesterday = today - timedelta(days=1)
    kwh_day = History.objects.filter(device=device_id, action=0, user="******", result=0, date=yesterday).aggregate(Count('result'))["result__count"] * power / 1000.0
    week = datetime.today() - timedelta(days=7)
    kwh_week = History.objects.filter(device=device_id, action=0, user="******", result=0, date__gt=week).aggregate(Count('result'))["result__count"] * power / 1000.0
    img = history_img(device_id, 10)
    return render_to_response('bodhi/device.html',
                                {'device': dev,
                                'kwh_today' : kwh_today,
                                'kwh_day' : kwh_day,
                                'kwh_week': kwh_week,
                                'history' : his,
                                'img' : img,
                                'latest' : last})
Ejemplo n.º 2
0
def history_image(request, device_id, days):

  return HttpResponse(history_img(device_id, days), mimetype='image/svg+xml')