from asyncio.windows_events import NULL from inspect import _void import json import os import requests from django.conf import settings from django.template import Context from django.template.loader import get_template from django.contrib import messages from django.shortcuts import render, redirect, get_object_or_404 from django.http import HttpResponse from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from datetime import datetime, timedelta import time from urllib.parse import urlencode from urllib.request import Request, urlopen from django.core.mail import EmailMultiAlternatives from .models import sensoresfijos, AltaSensoresFijos, telegram from .forms import Formsensoresf, FormAltasensoresf, Formtelegram from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt from django.http.response import JsonResponse from django.views import View from django.views.generic import TemplateView from random import randint # Create your views here. def ConsultaSensoresf(request): if request.method == "POST": sensor = request.POST.get("sensor") fecha = request.POST.get("fecha") datos = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__icontains=fecha) sensores = AltaSensoresFijos.objects.all().order_by('nombre') return render(request, "sensoresf.html", {"datos": datos, "sensores": sensores,"menus": sensores}) sensor = request.POST.get("sensor") fecha = request.POST.get("fecha") datos = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__icontains=fecha) sensores = AltaSensoresFijos.objects.all().order_by('nombre') return render(request, "sensoresf.html", {"datos": datos, "sensores": sensores,"menus": sensores}) def ConsultaSensoresRango(request): if request.method == "POST": sensor = request.POST.get("sensor") ini = request.POST.get("fecha_ini") fin = request.POST.get("fecha_fin") datos = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__range=(ini, fin)) sensores = AltaSensoresFijos.objects.all().order_by('nombre') return render(request, "sensoresf.html", {"datos": datos, "sensores": sensores,"menus": sensores, "fecha_ini": ini, "fecha_fin": fin}) ## sensores fijos @method_decorator(csrf_exempt) def sensoresf(request): if request.method == "POST": form = Formsensoresf(request.POST) if form.is_valid(): post = form.save(commit=False) post.save() sensor = request.POST.get("idsensor") temp = request.POST.get("temp") hume = request.POST.get("hume") temp1 = float(temp) hume1 = float(hume) nombres = AltaSensoresFijos.objects.filter(idsensor=sensor) nombre = nombres[0].nombre telegram1 = 'El sensor: ' + nombre +' Salio Fuera del rango con temperatura actual de: '+ temp telegram2 = 'El sensor: ' + nombre +' Salio Fuera del rango con humedad actual de: '+ hume datos1 = AltaSensoresFijos.objects.all() t01id = datos1[0].idsensor t01tmin = datos1[0].temp_min t01tmax = datos1[0].temp_max t01hmin = datos1[0].hume_min t01hmax = datos1[0].hume_max t02id = datos1[1].idsensor t02tmin = datos1[1].temp_min t02tmax = datos1[1].temp_max t02hmin = datos1[1].hume_min t02hmax = datos1[1].hume_max t03id = datos1[2].idsensor t03tmin = datos1[2].temp_min t03tmax = datos1[2].temp_max t03hmin = datos1[2].hume_min t03hmax = datos1[2].hume_max t04id = datos1[3].idsensor t04tmin = datos1[3].temp_min t04tmax = datos1[3].temp_max t04hmin = datos1[3].hume_min t04hmax = datos1[3].hume_max t05id = datos1[4].idsensor t05tmin = datos1[4].temp_min t05tmax = datos1[4].temp_max t05hmin = datos1[4].hume_min t05hmax = datos1[4].hume_max t06id = datos1[5].idsensor t06tmin = datos1[5].temp_min t06tmax = datos1[5].temp_max t06hmin = datos1[5].hume_min t06hmax = datos1[5].hume_max t07id = datos1[6].idsensor t07tmin = datos1[6].temp_min t07tmax = datos1[6].temp_max t07hmin = datos1[6].hume_min t07hmax = datos1[6].hume_max t08id = datos1[7].idsensor t08tmin = datos1[7].temp_min t08tmax = datos1[7].temp_max t08hmin = datos1[7].hume_min t08hmax = datos1[7].hume_max t09id = datos1[8].idsensor t09tmin = datos1[8].temp_min t09tmax = datos1[8].temp_max t09hmin = datos1[8].hume_min t09hmax = datos1[8].hume_max t010id = datos1[9].idsensor t010tmin = datos1[9].temp_min t010tmax = datos1[9].temp_max t010hmin = datos1[9].hume_min t010hmax = datos1[9].hume_max t011id = datos1[10].idsensor t011tmin = datos1[10].temp_min t011tmax = datos1[10].temp_max t011hmin = datos1[10].hume_min t011hmax = datos1[10].hume_max t012id = datos1[11].idsensor t012tmin = datos1[11].temp_min t012tmax = datos1[11].temp_max t012hmin = datos1[11].hume_min t012hmax = datos1[11].hume_max t013id = datos1[12].idsensor t013tmin = datos1[12].temp_min t013tmax = datos1[12].temp_max t013hmin = datos1[12].hume_min t013hmax = datos1[12].hume_max t014id = datos1[13].idsensor t014tmin = datos1[13].temp_min t014tmax = datos1[13].temp_max t014hmin = datos1[13].hume_min t014hmax = datos1[13].hume_max t015id = datos1[14].idsensor t015tmin = datos1[14].temp_min t015tmax = datos1[14].temp_max t015hmin = datos1[14].hume_min t015hmax = datos1[14].hume_max t016id = datos1[15].idsensor t016tmin = datos1[15].temp_min t016tmax = datos1[15].temp_max t016hmin = datos1[15].hume_min t016hmax = datos1[15].hume_max t017id = datos1[16].idsensor t017tmin = datos1[16].temp_min t017tmax = datos1[16].temp_max t017hmin = datos1[16].hume_min t017hmax = datos1[16].hume_max t018id = datos1[17].idsensor t018tmin = datos1[17].temp_min t018tmax = datos1[17].temp_max t018hmin = datos1[17].hume_min t018hmax = datos1[17].hume_max t019id = datos1[18].idsensor t019tmin = datos1[18].temp_min t019tmax = datos1[18].temp_max t019hmin = datos1[18].hume_min t019hmax = datos1[18].hume_max t020id = datos1[19].idsensor t020tmin = datos1[19].temp_min t020tmax = datos1[19].temp_max t020hmin = datos1[19].hume_min t020hmax = datos1[19].hume_max t021id = datos1[20].idsensor t021tmin = datos1[20].temp_min t021tmax = datos1[20].temp_max t021hmin = datos1[20].hume_min t021hmax = datos1[20].hume_max t022id = datos1[21].idsensor t022tmin = datos1[21].temp_min t022tmax = datos1[21].temp_max t022hmin = datos1[21].hume_min t022hmax = datos1[21].hume_max t023id = datos1[22].idsensor t023tmin = datos1[22].temp_min t023tmax = datos1[22].temp_max t023hmin = datos1[22].hume_min t023hmax = datos1[22].hume_max t024id = datos1[23].idsensor t024tmin = datos1[23].temp_min t024tmax = datos1[23].temp_max t024hmin = datos1[23].hume_min t024hmax = datos1[23].hume_max t025id = datos1[24].idsensor t025tmin = datos1[24].temp_min t025tmax = datos1[24].temp_max t025hmin = datos1[24].hume_min t025hmax = datos1[24].hume_max t026id = datos1[25].idsensor t026tmin = datos1[25].temp_min t026tmax = datos1[25].temp_max t026hmin = datos1[25].hume_min t026hmax = datos1[25].hume_max t027id = datos1[26].idsensor t027tmin = datos1[26].temp_min t027tmax = datos1[26].temp_max t027hmin = datos1[26].hume_min t027hmax = datos1[26].hume_max t028id = datos1[27].idsensor t028tmin = datos1[27].temp_min t028tmax = datos1[27].temp_max t028hmin = datos1[27].hume_min t028hmax = datos1[27].hume_max t029id = datos1[28].idsensor t029tmin = datos1[28].temp_min t029tmax = datos1[28].temp_max t029hmin = datos1[28].hume_min t029hmax = datos1[28].hume_max t030id = datos1[29].idsensor t030tmin = datos1[29].temp_min t030tmax = datos1[29].temp_max t030hmin = datos1[29].hume_min t030hmax = datos1[29].hume_max t031id = datos1[30].idsensor t031tmin = datos1[30].temp_min t031tmax = datos1[30].temp_max t031hmin = datos1[30].hume_min t031hmax = datos1[30].hume_max t032id = datos1[31].idsensor t032tmin = datos1[31].temp_min t032tmax = datos1[31].temp_max t032hmin = datos1[31].hume_min t032hmax = datos1[31].hume_max t033id = datos1[32].idsensor t033tmin = datos1[32].temp_min t033tmax = datos1[32].temp_max t033hmin = datos1[32].hume_min t033hmax = datos1[32].hume_max t034id = datos1[33].idsensor t034tmin = datos1[33].temp_min t034tmax = datos1[33].temp_max t034hmin = datos1[33].hume_min t034hmax = datos1[33].hume_max t035id = datos1[34].idsensor t035tmin = datos1[34].temp_min t035tmax = datos1[34].temp_max t035hmin = datos1[34].hume_min t035hmax = datos1[34].hume_max t036id = datos1[35].idsensor t036tmin = datos1[35].temp_min t036tmax = datos1[35].temp_max t036hmin = datos1[35].hume_min t036hmax = datos1[35].hume_max t037id = datos1[36].idsensor t037tmin = datos1[36].temp_min t037tmax = datos1[36].temp_max t037hmin = datos1[36].hume_min t037hmax = datos1[36].hume_max t038id = datos1[37].idsensor t038tmin = datos1[37].temp_min t038tmax = datos1[37].temp_max t038hmin = datos1[37].hume_min t038hmax = datos1[37].hume_max t039id = datos1[38].idsensor t039tmin = datos1[38].temp_min t039tmax = datos1[38].temp_max t039hmin = datos1[38].hume_min t039hmax = datos1[38].hume_max t040id = datos1[39].idsensor t040tmin = datos1[39].temp_min t040tmax = datos1[39].temp_max t040hmin = datos1[30].hume_min t040hmax = datos1[30].hume_max t041id = datos1[40].idsensor t041tmin = datos1[40].temp_min t041tmax = datos1[40].temp_max t041hmin = datos1[40].hume_min t041hmax = datos1[40].hume_max t042id = datos1[41].idsensor t042tmin = datos1[41].temp_min t042tmax = datos1[41].temp_max t042hmin = datos1[41].hume_min t042hmax = datos1[41].hume_max t043id = datos1[42].idsensor t043tmin = datos1[42].temp_min t043tmax = datos1[42].temp_max t043hmin = datos1[42].hume_min t043hmax = datos1[42].hume_max t044id = datos1[43].idsensor t044tmin = datos1[43].temp_min t044tmax = datos1[43].temp_max t044hmin = datos1[43].hume_min t044hmax = datos1[43].hume_max t045id = datos1[44].idsensor t045tmin = datos1[44].temp_min t045tmax = datos1[44].temp_max t045hmin = datos1[44].hume_min t045hmax = datos1[44].hume_max t046id = datos1[45].idsensor t046tmin = datos1[45].temp_min t046tmax = datos1[45].temp_max t046hmin = datos1[45].hume_min t046hmax = datos1[45].hume_max t047id = datos1[46].idsensor t047tmin = datos1[46].temp_min t047tmax = datos1[46].temp_max t047hmin = datos1[46].hume_min t047hmax = datos1[46].hume_max t048id = datos1[47].idsensor t048tmin = datos1[47].temp_min t048tmax = datos1[47].temp_max t048hmin = datos1[47].hume_min t048hmax = datos1[47].hume_max t049id = datos1[48].idsensor t049tmin = datos1[48].temp_min t049tmax = datos1[48].temp_max t049hmin = datos1[48].hume_min t049hmax = datos1[48].hume_max t050id = datos1[49].idsensor t050tmin = datos1[49].temp_min t050tmax = datos1[49].temp_max t050hmin = datos1[49].hume_min t050hmax = datos1[49].hume_max if sensor == t01id: if temp1 < t01tmin or temp1 > t01tmax: enviarmensaje(telegram1) elif hume1 < t01hmin or hume1 > t01hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t02id: if temp1 < t02tmin or temp1 > t02tmax: enviarmensaje(telegram1) elif hume1 < t02hmin or hume1 > t02hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t03id: if temp1 < t03tmin or temp1 > t03tmax: enviarmensaje(telegram1) elif hume1 < t03hmin or hume1 > t03hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t04id: if temp1 < t04tmin or temp1 > t04tmax: enviarmensaje(telegram1) elif hume1 < t04hmin or hume1 > t04hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t05id: if temp1 < t05tmin or temp1 > t05tmax: enviarmensaje(telegram1) elif hume1 < t05hmin or hume1 > t05hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t06id: if temp1 < t06tmin or temp1 > t06tmax: enviarmensaje(telegram1) elif hume1 < t06hmin or hume1 > t06hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t07id: if temp1 < t07tmin or temp1 > t07tmax: enviarmensaje(telegram1) elif hume1 < t07hmin or hume1 > t07hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t08id: if temp1 < t08tmin or temp1 > t08tmax: enviarmensaje(telegram1) elif hume1 < t08hmin or hume1 > t08hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t09id: if temp1 < t09tmin or temp1 > t09tmax: enviarmensaje(telegram1) elif hume1 < t09hmin or hume1 > t09hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t010id: if temp1 < t010tmin or temp1 > t010tmax: enviarmensaje(telegram1) elif hume1 < t010hmin or hume1 > t010hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t011id: if temp1 < t011tmin or temp1 > t011tmax: enviarmensaje(telegram1) elif hume1 < t011hmin or hume1 > t011hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t012id: if temp1 < t012tmin or temp1 > t012tmax: enviarmensaje(telegram1) elif hume1 < t012hmin or hume1 > t012hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t013id: if temp1 < t013tmin or temp1 > t013tmax: enviarmensaje(telegram1) elif hume1 < t013hmin or hume1 > t013hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t014id: if temp1 < t014tmin or temp1 > t014tmax: enviarmensaje(telegram1) elif hume1 < t014hmin or hume1 > t014hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t015id: if temp1 < t015tmin or temp1 > t015tmax: enviarmensaje(telegram1) elif hume1 < t015hmin or hume1 > t015hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t016id: if temp1 < t016tmin or temp1 > t016tmax: enviarmensaje(telegram1) elif hume1 < t016hmin or hume1 > t016hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t017id: if temp1 < t017tmin or temp1 > t017tmax: enviarmensaje(telegram1) elif hume1 < t017hmin or hume1 > t017hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t018id: if temp1 < t018tmin or temp1 > t018tmax: enviarmensaje(telegram1) elif hume1 < t018hmin or hume1 > t018hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t019id: if temp1 < t019tmin or temp1 > t019tmax: enviarmensaje(telegram1) elif hume1 < t019hmin or hume1 > t019hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t020id: if temp1 < t020tmin or temp1 > t020tmax: enviarmensaje(telegram1) elif hume1 < t020hmin or hume1 > t020hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t021id: if temp1 < t021tmin or temp1 > t021tmax: enviarmensaje(telegram1) elif hume1 < t021hmin or hume1 > t021hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t022id: if temp1 < t022tmin or temp1 > t022tmax: enviarmensaje(telegram1) elif hume1 < t022hmin or hume1 > t022hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t023id: if temp1 < t023tmin or temp1 > t023tmax: enviarmensaje(telegram1) elif hume1 < t023hmin or hume1 > t023hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t024id: if temp1 < t024tmin or temp1 > t024tmax: enviarmensaje(telegram1) elif hume1 < t024hmin or hume1 > t024hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t025id: if temp1 < t025tmin or temp1 > t025tmax: enviarmensaje(telegram1) elif hume1 < t025hmin or hume1 > t025hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t026id: if temp1 < t026tmin or temp1 > t026tmax: enviarmensaje(telegram1) elif hume1 < t026hmin or hume1 > t026hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t027id: if temp1 < t027tmin or temp1 > t027tmax: enviarmensaje(telegram1) elif hume1 < t027hmin or hume1 > t027hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t028id: if temp1 < t028tmin or temp1 > t028tmax: enviarmensaje(telegram1) elif hume1 < t028hmin or hume1 > t028hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t029id: if temp1 < t029tmin or temp1 > t029tmax: enviarmensaje(telegram1) elif hume1 < t029hmin or hume1 > t029hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t030id: if temp1 < t030tmin or temp1 > t030tmax: enviarmensaje(telegram1) elif hume1 < t030hmin or hume1 > t030hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t031id: if temp1 < t031tmin or temp1 > t031tmax: enviarmensaje(telegram1) elif hume1 < t031hmin or hume1 > t031hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t032id: if temp1 < t032tmin or temp1 > t032tmax: enviarmensaje(telegram1) elif hume1 < t032hmin or hume1 > t032hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t033id: if temp1 < t033tmin or temp1 > t033tmax: enviarmensaje(telegram1) elif hume1 < t033hmin or hume1 > t033hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t034id: if temp1 < t034tmin or temp1 > t034tmax: enviarmensaje(telegram1) elif hume1 < t034hmin or hume1 > t034hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t035id: if temp1 < t035tmin or temp1 > t035tmax: enviarmensaje(telegram1) elif hume1 < t035hmin or hume1 > t035hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t036id: if temp1 < t036tmin or temp1 > t036tmax: enviarmensaje(telegram1) elif hume1 < t036hmin or hume1 > t036hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t037id: if temp1 < t037tmin or temp1 > t037tmax: enviarmensaje(telegram1) elif hume1 < t037hmin or hume1 > t037hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t038id: if temp1 < t038tmin or temp1 > t038tmax: enviarmensaje(telegram1) elif hume1 < t038hmin or hume1 > t038hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t039id: if temp1 < t039tmin or temp1 > t039tmax: enviarmensaje(telegram1) elif hume1 < t039hmin or hume1 > t039hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t040id: if temp1 < t040tmin or temp1 > t040tmax: enviarmensaje(telegram1) elif hume1 < t040hmin or hume1 > t040hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t041id: if temp1 < t041tmin or temp1 > t041tmax: enviarmensaje(telegram1) elif hume1 < t041hmin or hume1 > t041hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t042id: if temp1 < t042tmin or temp1 > t042tmax: enviarmensaje(telegram1) elif hume1 < t042hmin or hume1 > t042hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t043id: if temp1 < t043tmin or temp1 > t043tmax: enviarmensaje(telegram1) elif hume1 < t043hmin or hume1 > t043hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t044id: if temp1 < t044tmin or temp1 > t044tmax: enviarmensaje(telegram1) elif hume1 < t044hmin or hume1 > t044hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t045id: if temp1 < t045tmin or temp1 > t045tmax: enviarmensaje(telegram1) elif hume1 < t045hmin or hume1 > t045hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t046id: if temp1 < t046tmin or temp1 > t046tmax: enviarmensaje(telegram1) elif hume1 < t046hmin or hume1 > t046hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t047id: if temp1 < t047tmin or temp1 > t047tmax: enviarmensaje(telegram1) elif hume1 < t047hmin or hume1 > t047hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t048id: if temp1 < t048tmin or temp1 > t048tmax: enviarmensaje(telegram1) elif hume1 < t048hmin or hume1 > t048hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t049id: if temp1 < t049tmin or temp1 > t049tmax: enviarmensaje(telegram1) elif hume1 < t049hmin or hume1 > t049hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) elif sensor == t050id: if temp1 < t050tmin or temp1 > t050tmax: enviarmensaje(telegram1) elif hume1 < t050hmin or hume1 > t050hmax: enviarmensajeh(telegram2) return HttpResponse(status=200) else: for field, items in form.errors.items(): for item in items: messages.error(request, '{}: {}'.format(field, item)) form = Formsensoresf() date = datetime.now() date1 = date.strftime("%Y-%m-%d") datos = sensoresfijos.objects.filter(fecha__icontains=date1) sensores = AltaSensoresFijos.objects.all().order_by('nombre') return render(request, "sensoresf.html", {"form": form, "datos": datos, "sensores": sensores,"menus": sensores}) class SensoresFijos(TemplateView): template_name = 'sensores.html' @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return super().dispatch(request, *args, **kwargs) def post(self, request, *args, **kwargs): data = {} try: action = request.POST['action'] if action == 'get_graph_online1': sf = sensoresfijos.objects.filter(idsensor='T001').latest('id') data = {'y': sf.temp} elif action == 'get_graph_online2': sf = sensoresfijos.objects.filter(idsensor='T002').latest('id') data = {'y': sf.temp} elif action == 'get_graph_online3': sf = sensoresfijos.objects.filter(idsensor='T003').latest('id') data = {'y': sf.temp} elif action == 'get_graph_online4': sf = sensoresfijos.objects.filter(idsensor='T004').latest('id') data = {'y': sf.temp} else: data['error'] = 'Ha ocurrido un error' except Exception as e: data['error'] = str(e) return JsonResponse(data, safe=False) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['panel'] = 'Panel de administrador' return context ### alta sensores fijos ### @login_required(login_url='acceder') def altasensoresf(request): if request.method == "POST": form = FormAltasensoresf(request.POST) if form.is_valid(): post = form.save(commit=False) post.save() return redirect("altasensoresf") else: for field, items in form.errors.items(): for item in items: messages.error(request, '{}: {}'.format(field, item)) datos = AltaSensoresFijos.objects.all().order_by('nombre') teleg = telegram.objects.all() return render(request, "altasensoresf.html", {"datos": datos, "teleg": teleg, "menus": datos}) @login_required(login_url='acceder') def ActualizarSensor(request, id): instance= get_object_or_404(AltaSensoresFijos, pk=id) form = FormAltasensoresf(request.POST or None, instance=instance) context= {'form': form} if form.is_valid(): obj= form.save(commit= False) obj.save() messages.success(request, "El perfil fue actualizado") return redirect("altasensoresf") else: context= {'form': form, 'error': 'Error al actualizar'} return render(request,'actualizar_sensorf.html' , context) ############################################################################################# ################# telegram conf ################################### def enviarmensaje(telegram1): teleg = telegram.objects.all() bot = teleg[0].idBot grupo = teleg[0].idGrupo idBot = bot idGrupo = grupo requests.post('https://api.telegram.org/bot' + idBot + '/sendMessage', data={'chat_id': idGrupo, 'text': telegram1, 'parse_mode': 'HTML'}) def enviarmensajeh(telegram2): teleg = telegram.objects.all() bot = teleg[0].idBot grupo = teleg[0].idGrupo idBot = bot idGrupo = grupo requests.post('https://api.telegram.org/bot' + idBot + '/sendMessage', data={'chat_id': idGrupo, 'text': telegram2, 'parse_mode': 'HTML'}) def enviarmensajeact(telegram3): teleg = telegram.objects.all() bot = teleg[0].idBot grupo = teleg[0].idGrupo idBot = bot idGrupo = grupo requests.post('https://api.telegram.org/bot' + idBot + '/sendMessage', data={'chat_id': idGrupo, 'text': telegram3, 'parse_mode': 'HTML'}) def confTelegram(request): datos = telegram.objects.all() return render(request, "telegram.html", {"datos": datos}) @login_required(login_url='acceder') def ActualizarTelegram(request, id): instance= get_object_or_404(telegram, pk=id) form = Formtelegram(request.POST or None, instance=instance) context= {'form': form} if form.is_valid(): obj= form.save(commit= False) obj.save() messages.success(request, "El perfil fue actualizado") return redirect("altasensoresf") else: context= {'form': form, 'error': 'Error al actualizar'} return render(request,'actualizar_telegram.html' , context) #### ENVIO EMAIL ####### def enviaremail(sensor, temp): subject = 'Sensor fuera de rango' template = get_template('email.html') email = "********@gmail.com" content = template.render({ 'sensor': sensor,'temp': temp, }) message = EmailMultiAlternatives(subject, '', settings.EMAIL_HOST_USER, [email]) message.attach_alternative(content, 'text/html') message.send() ####### ruta post sensor con dia especifico sin actualizacion def sensoresfijosdia(request): if request.method == "POST": sensor = request.POST.get("sensor") ini = request.POST.get("fecha_ini") fin = request.POST.get("fecha_fin") if sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__range=(ini, fin)).exists(): datos = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__range=(ini, fin)) datos2 = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__range=(ini, fin)).order_by('temp').values() datos3 = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__range=(ini, fin)).order_by('-temp').values() datos4 = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__range=(ini, fin)).order_by('hume').values() datos5 = sensoresfijos.objects.filter(idsensor=sensor).filter(fecha__range=(ini, fin)).order_by('-hume').values() temmin = datos2[0] temmax = datos3[0] humemin = datos4[0] humemax = datos5[0] sensores = AltaSensoresFijos.objects.all().order_by('nombre') nombre = AltaSensoresFijos.objects.filter(idsensor=sensor)[0] sensort = nombre return render(request, "sensores_fijos_dia.html", {"datos": datos, "sensores": sensores, "sensort": sensort, "fecha_ini": ini, "fecha_fin": fin, "temmin": temmin, "temmax": temmax, "humemin": humemin, "humemax": humemax, "menus": sensores}) date = 'No Existen Datos del Sensor' date1 = 'No Existen Datos del Sensor' datos = 'No Existen Datos del Sensor' datos2 = 'No Existen Datos del Sensor' datos3 = 'No Existen Datos del Sensor' datos4 = 'No Existen Datos del Sensor' datos5 = 'No Existen Datos del Sensor' temmin = 'No Existen Datos del Sensor' temmax = 'No Existen Datos del Sensor' humemin = 'No Existen Datos del Sensor' humemax = 'No Existen Datos del Sensor' sensores = AltaSensoresFijos.objects.all().order_by('nombre') sensort = 'No Existen Datos del Sensor' return render(request, "sensores_fijos_dia.html", {"datos": datos, "sensores": sensores, "sensort": sensort, "fecha": date1, "temmin": temmin, "temmax": temmax, "humemin": humemin, "humemax": humemax, "menus": sensores}) #### ruta get por sensor con actualizacion cada 20 segundos def sensoresfijosdianew(request, id): date = datetime.now() date1 = date.strftime("%Y-%m-%d") sen = str(id).zfill(3) sen1 = id sensores = AltaSensoresFijos.objects.all().order_by('nombre') if sensoresfijos.objects.filter(idsensor=sen1).exists(): if sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=date1).exists(): datos = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=date1).order_by('fecharango').values() datos2 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=date1).order_by('temp').values() datos3 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=date1).order_by('-temp').values() datos4 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=date1).order_by('hume').values() datos5 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=date1).order_by('-hume').values() temmin = datos2[0] temmax = datos3[0] humemin = datos4[0] humemax = datos5[0] nombre = AltaSensoresFijos.objects.filter(idsensor=sen1)[0] sensort = nombre return render(request, "sensores_fijos_dianew.html", {"datos": datos, "sensores": sensores, "sensort": sensort, "fecha": date1, "temmin": temmin, "temmax": temmax, "humemin": humemin, "humemax": humemax, "menus": sensores}) uf = sensoresfijos.objects.filter(idsensor=sen1).latest('id') uf1 = uf.fecha datos = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=uf1).order_by('fecharango').values() datos2 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=uf1).order_by('temp').values() datos3 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=uf1).order_by('-temp').values() datos4 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=uf1).order_by('hume').values() datos5 = sensoresfijos.objects.filter(idsensor=sen1).filter(fecha__icontains=uf1).order_by('-hume').values() temmin = datos2[0] temmax = datos3[0] humemin = datos4[0] humemax = datos5[0] nombre = AltaSensoresFijos.objects.filter(idsensor=sen1)[0] sensort = nombre return render(request, "sensores_fijos_dianew.html", {"datos": datos, "sensores": sensores, "sensort": sensort, "fecha": uf1, "temmin": temmin, "temmax": temmax, "humemin": humemin, "humemax": humemax, "menus": sensores}) sensort = 'No Existen Datos del Sensor' temmin = sensort temmax = sensort hummin = sensort hummax = sensort return render(request, "sensores_fijos_dianew.html", {"sensores": sensores, "sensort": sensort, "fecha": date1, "temmin": temmin, "temmax": temmax, "hummin": hummin, "hummax": hummax, "menus": sensores}) ######################### check activo ##################### def CheckSensoresActivos(request): datos = AltaSensoresFijos.objects.all() for i in range(50): idt = datos[i].idsensor act = datos[i].activo min = 29 timer = str(min) if act == 'SI': if sensoresfijos.objects.filter(idsensor=idt).exists(): uf = sensoresfijos.objects.filter(idsensor=idt).latest('id') oldf = uf.fecharango.strftime("%Y-%m-%d %H:%M:%S") tn = datetime.strptime(oldf, "%Y-%m-%d %H:%M:%S") + timedelta(minutes=min) hoy = datetime.now() f1 = tn.strftime("%Y-%m-%d %H:%M:%S") f2 = hoy.strftime("%Y-%m-%d %H:%M:%S") if f1 < f2: telegram3 = 'El sensor: ' + idt +' No ha enviado datos en mas de '+ timer +' minutos.' enviaractivo(idt) enviarmensajeact(telegram3) return HttpResponse(status=200) def enviaractivo(id): url = "http://server.mxsig.com.mx:9000/sensoresf/" post_fields = {'idsensor': id , "temp": "-200.0", "hume": "-200.0"} request = Request(url, urlencode(post_fields).encode()) json = urlopen(request).read().decode() print(json)