def index(request): s = Plant().getProxy() if request.method == "POST": if "turn_light" in request.POST: state = request.POST["turn_light"] if state == "1": s.setLight(True) else: s.setLight(False) if "turn_water" in request.POST: s.water() template = loader.get_template("plant/index.html") data = {} turn_on_light = False time = None connected = False try: data = s.getSensors() time = s.getTime() connected = True if int(data["light"]) == 0: turn_on_light = True except Exception as inst: print inst if len(data) == 0: sensors = Sensor.objects.all() for s in sensors: latest = Value.objects.filter(sensor=s).order_by("-pub_date")[0] data[s.name] = latest.value last_connected = Value.objects.all().order_by("-pub_date")[0].pub_date last_connected = timezone.localtime(last_connected) status = "Arduino state: " + ("Connected" if connected else "Disconnected") status = status + "<br>Last connected: " + last_connected.strftime("%B %d, %H:%M:%S") # time.strftime('%B %d, %H:%M:%S') context = RequestContext( request, {"sensors": data, "turn_on_light": turn_on_light, "status": status, "connected": connected} ) return HttpResponse(template.render(context))
def index2(request): s = Plant().getProxy() if request.method == "POST": state = request.POST["turn_light"] if state == "1": s.setLight(True) else: s.setLight(False) template = loader.get_template("plant/index2.html") d = s.getSensors() turn_on_light = False if int(d["light"]) == 0: turn_on_light = True context = RequestContext(request, {"sensors": d, "turn_on_light": turn_on_light}) return HttpResponse(template.render(context))