diff options
-rw-r--r-- | map_machine_ids.txt | 2 | ||||
-rw-r--r-- | myadmin/urls.py | 4 | ||||
-rw-r--r-- | myadmin/views.py | 124 | ||||
-rw-r--r-- | sbhs_server/sbhs.py | 3 | ||||
-rw-r--r-- | sbhs_server/settings.py | 12 |
5 files changed, 137 insertions, 8 deletions
diff --git a/map_machine_ids.txt b/map_machine_ids.txt index bed4558..7b66f9f 100644 --- a/map_machine_ids.txt +++ b/map_machine_ids.txt @@ -1,6 +1,6 @@ 32=/dev/ttyUSB2 25=/dev/ttyUSB1 -27=/dev/ttyUSB0 +52=/dev/ttyUSB0 15=/dev/ttyUSB30 16=/dev/ttyUSB36 14=/dev/ttyUSB31 diff --git a/myadmin/urls.py b/myadmin/urls.py index 39a24b2..c1831e5 100644 --- a/myadmin/urls.py +++ b/myadmin/urls.py @@ -8,6 +8,10 @@ urlpatterns = [ url(r'^admin/webcam/?$', views.webcam_index, name='admin_webcam'), url(r'^admin/profile/([0-9]+)/?$', views.profile, name='admin_profile'), url(r'^admin/testing/?$', views.testing, name='admin_testing'), + 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/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 37dc7fc..b868c4f 100644 --- a/myadmin/views.py +++ b/myadmin/views.py @@ -1,9 +1,10 @@ from django.shortcuts import render, redirect -from django.http import Http404 +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 -from sbhs_server import settings -import subprocess +from sbhs_server import settings,sbhs +import subprocess,json,serial,os # Create your views here. def checkadmin(req): @@ -79,4 +80,119 @@ def testing(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/testexp.html', {"boards": boards, "allotment_mode": allotment_mode})
\ No newline at end of file + return render(req, 'admin/testexp.html', {"boards": boards, "allotment_mode": allotment_mode}) + +@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} + """ + mid=int(req.POST.get('mid')) + usb_path=settings.MID_PORT_MAP.get(mid,None) + + if usb_path is None: + retVal={"status_code":400,"message":"Invalid MID"} + return HttpResponse(json.dumps(retVal)) + + #trying to connect to device + + # check if SBHS device is connected + if not os.path.exists(usb_path): + retVal={"status_code":500,"message":"Device Not connected to defined USB Port"} + return HttpResponse(json.dumps(retVal)) + + try: + board = sbhs.Sbhs() + board.machine_id=mid + board.boardcon= serial.Serial(port=usb_path, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=2) #orignal stopbits = 1 + board.status = 1 + if board.reset_board(): + retVal={"status_code":200,"message":board.getTemp()} + return HttpResponse(json.dumps(retVal)) + else: + retVal={"status_code":500,"message":"Could not set the parameters.Try again."} + return HttpResponse(json.dumps(retVal)) + except serial.serialutil.SerialException: + retVal={"status_code":500,"message":"Could not connect to the device.Try again."} + return HttpResponse(json.dumps(retVal)) + + +@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} + """ + mid=int(req.POST.get('mid')) + fan=int(req.POST.get('fan')) + heat=int(req.POST.get('heat')) + usb_path=settings.MID_PORT_MAP.get(mid,None) + + if usb_path is None: + retVal={"status_code":400,"message":"Invalid MID"} + return HttpResponse(json.dumps(retVal)) + + #trying to connect to device + + # check if SBHS device is connected + if not os.path.exists(usb_path): + retVal={"status_code":500,"message":"Device Not connected to defined USB Port"} + return HttpResponse(json.dumps(retVal)) + + try: + board = sbhs.Sbhs() + board.machine_id=mid + board.boardcon= serial.Serial(port=usb_path, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=2) #orignal stopbits = 1 + board.status = 1 + if board.setFan(fan) and board.setHeat(heat): + retVal={"status_code":200,"message":board.getTemp()} + return HttpResponse(json.dumps(retVal)) + else: + retVal={"status_code":500,"message":"Could not set the parameters.Try again."} + return HttpResponse(json.dumps(retVal)) + except serial.serialutil.SerialException: + retVal={"status_code":500,"message":"Could not connect to the device.Try again."} + return HttpResponse(json.dumps(retVal)) + +@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} + """ + mid=int(req.POST.get('mid')) + usb_path=settings.MID_PORT_MAP.get(mid,None) + + if usb_path is None: + retVal={"status_code":400,"message":"Invalid MID"} + return HttpResponse(json.dumps(retVal)) + + #trying to connect to device + + # check if SBHS device is connected + if not os.path.exists(usb_path): + retVal={"status_code":500,"message":"Device Not connected to defined USB Port"} + return HttpResponse(json.dumps(retVal)) + + try: + board = sbhs.Sbhs() + board.machine_id=mid + board.boardcon= serial.Serial(port=usb_path, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=2) #orignal stopbits = 1 + board.status = 1 + temp=board.getTemp() + if temp!=0.0: + retVal={"status_code":200,"message":temp} + return HttpResponse(json.dumps(retVal)) + else: + retVal={"status_code":500,"message":"Could not set the parameters.Try again."} + return HttpResponse(json.dumps(retVal)) + except serial.serialutil.SerialException: + retVal={"status_code":500,"message":"Could not connect to the device.Try again."} + return HttpResponse(json.dumps(retVal)) diff --git a/sbhs_server/sbhs.py b/sbhs_server/sbhs.py index 5c64fd7..c9620e9 100644 --- a/sbhs_server/sbhs.py +++ b/sbhs_server/sbhs.py @@ -174,8 +174,7 @@ class Sbhs: return False def reset_board(self): - self.setFan(100) - self.setHeat(0) + return self.setHeat(0) and self.setFan(100) def _read(self, size): try: diff --git a/sbhs_server/settings.py b/sbhs_server/settings.py index ca3d133..b7ec8b7 100644 --- a/sbhs_server/settings.py +++ b/sbhs_server/settings.py @@ -10,6 +10,9 @@ https://docs.djangoproject.com/en/1.6/ref/settings/ import sys #srikant import socket import sbhs_server.credentials as credentials + + + hostname = socket.gethostname() is_production = hostname == "vlabs-Veriton-Series" @@ -32,6 +35,8 @@ TEMPLATE_DEBUG = not is_production ALLOWED_HOSTS = [ "localhost", "127.0.0.1", + "192.168.43.208", + "192.168.43.144", ] if not DEBUG: @@ -57,6 +62,7 @@ INSTALLED_APPS = ( 'undelete', #'yaksh', 'taggit', + #'corsheaders' 'account', 'myadmin', @@ -68,19 +74,21 @@ INSTALLED_APPS = ( 'webcam', ) -MIDDLEWARE_CLASSES = ( +MIDDLEWARE = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + #'corsheaders.middleware.CorsMiddleware', ) ROOT_URLCONF = 'sbhs_server.urls' WSGI_APPLICATION = 'sbhs_server.wsgi.application' +#CORS_ORIGIN_ALLOW_ALL=True # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases @@ -238,10 +246,12 @@ SBHS_GLOBAL_LOG_DIR = os.path.join(BASE_DIR, 'log') from sbhs_server import sbhs boards = {} +MID_PORT_MAP={} with open(os.path.join(BASE_DIR, 'map_machine_ids.txt')) as f: for line in f: try: data = line.split("=") + MID_PORT_MAP[int(data[0])]=data[1].strip() brd = sbhs.Sbhs() b = brd.connect(int(data[0])) assert b == True |