|
|
|
|
|
import json
|
|
|
|
|
|
from django.contrib import messages
|
|
|
from django.utils.safestring import mark_safe
|
|
|
from django.shortcuts import render, redirect, get_object_or_404, HttpResponse
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
|
|
from apps.main.models import Experiment, Device
|
|
|
from apps.main.views import sidebar
|
|
|
|
|
|
from .models import USRPRXConfiguration
|
|
|
from .forms import USRPRXConfigurationForm, USRPRXImportForm
|
|
|
|
|
|
def is_developer(user):
|
|
|
groups = [str(g.name) for g in user.groups.all()]
|
|
|
#return 'Developer' in groups or user.is_staff
|
|
|
return 'Developer' in groups or user.is_superuser
|
|
|
|
|
|
|
|
|
def is_operator(user):
|
|
|
groups = [str(g.name) for g in user.groups.all()]
|
|
|
#return 'Operator' in groups or user.is_staff
|
|
|
return 'Operator' in groups or user.is_superuser
|
|
|
|
|
|
def conf(request, conf_id):
|
|
|
|
|
|
conf = get_object_or_404(USRPRXConfiguration, pk=conf_id)
|
|
|
|
|
|
kwargs = {}
|
|
|
kwargs['dev_conf'] = conf
|
|
|
kwargs['dev_conf_keys'] = ['ip_address_rx', 'daughterboard_rx', 'antenna_rx', 'samplerate_rx', 'frequency_rx', 'datadir', 'clocksource', 'timesource', 'clockrate']
|
|
|
|
|
|
kwargs['title'] = 'Configuration'
|
|
|
kwargs['suptitle'] = 'Detail'
|
|
|
|
|
|
kwargs['button'] = 'Edit Configuration'
|
|
|
|
|
|
conf.status_device()
|
|
|
|
|
|
###### SIDEBAR ######
|
|
|
kwargs.update(sidebar(conf=conf))
|
|
|
|
|
|
return render(request, 'usrp_rx_conf.html', kwargs)
|
|
|
|
|
|
@login_required
|
|
|
def conf_edit(request, conf_id):
|
|
|
|
|
|
conf = get_object_or_404(USRPRXConfiguration, pk=conf_id)
|
|
|
|
|
|
if not is_developer(request.user):
|
|
|
messages.error(request, 'You must be an developer to edit this configuration')
|
|
|
return redirect(conf.get_absolute_url())
|
|
|
|
|
|
if request.method=='GET':
|
|
|
form = USRPRXConfigurationForm(instance=conf)
|
|
|
|
|
|
elif request.method=='POST':
|
|
|
line_data = {}
|
|
|
conf_data = {}
|
|
|
clock_data = {}
|
|
|
extras = []
|
|
|
|
|
|
#classified post fields
|
|
|
for label,value in request.POST.items():
|
|
|
if label=='csrfmiddlewaretoken':
|
|
|
continue
|
|
|
|
|
|
if label.count('|')==0:
|
|
|
if label in ('mode', 'multiplier', 'divisor', 'reference'):
|
|
|
clock_data[label] = value
|
|
|
else:
|
|
|
conf_data[label] = value
|
|
|
continue
|
|
|
|
|
|
elif label.split('|')[0]!='-1':
|
|
|
extras.append(label)
|
|
|
continue
|
|
|
|
|
|
x, pk, name = label.split('|')
|
|
|
|
|
|
if name=='codes':
|
|
|
value = [s for s in value.split('\r\n') if s]
|
|
|
|
|
|
if pk in line_data:
|
|
|
line_data[pk][name] = value
|
|
|
else:
|
|
|
line_data[pk] = {name:value}
|
|
|
|
|
|
form = USRPRXConfigurationForm(conf_data, instance=conf)
|
|
|
|
|
|
if form.is_valid():
|
|
|
form.save()
|
|
|
|
|
|
messages.success(request, 'USRP Rx configuration successfully updated')
|
|
|
|
|
|
return redirect(conf.get_absolute_url())
|
|
|
|
|
|
kwargs = {}
|
|
|
kwargs['dev_conf'] = conf
|
|
|
kwargs['form'] = form
|
|
|
kwargs['edit'] = True
|
|
|
|
|
|
kwargs['title'] = 'USRP Rx Configuration'
|
|
|
kwargs['suptitle'] = 'Edit'
|
|
|
kwargs['button'] = 'Update'
|
|
|
|
|
|
return render(request, 'usrp_rx_conf_edit.html', kwargs)
|
|
|
|
|
|
def import_file(request, conf_id):
|
|
|
|
|
|
conf = get_object_or_404(USRPRXConfiguration, pk=conf_id)
|
|
|
if request.method=='POST':
|
|
|
form = USRPRXImportForm(request.POST, request.FILES)
|
|
|
if form.is_valid():
|
|
|
try:
|
|
|
data = conf.import_from_file(request.FILES['file_name'])
|
|
|
conf.dict_to_parms(data)
|
|
|
conf.update_pulses()
|
|
|
messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name'])
|
|
|
return redirect(conf.get_absolute_url_edit())
|
|
|
|
|
|
except Exception as e:
|
|
|
messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], repr(e)))
|
|
|
else:
|
|
|
messages.warning(request, 'Your current configuration will be replaced')
|
|
|
form = USRPRXImportForm()
|
|
|
|
|
|
kwargs = {}
|
|
|
kwargs['form'] = form
|
|
|
kwargs['title'] = 'USRP Rx Configuration'
|
|
|
kwargs['suptitle'] = 'Import file'
|
|
|
kwargs['button'] = 'Upload'
|
|
|
kwargs['previous'] = conf.get_absolute_url()
|
|
|
|
|
|
return render(request, 'usrp_rx_import.html', kwargs)
|
|
|
|
|
|
def conf_raw(request, conf_id):
|
|
|
conf = get_object_or_404(USRPRXConfiguration, pk=conf_id)
|
|
|
raw = conf.write_device(raw=True)
|
|
|
return HttpResponse(raw, content_type='application/json')
|