-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.py
119 lines (105 loc) · 5.33 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from django.template import loader
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render
from .models import Socket, Schedule, DaysOfTheWeek
from datetime import datetime, timedelta
# Create your views here.
def index(request):
sockets = Socket.objects.all()
schedules = Schedule.objects.all()
template = loader.get_template('powersocket/socketcontrol.html')
context = {
'sockets': sockets,
'schedules': schedules,
}
return HttpResponse(template.render(context, request))
def update_socket(request, socket_id):
if socket_id == 'alloff' or socket_id == 'allon':
print 'Setting sockets to: ' + socket_id
for socket in Socket.objects.all():
socket.override_time_end = datetime.now() + timedelta(minutes=10)
if socket_id == 'alloff':
state = False
elif socket_id == 'allon':
state = True
print 'Setting ' + socket.name + ' to: ' + str(state)
socket.current_state = state
socket.save()
else:
try:
int(socket_id)
socket = get_object_or_404(Socket, socket_id=socket_id)
except ValueError:
socket = get_object_or_404(Socket, name=socket_id)
print 'Setting socket ' + socket.name + ' to: ' + request.POST['change_to']
socket.override_time_end = datetime.now() + timedelta(minutes=10)
if request.POST['change_to'] == 'on':
socket.current_state = True
socket.save()
else:
socket.current_state = False
socket.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
#return HttpResponseRedirect(reverse('powersocket:index', args=(p.id,)))
return HttpResponseRedirect(reverse('powersocket:index'))
def sched_check(request):
schedules = Schedule.objects.all()
sockets_checked = []
print '---Starting task (' + str(datetime.now()) + ')'
for schedule in schedules:
deviated_now = datetime.now() + timedelta(minutes=-schedule.current_random_deviation)
change_socket = False
sockets_on = False
print 'Checking schedule: ' + schedule.description + ' (minute deviation: ' + str(schedule.current_random_deviation) + ')'
for timeslot in schedule.time_slots.all():
print 'Checking time slot: ' + str(timeslot)
if str(deviated_now.weekday()) in timeslot.days_of_week:
if deviated_now.time().strftime('%H:%M') == timeslot.start_time.strftime('%H:%M'):
print ' - Triggering socket switch on! (time deveation: ' + str(schedule.current_random_deviation) + ')'
sockets_on = True
change_socket = True
timeslot.save() # trigger non manual time generation
elif deviated_now.time().strftime('%H:%M') == timeslot.end_time.strftime('%H:%M'):
print ' - Triggering socket switch off! (time deveation: ' + str(schedule.current_random_deviation) + ')'
change_socket = True
timeslot.save() # trigger non manual time generation
# actually do the socket changes
if change_socket == True:
for socket in schedule.socket.all():
currenttime = datetime.now()
print '...Checking socket: ' + socket.name
if socket not in sockets_checked:
sockets_checked.append(socket)
# clear up old override times
if socket.override_time_end != None:
if socket.override_time_end.strftime('%H:$M') < datetime.now().strftime('%H:%M'):
socket.override_time_end == None
socket.save()
if socket.current_state != sockets_on:
override = False
if socket.override_time_end != None:
if currenttime.strftime('%H:%M') < socket.override_time_end.strftime('%H:%M'):
override = True
if override == False:
if sockets_on == True:
print '......turning socket on'
else:
print '......turning socket off'
schedule.save() # trigger save to generate new random deviation value
socket.current_state = sockets_on
socket.override_time_end = None
socket.save()
else:
print 'Scheduled change has been overridden by manual socket state change! Socket state will not change'
# clear up old override times
for socket in Socket.objects.all():
if socket.override_time_end != None:
if socket.override_time_end.strftime('%H:%M') < datetime.now().strftime('%H:%M') or datetime.now().strftime('%H:%M') == "00:00":
print 'Clearing overide time for socket: ' + socket.name
socket.override_time_end = None
socket.save()
print '---Task finished'
return HttpResponse('Scheduled check complete')