summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsriyasainath2017-05-29 14:56:23 +0530
committersriyasainath2017-05-29 14:56:23 +0530
commit5c0587ddf33720cd017a8e7b3a3b2e51ef6b8720 (patch)
treeb107a55bad68f1c3df297e3a102396d8f58c14dc
parent9e08c33cef4542cf222a37488ee8e8887a91080a (diff)
parent9ad308de090c3db0e91d853afe861f5d9b828a8d (diff)
downloadSBHS-2018-Rpi-5c0587ddf33720cd017a8e7b3a3b2e51ef6b8720.tar.gz
SBHS-2018-Rpi-5c0587ddf33720cd017a8e7b3a3b2e51ef6b8720.tar.bz2
SBHS-2018-Rpi-5c0587ddf33720cd017a8e7b3a3b2e51ef6b8720.zip
Merge branch 'deep' of https://github.com/coderick14/sbhs into deep
-rw-r--r--date.txt2
-rw-r--r--experiment/views.py8
-rw-r--r--myadmin/urls.py3
-rw-r--r--myadmin/views.py50
-rw-r--r--new.csv2
-rw-r--r--templates/admin/index.html5
-rw-r--r--templates/admin/testexp.html43
7 files changed, 104 insertions, 9 deletions
diff --git a/date.txt b/date.txt
index 0ac25fc..79cacdc 100644
--- a/date.txt
+++ b/date.txt
@@ -1 +1 @@
-Fri May 26 00:00:46 IST 2017
+Fri May 26 14:56:51 IST 2017
diff --git a/experiment/views.py b/experiment/views.py
index 685c2e2..e4ab2ab 100644
--- a/experiment/views.py
+++ b/experiment/views.py
@@ -58,8 +58,12 @@ def initiation(req):
key = str(user_board.mid)
settings.boards[key]["experiment_id"] = e.id
+ global_logfile = settings.SBHS_GLOBAL_LOG_DIR + "/" + key + ".log"
+ with open(global_logfile, "a") as global_loghandler:
+ data = "\n\n===================New experiment====================\nUsername :", user.username, "\nExperiment Id :", e.id, "\n"
+ global_loghandler.write(data)
+
reset(req)
-
STATUS = 1
MESSAGE = filename
@@ -200,7 +204,7 @@ def log_data(sbhs, mid, experiment_id, heat=None, fan=None, temp=None):
if temp is None:
temp = sbhs.getTemp()
- data = "%d %s %s %s\n" % (int(time.time()), str(heat), str(fan), str(temp))
+ data = "%s %s %s %s\n" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(heat), str(fan), str(temp))
experiment_logfile = Experiment.objects.get(id=experiment_id).log
global_logfile = settings.SBHS_GLOBAL_LOG_DIR + "/" + str(mid) + ".log"
with open(global_logfile, "a") as global_loghandler, open(experiment_logfile, "a") as experiment_loghandler:
diff --git a/myadmin/urls.py b/myadmin/urls.py
index c1831e5..417fbb0 100644
--- a/myadmin/urls.py
+++ b/myadmin/urls.py
@@ -11,7 +11,8 @@ urlpatterns = [
url(r'^admin/resetdevice/?$', views.reset_device, name='admin_reset_device'),
url(r'^admin/setdevice/?$', views.set_device_params, name='admin_set_device'),
url(r'^admin/gettemp/?$', views.get_device_temp, name='admin_get_temp'),
-
+ url(r'^admin/monitor/?$', views.monitor_experiment, name='admin_monitor'),
+ url(r'^admin/logs/([0-9]+)/?$', views.download_log, name='admin_logs'),
url(r'^admin/toggle_allotment_mode/?$', views.toggle_allotment_mode, name='admin_toggle_allotment_mode'),
] \ No newline at end of file
diff --git a/myadmin/views.py b/myadmin/views.py
index 9e9c314..bd896e3 100644
--- a/myadmin/views.py
+++ b/myadmin/views.py
@@ -2,7 +2,7 @@ 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 sbhs_server.tables.models import Board, Booking, Slot
+from sbhs_server.tables.models import Board, Booking, Slot, Experiment
from sbhs_server import settings,sbhs
import subprocess,json,serial,os, datetime
# Create your views here.
@@ -94,6 +94,54 @@ def testing(req):
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")
+
+ 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
+
+ try:
+ current_booking = Booking.objects.get(slot_id=current_slot_id,
+ booking_date=datetime.date.today(),
+ account__board__mid=mid)
+ except Exception as e:
+ return HttpResponse(json.dumps({"status_code":400, "message":"Invalid MID"}), content_type="application/json")
+
+ current_booking_id, current_user = current_booking.id, current_booking.account.username
+
+ logfile = Experiment.objects.get(booking_id=current_booking_id).log
+
+ # get last 50 lines from logs
+ stdin,stdout = os.popen2("tail -n 10 "+logfile)
+ stdin.close()
+ logs = stdout.readlines(); stdout.close()
+ logs = "".join(logs)
+
+ data = {"user": current_user, "logs": logs}
+ return HttpResponse(json.dumps({"status_code":200, "message":data}), 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.")
+
+
+@csrf_exempt
def reset_device(req):
"""Resets the device to fan = 100 and heat = 0
Takes mid as paramter
diff --git a/new.csv b/new.csv
index d8b8930..43da5b7 100644
--- a/new.csv
+++ b/new.csv
@@ -4695,3 +4695,5 @@
2017-05-25 23:58:31.794913 False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False True
2017-05-25 23:58:55.790097 False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False
2017-05-26 00:00:46.763190 False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False True
+2017-05-26 14:55:45.419017 True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True
+2017-05-26 14:56:51.481469 False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False True
diff --git a/templates/admin/index.html b/templates/admin/index.html
index 95956f5..3e9ce15 100644
--- a/templates/admin/index.html
+++ b/templates/admin/index.html
@@ -15,6 +15,8 @@
<th>Board MID</th>
<th>Status</th>
<th>Webcam</th>
+ <th>Temperature Profile</th>
+ <th>Download Logs</th>
</tr>
</thead>
<tbody>
@@ -23,7 +25,8 @@
<td>{{ b.mid }}</td>
<td><span class="label label-{% if b.online %}success{% else %}important{% endif %}">{% if b.online %}Online{% else %}Offline{% endif %}</span></td>
<td><a href="{% url 'webcam_show_video_to_admin' b.mid %}" target="_blank">View image</a></td>
- <td><a href="{% url 'admin_profile' b.mid %}">View temperature profile</a></td>
+ <td><a href="{% url 'admin_profile' b.mid %}">View</a></td>
+ <td><a href="{% url 'admin_logs' b.mid %}">Download</a></td>
</tr>
{% endfor %}
</tbody>
diff --git a/templates/admin/testexp.html b/templates/admin/testexp.html
index 39291fa..052cfe9 100644
--- a/templates/admin/testexp.html
+++ b/templates/admin/testexp.html
@@ -63,7 +63,9 @@
</div>
<div class="span5" id="monitor-logs">
- <span>chal raha hain bhai</span>
+ <strong><p id="username"></p></strong>
+ <p id="log-data" style="word-spacing: 4em"></p>
+ <button class="btn btn-primary" onclick="getLogs()">Refresh Logs</button>
</div>
</div>
@@ -150,7 +152,7 @@
}
});
}
- var mydata;
+
function getTemp() {
var selected_machine = document.getElementsByClassName("highlight");
if (selected_machine.length == 0) {
@@ -167,7 +169,6 @@
});
request.done(function(data){
- mydata = data;
if (data.status_code == 200) {
document.getElementById("temp").value = data.message;
}
@@ -176,6 +177,42 @@
}
});
}
+
+ function getLogs() {
+ var selected_machine = document.getElementsByClassName("highlight");
+ if (selected_machine.length == 0) {
+ alert("Please select a machine first");
+ return;
+ }
+ var selected_mid = selected_machine[0].getElementsByTagName('td')[0].innerHTML;
+ var request = $.ajax({
+ url : BASE_URL + '/admin/monitor',
+ method : 'POST',
+ data : {
+ 'mid' : selected_mid
+ }
+ });
+
+ header = "<th>Time</th><th>Fan</th><th>Heat</th><th>Temp</th>"
+ request.done(function(data){
+ if (data.status_code == 200) {
+ console.log(data.message.logs.replace("/\n/g", "<br/>"));
+ document.getElementById("username").innerHTML = "User : " + data.message.user;
+ document.getElementById("log-data").innerHTML = "<pre>Date Time Heater Fan Temp</pre>" + data.message.logs.replace(new RegExp("\n","g"), "<br/>");
+ }
+ else {
+ alert(data.status_code);
+ }
+ });
+ }
+
+ // update status of ongoing experiments
+ setInterval(function() {
+ var now = new Date();
+ if (now.getMinutes() == 56 || now.getMinutes() == 1) {
+ location.reload();
+ }
+ }, 60000);
</script>
{% endblock %}