def new(self, request): context = {} encendido = request.POST.get("encendido") apagado = request.POST.get("apagado") id_luz = request.POST.get("id_luz") if encendido and apagado and id_luz: encendido = encendido.replace("/", "-") apagado = apagado.replace("/", "-") luz = Light.objects.filter(pk=int(id_luz))[0] wrapperLight = WrapperLight(luz) encendido_db = encendido.split("-") encendido_db = ( encendido_db[2].split(" ")[0] + "/" + encendido_db[1] + "/" + encendido_db[0] + " a las " + encendido_db[2].split(" ")[1] ) apagado_db = apagado.split("-") apagado_db = ( apagado_db[2].split(" ")[0] + "/" + apagado_db[1] + "/" + apagado_db[0] + " a las " + apagado_db[2].split(" ")[1] ) automation = Automation.objects.create( encendido=encendido_db, apagado=apagado_db, luz=luz, estado="esperando" ) task = Task(wrapperLight, automation, encendido, apagado) settings.TASKS[automation.id] = task task.start() else: context["msg_error"] = 1 lights = Light.objects.all() context["lights"] = lights return render(request, self.template_new, context) automations = Automation.objects.all() context["automations"] = automations return render(request, self.template_get, context)
def run(): automations_ejecutandose = Automation.objects.filter(estado = "ejecutandose") automations_esperando = Automation.objects.filter(estado = "esperando") lights = Light.objects.all() automation = '' for automation in automations_esperando: if automation != '': wrapperLight = WrapperLight(automation.luz) s = automation.encendido encendido = s.split(" a las")[0].split("/")[2]+"-"+s.split(" a las")[0].split("/")[1]+"-"+s.split(" a las")[0].split("/")[0]+s.split(" a las")[1] apagado = automation.apagado.split(" a las")[0].split("/")[2]+"-"+automation.apagado.split(" a las")[0].split("/")[1]+"-"+automation.apagado.split(" a las")[0].split("/")[0]+automation.apagado.split(" a las")[1] now = str(datetime.now()) if now < apagado: task = Task(wrapperLight,automation,encendido,apagado) settings.TASKS[automation.id] = task task.start() else: wrapperLight.stop() automation.estado = "corte electrico" automation.save() for light in lights: if not light.estado: settings.ARDUINOS[light.arduino_path].digitalWriteDown(light.pin) task = '' for automation in automations_ejecutandose: wrapperLight = WrapperLight(automation.luz) encendido = automation.encendido.split(" a las")[0].split("/")[2]+"-"+automation.encendido.split(" a las")[0].split("/")[1]+"-"+automation.encendido.split(" a las")[0].split("/")[0]+automation.encendido.split(" a las")[1] apagado = automation.apagado.split(" a las")[0].split("/")[2]+"-"+automation.apagado.split(" a las")[0].split("/")[1]+"-"+automation.apagado.split(" a las")[0].split("/")[0]+automation.apagado.split(" a las")[1] now = str(datetime.now()) if now < apagado: task = Task(wrapperLight,automation,encendido,apagado) if task!='': settings.TASKS[automation.id] = task task.start() else: wrapperLight.stop() automation.estado = "no concluyo por corte electrico" automation.save()