summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sbhs/management/commands/update_boards.py40
-rw-r--r--sbhs/views.py43
-rw-r--r--sbhs_server/settings.py5
3 files changed, 45 insertions, 43 deletions
diff --git a/sbhs/management/commands/update_boards.py b/sbhs/management/commands/update_boards.py
index 238646c..849f020 100644
--- a/sbhs/management/commands/update_boards.py
+++ b/sbhs/management/commands/update_boards.py
@@ -9,6 +9,7 @@ from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User, Group, Permission
# local imports
+from django.conf import settings
from sbhs.models import Board
from sbhs.views import map_sbhs_to_rpi
@@ -16,23 +17,26 @@ from sbhs.views import map_sbhs_to_rpi
class Command(BaseCommand):
help = 'Ping all boards and update status of boards'
- def add_arguments(self, parser):
- # Positional arguments
- parser.add_argument('host_ip', type=str)
-
def handle(self, *args, **options):
app_label = 'sbhs'
- try:
- board_check, dead_servers = map_sbhs_to_rpi(options["host_ip"])
- board = Board()
- all_mac_ids = []
- for machines in board_check:
- all_mac_ids.extend(machines["mac_ids"])
- board.switch_off_inactive_boards(all_mac_ids)
- self.stdout.write('Updated Board Status')
- if dead_servers:
- self.stdout.write('Servers {0} are not responding.'\
- .format(", ".join(dead_servers))
- )
- except Exception as e:
- self.stdout.write('Failed updating Board because {0}'.format(e)) \ No newline at end of file
+ if settings.SBHS_API_IPS:
+ try:
+ board_check, dead_servers = map_sbhs_to_rpi()
+ board = Board()
+ all_mac_ids = []
+ for machines in board_check:
+ all_mac_ids.extend(machines["mac_ids"])
+ board.switch_off_inactive_boards(all_mac_ids)
+ self.stdout.write('Updated Board Status')
+ if dead_servers:
+ self.stdout.write('Servers {0} are not responding.'\
+ .format(", ".join(dead_servers))
+ )
+ except Exception as e:
+ self.stdout.write('Failed updating Board because {0}'\
+ .format(e)
+ )
+ else:
+ self.stdout.write('No API IP added in settings.py. '
+ " Please try with adding IPs in the SBHS_API_IPS variable "
+ )
diff --git a/sbhs/views.py b/sbhs/views.py
index 58a3f24..3b6db2f 100644
--- a/sbhs/views.py
+++ b/sbhs/views.py
@@ -404,6 +404,7 @@ def initiation(request):
password = request.POST.get("password")
user = authenticate(username=username, password=password)
if user:
+ login(request, user)
if user.is_active:
now = timezone.now()
slots = Slot.objects.get_user_slots(user).order_by("id")
@@ -459,12 +460,12 @@ def initiation(request):
}
return JsonResponse(message, safe=True, status=200)
-def map_sbhs_to_rpi(client_name):
+def map_sbhs_to_rpi():
"""
Scans if the machine are connected to the rpis.
If the machines are connected map them with their specific rpis.
"""
- r_pis = settings.RASP_PI_IPS
+ r_pis = settings.SBHS_API_IPS
map_machines = []
dead_machines = []
rpi_map = {}
@@ -480,31 +481,21 @@ def map_sbhs_to_rpi(client_name):
map_machines.append(rpi_map.copy())
except:
dead_machines.append(r_pi)
- rpi_map["rpi_ip"] = client_name
- try:
- mac_ids = connect_sbhs(client_name, "get_machine_ids")
- board = Board()
- board.save_board_details(client_name, mac_ids)
- rpi_map["mac_ids"] = [i['sbhs_mac_id'] for i in mac_ids]
- map_machines.append(rpi_map)
- except:
- dead_machines.append(client_name)
return map_machines, dead_machines
def connect_sbhs(rpi_ip, experiment_url):
connect_rpi = requests.get("http://{0}/experiment/{1}".format(
- rpi_ip, experiment_url
- )
- )
+ rpi_ip, experiment_url), timeout=5
+ )
data = json.loads(connect_rpi.text)
return data
+@login_required
@csrf_exempt
def experiment(request):
try:
- username = request.POST.get("username")
+ user = request.user
server_start_ts = int(tm.time() * 1000)
- user = User.objects.get(username=username)
slot = Slot.objects.get_user_slots(user)\
.order_by("start_time").last()
board = UserBoard.objects.get(user=user).board
@@ -788,9 +779,7 @@ def test_boards(request):
raise Http404("You are not allowed to see this page.")
else:
if request.POST.get("update_boards") == "update_boards":
- board_check, dead_servers = map_sbhs_to_rpi(
- request.META["SERVER_NAME"]
- )
+ board_check, dead_servers = map_sbhs_to_rpi()
board = Board()
all_mac_ids = []
for machines in board_check:
@@ -799,17 +788,25 @@ def test_boards(request):
if request.POST.get("reset_all") == "reset_all":
for board in boards:
- resp = connect_sbhs(board.raspi_path,"reset/{0}".format(
+ try:
+ resp = connect_sbhs(board.raspi_path,"reset/{0}".format(
board.usb_id
)
)
+ except requests.exceptions.ConnectionError:
+ if device.raspi_path not in dead_servers:
+ dead_servers.append(device.raspi_path)
all_devices = []
for device in boards:
devices = {}
- temp = connect_sbhs(device.raspi_path,
- "get_temp/{0}".format(device.usb_id)
- )
+ try:
+ temp = connect_sbhs(device.raspi_path,
+ "get_temp/{0}".format(device.usb_id)
+ )
+ except requests.exceptions.ConnectionError:
+ if device.raspi_path not in dead_servers:
+ dead_servers.append(device.raspi_path)
devices["board"] = device
devices["temp"] = temp
all_devices.append(devices)
diff --git a/sbhs_server/settings.py b/sbhs_server/settings.py
index d9ce67b..a3545dc 100644
--- a/sbhs_server/settings.py
+++ b/sbhs_server/settings.py
@@ -179,10 +179,11 @@ PRODUCTION_URL = credentials.PRODUCTION_URL
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
-RASP_PI_IPS = []
+SBHS_API_IPS = []
-# enter comma separated raspberry pi IPs here with ports
# for e.g 127.0.0.1:1234
+# Enter comma separated SBHS API IPs here with ports
+# (if hosted on specific ports)
CLIENT_VERSION = 3
#Client version supported by Django server