summaryrefslogtreecommitdiff
path: root/myadmin/views.py
blob: 32fa6d67a4198487c08f755aeb5ebe284d09cb22 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from django.shortcuts import render, redirect
from django.http import Http404,HttpResponse
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Count
from django.db import connection
from django.core.exceptions import ObjectDoesNotExist
from sbhs_server.tables.models import Board, Booking, Slot, Experiment, Account, Webcam
from sbhs_server import settings,sbhs
import subprocess,json,serial,os, datetime, requests
# Create your views here.

def checkadmin(req):
    """ Checks for valid admin
            Raises Http error if:
                Requested user is not admin.
        Input: s: request object.
        Output: HttpResponse      
    """
    if not req.user.is_admin:
        raise Http404

@login_required(redirect_field_name=None)
def index(req):
    checkadmin(req)
    boards = Board.objects.order_by('-online').all()
    allotment_mode = "Random" if Board.can_do_random_allotment() else "Workshop"
    return render(req, 'admin/index.html', {"boards": boards, "allotment_mode": allotment_mode})

@login_required(redirect_field_name=None)
def toggle_allotment_mode(req):
    checkadmin(req)
    Board.toggle_random_allotment()
    return redirect(index)

@login_required(redirect_field_name=None)
def booking_index(req):
    checkadmin(req)
    bookings = Booking.objects.order_by('-booking_date','-slot_id').filter(trashed_at__isnull=True).select_related()[:50]
    return render(req, 'admin/booking_index.html', {"bookings": bookings})

@login_required(redirect_field_name=None)
def webcam_index(req):
    checkadmin(req)
    boards = Board.objects.filter(online=True)
    for board in boards:
        Webcam.load_image(board.mid)
    return render(req, 'admin/webcam_index.html', {"boards": boards})

@login_required(redirect_field_name=None)
def logs_index(req):
    checkadmin(req)
    date = (datetime.datetime.now()).strftime("%Y-%m-%d")
    return render(req, 'admin/user_logs.html', {"nowdate" : date})

@login_required(redirect_field_name=None)
def profile(req, mid):
    checkadmin(req)
    try:
        filename = settings.SBHS_GLOBAL_LOG_DIR + "/" + mid + ".log"
        f = open(filename, "r")
        f.close()
    except:
        raise Http404

    delta_T = 1000
    data = subprocess.check_output("tail -n %d %s" % (delta_T, filename), shell=True)
    data = data.split("\n")
    plot = []
    heatcsv = ""
    fancsv = ""
    tempcsv = ""

    for t in xrange(len(data)):
        line = data[t]
        entry = line.strip().split(" ")
        try:
            plot.append([int(i) for i in entry[0:-1] + [float(entry[-1])]])
            heatcsv += "%d,%s\\n" % (t+1, entry[1])
            fancsv += "%d,%s\\n" % (t+1, entry[2])
            tempcsv += "%d,%s\\n" % (t+1, entry[3])
        except:
            continue

    plot = zip(*plot) # transpose

    return render(req, "admin/profile.html", {
        "mid": mid,
        "delta_T": delta_T,
        "heat": heatcsv,
        "fan": fancsv,
        "temp": tempcsv
    })

@login_required(redirect_field_name=None)
def testing(req):
    checkadmin(req)
    now = datetime.datetime.now()
    current_slot_id = Slot.objects.filter(start_hour=now.hour,
                                            start_minute__lt=now.minute,
                                            end_minute__gt=now.minute)

    current_slot_id = -1 if not current_slot_id else current_slot_id[0].id

    current_bookings = Booking.objects.filter(slot_id=current_slot_id,
                                                booking_date=datetime.date.today()).select_related()
    current_mids = list([-1]) if not current_bookings else [current_booking.account.board.mid for current_booking in current_bookings]

    boards = Board.objects.filter(online=1)
    allotment_mode = "Random" if Board.can_do_random_allotment() else "Workshop"
    return render(req, 'admin/testexp.html', {"boards": boards, "allotment_mode": allotment_mode, "mids": current_mids})

@csrf_exempt
def monitor_experiment(req):
    checkadmin(req)
    try:
        mid = int(req.POST.get("mid"))
    except Exception as e:
        return HttpResponse(json.dumps({"status_code":400, "message":"Invalid parameters"}), content_type="application/json")
    try:
        ip = settings.pi_ip_map.get(str(mid))
        if ip is None:
            return HttpResponse(json.dumps({"status_code":400, "message":"Board is offline"}), content_type="application/json")
        url = "http://" + str(ip) + "/pi/admin/monitor"
        payload = {"mid":mid}
        r = requests.post(url , data = payload)
        
        return HttpResponse(r.text, content_type="application/json")
    except Exception as e:
        retVal={"status_code":500,"message":"Could not fetch device logs.."}
        return HttpResponse(json.dumps(retVal),content_type='application/json')

@login_required(redirect_field_name=None)
def get_allocated_mids(req):
    checkadmin(req)
    with connection.cursor() as cursor:
        cursor.execute("SELECT tables_board.mid, COUNT(tables_account.id), tables_board.id FROM tables_account RIGHT OUTER JOIN tables_board ON tables_account.board_id = tables_board.id WHERE tables_board.online = 1 GROUP BY tables_board.mid ORDER BY COUNT(tables_account.id)")
        mid_count = cursor.fetchall()

    return render(req, 'admin/changeMID.html', {"mid_count" : mid_count})

@csrf_exempt
def get_users(req):
    checkadmin(req)
    try:
        users = list(Account.objects.values_list("username", flat=True))
        return HttpResponse(json.dumps({"status_code":200, "message":users}), content_type="application/json")
    except Exception as e:
        return HttpResponse(json.dumps({"status_code":500, "message":str(e)}), content_type="application/json")


@csrf_exempt
def toggle_device_status(req):
    checkadmin(req)

    try : 
        mid = req.POST.get('mid')
    except Exception as e:
        return HttpResponse(json.dumps({"status_code":400, "message":"Invalid parameters"}), content_type="application/json")

    try:
        now = datetime.datetime.now()
        current_slot_id = Slot.objects.filter(start_hour=now.hour,
                                                start_minute__lt=now.minute,
                                                end_minute__gt=now.minute)

        current_slot_id = -1 if not current_slot_id else current_slot_id[0].id

        current_bookings = Booking.objects.filter(slot_id=current_slot_id,
                                                    booking_date=datetime.date.today()).select_related()
        current_mids = list([-1]) if not current_bookings else [current_booking.account.board.mid for current_booking in current_bookings]
    except Exception as e:
        return HttpResponse(json.dumps({"status_code":400, "message":"Unsuccessful"}), content_type="application/json")

    if int(mid) in current_mids:
        return HttpResponse(json.dumps({"status_code":400, "message":"Board is in use."}), content_type="application/json")

    try:
        brd = Board.objects.get(mid = mid)
        brd.temp_offline = not brd.temp_offline
        brd.save()

        return HttpResponse(json.dumps({"status_code":200, "message":"Toggle successful"}), content_type="application/json")
    except Exception as e:
        return HttpResponse(json.dumps({"status_code":500, "message":"Unsuccessful"}), content_type="application/json")


def user_exists(username):
    try:
        user = Account.objects.get(username=username)
    except ObjectDoesNotExist:
        return None
    return user

@csrf_exempt
def update_allocated_mid(req):
    checkadmin(req)
    try:
        username = req.POST.get("username")
        board_id = req.POST.get("board_id")
    except Exception as e:
        return HttpResponse(json.dumps({"status_code":400, "message":"Invalid parameters"}), content_type="application/json")

    user = user_exists(username)
    if user is not None:
        user.board_id = board_id
        user.save()
    else:
        return HttpResponse(json.dumps({"status_code": 400, "message": "Username does not exist"}), content_type="application/json")

    return HttpResponse(json.dumps({"status_code": 200, "message": "MID changed successfully"}), content_type="application/json")

@login_required(redirect_field_name=None)
def download_log(req, mid):
    checkadmin(req)
    try:
        global_logfile = settings.SBHS_GLOBAL_LOG_DIR + "/" + mid + ".log"
        f = open(global_logfile, "r")
        data = f.read()
        f.close()
        return HttpResponse(data, content_type='text/text')
    except:
        return HttpResponse("Requested log file doesn't exist.Please Try in the next hour after your slot ends.")

@login_required(redirect_field_name=None)
@csrf_exempt
def range_logs(req):
    checkadmin(req)
    try:
        start_date = req.POST.get("start_date")
        end_date = req.POST.get("end_date")
        start_time = req.POST.get("start_time")
        end_time = req.POST.get("end_time")
    except:
        return HttpResponse(json.dumps({"status_code":400, "message":"Invalid parameters"}), content_type="application/json")

    try:
        start = start_date + " " + start_time
        end = end_date + " " + end_time
        log_files = Experiment.objects.filter(created_at__range=[start, end]).values("id", "log")

        return HttpResponse(json.dumps({"status_code":200, "message":list(log_files)}), content_type="application/json")
    except Exception as e:
        return HttpResponse(json.dumps({"status_code": 500, "message": "Some error occured" + str(e)}), content_type="application/json")

@login_required(redirect_field_name=None)
@csrf_exempt
def download_experiment_log(req, experiment_id):
    """ Downloads the experimental log file.
        Input: req: request object, experiment_id: experimental id
        Output: HttpResponse object
    """
    checkadmin(req)
    try:
        experiment_data = Experiment.objects.select_related("booking", "booking__account").get(id=experiment_id)
        f = open(os.path.join(settings.EXPERIMENT_LOGS_DIR, experiment_data.log), "r")
        data = f.read()
        f.close()
        return HttpResponse(data, content_type='text/text')
    except:
        return HttpResponse("Requested log file doesn't exist.")

@csrf_exempt
def reset_device(req):
    """Resets the device to fan = 100 and heat = 0
        Takes mid as paramter 
        Returns status_code = 200, data={temp:temp of the device} if succesful
                else 
                status_code = 500 , data={error:errorMessage}
    """ 
    checkadmin(req)
    mid=int(req.POST.get('mid'))

    try:
        ip = settings.pi_ip_map.get(str(mid))
        if ip is None:
            return HttpResponse(json.dumps({"status_code":400, "message":"Board is offline"}), content_type="application/json")
        url = "http://" + str(ip) + "/pi/admin/resetdevice"
        payload = {"mid":mid}
        r = requests.post(url , data = payload)
        
        return HttpResponse(r.text, content_type="application/json")
    except Exception as e:
        retVal={"status_code":500,"message":"Could not reset the device.."}
        return HttpResponse(json.dumps(retVal),content_type='application/json')


@csrf_exempt
def set_device_params(req):
    """Sets the device parameters as per the arguments sent
        Takes mid,fan,heat as paramter 
        Returns status_code = 200, data={temp:temp of the device} if succesful
                else 
                status_code = 500 , data={error:errorMessage}
    """ 
    checkadmin(req)
    mid=int(req.POST.get('mid'))
    fan=int(req.POST.get('fan'))
    heat=int(req.POST.get('heat'))
    try:
        ip = settings.pi_ip_map.get(str(mid))
        if ip is None:
            return HttpResponse(json.dumps({"status_code":400, "message":"Board is offline"}), content_type="application/json")
        url = "http://" + str(ip) + "/pi/admin/setdevice"
        payload = {"mid":mid, "fan":fan, "heat":heat}
        r = requests.post(url , data = payload)
        
        return HttpResponse(r.text, content_type="application/json")
    except Exception as e:
        retVal={"status_code":500,"message":"Could not set the device params.."}
        return HttpResponse(json.dumps(retVal),content_type='application/json')

@csrf_exempt
def get_device_temp(req):
    """Sets the device parameters as per the arguments sent
        Takes mid,fan,heat as paramter 
        Returns status_code = 200, data={temp:temp of the device} if succesful
                else 
                status_code = 500 , data={error:errorMessage}
    """ 
    checkadmin(req)
    mid=int(req.POST.get('mid'))
    try:
        ip = settings.pi_ip_map.get(str(mid))
        if ip is None:
            return HttpResponse(json.dumps({"status_code":400, "message":"Board is offline"}), content_type="application/json")
        url = "http://" + str(ip) + "/pi/admin/gettemp"
        payload = {"mid":mid}
        r = requests.post(url , data = payload)
        
        return HttpResponse(r.text, content_type="application/json")
    except Exception as e:
        retVal={"status_code":500,"message":"Could not get the device temperature.."+str(e)}
        return HttpResponse(json.dumps(retVal),content_type='application/json')