@@ -1,1284 +1,1296 | |||
|
1 | 1 | import ast |
|
2 | 2 | import json |
|
3 | 3 | import hashlib |
|
4 | 4 | from datetime import datetime, timedelta |
|
5 | 5 | |
|
6 | 6 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
7 | 7 | from django.utils.safestring import mark_safe |
|
8 | 8 | from django.http import HttpResponseRedirect |
|
9 | 9 | from django.urls import reverse |
|
10 | 10 | from django.db.models import Q |
|
11 | 11 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
|
12 | 12 | from django.contrib import messages |
|
13 | 13 | from django.http.request import QueryDict |
|
14 | 14 | from django.contrib.auth.decorators import login_required, user_passes_test |
|
15 | 15 | |
|
16 | 16 | from django.utils.timezone import is_aware |
|
17 | 17 | |
|
18 | 18 | try: |
|
19 | 19 | from urllib.parse import urlencode |
|
20 | 20 | except ImportError: |
|
21 | 21 | from urllib import urlencode |
|
22 | 22 | |
|
23 | 23 | from .forms import ExperimentForm, ExperimentEditionForm, DeviceForm, ConfigurationForm, UploadFileForm, DownloadFileForm, NewForm |
|
24 | 24 | from .forms import FilterForm, ChangeIpForm |
|
25 | 25 | |
|
26 | 26 | from apps.pedestal.forms import PedestalConfigurationForm, PedestalEditionForm |
|
27 | 27 | from apps.generator.forms import GeneratorConfigurationForm |
|
28 | 28 | from apps.usrp_rx.forms import USRPRXConfigurationForm, USRPRXEditionForm |
|
29 | 29 | from apps.usrp_tx.forms import USRPTXConfigurationForm, USRPTXEditionForm |
|
30 | 30 | from .utils import Params |
|
31 | 31 | |
|
32 | 32 | from .models import Experiment, Device, Configuration, DEV_STATES |
|
33 | 33 | from apps.pedestal.models import PedestalConfiguration |
|
34 | 34 | from apps.generator.models import GeneratorConfiguration |
|
35 | 35 | from apps.usrp_rx.models import USRPRXConfiguration |
|
36 | 36 | from apps.usrp_tx.models import USRPTXConfiguration |
|
37 | 37 | from apps.usrp_tx.validations import validation_usrp_tx_code |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | #comentario test |
|
41 | 41 | CONF_FORMS = { |
|
42 | 42 | 'pedestal': PedestalConfigurationForm, |
|
43 | 43 | 'generator': GeneratorConfigurationForm, |
|
44 | 44 | 'usrp_rx': USRPRXConfigurationForm, |
|
45 | 45 | 'usrp_tx': USRPTXConfigurationForm, |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | CONF_MODELS = { |
|
49 | 49 | 'pedestal': PedestalConfiguration, |
|
50 | 50 | 'generator': GeneratorConfiguration, |
|
51 | 51 | 'usrp_rx': USRPRXConfiguration, |
|
52 | 52 | 'usrp_tx': USRPTXConfiguration, |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | MIX_MODES = { |
|
56 | 56 | '0': 'P', |
|
57 | 57 | '1': 'S', |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | MIX_OPERATIONS = { |
|
61 | 61 | '0': 'OR', |
|
62 | 62 | '1': 'XOR', |
|
63 | 63 | '2': 'AND', |
|
64 | 64 | '3': 'NAND', |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | def is_developer(user): |
|
69 | 69 | |
|
70 | 70 | groups = [str(g.name) for g in user.groups.all()] |
|
71 | 71 | return 'Developer' in groups or user.is_staff |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | def is_operator(user): |
|
75 | 75 | |
|
76 | 76 | groups = [str(g.name) for g in user.groups.all()] |
|
77 | 77 | return 'Operator' in groups or user.is_staff |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | def has_been_modified(model): |
|
81 | 81 | |
|
82 | 82 | prev_hash = model.hash |
|
83 | 83 | new_hash = hashlib.sha256(str(model.parms_to_dict).encode()).hexdigest() |
|
84 | 84 | if prev_hash != new_hash: |
|
85 | 85 | model.hash = new_hash |
|
86 | 86 | model.save() |
|
87 | 87 | return True |
|
88 | 88 | return False |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | def index(request): |
|
92 | 92 | kwargs = {'no_sidebar': True} |
|
93 | 93 | |
|
94 | 94 | return render(request, 'index.html', kwargs) |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | def devices(request): |
|
98 | 98 | |
|
99 | 99 | page = request.GET.get('page') |
|
100 | 100 | order = ('device_type',) |
|
101 | 101 | |
|
102 | 102 | filters = request.GET.copy() |
|
103 | 103 | kwargs = get_paginator(Device, page, order, filters) |
|
104 | 104 | form = FilterForm(initial=request.GET, extra_fields=['tags']) |
|
105 | 105 | |
|
106 | 106 | kwargs['keys'] = ['device_type', |
|
107 | 107 | 'ip_address', 'port_address', 'actions'] |
|
108 | 108 | kwargs['title'] = 'Device' |
|
109 | 109 | kwargs['suptitle'] = 'List' |
|
110 | 110 | kwargs['no_sidebar'] = True |
|
111 | 111 | kwargs['form'] = form |
|
112 | 112 | kwargs['add_url'] = reverse('url_add_device') |
|
113 | 113 | filters.pop('page', None) |
|
114 | 114 | kwargs['q'] = urlencode(filters) |
|
115 | 115 | kwargs['menu_devices'] = 'active' |
|
116 | 116 | return render(request, 'base_list.html', kwargs) |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | def device(request, id_dev): |
|
120 | 120 | |
|
121 | 121 | device = get_object_or_404(Device, pk=id_dev) |
|
122 | 122 | |
|
123 | 123 | kwargs = {} |
|
124 | 124 | kwargs['device'] = device |
|
125 | 125 | kwargs['device_keys'] = ['device_type', |
|
126 | 126 | 'ip_address', 'port_address', 'description'] |
|
127 | 127 | |
|
128 | 128 | kwargs['title'] = 'Device' |
|
129 | 129 | kwargs['suptitle'] = 'Details' |
|
130 | 130 | kwargs['menu_devices'] = 'active' |
|
131 | 131 | |
|
132 | 132 | return render(request, 'device.html', kwargs) |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | @login_required |
|
136 | 136 | def device_new(request): |
|
137 | 137 | |
|
138 | 138 | if request.method == 'GET': |
|
139 | 139 | form = DeviceForm() |
|
140 | 140 | |
|
141 | 141 | if request.method == 'POST': |
|
142 | 142 | form = DeviceForm(request.POST) |
|
143 | 143 | |
|
144 | 144 | if form.is_valid(): |
|
145 | 145 | form.save() |
|
146 | 146 | return redirect('url_devices') |
|
147 | 147 | |
|
148 | 148 | kwargs = {} |
|
149 | 149 | kwargs['form'] = form |
|
150 | 150 | kwargs['title'] = 'Device' |
|
151 | 151 | kwargs['suptitle'] = 'New' |
|
152 | 152 | kwargs['button'] = 'Create' |
|
153 | 153 | kwargs['menu_devices'] = 'active' |
|
154 | 154 | |
|
155 | 155 | return render(request, 'base_edit.html', kwargs) |
|
156 | 156 | |
|
157 | 157 | |
|
158 | 158 | @login_required |
|
159 | 159 | def device_edit(request, id_dev): |
|
160 | 160 | |
|
161 | 161 | device = get_object_or_404(Device, pk=id_dev) |
|
162 | 162 | |
|
163 | 163 | if request.method == 'GET': |
|
164 | 164 | form = DeviceForm(instance=device) |
|
165 | 165 | |
|
166 | 166 | if request.method == 'POST': |
|
167 | 167 | form = DeviceForm(request.POST, instance=device) |
|
168 | 168 | |
|
169 | 169 | if form.is_valid(): |
|
170 | 170 | form.save() |
|
171 | 171 | return redirect(device.get_absolute_url()) |
|
172 | 172 | |
|
173 | 173 | kwargs = {} |
|
174 | 174 | kwargs['form'] = form |
|
175 | 175 | kwargs['title'] = 'Device' |
|
176 | 176 | kwargs['suptitle'] = 'Edit' |
|
177 | 177 | kwargs['button'] = 'Update' |
|
178 | 178 | kwargs['menu_devices'] = 'active' |
|
179 | 179 | |
|
180 | 180 | return render(request, 'base_edit.html', kwargs) |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | @login_required |
|
184 | 184 | def device_delete(request, id_dev): |
|
185 | 185 | |
|
186 | 186 | device = get_object_or_404(Device, pk=id_dev) |
|
187 | 187 | |
|
188 | 188 | if request.method == 'POST': |
|
189 | 189 | |
|
190 | 190 | if is_developer(request.user): |
|
191 | 191 | device.delete() |
|
192 | 192 | return redirect('url_devices') |
|
193 | 193 | |
|
194 | 194 | messages.error(request, 'Not enough permission to delete this object') |
|
195 | 195 | return redirect(device.get_absolute_url()) |
|
196 | 196 | |
|
197 | 197 | kwargs = { |
|
198 | 198 | 'title': 'Delete', |
|
199 | 199 | 'suptitle': 'Device', |
|
200 | 200 | 'object': device, |
|
201 | 201 | 'delete': True |
|
202 | 202 | } |
|
203 | 203 | kwargs['menu_devices'] = 'active' |
|
204 | 204 | |
|
205 | 205 | return render(request, 'confirm.html', kwargs) |
|
206 | 206 | |
|
207 | 207 | |
|
208 | 208 | @login_required |
|
209 | 209 | def device_change_ip(request, id_dev): |
|
210 | 210 | |
|
211 | 211 | device = get_object_or_404(Device, pk=id_dev) |
|
212 | 212 | |
|
213 | 213 | if request.method == 'POST': |
|
214 | 214 | |
|
215 | 215 | if is_developer(request.user): |
|
216 | 216 | device.change_ip(**request.POST.dict()) |
|
217 | 217 | level, message = device.message.split('|') |
|
218 | 218 | messages.add_message(request, level, message) |
|
219 | 219 | else: |
|
220 | 220 | messages.error( |
|
221 | 221 | request, 'Not enough permission to delete this object') |
|
222 | 222 | return redirect(device.get_absolute_url()) |
|
223 | 223 | |
|
224 | 224 | kwargs = { |
|
225 | 225 | 'title': 'Device', |
|
226 | 226 | 'suptitle': 'Change IP', |
|
227 | 227 | 'object': device, |
|
228 | 228 | 'previous': device.get_absolute_url(), |
|
229 | 229 | 'form': ChangeIpForm(initial={'ip_address': device.ip_address}), |
|
230 | 230 | 'message': ' ', |
|
231 | 231 | } |
|
232 | 232 | kwargs['menu_devices'] = 'active' |
|
233 | 233 | |
|
234 | 234 | return render(request, 'confirm.html', kwargs) |
|
235 | 235 | |
|
236 | 236 | |
|
237 | 237 | def experiments(request): |
|
238 | 238 | |
|
239 | 239 | page = request.GET.get('page') |
|
240 | 240 | order = ('id',) |
|
241 | 241 | filters = request.GET.copy() |
|
242 | 242 | |
|
243 | 243 | if 'my experiments' in filters: |
|
244 | 244 | filters.pop('my experiments', None) |
|
245 | 245 | filters['mine'] = request.user.id |
|
246 | 246 | |
|
247 | 247 | kwargs = get_paginator(Experiment, page, order, filters) |
|
248 | 248 | |
|
249 | 249 | fields = ['tags'] |
|
250 | 250 | if request.user.is_authenticated: |
|
251 | 251 | fields.append('my experiments') |
|
252 | 252 | |
|
253 | 253 | form = FilterForm(initial=request.GET, extra_fields=fields) |
|
254 | 254 | |
|
255 | 255 | kwargs['keys'] = ['name', 'pedestal', 'reception_rx', 'transmission_tx', 'actions'] |
|
256 | 256 | kwargs['title'] = 'Experiment' |
|
257 | 257 | kwargs['suptitle'] = 'List' |
|
258 | 258 | kwargs['no_sidebar'] = True |
|
259 | 259 | kwargs['form'] = form |
|
260 | 260 | kwargs['add_url'] = reverse('url_add_experiment') |
|
261 | 261 | filters = request.GET.copy() |
|
262 | 262 | filters.pop('page', None) |
|
263 | 263 | kwargs['q'] = urlencode(filters) |
|
264 | 264 | kwargs['menu_experiments'] = 'active' |
|
265 | 265 | |
|
266 | 266 | return render(request, 'base_list.html', kwargs) |
|
267 | 267 | |
|
268 | 268 | |
|
269 | 269 | def experiment(request, id_exp): |
|
270 | 270 | |
|
271 | 271 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
272 | 272 | id_p = experiment.pedestal_id |
|
273 | 273 | id_rx = experiment.reception_rx_id |
|
274 | 274 | id_tx = experiment.transmission_tx_id |
|
275 | 275 | conf_pedestal = PedestalConfiguration.objects.get(id = id_p) |
|
276 | 276 | conf_rx = USRPRXConfiguration.objects.get(id = id_rx) |
|
277 | 277 | conf_tx = USRPTXConfiguration.objects.get(id = id_tx) |
|
278 | 278 | |
|
279 | 279 | kwargs = {} |
|
280 | 280 | kwargs['experiment_keys'] = ['name', 'latitude', 'longitude', 'heading'] |
|
281 | 281 | kwargs['experiment'] = experiment |
|
282 | kwargs['experiment_pedestal_keys'] = ['mode', 'axis', 'speed', 'angle'] | |
|
282 | ||
|
283 | if conf_pedestal.mode == 'position': | |
|
284 | kwargs['experiment_pedestal_keys'] = ['mode', 'axis', 'angle'] | |
|
285 | elif conf_pedestal.mode == 'speed': | |
|
286 | kwargs['experiment_pedestal_keys'] = ['mode', 'axis', 'speed'] | |
|
287 | else: | |
|
288 | kwargs['experiment_pedestal_keys'] = ['mode', 'axis', 'speed', 'angle', 'min_value', 'max_value'] | |
|
283 | 289 | kwargs['experiment_pedestal'] = conf_pedestal |
|
284 | kwargs['experiment_rx_keys'] = ['samplerate_rx', 'frequency_rx', 'datadir', 'clocksource', 'timesource', 'clockrate'] | |
|
290 | ||
|
291 | kwargs['experiment_rx_keys'] = ['ip_address_rx', 'daughterboard_rx', 'antenna_rx', 'samplerate_rx', 'frequency_rx', 'datadir', 'clocksource', 'timesource', 'clockrate'] | |
|
285 | 292 | kwargs['experiment_rx'] = conf_rx |
|
286 | kwargs['experiment_tx_keys'] = ['frequency', 'samplerate', 'ipp', 'delay', 'pulse_1', 'code_type_1', 'code_1', 'repetitions_1', | |
|
287 | 'enable_2', 'pulse_2', 'code_type_2', 'code_2', 'repetitions_2'] | |
|
293 | ||
|
294 | if not conf_tx.enable_2: | |
|
295 | kwargs['experiment_tx_keys'] = ['ip_address', 'daughterboard', 'antenna', 'frequency', 'samplerate', 'ipp', 'delay', 'pulse_1', 'code_type_1', 'code_1', 'repetitions_1'] | |
|
296 | else: | |
|
297 | kwargs['experiment_tx_keys'] = ['ip_address', 'daughterboard', 'antenna', 'frequency', 'samplerate', 'ipp', 'delay', 'pulse_1', 'code_type_1', 'code_1', 'repetitions_1', | |
|
298 | 'pulse_2', 'code_type_2', 'code_2', 'repetitions_2'] | |
|
288 | 299 | kwargs['experiment_tx'] = conf_tx |
|
300 | ||
|
289 | 301 | kwargs['title'] = 'Experiment' |
|
290 | 302 | kwargs['suptitle'] = 'Details' |
|
291 | 303 | kwargs['button'] = 'Add Configuration' |
|
292 | 304 | kwargs['menu_experiments'] = 'active' |
|
293 | 305 | |
|
294 | 306 | ###### SIDEBAR ###### |
|
295 | 307 | kwargs.update(sidebar(confs=[conf_pedestal, conf_rx, conf_tx])) |
|
296 | 308 | |
|
297 | 309 | return render(request, 'experiment.html', kwargs) |
|
298 | 310 | |
|
299 | 311 | |
|
300 | 312 | @login_required |
|
301 | 313 | def experiment_new(request, id_camp=None): |
|
302 | 314 | |
|
303 | 315 | if not is_developer(request.user): |
|
304 | 316 | messages.error( |
|
305 | 317 | request, 'Developer required, to create new Experiments') |
|
306 | 318 | return redirect('index') |
|
307 | 319 | kwargs = {} |
|
308 | 320 | |
|
309 | 321 | if request.method == 'GET': |
|
310 | 322 | kwargs['button'] = 'Create' |
|
311 | 323 | form = ExperimentForm() |
|
312 | 324 | |
|
313 | 325 | if request.method == 'POST': |
|
314 | 326 | form = ExperimentForm(request.POST) |
|
315 | 327 | if form.is_valid(): |
|
316 | 328 | experiment = form.save(commit=False) |
|
317 | 329 | experiment.author = request.user |
|
318 | 330 | experiment.save() |
|
319 | 331 | messages.success(request, 'Experiment configuration successfully created') |
|
320 | 332 | return redirect('url_experiment', id_exp=experiment.id) |
|
321 | 333 | |
|
322 | 334 | kwargs['form'] = form |
|
323 | 335 | kwargs['title'] = 'Experiment' |
|
324 | 336 | kwargs['suptitle'] = 'New' |
|
325 | 337 | kwargs['menu_experiments'] = 'active' |
|
326 | 338 | |
|
327 | 339 | return render(request, 'experiment_edit.html', kwargs) |
|
328 | 340 | |
|
329 | 341 | |
|
330 | 342 | @login_required |
|
331 | 343 | def experiment_edit(request, id_exp): |
|
332 | 344 | |
|
333 | 345 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
334 | 346 | id_p = experiment.pedestal_id |
|
335 | 347 | id_rx = experiment.reception_rx_id |
|
336 | 348 | id_tx = experiment.transmission_tx_id |
|
337 | 349 | conf_pedestal = PedestalConfiguration.objects.get(id = id_p) |
|
338 | 350 | conf_rx = USRPRXConfiguration.objects.get(id = id_rx) |
|
339 | 351 | conf_tx = USRPTXConfiguration.objects.get(id = id_tx) |
|
340 | 352 | |
|
341 | 353 | if request.method == 'GET': |
|
342 | 354 | form = ExperimentEditionForm(instance=experiment) |
|
343 | 355 | form_pedestal = PedestalEditionForm(instance=conf_pedestal) |
|
344 | 356 | form_rx = USRPRXEditionForm(instance=conf_rx) |
|
345 | 357 | form_tx = USRPTXEditionForm(instance=conf_tx) |
|
346 | 358 | |
|
347 | 359 | if request.method == 'POST': |
|
348 | 360 | form = ExperimentEditionForm(request.POST, instance=experiment) |
|
349 | 361 | form_pedestal = PedestalEditionForm(request.POST, instance=conf_pedestal) |
|
350 | 362 | form_rx = USRPRXEditionForm(request.POST, instance=conf_rx) |
|
351 | 363 | form_tx = USRPTXEditionForm(request.POST, instance=conf_tx) |
|
352 | 364 | |
|
353 | 365 | if form.is_valid() and form_pedestal.is_valid() and form_rx.is_valid() and form_tx.is_valid(): |
|
354 | 366 | experiment = form.save(commit=False) |
|
355 | 367 | pedestal = form_pedestal.save(commit=False) |
|
356 | 368 | rx = form_rx.save(commit=False) |
|
357 | 369 | tx = form_tx.save(commit=False) |
|
358 | 370 | |
|
359 | 371 | pedestal.save() |
|
360 | 372 | rx.save() |
|
361 | 373 | validation_usrp_tx_code(request, tx) |
|
362 | 374 | tx.save() |
|
363 | 375 | messages.success(request, 'Experiment configuration successfully updated') |
|
364 | 376 | return redirect('url_experiment', id_exp=experiment.id) |
|
365 | 377 | |
|
366 | 378 | kwargs = {} |
|
367 | 379 | kwargs['form'] = form |
|
368 | 380 | kwargs['form_pedestal'] = form_pedestal |
|
369 | 381 | kwargs['form_rx'] = form_rx |
|
370 | 382 | kwargs['form_tx'] = form_tx |
|
371 | 383 | kwargs['title'] = 'Experiment' |
|
372 | 384 | kwargs['suptitle'] = 'Edit' |
|
373 | 385 | kwargs['button'] = 'Update' |
|
374 | 386 | kwargs['menu_experiments'] = 'active' |
|
375 | 387 | |
|
376 | 388 | return render(request, 'experiment_edit.html', kwargs) |
|
377 | 389 | |
|
378 | 390 | |
|
379 | 391 | @login_required |
|
380 | 392 | def experiment_delete(request, id_exp): |
|
381 | 393 | |
|
382 | 394 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
383 | 395 | |
|
384 | 396 | if request.method == 'POST': |
|
385 | 397 | if is_developer(request.user): |
|
386 | 398 | #for conf in Configuration.objects.filter(experiment=experiment): |
|
387 | 399 | #conf.delete() |
|
388 | 400 | experiment.delete() |
|
389 | 401 | return redirect('url_experiments') |
|
390 | 402 | |
|
391 | 403 | messages.error(request, 'Not enough permission to delete this object') |
|
392 | 404 | return redirect(experiment.get_absolute_url()) |
|
393 | 405 | |
|
394 | 406 | kwargs = { |
|
395 | 407 | 'title': 'Delete', |
|
396 | 408 | 'suptitle': 'Experiment', |
|
397 | 409 | 'object': experiment, |
|
398 | 410 | 'delete': True |
|
399 | 411 | } |
|
400 | 412 | |
|
401 | 413 | return render(request, 'confirm.html', kwargs) |
|
402 | 414 | |
|
403 | 415 | |
|
404 | 416 | @login_required |
|
405 | 417 | def experiment_export(request, id_exp): |
|
406 | 418 | |
|
407 | 419 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
408 | 420 | content = experiment.parms_to_dict() |
|
409 | 421 | content_type = 'application/json' |
|
410 | 422 | filename = '%s_%s.json' % (experiment.name, experiment.id) |
|
411 | 423 | |
|
412 | 424 | response = HttpResponse(content_type=content_type) |
|
413 | 425 | response['Content-Disposition'] = 'attachment; filename="%s"' % filename |
|
414 | 426 | response.write(json.dumps(content, indent=2)) |
|
415 | 427 | |
|
416 | 428 | return response |
|
417 | 429 | |
|
418 | 430 | |
|
419 | 431 | @login_required |
|
420 | 432 | def experiment_import(request, id_exp): |
|
421 | 433 | |
|
422 | 434 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
423 | 435 | configurations = Configuration.objects.filter(experiment=experiment) |
|
424 | 436 | |
|
425 | 437 | if request.method == 'GET': |
|
426 | 438 | file_form = UploadFileForm() |
|
427 | 439 | |
|
428 | 440 | if request.method == 'POST': |
|
429 | 441 | file_form = UploadFileForm(request.POST, request.FILES) |
|
430 | 442 | |
|
431 | 443 | if file_form.is_valid(): |
|
432 | 444 | new_exp = experiment.dict_to_parms( |
|
433 | 445 | json.load(request.FILES['file']), CONF_MODELS) |
|
434 | 446 | messages.success( |
|
435 | 447 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
436 | 448 | return redirect(new_exp.get_absolute_url_edit()) |
|
437 | 449 | |
|
438 | 450 | messages.error(request, "Could not import parameters from file") |
|
439 | 451 | |
|
440 | 452 | kwargs = {} |
|
441 | 453 | kwargs['title'] = 'Experiment' |
|
442 | 454 | kwargs['form'] = file_form |
|
443 | 455 | kwargs['suptitle'] = 'Importing file' |
|
444 | 456 | kwargs['button'] = 'Import' |
|
445 | 457 | kwargs['menu_experiments'] = 'active' |
|
446 | 458 | |
|
447 | 459 | kwargs.update(sidebar(experiment=experiment)) |
|
448 | 460 | |
|
449 | 461 | return render(request, 'experiment_import.html', kwargs) |
|
450 | 462 | |
|
451 | 463 | |
|
452 | 464 | @login_required |
|
453 | 465 | def experiment_start(request, id_exp): |
|
454 | 466 | |
|
455 | 467 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
456 | 468 | exp.status = 0 |
|
457 | 469 | if exp.status == 2: |
|
458 | 470 | messages.warning(request, 'Experiment {} already runnnig'.format(exp)) |
|
459 | 471 | else: |
|
460 | 472 | exp.status = exp.start() |
|
461 | 473 | if exp.status == 0: |
|
462 | 474 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
463 | 475 | if exp.status == 2: |
|
464 | 476 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
465 | 477 | |
|
466 | 478 | exp.save() |
|
467 | 479 | |
|
468 | 480 | return redirect(exp.get_absolute_url()) |
|
469 | 481 | |
|
470 | 482 | |
|
471 | 483 | @login_required |
|
472 | 484 | def experiment_stop(request, id_exp): |
|
473 | 485 | |
|
474 | 486 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
475 | 487 | |
|
476 | 488 | if exp.status == 2 or exp.status == 4: |
|
477 | 489 | exp.status = exp.stop() |
|
478 | 490 | exp.save() |
|
479 | 491 | messages.success(request, 'Experiment {} stopped'.format(exp)) |
|
480 | 492 | else: |
|
481 | 493 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
482 | 494 | |
|
483 | 495 | return redirect(exp.get_absolute_url()) |
|
484 | 496 | |
|
485 | 497 | |
|
486 | 498 | def experiment_status(request, id_exp): |
|
487 | 499 | |
|
488 | 500 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
489 | 501 | |
|
490 | 502 | exp.get_status() |
|
491 | 503 | |
|
492 | 504 | return redirect(exp.get_absolute_url()) |
|
493 | 505 | |
|
494 | 506 | |
|
495 | 507 | def experiment_summary(request, id_exp): |
|
496 | 508 | |
|
497 | 509 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
498 | 510 | configurations = Configuration.objects.filter( |
|
499 | 511 | experiment=experiment, type=0) |
|
500 | 512 | |
|
501 | 513 | kwargs = {} |
|
502 | 514 | kwargs['experiment_keys'] = ['radar_system', |
|
503 | 515 | 'name', 'freq', 'start_time', 'end_time'] |
|
504 | 516 | kwargs['experiment'] = experiment |
|
505 | 517 | kwargs['configurations'] = [] |
|
506 | 518 | kwargs['title'] = 'Experiment Summary' |
|
507 | 519 | kwargs['suptitle'] = 'Details' |
|
508 | 520 | kwargs['button'] = 'Verify Parameters' |
|
509 | 521 | |
|
510 | 522 | c_vel = 3.0*(10**8) # m/s |
|
511 | 523 | ope_freq = experiment.freq*(10**6) # 1/s |
|
512 | 524 | radar_lambda = c_vel/ope_freq # m |
|
513 | 525 | kwargs['radar_lambda'] = radar_lambda |
|
514 | 526 | |
|
515 | 527 | ipp = None |
|
516 | 528 | nsa = 1 |
|
517 | 529 | code_id = 0 |
|
518 | 530 | tx_line = {} |
|
519 | 531 | |
|
520 | 532 | for configuration in configurations.filter(device__device_type__name = 'pedestal'): |
|
521 | 533 | |
|
522 | 534 | if configuration.mix: |
|
523 | 535 | continue |
|
524 | 536 | conf = {'conf': configuration} |
|
525 | 537 | conf['keys'] = [] |
|
526 | 538 | conf['NTxs'] = configuration.ntx |
|
527 | 539 | conf['keys'].append('NTxs') |
|
528 | 540 | ipp = configuration.ipp |
|
529 | 541 | conf['IPP'] = ipp |
|
530 | 542 | conf['keys'].append('IPP') |
|
531 | 543 | lines = configuration.get_lines(line_type__name='tx') |
|
532 | 544 | |
|
533 | 545 | for tx_line in lines: |
|
534 | 546 | tx_params = json.loads(tx_line.params) |
|
535 | 547 | conf[tx_line.get_name()] = '{} Km'.format(tx_params['pulse_width']) |
|
536 | 548 | conf['keys'].append(tx_line.get_name()) |
|
537 | 549 | delays = tx_params['delays'] |
|
538 | 550 | if delays not in ('', '0'): |
|
539 | 551 | n = len(delays.split(',')) |
|
540 | 552 | taus = '{} Taus: {}'.format(n, delays) |
|
541 | 553 | else: |
|
542 | 554 | taus = '-' |
|
543 | 555 | conf['Taus ({})'.format(tx_line.get_name())] = taus |
|
544 | 556 | conf['keys'].append('Taus ({})'.format(tx_line.get_name())) |
|
545 | 557 | for code_line in configuration.get_lines(line_type__name='codes'): |
|
546 | 558 | code_params = json.loads(code_line.params) |
|
547 | 559 | code_id = code_params['code'] |
|
548 | 560 | if tx_line.pk == int(code_params['TX_ref']): |
|
549 | 561 | conf['Code ({})'.format(tx_line.get_name())] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), |
|
550 | 562 | '-'.join(code_params['codes'])) |
|
551 | 563 | conf['keys'].append('Code ({})'.format(tx_line.get_name())) |
|
552 | 564 | |
|
553 | 565 | for windows_line in configuration.get_lines(line_type__name='windows'): |
|
554 | 566 | win_params = json.loads(windows_line.params) |
|
555 | 567 | if tx_line.pk == int(win_params['TX_ref']): |
|
556 | 568 | windows = '' |
|
557 | 569 | nsa = win_params['params'][0]['number_of_samples'] |
|
558 | 570 | for i, params in enumerate(win_params['params']): |
|
559 | 571 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format( |
|
560 | 572 | i, **params) |
|
561 | 573 | conf['Window'] = mark_safe(windows) |
|
562 | 574 | conf['keys'].append('Window') |
|
563 | 575 | |
|
564 | 576 | kwargs['configurations'].append(conf) |
|
565 | 577 | |
|
566 | 578 | for configuration in configurations.filter(device__device_type__name = 'jars'): |
|
567 | 579 | |
|
568 | 580 | conf = {'conf': configuration} |
|
569 | 581 | conf['keys'] = [] |
|
570 | 582 | conf['Type of Data'] = EXPERIMENT_TYPE[configuration.exp_type][1] |
|
571 | 583 | conf['keys'].append('Type of Data') |
|
572 | 584 | channels_number = configuration.channels_number |
|
573 | 585 | exp_type = configuration.exp_type |
|
574 | 586 | fftpoints = configuration.fftpoints |
|
575 | 587 | filter_parms = json.loads(configuration.filter_parms) |
|
576 | 588 | spectral_number = configuration.spectral_number |
|
577 | 589 | acq_profiles = configuration.acq_profiles |
|
578 | 590 | cohe_integr = configuration.cohe_integr |
|
579 | 591 | profiles_block = configuration.profiles_block |
|
580 | 592 | |
|
581 | 593 | conf['Num of Profiles'] = acq_profiles |
|
582 | 594 | conf['keys'].append('Num of Profiles') |
|
583 | 595 | |
|
584 | 596 | conf['Prof per Block'] = profiles_block |
|
585 | 597 | conf['keys'].append('Prof per Block') |
|
586 | 598 | |
|
587 | 599 | conf['Blocks per File'] = configuration.raw_data_blocks |
|
588 | 600 | conf['keys'].append('Blocks per File') |
|
589 | 601 | |
|
590 | 602 | if exp_type == 0: # Short |
|
591 | 603 | bytes_ = 2 |
|
592 | 604 | b = nsa*2*bytes_*channels_number |
|
593 | 605 | else: # Float |
|
594 | 606 | bytes_ = 4 |
|
595 | 607 | channels = channels_number + spectral_number |
|
596 | 608 | b = nsa*2*bytes_*fftpoints*channels |
|
597 | 609 | |
|
598 | 610 | codes_num = 7 |
|
599 | 611 | if code_id == 2: |
|
600 | 612 | codes_num = 7 |
|
601 | 613 | elif code_id == 12: |
|
602 | 614 | codes_num = 15 |
|
603 | 615 | |
|
604 | 616 | #Jars filter values: |
|
605 | 617 | |
|
606 | 618 | clock = float(filter_parms['clock']) |
|
607 | 619 | filter_2 = int(filter_parms['cic_2']) |
|
608 | 620 | filter_5 = int(filter_parms['cic_5']) |
|
609 | 621 | filter_fir = int(filter_parms['fir']) |
|
610 | 622 | Fs_MHz = clock/(filter_2*filter_5*filter_fir) |
|
611 | 623 | |
|
612 | 624 | #Jars values: |
|
613 | 625 | if ipp is not None: |
|
614 | 626 | IPP_units = ipp/0.15*Fs_MHz |
|
615 | 627 | IPP_us = IPP_units / Fs_MHz |
|
616 | 628 | IPP_s = IPP_units / (Fs_MHz * (10**6)) |
|
617 | 629 | Ts = 1/(Fs_MHz*(10**6)) |
|
618 | 630 | |
|
619 | 631 | Va = radar_lambda/(4*Ts*cohe_integr) |
|
620 | 632 | rate_bh = ((nsa-codes_num)*channels_number*2 * |
|
621 | 633 | bytes_/IPP_us)*(36*(10**8)/cohe_integr) |
|
622 | 634 | rate_gh = rate_bh/(1024*1024*1024) |
|
623 | 635 | |
|
624 | 636 | conf['Time per Block'] = IPP_s * profiles_block * cohe_integr |
|
625 | 637 | conf['keys'].append('Time per Block') |
|
626 | 638 | conf['Acq time'] = IPP_s * acq_profiles |
|
627 | 639 | conf['keys'].append('Acq time') |
|
628 | 640 | conf['Data rate'] = str(rate_gh)+" (GB/h)" |
|
629 | 641 | conf['keys'].append('Data rate') |
|
630 | 642 | conf['Va (m/s)'] = Va |
|
631 | 643 | conf['keys'].append('Va (m/s)') |
|
632 | 644 | conf['Vrange (m/s)'] = 3/(2*IPP_s*cohe_integr) |
|
633 | 645 | conf['keys'].append('Vrange (m/s)') |
|
634 | 646 | |
|
635 | 647 | kwargs['configurations'].append(conf) |
|
636 | 648 | kwargs['menu_experiments'] = 'active' |
|
637 | 649 | |
|
638 | 650 | ###### SIDEBAR ###### |
|
639 | 651 | kwargs.update(sidebar(experiment=experiment)) |
|
640 | 652 | |
|
641 | 653 | return render(request, 'experiment_summary.html', kwargs) |
|
642 | 654 | |
|
643 | 655 | |
|
644 | 656 | @login_required |
|
645 | 657 | def experiment_verify(request, id_exp): |
|
646 | 658 | |
|
647 | 659 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
648 | 660 | experiment_data = experiment.parms_to_dict() |
|
649 | 661 | configurations = Configuration.objects.filter( |
|
650 | 662 | experiment=experiment, type=0) |
|
651 | 663 | |
|
652 | 664 | kwargs = {} |
|
653 | 665 | |
|
654 | 666 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
655 | 667 | kwargs['experiment'] = experiment |
|
656 | 668 | |
|
657 | 669 | kwargs['configuration_keys'] = ['name', 'device__ip_address', |
|
658 | 670 | 'device__port_address', 'device__status'] |
|
659 | 671 | kwargs['configurations'] = configurations |
|
660 | 672 | kwargs['experiment_data'] = experiment_data |
|
661 | 673 | |
|
662 | 674 | kwargs['title'] = 'Verify Experiment' |
|
663 | 675 | kwargs['suptitle'] = 'Parameters' |
|
664 | 676 | |
|
665 | 677 | kwargs['button'] = 'Update' |
|
666 | 678 | |
|
667 | 679 | jars_conf = False |
|
668 | 680 | rc_conf = False |
|
669 | 681 | dds_conf = False |
|
670 | 682 | |
|
671 | 683 | for configuration in configurations: |
|
672 | 684 | #-------------------- JARS -----------------------: |
|
673 | 685 | if configuration.device.device_type.name == 'jars': |
|
674 | 686 | jars_conf = True |
|
675 | 687 | jars = configuration |
|
676 | 688 | kwargs['jars_conf'] = jars_conf |
|
677 | 689 | filter_parms = json.loads(jars.filter_parms) |
|
678 | 690 | kwargs['filter_parms'] = filter_parms |
|
679 | 691 | #--Sampling Frequency |
|
680 | 692 | clock = filter_parms['clock'] |
|
681 | 693 | filter_2 = filter_parms['cic_2'] |
|
682 | 694 | filter_5 = filter_parms['cic_5'] |
|
683 | 695 | filter_fir = filter_parms['fir'] |
|
684 | 696 | samp_freq_jars = clock/filter_2/filter_5/filter_fir |
|
685 | 697 | |
|
686 | 698 | kwargs['samp_freq_jars'] = samp_freq_jars |
|
687 | 699 | kwargs['jars'] = configuration |
|
688 | 700 | |
|
689 | 701 | #--------------------- RC ----------------------: |
|
690 | 702 | if configuration.device.device_type.name == 'pedestal' and not configuration.mix: |
|
691 | 703 | rc_conf = True |
|
692 | 704 | rc = configuration |
|
693 | 705 | |
|
694 | 706 | rc_parms = configuration.parms_to_dict() |
|
695 | 707 | |
|
696 | 708 | win_lines = rc.get_lines(line_type__name='windows') |
|
697 | 709 | if win_lines: |
|
698 | 710 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] |
|
699 | 711 | #--Sampling Frequency |
|
700 | 712 | samp_freq_rc = 0.15/dh |
|
701 | 713 | kwargs['samp_freq_rc'] = samp_freq_rc |
|
702 | 714 | |
|
703 | 715 | kwargs['rc_conf'] = rc_conf |
|
704 | 716 | kwargs['rc'] = configuration |
|
705 | 717 | |
|
706 | 718 | #-------------------- DDS ----------------------: |
|
707 | 719 | if configuration.device.device_type.name == 'dds': |
|
708 | 720 | dds_conf = True |
|
709 | 721 | dds = configuration |
|
710 | 722 | dds_parms = configuration.parms_to_dict() |
|
711 | 723 | |
|
712 | 724 | kwargs['dds_conf'] = dds_conf |
|
713 | 725 | kwargs['dds'] = configuration |
|
714 | 726 | |
|
715 | 727 | #------------Validation------------: |
|
716 | 728 | #Clock |
|
717 | 729 | if dds_conf and rc_conf and jars_conf: |
|
718 | 730 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) and float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): |
|
719 | 731 | messages.warning(request, "Devices don't have the same clock.") |
|
720 | 732 | elif rc_conf and jars_conf: |
|
721 | 733 | if float(filter_parms['clock']) != float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']): |
|
722 | 734 | messages.warning(request, "Devices don't have the same clock.") |
|
723 | 735 | elif rc_conf and dds_conf: |
|
724 | 736 | if float(rc_parms['configurations']['byId'][str(rc.pk)]['clock_in']) != float(dds_parms['configurations']['byId'][str(dds.pk)]['clock']): |
|
725 | 737 | messages.warning(request, "Devices don't have the same clock.") |
|
726 | 738 | if float(samp_freq_rc) != float(dds_parms['configurations']['byId'][str(dds.pk)]['frequencyA']): |
|
727 | 739 | messages.warning( |
|
728 | 740 | request, "Devices don't have the same Frequency A.") |
|
729 | 741 | |
|
730 | 742 | #------------POST METHOD------------: |
|
731 | 743 | if request.method == 'POST': |
|
732 | 744 | if request.POST['suggest_clock']: |
|
733 | 745 | try: |
|
734 | 746 | suggest_clock = float(request.POST['suggest_clock']) |
|
735 | 747 | except: |
|
736 | 748 | messages.warning(request, "Invalid value in CLOCK IN.") |
|
737 | 749 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
738 | 750 | else: |
|
739 | 751 | suggest_clock = "" |
|
740 | 752 | if suggest_clock: |
|
741 | 753 | if rc_conf: |
|
742 | 754 | rc.clock_in = suggest_clock |
|
743 | 755 | rc.save() |
|
744 | 756 | if jars_conf: |
|
745 | 757 | filter_parms = jars.filter_parms |
|
746 | 758 | filter_parms = ast.literal_eval(filter_parms) |
|
747 | 759 | filter_parms['clock'] = suggest_clock |
|
748 | 760 | jars.filter_parms = json.dumps(filter_parms) |
|
749 | 761 | jars.save() |
|
750 | 762 | kwargs['filter_parms'] = filter_parms |
|
751 | 763 | if dds_conf: |
|
752 | 764 | dds.clock = suggest_clock |
|
753 | 765 | dds.save() |
|
754 | 766 | |
|
755 | 767 | if request.POST['suggest_frequencyA']: |
|
756 | 768 | try: |
|
757 | 769 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) |
|
758 | 770 | except: |
|
759 | 771 | messages.warning(request, "Invalid value in FREQUENCY A.") |
|
760 | 772 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
761 | 773 | else: |
|
762 | 774 | suggest_frequencyA = "" |
|
763 | 775 | if suggest_frequencyA: |
|
764 | 776 | if jars_conf: |
|
765 | 777 | filter_parms = jars.filter_parms |
|
766 | 778 | filter_parms = ast.literal_eval(filter_parms) |
|
767 | 779 | filter_parms['fch'] = suggest_frequencyA |
|
768 | 780 | jars.filter_parms = json.dumps(filter_parms) |
|
769 | 781 | jars.save() |
|
770 | 782 | kwargs['filter_parms'] = filter_parms |
|
771 | 783 | if dds_conf: |
|
772 | 784 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] |
|
773 | 785 | dds.save() |
|
774 | 786 | |
|
775 | 787 | kwargs['menu_experiments'] = 'active' |
|
776 | 788 | kwargs.update(sidebar(experiment=experiment)) |
|
777 | 789 | return render(request, 'experiment_verify.html', kwargs) |
|
778 | 790 | |
|
779 | 791 | |
|
780 | 792 | def dev_confs(request): |
|
781 | 793 | |
|
782 | 794 | page = request.GET.get('page') |
|
783 | 795 | order = ('-programmed_date', ) |
|
784 | 796 | filters = request.GET.copy() |
|
785 | 797 | if 'my configurations' in filters: |
|
786 | 798 | filters.pop('my configurations', None) |
|
787 | 799 | filters['mine'] = request.user.id |
|
788 | 800 | kwargs = get_paginator(Configuration, page, order, filters) |
|
789 | 801 | fields = ['tags', 'historical'] |
|
790 | 802 | if request.user.is_authenticated: |
|
791 | 803 | fields.append('my configurations') |
|
792 | 804 | form = FilterForm(initial=request.GET, extra_fields=fields) |
|
793 | 805 | kwargs['keys'] = ['name', 'device', |
|
794 | 806 | 'type', 'programmed_date', 'actions'] |
|
795 | 807 | kwargs['title'] = 'Configuration' |
|
796 | 808 | kwargs['suptitle'] = 'List' |
|
797 | 809 | kwargs['no_sidebar'] = True |
|
798 | 810 | kwargs['form'] = form |
|
799 | 811 | kwargs['add_url'] = reverse('url_add_dev_conf', args=[0]) |
|
800 | 812 | filters = request.GET.copy() |
|
801 | 813 | filters.pop('page', None) |
|
802 | 814 | kwargs['q'] = urlencode(filters) |
|
803 | 815 | kwargs['menu_configurations'] = 'active' |
|
804 | 816 | |
|
805 | 817 | return render(request, 'base_list.html', kwargs) |
|
806 | 818 | |
|
807 | 819 | |
|
808 | 820 | def dev_conf(request, id_conf): |
|
809 | 821 | |
|
810 | 822 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
811 | 823 | |
|
812 | 824 | return redirect(conf.get_absolute_url()) |
|
813 | 825 | |
|
814 | 826 | |
|
815 | 827 | @login_required |
|
816 | 828 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
817 | 829 | |
|
818 | 830 | if not is_developer(request.user): |
|
819 | 831 | messages.error( |
|
820 | 832 | request, 'Developer required, to create new configurations') |
|
821 | 833 | return redirect('index') |
|
822 | 834 | |
|
823 | 835 | initial = {} |
|
824 | 836 | kwargs = {} |
|
825 | 837 | |
|
826 | 838 | if id_exp != 0: |
|
827 | 839 | initial['experiment'] = id_exp |
|
828 | 840 | |
|
829 | 841 | if id_dev != 0: |
|
830 | 842 | initial['device'] = id_dev |
|
831 | 843 | |
|
832 | 844 | if request.method == 'GET': |
|
833 | 845 | |
|
834 | 846 | if id_dev: |
|
835 | 847 | kwargs['button'] = 'Create' |
|
836 | 848 | device = Device.objects.get(pk=id_dev) |
|
837 | 849 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
838 | 850 | initial['name'] = request.GET['name'] |
|
839 | 851 | form = DevConfForm(initial=initial) |
|
840 | 852 | else: |
|
841 | 853 | kwargs['button'] = 'Create' |
|
842 | 854 | form = ConfigurationForm(initial=initial) |
|
843 | 855 | |
|
844 | 856 | if request.method == 'POST': |
|
845 | 857 | |
|
846 | 858 | device = Device.objects.get(pk=request.POST['device']) |
|
847 | 859 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
848 | 860 | |
|
849 | 861 | form = DevConfForm(request.POST) |
|
850 | 862 | kwargs['button'] = 'Create' |
|
851 | 863 | if form.is_valid(): |
|
852 | 864 | conf = form.save(commit=False) |
|
853 | 865 | |
|
854 | 866 | if device.device_type.name == 'usrp_tx': |
|
855 | 867 | validation_usrp_tx_code(request, conf) |
|
856 | 868 | |
|
857 | 869 | conf.save() |
|
858 | 870 | conf.author = request.user |
|
859 | 871 | |
|
860 | 872 | messages.success(request, device.device_type.name + ' configuration successfully created') |
|
861 | 873 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
862 | 874 | |
|
863 | 875 | kwargs['id_exp'] = id_exp |
|
864 | 876 | kwargs['form'] = form |
|
865 | 877 | kwargs['title'] = 'Configuration' |
|
866 | 878 | kwargs['suptitle'] = 'New' |
|
867 | 879 | kwargs['menu_configurations'] = 'active' |
|
868 | 880 | |
|
869 | 881 | if id_dev != 0: |
|
870 | 882 | device = Device.objects.get(pk=id_dev) |
|
871 | 883 | kwargs['device'] = device.device_type.name |
|
872 | 884 | return render(request, 'dev_conf_edit.html', kwargs) |
|
873 | 885 | |
|
874 | 886 | |
|
875 | 887 | @login_required |
|
876 | 888 | def dev_conf_edit(request, id_conf): |
|
877 | 889 | |
|
878 | 890 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
879 | 891 | |
|
880 | 892 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
881 | 893 | |
|
882 | 894 | if request.method == 'GET': |
|
883 | 895 | form = DevConfForm(instance=conf) |
|
884 | 896 | |
|
885 | 897 | if request.method == 'POST': |
|
886 | 898 | form = DevConfForm(request.POST, instance=conf) |
|
887 | 899 | |
|
888 | 900 | if form.is_valid(): |
|
889 | 901 | form.save() |
|
890 | 902 | return redirect('url_dev_conf', id_conf=id_conf) |
|
891 | 903 | |
|
892 | 904 | kwargs = {} |
|
893 | 905 | kwargs['form'] = form |
|
894 | 906 | kwargs['title'] = 'Device Configuration' |
|
895 | 907 | kwargs['suptitle'] = 'Edit' |
|
896 | 908 | kwargs['button'] = 'Update' |
|
897 | 909 | kwargs['menu_configurations'] = 'active' |
|
898 | 910 | |
|
899 | 911 | ###### SIDEBAR ###### |
|
900 | 912 | kwargs.update(sidebar(conf=conf)) |
|
901 | 913 | |
|
902 | 914 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
903 | 915 | |
|
904 | 916 | |
|
905 | 917 | @login_required |
|
906 | 918 | def dev_conf_start(request, id_conf): |
|
907 | 919 | |
|
908 | 920 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
909 | 921 | |
|
910 | 922 | if conf.start_device(): |
|
911 | 923 | messages.success(request, conf.message) |
|
912 | 924 | else: |
|
913 | 925 | messages.error(request, conf.message) |
|
914 | 926 | |
|
915 | 927 | #conf.status_device() |
|
916 | 928 | |
|
917 | 929 | return redirect(conf.get_absolute_url()) |
|
918 | 930 | |
|
919 | 931 | |
|
920 | 932 | @login_required |
|
921 | 933 | def dev_conf_stop(request, id_conf): |
|
922 | 934 | |
|
923 | 935 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
924 | 936 | |
|
925 | 937 | if conf.stop_device(): |
|
926 | 938 | messages.success(request, conf.message) |
|
927 | 939 | else: |
|
928 | 940 | messages.error(request, conf.message) |
|
929 | 941 | |
|
930 | 942 | #conf.status_device() |
|
931 | 943 | |
|
932 | 944 | return redirect(conf.get_absolute_url()) |
|
933 | 945 | |
|
934 | 946 | |
|
935 | 947 | @login_required |
|
936 | 948 | def dev_conf_status(request, id_conf): |
|
937 | 949 | |
|
938 | 950 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
939 | 951 | |
|
940 | 952 | conf_active = Configuration.objects.filter(pk=conf.device.conf_active).first() |
|
941 | 953 | if conf_active!=conf: |
|
942 | 954 | url = '#' if conf_active is None else conf_active.get_absolute_url() |
|
943 | 955 | label = 'None' if conf_active is None else conf_active.label |
|
944 | 956 | messages.warning( |
|
945 | 957 | request, |
|
946 | 958 | mark_safe('The current configuration has not been written to device, the active configuration is <a href="{}">{}</a>'.format( |
|
947 | 959 | url, |
|
948 | 960 | label |
|
949 | 961 | )) |
|
950 | 962 | ) |
|
951 | 963 | |
|
952 | 964 | return redirect(conf.get_absolute_url()) |
|
953 | 965 | |
|
954 | 966 | if conf.status_device(): |
|
955 | 967 | messages.success(request, conf.message) |
|
956 | 968 | else: |
|
957 | 969 | messages.error(request, conf.message) |
|
958 | 970 | |
|
959 | 971 | return redirect(conf.get_absolute_url()) |
|
960 | 972 | |
|
961 | 973 | |
|
962 | 974 | @login_required |
|
963 | 975 | def dev_conf_reset(request, id_conf): |
|
964 | 976 | |
|
965 | 977 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
966 | 978 | |
|
967 | 979 | if conf.reset_device(): |
|
968 | 980 | messages.success(request, conf.message) |
|
969 | 981 | else: |
|
970 | 982 | messages.error(request, conf.message) |
|
971 | 983 | |
|
972 | 984 | return redirect(conf.get_absolute_url()) |
|
973 | 985 | |
|
974 | 986 | |
|
975 | 987 | @login_required |
|
976 | 988 | def dev_conf_write(request, id_conf): |
|
977 | 989 | |
|
978 | 990 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
979 | 991 | |
|
980 | 992 | if request.method == 'POST': |
|
981 | 993 | if conf.write_device(): |
|
982 | 994 | conf.device.conf_active = conf.pk |
|
983 | 995 | conf.device.save() |
|
984 | 996 | messages.success(request, conf.message) |
|
985 | 997 | if has_been_modified(conf): |
|
986 | 998 | conf.clone(type=1) |
|
987 | 999 | else: |
|
988 | 1000 | messages.error(request, conf.message) |
|
989 | 1001 | |
|
990 | 1002 | return redirect(get_object_or_404(Configuration, pk=id_conf).get_absolute_url()) |
|
991 | 1003 | |
|
992 | 1004 | kwargs = { |
|
993 | 1005 | 'title': 'Write Configuration', |
|
994 | 1006 | 'suptitle': conf.label, |
|
995 | 1007 | 'message': 'Are you sure yo want to write this {} configuration?'.format(conf.device), |
|
996 | 1008 | 'delete': False |
|
997 | 1009 | } |
|
998 | 1010 | kwargs['menu_configurations'] = 'active' |
|
999 | 1011 | |
|
1000 | 1012 | return render(request, 'confirm.html', kwargs) |
|
1001 | 1013 | |
|
1002 | 1014 | |
|
1003 | 1015 | @login_required |
|
1004 | 1016 | def dev_conf_read(request, id_conf): |
|
1005 | 1017 | |
|
1006 | 1018 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1007 | 1019 | |
|
1008 | 1020 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1009 | 1021 | |
|
1010 | 1022 | if request.method == 'GET': |
|
1011 | 1023 | parms = conf.read_device() |
|
1012 | 1024 | #conf.status_device() |
|
1013 | 1025 | |
|
1014 | 1026 | if not parms: |
|
1015 | 1027 | messages.error(request, conf.message) |
|
1016 | 1028 | return redirect(conf.get_absolute_url()) |
|
1017 | 1029 | |
|
1018 | 1030 | form = DevConfForm(initial=parms, instance=conf) |
|
1019 | 1031 | |
|
1020 | 1032 | if request.method == 'POST': |
|
1021 | 1033 | form = DevConfForm(request.POST, instance=conf) |
|
1022 | 1034 | |
|
1023 | 1035 | if form.is_valid(): |
|
1024 | 1036 | form.save() |
|
1025 | 1037 | return redirect(conf.get_absolute_url()) |
|
1026 | 1038 | |
|
1027 | 1039 | messages.error(request, "Parameters could not be saved") |
|
1028 | 1040 | |
|
1029 | 1041 | kwargs = {} |
|
1030 | 1042 | kwargs['id_dev'] = conf.id |
|
1031 | 1043 | kwargs['form'] = form |
|
1032 | 1044 | kwargs['title'] = 'Device Configuration' |
|
1033 | 1045 | kwargs['suptitle'] = 'Parameters read from device' |
|
1034 | 1046 | kwargs['button'] = 'Save' |
|
1035 | 1047 | |
|
1036 | 1048 | ###### SIDEBAR ###### |
|
1037 | 1049 | kwargs.update(sidebar(conf=conf)) |
|
1038 | 1050 | |
|
1039 | 1051 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1040 | 1052 | |
|
1041 | 1053 | |
|
1042 | 1054 | @login_required |
|
1043 | 1055 | def dev_conf_import(request, id_conf): |
|
1044 | 1056 | |
|
1045 | 1057 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1046 | 1058 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1047 | 1059 | |
|
1048 | 1060 | if request.method == 'GET': |
|
1049 | 1061 | file_form = UploadFileForm() |
|
1050 | 1062 | |
|
1051 | 1063 | if request.method == 'POST': |
|
1052 | 1064 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1053 | 1065 | |
|
1054 | 1066 | if file_form.is_valid(): |
|
1055 | 1067 | |
|
1056 | 1068 | data = conf.import_from_file(request.FILES['file']) |
|
1057 | 1069 | parms = Params(data=data).get_conf( |
|
1058 | 1070 | dtype=conf.device.device_type.name) |
|
1059 | 1071 | |
|
1060 | 1072 | if parms: |
|
1061 | 1073 | |
|
1062 | 1074 | form = DevConfForm(initial=parms, instance=conf) |
|
1063 | 1075 | |
|
1064 | 1076 | kwargs = {} |
|
1065 | 1077 | kwargs['id_dev'] = conf.id |
|
1066 | 1078 | kwargs['form'] = form |
|
1067 | 1079 | kwargs['title'] = 'Device Configuration' |
|
1068 | 1080 | kwargs['suptitle'] = 'Parameters imported' |
|
1069 | 1081 | kwargs['button'] = 'Save' |
|
1070 | 1082 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1071 | 1083 | kwargs['previous'] = conf.get_absolute_url() |
|
1072 | 1084 | |
|
1073 | 1085 | ###### SIDEBAR ###### |
|
1074 | 1086 | kwargs.update(sidebar(conf=conf)) |
|
1075 | 1087 | |
|
1076 | 1088 | messages.success( |
|
1077 | 1089 | request, "Parameters imported from: '%s'." % request.FILES['file'].name) |
|
1078 | 1090 | |
|
1079 | 1091 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1080 | 1092 | |
|
1081 | 1093 | messages.error(request, "Could not import parameters from file") |
|
1082 | 1094 | |
|
1083 | 1095 | kwargs = {} |
|
1084 | 1096 | kwargs['id_dev'] = conf.id |
|
1085 | 1097 | kwargs['title'] = 'Device Configuration' |
|
1086 | 1098 | kwargs['form'] = file_form |
|
1087 | 1099 | kwargs['suptitle'] = 'Importing file' |
|
1088 | 1100 | kwargs['button'] = 'Import' |
|
1089 | 1101 | kwargs['menu_configurations'] = 'active' |
|
1090 | 1102 | |
|
1091 | 1103 | kwargs.update(sidebar(conf=conf)) |
|
1092 | 1104 | |
|
1093 | 1105 | return render(request, 'dev_conf_import.html', kwargs) |
|
1094 | 1106 | |
|
1095 | 1107 | |
|
1096 | 1108 | @login_required |
|
1097 | 1109 | def dev_conf_export(request, id_conf): |
|
1098 | 1110 | |
|
1099 | 1111 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1100 | 1112 | |
|
1101 | 1113 | if request.method == 'GET': |
|
1102 | 1114 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1103 | 1115 | |
|
1104 | 1116 | if request.method == 'POST': |
|
1105 | 1117 | file_form = DownloadFileForm( |
|
1106 | 1118 | conf.device.device_type.name, request.POST) |
|
1107 | 1119 | |
|
1108 | 1120 | if file_form.is_valid(): |
|
1109 | 1121 | fields = conf.export_to_file( |
|
1110 | 1122 | format=file_form.cleaned_data['format']) |
|
1111 | 1123 | |
|
1112 | 1124 | if not fields['content']: |
|
1113 | 1125 | messages.error(request, conf.message) |
|
1114 | 1126 | return redirect(conf.get_absolute_url_export()) |
|
1115 | 1127 | response = HttpResponse(content_type=fields['content_type']) |
|
1116 | 1128 | response['Content-Disposition'] = 'attachment; filename="%s"' % fields['filename'] |
|
1117 | 1129 | response.write(fields['content']) |
|
1118 | 1130 | |
|
1119 | 1131 | return response |
|
1120 | 1132 | |
|
1121 | 1133 | messages.error(request, "Could not export parameters") |
|
1122 | 1134 | |
|
1123 | 1135 | kwargs = {} |
|
1124 | 1136 | kwargs['id_dev'] = conf.id |
|
1125 | 1137 | kwargs['title'] = 'Device Configuration' |
|
1126 | 1138 | kwargs['form'] = file_form |
|
1127 | 1139 | kwargs['suptitle'] = 'Exporting file' |
|
1128 | 1140 | kwargs['button'] = 'Export' |
|
1129 | 1141 | kwargs['menu_configurations'] = 'active' |
|
1130 | 1142 | |
|
1131 | 1143 | return render(request, 'dev_conf_export.html', kwargs) |
|
1132 | 1144 | |
|
1133 | 1145 | |
|
1134 | 1146 | @login_required |
|
1135 | 1147 | def dev_conf_delete(request, id_conf): |
|
1136 | 1148 | |
|
1137 | 1149 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1138 | 1150 | |
|
1139 | 1151 | if request.method == 'POST': |
|
1140 | 1152 | if is_developer(request.user): |
|
1141 | 1153 | try: |
|
1142 | 1154 | conf.delete() |
|
1143 | 1155 | except Exception as e: |
|
1144 | 1156 | messages.error(request, "The device configuration is protected") |
|
1145 | 1157 | return redirect('url_dev_confs') |
|
1146 | 1158 | |
|
1147 | 1159 | messages.error(request, 'Not enough permission to delete this object') |
|
1148 | 1160 | return redirect(conf.get_absolute_url()) |
|
1149 | 1161 | |
|
1150 | 1162 | kwargs = { |
|
1151 | 1163 | 'title': 'Delete', |
|
1152 | 1164 | 'suptitle': 'Configuration', |
|
1153 | 1165 | 'object': conf, |
|
1154 | 1166 | 'delete': True |
|
1155 | 1167 | } |
|
1156 | 1168 | kwargs['menu_configurations'] = 'active' |
|
1157 | 1169 | |
|
1158 | 1170 | return render(request, 'confirm.html', kwargs) |
|
1159 | 1171 | |
|
1160 | 1172 | |
|
1161 | 1173 | def sidebar(**kwargs): |
|
1162 | 1174 | |
|
1163 | 1175 | side_data = {} |
|
1164 | 1176 | |
|
1165 | 1177 | conf = kwargs.get('conf', None) |
|
1166 | 1178 | confs = kwargs.get('confs', None) |
|
1167 | 1179 | experiment = kwargs.get('experiment', None) |
|
1168 | 1180 | |
|
1169 | 1181 | if confs: |
|
1170 | 1182 | side_data['side_configurations'] = confs |
|
1171 | 1183 | |
|
1172 | 1184 | if experiment: |
|
1173 | 1185 | side_data['experiment'] = experiment |
|
1174 | 1186 | experiments = [experiment] |
|
1175 | 1187 | #configurations = experiment.configuration_set.filter(type=0) |
|
1176 | 1188 | side_data['side_experiments'] = experiments |
|
1177 | 1189 | #side_data['side_configurations'] = configurations.order_by('device__device_type__name') |
|
1178 | 1190 | |
|
1179 | 1191 | return side_data |
|
1180 | 1192 | |
|
1181 | 1193 | |
|
1182 | 1194 | def get_paginator(model, page, order, filters={}, n=8): |
|
1183 | 1195 | |
|
1184 | 1196 | kwargs = {} |
|
1185 | 1197 | query = Q() |
|
1186 | 1198 | if isinstance(filters, QueryDict): |
|
1187 | 1199 | filters = filters.dict() |
|
1188 | 1200 | [filters.pop(key) for key in list(filters) if filters[key] in ('', ' ')] |
|
1189 | 1201 | filters.pop('page', None) |
|
1190 | 1202 | |
|
1191 | 1203 | fields = [f.name for f in model._meta.get_fields()] |
|
1192 | 1204 | |
|
1193 | 1205 | if 'historical' in filters: |
|
1194 | 1206 | filters.pop('historical') |
|
1195 | 1207 | filters['type'] = 1 |
|
1196 | 1208 | elif 'type' in fields: |
|
1197 | 1209 | filters['type'] = 0 |
|
1198 | 1210 | if 'start_date' in filters: |
|
1199 | 1211 | filters['start_date__gte'] = filters.pop('start_date') |
|
1200 | 1212 | if 'end_date' in filters: |
|
1201 | 1213 | filters['start_date__lte'] = filters.pop('end_date') |
|
1202 | 1214 | if 'tags' in filters: |
|
1203 | 1215 | tags = filters.pop('tags') |
|
1204 | 1216 | if 'tags' in fields: |
|
1205 | 1217 | query = query | Q(tags__icontains=tags) |
|
1206 | 1218 | if 'label' in fields: |
|
1207 | 1219 | query = query | Q(label__icontains=tags) |
|
1208 | 1220 | if 'device' in fields: |
|
1209 | 1221 | query = query | Q(device__device_type__name__icontains=tags) |
|
1210 | 1222 | if 'device_type' in fields: |
|
1211 | 1223 | query = query | Q(device_type__name__icontains=tags) |
|
1212 | 1224 | |
|
1213 | 1225 | if 'mine' in filters: |
|
1214 | 1226 | filters['author_id'] = filters['mine'] |
|
1215 | 1227 | filters.pop('mine') |
|
1216 | 1228 | object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1217 | 1229 | paginator = Paginator(object_list, n) |
|
1218 | 1230 | |
|
1219 | 1231 | try: |
|
1220 | 1232 | objects = paginator.page(page) |
|
1221 | 1233 | except PageNotAnInteger: |
|
1222 | 1234 | objects = paginator.page(1) |
|
1223 | 1235 | except EmptyPage: |
|
1224 | 1236 | objects = paginator.page(paginator.num_pages) |
|
1225 | 1237 | |
|
1226 | 1238 | kwargs['objects'] = objects |
|
1227 | 1239 | kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1228 | 1240 | |
|
1229 | 1241 | return kwargs |
|
1230 | 1242 | |
|
1231 | 1243 | @login_required |
|
1232 | 1244 | def revoke_tasks(request, id_camp): |
|
1233 | 1245 | |
|
1234 | 1246 | i = app.control.inspect() |
|
1235 | 1247 | scheduled = list(i.scheduled().values())[0] |
|
1236 | 1248 | revoked = list(i.revoked().values())[0] |
|
1237 | 1249 | |
|
1238 | 1250 | for t in scheduled: |
|
1239 | 1251 | if t['request']['id'] in revoked: |
|
1240 | 1252 | continue |
|
1241 | 1253 | app.control.revoke(t['request']['id']) |
|
1242 | 1254 | exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) |
|
1243 | 1255 | eta = t['eta'] |
|
1244 | 1256 | task = t['request']['name'].split('.')[-1] |
|
1245 | 1257 | messages.warning(request, 'Scheduled {} at {} for experiment {} revoked'.format(task, eta, exp.name)) |
|
1246 | 1258 | |
|
1247 | 1259 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1248 | 1260 | |
|
1249 | 1261 | @login_required |
|
1250 | 1262 | def show_tasks(request, id_camp): |
|
1251 | 1263 | |
|
1252 | 1264 | i = app.control.inspect() |
|
1253 | 1265 | scheduled = list(i.scheduled().values())[0] |
|
1254 | 1266 | revoked = list(i.revoked().values())[0] |
|
1255 | 1267 | |
|
1256 | 1268 | for t in scheduled: |
|
1257 | 1269 | if t['request']['id'] in revoked: |
|
1258 | 1270 | continue |
|
1259 | 1271 | exp = Experiment.objects.get(pk=eval(str(t['request']['args']))[0]) |
|
1260 | 1272 | eta = t['eta'] |
|
1261 | 1273 | task = t['request']['name'].split('.')[-1] |
|
1262 | 1274 | messages.success(request, 'Task {} scheduled at {} for experiment {}'.format(task, eta, exp.name)) |
|
1263 | 1275 | |
|
1264 | 1276 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1265 | 1277 | |
|
1266 | 1278 | def real_time(request): |
|
1267 | 1279 | |
|
1268 | 1280 | graphic_path = "/home/fiorella/Pictures/catwbeanie.jpg" |
|
1269 | 1281 | |
|
1270 | 1282 | kwargs = {} |
|
1271 | 1283 | kwargs['title'] = 'CLAIRE' |
|
1272 | 1284 | kwargs['suptitle'] = 'Real Time' |
|
1273 | 1285 | kwargs['no_sidebar'] = True |
|
1274 | 1286 | kwargs['graphic_path'] = graphic_path |
|
1275 | 1287 | kwargs['graphic1_path'] = 'http://www.bluemaize.net/im/girls-accessories/shark-beanie-11.jpg' |
|
1276 | 1288 | |
|
1277 | 1289 | return render(request, 'real_time.html', kwargs) |
|
1278 | 1290 | |
|
1279 | 1291 | def theme(request, theme): |
|
1280 | 1292 | |
|
1281 | 1293 | user = request.user |
|
1282 | 1294 | user.profile.theme = theme |
|
1283 | 1295 | user.save() |
|
1284 | 1296 | return redirect('index') |
@@ -1,273 +1,277 | |||
|
1 | 1 | import ast |
|
2 | 2 | import json |
|
3 | 3 | import requests |
|
4 | 4 | import base64 |
|
5 | 5 | import struct |
|
6 | 6 | from struct import pack |
|
7 | 7 | import time |
|
8 | 8 | from django.contrib import messages |
|
9 | 9 | from django.db import models |
|
10 | 10 | from django.urls import reverse |
|
11 | 11 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
12 | 12 | |
|
13 | 13 | from apps.main.models import Configuration |
|
14 | 14 | |
|
15 | 15 | MODE_VALUE = ( |
|
16 | 16 | ('position', 'Position'), |
|
17 | 17 | ('speed', 'Speed'), |
|
18 | 18 | ('table', 'Table') |
|
19 | 19 | ) |
|
20 | 20 | |
|
21 | 21 | class PedestalConfiguration(Configuration): |
|
22 | 22 | |
|
23 | 23 | mode = models.CharField( |
|
24 | 24 | verbose_name='Mode', |
|
25 | 25 | max_length=10, |
|
26 | 26 | choices=MODE_VALUE, |
|
27 | 27 | null=False, |
|
28 | 28 | blank=False |
|
29 | 29 | ) |
|
30 | 30 | |
|
31 | 31 | axis = models.CharField( |
|
32 | 32 | verbose_name="Axis", |
|
33 | 33 | max_length=100, |
|
34 | 34 | blank=False, |
|
35 | 35 | null=False, |
|
36 | 36 | help_text="Please separate the values with commas when using table mode" |
|
37 | 37 | ) |
|
38 | 38 | |
|
39 | 39 | speed = models.CharField( |
|
40 | 40 | verbose_name='Speed', |
|
41 | 41 | max_length=100, |
|
42 | 42 | blank=True, |
|
43 | 43 | null=True |
|
44 | 44 | ) |
|
45 | 45 | |
|
46 | 46 | angle = models.CharField( |
|
47 | 47 | verbose_name="Angle(s)", |
|
48 | 48 | max_length=100, |
|
49 | 49 | blank=True, |
|
50 | 50 | null=True, |
|
51 | 51 | help_text="Please separate the values with commas when using table mode" |
|
52 | 52 | ) |
|
53 | 53 | |
|
54 | 54 | min_value = models.FloatField( |
|
55 | 55 | verbose_name='Min angle', |
|
56 | 56 | validators=[MinValueValidator(-5), MaxValueValidator(185)], |
|
57 | 57 | blank=True, |
|
58 | 58 | null=True |
|
59 | 59 | ) |
|
60 | 60 | |
|
61 | 61 | max_value = models.FloatField( |
|
62 | 62 | verbose_name='Max angle', |
|
63 | 63 | validators=[MinValueValidator(-5), MaxValueValidator(185)], |
|
64 | 64 | blank=True, |
|
65 | 65 | null=True |
|
66 | 66 | ) |
|
67 | 67 | |
|
68 | 68 | class Meta: |
|
69 | 69 | db_table = 'pedestal_configurations' |
|
70 | 70 | |
|
71 | 71 | def __str__(self): |
|
72 | 72 | if self.mode=='position': |
|
73 | 73 | return u'Position: {}ΒΊ {}'.format(self.angle, self.axis.upper()) |
|
74 | 74 | if self.mode=='speed': |
|
75 | 75 | return u'Speed: {}ΒΊ/s {}'.format(self.speed, self.axis.upper()) |
|
76 | 76 | if self.mode=='table': |
|
77 | 77 | axis = [x.strip().upper() for x in self.axis.split(',')] |
|
78 | 78 | speeds = [float(x.strip()) for x in self.speed.split(',')] |
|
79 | 79 | table = [float(x.strip()) for x in self.angle.split(',')] |
|
80 | 80 | return u'Table: Axis {}, Speed {}ΒΊ/s, Steps {}'.format(axis, speeds, table) |
|
81 | 81 | |
|
82 | 82 | @property |
|
83 | 83 | def label(self): |
|
84 | 84 | return str(self) |
|
85 | 85 | |
|
86 | 86 | def get_absolute_url_plot(self): |
|
87 | 87 | return reverse('url_plot_pedestal_pulses', args=[str(self.id)]) |
|
88 | 88 | |
|
89 | 89 | def request(self, cmd, method='get', **kwargs): |
|
90 | 90 | |
|
91 | 91 | req = getattr(requests, method)(self.device.url(cmd), **kwargs) |
|
92 | 92 | payload = req.json() |
|
93 | 93 | |
|
94 | 94 | return payload |
|
95 | 95 | |
|
96 | 96 | def status_device(self): |
|
97 | 97 | |
|
98 | 98 | try: |
|
99 | 99 | payload = requests.get(self.device.url()) |
|
100 | 100 | |
|
101 | 101 | if payload: |
|
102 | 102 | self.device.status = 1 |
|
103 | 103 | elif payload['status']=='disable': |
|
104 | 104 | self.device.status = 2 |
|
105 | 105 | else: |
|
106 | 106 | self.device.status = 1 |
|
107 | 107 | self.device.save() |
|
108 | 108 | self.message = 'Pedestal status: {}'.format(payload['status']) |
|
109 | 109 | return False |
|
110 | 110 | except Exception as e: |
|
111 | 111 | if 'No route to host' not in str(e): |
|
112 | 112 | self.device.status = 4 |
|
113 | 113 | self.device.save() |
|
114 | 114 | self.message = 'Pedestal status: {}'.format(str(e)) |
|
115 | 115 | return False |
|
116 | 116 | |
|
117 | 117 | self.device.save() |
|
118 | 118 | return True |
|
119 | 119 | |
|
120 | 120 | def reset_device(self, axi, angle): |
|
121 | 121 | |
|
122 | 122 | try: |
|
123 | 123 | url = self.device.url() + "position?params=" |
|
124 | 124 | |
|
125 | 125 | payload_el = {'axis': 'elevation'} |
|
126 | 126 | payload_az = {'axis': 'azimuth'} |
|
127 | 127 | |
|
128 | 128 | if axi == 'elevation': |
|
129 | 129 | payload_az['position'] = angle |
|
130 | 130 | payload_el['position'] = 0 |
|
131 | 131 | elif axi == 'azimuth': |
|
132 | 132 | payload_el['position'] = angle |
|
133 | 133 | payload_az['position'] = 0 |
|
134 | 134 | else: |
|
135 | 135 | payload_el['position'] = 0 |
|
136 | 136 | payload_az['position'] = 0 |
|
137 | 137 | |
|
138 | 138 | json_data_el = json.dumps(payload_el) |
|
139 | 139 | json_data_az = json.dumps(payload_az) |
|
140 | 140 | |
|
141 | 141 | base64_table_el = base64.standard_b64encode(json_data_el.encode('ascii')) |
|
142 | 142 | base64_table_az = base64.standard_b64encode(json_data_az.encode('ascii')) |
|
143 | 143 | |
|
144 | 144 | time.sleep(0.1) |
|
145 | 145 | r = requests.get(url + base64_table_el.decode('ascii')) |
|
146 | 146 | time.sleep(0.1) |
|
147 | 147 | r = requests.get(url + base64_table_az.decode('ascii')) |
|
148 | 148 | |
|
149 | 149 | if r: |
|
150 | 150 | self.device.status = 3 |
|
151 | 151 | self.device.save() |
|
152 | 152 | self.message = 'Pedestal reset' |
|
153 | 153 | else: |
|
154 | 154 | return False |
|
155 | 155 | |
|
156 | 156 | except Exception as e: |
|
157 | 157 | self.message = 'Pedestal reset: {}'.format(str(e)) |
|
158 | 158 | return False |
|
159 | 159 | |
|
160 | 160 | return True |
|
161 | 161 | |
|
162 | 162 | def stop_device(self): |
|
163 | 163 | |
|
164 | 164 | try: |
|
165 | 165 | command = self.device.url() + "stop" |
|
166 | 166 | r = requests.get(command) |
|
167 | 167 | |
|
168 | 168 | if self.mode == 'table': |
|
169 | 169 | AX = {'az':'azimuth', 'el':'elevation'} |
|
170 | 170 | axis = [AX[x.lower().strip()] for x in self.axis.split(',')] |
|
171 | 171 | list_of_floats = [float(x.strip()) for x in self.angle.split(",")] |
|
172 | 172 | self.reset_device(axis[0], list_of_floats[0]) |
|
173 | 173 | |
|
174 | 174 | if r: |
|
175 | 175 | self.device.status = 4 |
|
176 | 176 | self.device.save() |
|
177 | 177 | self.message = 'Pedestal stopped' |
|
178 | 178 | else: |
|
179 | 179 | self.device.status = 4 |
|
180 | 180 | self.device.save() |
|
181 | 181 | return False |
|
182 | 182 | except Exception as e: |
|
183 | 183 | if 'No route to host' not in str(e): |
|
184 | 184 | self.device.status = 4 |
|
185 | 185 | else: |
|
186 | 186 | self.device.status = 0 |
|
187 | 187 | #self.message = 'Pedestal stop: {}'.format(str(e)) |
|
188 | 188 | self.message = "Pedestal can't start, please check network/device connection or IP address/port configuration" |
|
189 | 189 | self.device.save() |
|
190 | 190 | return False |
|
191 | 191 | |
|
192 | 192 | return True |
|
193 | 193 | |
|
194 | 194 | def start_device(self): |
|
195 | 195 | |
|
196 | 196 | if self.mode == 'table': |
|
197 | 197 | if len(self.angle.split(',')) > 1: |
|
198 | 198 | list_speed = [] |
|
199 | 199 | list_axis = [] |
|
200 | 200 | for _ in range(len(self.angle.split(','))): |
|
201 | 201 | list_axis.append(self.axis) |
|
202 | 202 | list_speed.append(self.speed) |
|
203 | 203 | |
|
204 | 204 | if len(self.axis.split(',')) == 1: |
|
205 | 205 | self.axis = ",".join(map(str, list_axis)) |
|
206 | 206 | if len(self.speed.split(',')) == 1: |
|
207 | 207 | self.speed = ",".join(map(str, list_speed)) |
|
208 | 208 | |
|
209 | 209 | AX = {'az':'azimuth', 'el':'elevation'} |
|
210 | 210 | axis = [AX[x.lower().strip()] for x in self.axis.split(',')] |
|
211 | 211 | if len(axis)==1: |
|
212 | 212 | axis = axis[0] |
|
213 | 213 | |
|
214 | 214 | try: |
|
215 | 215 | if self.mode == 'position': |
|
216 | 216 | url = self.device.url() + "position?params=" |
|
217 | 217 | payload = {'axis': axis, 'position': float(self.angle)} |
|
218 | 218 | elif self.mode == 'speed': |
|
219 | 219 | url = self.device.url() + "speed?params=" |
|
220 | 220 | payload = {'axis': axis, 'speed': float(self.speed)} |
|
221 | 221 | elif self.mode == 'table': |
|
222 | 222 | url = self.device.url() + "combinedtable?params=" |
|
223 | 223 | list_of_floats = [float(x.strip()) for x in self.angle.split(",")] |
|
224 | 224 | byte_table = [] |
|
225 | 225 | for x in list_of_floats: |
|
226 | 226 | temp = bytearray(struct.pack("f", x)) |
|
227 | 227 | byte_table.append(temp[3]) |
|
228 | 228 | byte_table.append(temp[2]) |
|
229 | 229 | byte_table.append(temp[1]) |
|
230 | 230 | byte_table.append(temp[0]) |
|
231 | 231 | |
|
232 | 232 | coded_table = base64.standard_b64encode(bytes(byte_table)) |
|
233 | 233 | coded_table_ascii = coded_table.decode('ascii') |
|
234 | 234 | speed = [float(x.strip()) for x in self.speed.split(',')] |
|
235 | ||
|
236 | if isinstance(axis, str): | |
|
237 | axis = [axis] | |
|
238 | ||
|
235 | 239 | payload = { |
|
236 | 240 | 'arraylength': len(speed), |
|
237 | 241 | 'axis': axis, |
|
238 | 242 | 'speed': speed, |
|
239 | 243 | 'bottom': self.min_value, |
|
240 | 244 | 'top': self.max_value, |
|
241 | 245 | 'table': coded_table_ascii |
|
242 | 246 | } |
|
243 | 247 | |
|
244 | 248 | json_data = json.dumps(payload) |
|
245 | 249 | print(json_data) |
|
246 | 250 | base64_table = base64.standard_b64encode(json_data.encode('ascii')) |
|
247 | 251 | url += base64_table.decode('ascii') |
|
248 | 252 | print(url) |
|
249 | 253 | r = requests.get(url) |
|
250 | 254 | |
|
251 | 255 | if self.mode == 'table': |
|
252 | 256 | payload['table'] = list_of_floats |
|
253 | 257 | |
|
254 | 258 | if r: |
|
255 | 259 | self.device.status = 3 |
|
256 | 260 | self.device.save() |
|
257 | 261 | self.message = 'Pedestal configured and started' |
|
258 | 262 | else: |
|
259 | 263 | return False |
|
260 | 264 | except Exception as e: |
|
261 | 265 | if 'No route to host' not in str(e): |
|
262 | 266 | self.device.status = 4 |
|
263 | 267 | else: |
|
264 | 268 | self.device.status = 0 |
|
265 | 269 | #self.message = 'Pedestal start: {}'.format(str(e)) |
|
266 | 270 | self.message = "Pedestal can't start, please check network/device connection or IP address/port configuration" |
|
267 | 271 | self.device.save() |
|
268 | 272 | return False |
|
269 | 273 | |
|
270 | 274 | return payload |
|
271 | 275 | |
|
272 | 276 | def get_absolute_url_import(self): |
|
273 | 277 | return reverse('url_import_pedestal_conf', args=[str(self.id)]) |
@@ -1,111 +1,111 | |||
|
1 | 1 | import os |
|
2 | 2 | import json |
|
3 | 3 | |
|
4 | 4 | from django import forms |
|
5 | 5 | from django.utils.safestring import mark_safe |
|
6 | 6 | from apps.main.models import Device |
|
7 | 7 | from apps.main.forms import add_empty_choice |
|
8 | 8 | from .models import USRPRXConfiguration |
|
9 | 9 | |
|
10 | 10 | def create_choices_from_model(model, conf_id, all_choice=False): |
|
11 | 11 | |
|
12 | 12 | instance = globals()[model] |
|
13 | 13 | choices = instance.objects.all().values_list('pk', 'name') |
|
14 | 14 | |
|
15 | 15 | return choices |
|
16 | 16 | |
|
17 | 17 | class USRPRXConfigurationForm(forms.ModelForm): |
|
18 | 18 | |
|
19 | 19 | def __init__(self, *args, **kwargs): |
|
20 | 20 | super(USRPRXConfigurationForm, self).__init__(*args, **kwargs) |
|
21 | 21 | |
|
22 | 22 | instance = getattr(self, 'instance', None) |
|
23 | 23 | |
|
24 | 24 | if instance and instance.pk: |
|
25 | 25 | |
|
26 | 26 | devices = Device.objects.filter(device_type__name='usrp_rx') |
|
27 | 27 | #if instance.experiment: |
|
28 | 28 | #self.fields['experiment'].widget.attrs['read_only'] = True |
|
29 | 29 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] |
|
30 | 30 | #self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
31 | 31 | |
|
32 | 32 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
33 | 33 | self.fields['experiment'].widget.attrs['readonly'] = True |
|
34 | 34 | |
|
35 | 35 | class Meta: |
|
36 | 36 | model = USRPRXConfiguration |
|
37 | 37 | exclude = ('type', 'parameters', 'status', 'total_units', 'author', 'hash') |
|
38 | 38 | |
|
39 | 39 | def clean(self): |
|
40 | 40 | form_data = super(USRPRXConfigurationForm, self).clean() |
|
41 | 41 | return form_data |
|
42 | 42 | |
|
43 | 43 | def save(self, *args, **kwargs): |
|
44 | 44 | conf = super(USRPRXConfigurationForm, self).save(*args, **kwargs) |
|
45 | 45 | conf.save() |
|
46 | 46 | return conf |
|
47 | 47 | |
|
48 | 48 | class USRPRXEditionForm(forms.ModelForm): |
|
49 | 49 | |
|
50 | 50 | def __init__(self, *args, **kwargs): |
|
51 | 51 | super(USRPRXEditionForm, self).__init__(*args, **kwargs) |
|
52 | 52 | |
|
53 | 53 | instance = getattr(self, 'instance', None) |
|
54 | 54 | |
|
55 | 55 | if instance and instance.pk: |
|
56 | 56 | |
|
57 | 57 | devices = Device.objects.filter(device_type__name='usrp_rx') |
|
58 | 58 | #if instance.experiment: |
|
59 | 59 | #self.fields['experiment'].widget.attrs['read_only'] = True |
|
60 | 60 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] |
|
61 | 61 | #self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
62 | 62 | |
|
63 | 63 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
64 | 64 | self.fields['experiment'].widget.attrs['readonly'] = True |
|
65 | 65 | |
|
66 | 66 | class Meta: |
|
67 | 67 | model = USRPRXConfiguration |
|
68 |
exclude = ('template', 'device', 'label |
|
|
68 | exclude = ('template', 'device', 'label', 'type', 'parameters', 'status', 'total_units', 'author', 'hash') | |
|
69 | 69 | |
|
70 | 70 | def clean(self): |
|
71 | 71 | form_data = super(USRPRXEditionForm, self).clean() |
|
72 | 72 | return form_data |
|
73 | 73 | |
|
74 | 74 | def save(self, *args, **kwargs): |
|
75 | 75 | conf = super(USRPRXEditionForm, self).save(*args, **kwargs) |
|
76 | 76 | conf.save() |
|
77 | 77 | return conf |
|
78 | 78 | |
|
79 | 79 | class ExtFileField(forms.FileField): |
|
80 | 80 | """ |
|
81 | 81 | Same as forms.FileField, but you can specify a file extension whitelist. |
|
82 | 82 | |
|
83 | 83 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |
|
84 | 84 | >>> |
|
85 | 85 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) |
|
86 | 86 | >>> |
|
87 | 87 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) |
|
88 | 88 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) |
|
89 | 89 | >>> |
|
90 | 90 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) |
|
91 | 91 | Traceback (most recent call last): |
|
92 | 92 | ... |
|
93 | 93 | ValidationError: [u'Not allowed filetype!'] |
|
94 | 94 | """ |
|
95 | 95 | def __init__(self, *args, **kwargs): |
|
96 | 96 | extensions = kwargs.pop("extensions") |
|
97 | 97 | self.extensions = [i.lower() for i in extensions] |
|
98 | 98 | |
|
99 | 99 | super(ExtFileField, self).__init__(*args, **kwargs) |
|
100 | 100 | |
|
101 | 101 | def clean(self, *args, **kwargs): |
|
102 | 102 | data = super(ExtFileField, self).clean(*args, **kwargs) |
|
103 | 103 | filename = data.name |
|
104 | 104 | ext = os.path.splitext(filename)[1] |
|
105 | 105 | ext = ext.lower() |
|
106 | 106 | if ext not in self.extensions: |
|
107 | 107 | raise forms.ValidationError('Not allowed file type: %s' % ext) |
|
108 | 108 | |
|
109 | 109 | class USRPRXImportForm(forms.Form): |
|
110 | 110 | |
|
111 | 111 | file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) No newline at end of file |
@@ -1,222 +1,222 | |||
|
1 | 1 | import ast |
|
2 | 2 | import json |
|
3 | 3 | import requests |
|
4 | 4 | import base64 |
|
5 | 5 | from struct import pack |
|
6 | 6 | |
|
7 | 7 | from django.db import models |
|
8 | 8 | from django.urls import reverse |
|
9 | 9 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
10 | 10 | |
|
11 | 11 | from apps.main.models import Configuration |
|
12 | 12 | |
|
13 | 13 | BOARD_VALUE = ( |
|
14 | 14 | ('1', 'A:AB'), |
|
15 | 15 | ('2', 'AB'), |
|
16 | 16 | ('3', 'A:A A:B'), |
|
17 | 17 | ) |
|
18 | 18 | |
|
19 | 19 | ANT_VALUE = ( |
|
20 | 20 | ('1', 'RX'), |
|
21 | 21 | ('2', 'TX') |
|
22 | 22 | ) |
|
23 | 23 | |
|
24 | 24 | CLK_VALUE = ( |
|
25 | 25 | ('1', 'internal'), |
|
26 | 26 | ('2', 'external') |
|
27 | 27 | ) |
|
28 | 28 | |
|
29 | 29 | TIME_VALUE = ( |
|
30 | 30 | ('1', 'internal'), |
|
31 | 31 | ('2', 'external') |
|
32 | 32 | ) |
|
33 | 33 | |
|
34 | 34 | class USRPRXConfiguration(Configuration): |
|
35 | 35 | |
|
36 | ip_address = models.GenericIPAddressField( | |
|
36 | ip_address_rx = models.GenericIPAddressField( | |
|
37 | 37 | verbose_name = 'IP address', |
|
38 | 38 | protocol='IPv4', |
|
39 | 39 | default='0.0.0.0') |
|
40 | 40 | |
|
41 | daughterboard = models.CharField( | |
|
41 | daughterboard_rx = models.CharField( | |
|
42 | 42 | verbose_name='Daughterboard', |
|
43 | 43 | max_length=3, |
|
44 | 44 | choices=BOARD_VALUE, |
|
45 | 45 | null=False, |
|
46 | 46 | blank=False |
|
47 | 47 | ) |
|
48 | 48 | |
|
49 | antenna = models.CharField( | |
|
49 | antenna_rx = models.CharField( | |
|
50 | 50 | verbose_name='Antenna', |
|
51 | 51 | max_length=3, |
|
52 | 52 | choices=ANT_VALUE, |
|
53 | 53 | null=False, |
|
54 | 54 | blank=False |
|
55 | 55 | ) |
|
56 | 56 | |
|
57 | 57 | samplerate_rx = models.FloatField( |
|
58 | verbose_name='Sample Rate', | |
|
58 | verbose_name='Sample Rate [MHz]', | |
|
59 | 59 | validators=[MinValueValidator(0.01), MaxValueValidator(10.00)], |
|
60 | 60 | blank=False, |
|
61 | 61 | null=False, |
|
62 | 62 | help_text='Introduce the value in MHz (10^6)' |
|
63 | 63 | ) |
|
64 | 64 | |
|
65 | 65 | frequency_rx = models.FloatField( |
|
66 | verbose_name='Frequency', | |
|
66 | verbose_name='Frequency [MHz]', | |
|
67 | 67 | validators=[MinValueValidator(0.01)], |
|
68 | 68 | blank=False, |
|
69 | 69 | null=False, |
|
70 | 70 | help_text='Introduce the value in MHz (10^6)' |
|
71 | 71 | ) |
|
72 | 72 | |
|
73 | 73 | datadir = models.CharField( |
|
74 | 74 | verbose_name="Data Directory", |
|
75 | 75 | max_length=100, |
|
76 | 76 | default='', |
|
77 | 77 | blank=False, |
|
78 | 78 | null=False, |
|
79 | 79 | help_text='Fill with a directory. Example: /media/soporte/DATA' |
|
80 | 80 | ) |
|
81 | 81 | |
|
82 | 82 | clocksource = models.CharField( |
|
83 | 83 | verbose_name='Clock Source', |
|
84 | 84 | max_length=3, |
|
85 | 85 | choices=CLK_VALUE, |
|
86 | 86 | null=False, |
|
87 | 87 | blank=False |
|
88 | 88 | ) |
|
89 | 89 | |
|
90 | 90 | timesource = models.CharField( |
|
91 | 91 | verbose_name='Time Source', |
|
92 | 92 | max_length=3, |
|
93 | 93 | choices=TIME_VALUE, |
|
94 | 94 | null=False, |
|
95 | 95 | blank=False |
|
96 | 96 | ) |
|
97 | 97 | |
|
98 | 98 | clockrate = models.FloatField( |
|
99 | verbose_name='Clock Rate', | |
|
99 | verbose_name='Clock Rate [MHz]', | |
|
100 | 100 | blank=False, |
|
101 | 101 | null=False, |
|
102 | 102 | help_text='Introduce the value in MHz (10^6)' |
|
103 | 103 | ) |
|
104 | 104 | |
|
105 | 105 | class Meta: |
|
106 | 106 | db_table = 'usrp_rx_configurations' |
|
107 | 107 | |
|
108 | 108 | def __str__(self): |
|
109 | 109 | return u'RX at {} MHz @ {} MHz'.format(self.frequency_rx, self.samplerate_rx) |
|
110 | 110 | |
|
111 | 111 | def get_absolute_url_plot(self): |
|
112 | 112 | return reverse('url_plot_usrp_rx_pulses', args=[str(self.id)]) |
|
113 | 113 | |
|
114 | 114 | def request(self, cmd, method='get', **kwargs): |
|
115 | 115 | |
|
116 | 116 | req = getattr(requests, method)(self.device.url(cmd), **kwargs) |
|
117 | 117 | payload = req.json() |
|
118 | 118 | return payload |
|
119 | 119 | |
|
120 | 120 | def status_device(self): |
|
121 | 121 | |
|
122 | 122 | try: |
|
123 | 123 | #self.device.status = 0 |
|
124 | 124 | #payload = self.request('status') |
|
125 | 125 | payload = requests.get(self.device.url()) |
|
126 | 126 | |
|
127 | 127 | if payload: |
|
128 | 128 | self.device.status = 1 |
|
129 | 129 | elif payload['status']=='disable': |
|
130 | 130 | self.device.status = 2 |
|
131 | 131 | else: |
|
132 | 132 | self.device.status = 1 |
|
133 | 133 | self.device.save() |
|
134 | 134 | self.message = 'USRP Rx Dev status: {}'.format(payload['status']) |
|
135 | 135 | return False |
|
136 | 136 | except Exception as e: |
|
137 | 137 | if 'No route to host' not in str(e): |
|
138 | 138 | self.device.status = 4 |
|
139 | 139 | self.device.save() |
|
140 | 140 | self.message = 'USRP Rx status: {}'.format(str(e)) |
|
141 | 141 | return False |
|
142 | 142 | |
|
143 | 143 | self.device.save() |
|
144 | 144 | return True |
|
145 | 145 | |
|
146 | 146 | def reset_device(self): |
|
147 | 147 | |
|
148 | 148 | try: |
|
149 | 149 | payload = self.request('reset', 'post') |
|
150 | 150 | if payload['reset']=='ok': |
|
151 | 151 | self.message = 'USRP Rx restarted OK' |
|
152 | 152 | self.device.status = 2 |
|
153 | 153 | self.device.save() |
|
154 | 154 | else: |
|
155 | 155 | self.message = 'USRP Rx restart fail' |
|
156 | 156 | self.device.status = 4 |
|
157 | 157 | self.device.save() |
|
158 | 158 | except Exception as e: |
|
159 | 159 | self.message = 'USRP Rx reset: {}'.format(str(e)) |
|
160 | 160 | return False |
|
161 | 161 | |
|
162 | 162 | return True |
|
163 | 163 | |
|
164 | 164 | def stop_device(self): |
|
165 | 165 | |
|
166 | 166 | try: |
|
167 | 167 | command = self.device.url() + "stoprx" |
|
168 | 168 | r = requests.get(command) |
|
169 | 169 | if r: |
|
170 | 170 | self.device.status = 4 |
|
171 | 171 | self.device.save() |
|
172 | 172 | self.message = 'USRP stopped' |
|
173 | 173 | else: |
|
174 | 174 | self.device.status = 4 |
|
175 | 175 | self.device.save() |
|
176 | 176 | return False |
|
177 | 177 | except Exception as e: |
|
178 | 178 | if 'No route to host' not in str(e): |
|
179 | 179 | self.device.status = 4 |
|
180 | 180 | else: |
|
181 | 181 | self.device.status = 0 |
|
182 | 182 | #self.message = 'USRP Rx stop: {}'.format(str(e)) |
|
183 | 183 | self.message = "USRP Rx can't start, please check network/device connection or IP address/port configuration" |
|
184 | 184 | self.device.save() |
|
185 | 185 | return False |
|
186 | 186 | |
|
187 | 187 | return True |
|
188 | 188 | |
|
189 | 189 | def start_device(self): |
|
190 | 190 | |
|
191 | 191 | try: |
|
192 | 192 | usrp = USRPRXConfiguration.objects.get(pk=self) |
|
193 | usrp_daughterboard = usrp.get_daughterboard_display() | |
|
194 | usrp_antenna = usrp.get_antenna_display() | |
|
193 | usrp_daughterboard_rx = usrp.get_daughterboard_rx_display() | |
|
194 | usrp_antenna_rx = usrp.get_antenna_rx_display() | |
|
195 | 195 | usrp_clocksource = usrp.get_clocksource_display() |
|
196 | 196 | usrp_timesource = usrp.get_timesource_display() |
|
197 | 197 | |
|
198 | payload = {'ip_address': usrp.ip_address, 'daughterboard': usrp_daughterboard, 'antenna': usrp_antenna, 'sample_rate': usrp.samplerate_rx, 'frequency': usrp.frequency_rx, | |
|
198 | payload = {'ip_address': usrp.ip_address_rx, 'daughterboard': usrp_daughterboard_rx, 'antenna': usrp_antenna_rx, 'sample_rate': usrp.samplerate_rx, 'frequency': usrp.frequency_rx, | |
|
199 | 199 | 'datadir': usrp.datadir, 'clock_source': usrp_clocksource, 'time_source': usrp_timesource, 'clock_rate':usrp.clockrate} |
|
200 | 200 | |
|
201 | 201 | r = requests.post(self.device.url("usrprx"), json=payload) |
|
202 | 202 | |
|
203 | 203 | if r: |
|
204 | 204 | self.device.status = 3 |
|
205 | 205 | self.device.save() |
|
206 | 206 | self.message = 'USRP Rx configured and started' |
|
207 | 207 | else: |
|
208 | 208 | return False |
|
209 | 209 | except Exception as e: |
|
210 | 210 | if 'No route to host' not in str(e): |
|
211 | 211 | self.device.status = 4 |
|
212 | 212 | else: |
|
213 | 213 | self.device.status = 0 |
|
214 | 214 | #self.message = 'USRP Rx start: {}'.format(str(e)) |
|
215 | 215 | self.message = "USRP Rx can't start, please check network/device connection or IP address/port configuration" |
|
216 | 216 | self.device.save() |
|
217 | 217 | return False |
|
218 | 218 | |
|
219 | 219 | return payload |
|
220 | 220 | |
|
221 | 221 | def get_absolute_url_import(self): |
|
222 | 222 | return reverse('url_import_usrp_rx_conf', args=[str(self.id)]) No newline at end of file |
@@ -1,129 +1,129 | |||
|
1 | 1 | |
|
2 | 2 | import json |
|
3 | 3 | |
|
4 | 4 | from django.contrib import messages |
|
5 | 5 | from django.utils.safestring import mark_safe |
|
6 | 6 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
7 | 7 | from django.contrib.auth.decorators import login_required |
|
8 | 8 | |
|
9 | 9 | from apps.main.models import Experiment, Device |
|
10 | 10 | from apps.main.views import sidebar |
|
11 | 11 | |
|
12 | 12 | from .models import USRPRXConfiguration |
|
13 | 13 | from .forms import USRPRXConfigurationForm, USRPRXImportForm |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | def conf(request, conf_id): |
|
17 | 17 | |
|
18 | 18 | conf = get_object_or_404(USRPRXConfiguration, pk=conf_id) |
|
19 | 19 | |
|
20 | 20 | kwargs = {} |
|
21 | 21 | kwargs['dev_conf'] = conf |
|
22 | kwargs['dev_conf_keys'] = ['ip_address', 'daughterboard', 'antenna', 'samplerate_rx', 'frequency_rx', 'datadir', 'clocksource', 'timesource', 'clockrate'] | |
|
22 | kwargs['dev_conf_keys'] = ['ip_address_rx', 'daughterboard_rx', 'antenna_rx', 'samplerate_rx', 'frequency_rx', 'datadir', 'clocksource', 'timesource', 'clockrate'] | |
|
23 | 23 | |
|
24 | 24 | kwargs['title'] = 'Configuration' |
|
25 | 25 | kwargs['suptitle'] = 'Detail' |
|
26 | 26 | |
|
27 | 27 | kwargs['button'] = 'Edit Configuration' |
|
28 | 28 | |
|
29 | 29 | conf.status_device() |
|
30 | 30 | |
|
31 | 31 | ###### SIDEBAR ###### |
|
32 | 32 | kwargs.update(sidebar(conf=conf)) |
|
33 | 33 | |
|
34 | 34 | return render(request, 'usrp_rx_conf.html', kwargs) |
|
35 | 35 | |
|
36 | 36 | @login_required |
|
37 | 37 | def conf_edit(request, conf_id): |
|
38 | 38 | |
|
39 | 39 | conf = get_object_or_404(USRPRXConfiguration, pk=conf_id) |
|
40 | 40 | |
|
41 | 41 | if request.method=='GET': |
|
42 | 42 | |
|
43 | 43 | form = USRPRXConfigurationForm(instance=conf) |
|
44 | 44 | |
|
45 | 45 | elif request.method=='POST': |
|
46 | 46 | |
|
47 | 47 | line_data = {} |
|
48 | 48 | conf_data = {} |
|
49 | 49 | clock_data = {} |
|
50 | 50 | extras = [] |
|
51 | 51 | |
|
52 | 52 | #classified post fields |
|
53 | 53 | for label,value in request.POST.items(): |
|
54 | 54 | if label=='csrfmiddlewaretoken': |
|
55 | 55 | continue |
|
56 | 56 | |
|
57 | 57 | if label.count('|')==0: |
|
58 | 58 | if label in ('mode', 'multiplier', 'divisor', 'reference'): |
|
59 | 59 | clock_data[label] = value |
|
60 | 60 | else: |
|
61 | 61 | conf_data[label] = value |
|
62 | 62 | continue |
|
63 | 63 | |
|
64 | 64 | elif label.split('|')[0]!='-1': |
|
65 | 65 | extras.append(label) |
|
66 | 66 | continue |
|
67 | 67 | |
|
68 | 68 | x, pk, name = label.split('|') |
|
69 | 69 | |
|
70 | 70 | if name=='codes': |
|
71 | 71 | value = [s for s in value.split('\r\n') if s] |
|
72 | 72 | |
|
73 | 73 | if pk in line_data: |
|
74 | 74 | line_data[pk][name] = value |
|
75 | 75 | else: |
|
76 | 76 | line_data[pk] = {name:value} |
|
77 | 77 | |
|
78 | 78 | form = USRPRXConfigurationForm(conf_data, instance=conf) |
|
79 | 79 | |
|
80 | 80 | if form.is_valid(): |
|
81 | 81 | form.save() |
|
82 | 82 | |
|
83 | 83 | messages.success(request, 'USRP Rx configuration successfully updated') |
|
84 | 84 | |
|
85 | 85 | return redirect(conf.get_absolute_url()) |
|
86 | 86 | |
|
87 | 87 | kwargs = {} |
|
88 | 88 | kwargs['dev_conf'] = conf |
|
89 | 89 | kwargs['form'] = form |
|
90 | 90 | kwargs['edit'] = True |
|
91 | 91 | |
|
92 | 92 | kwargs['title'] = 'USRP Rx Configuration' |
|
93 | 93 | kwargs['suptitle'] = 'Edit' |
|
94 | 94 | kwargs['button'] = 'Update' |
|
95 | 95 | |
|
96 | 96 | return render(request, 'usrp_rx_conf_edit.html', kwargs) |
|
97 | 97 | |
|
98 | 98 | def import_file(request, conf_id): |
|
99 | 99 | |
|
100 | 100 | conf = get_object_or_404(USRPRXConfiguration, pk=conf_id) |
|
101 | 101 | if request.method=='POST': |
|
102 | 102 | form = USRPRXImportForm(request.POST, request.FILES) |
|
103 | 103 | if form.is_valid(): |
|
104 | 104 | try: |
|
105 | 105 | data = conf.import_from_file(request.FILES['file_name']) |
|
106 | 106 | conf.dict_to_parms(data) |
|
107 | 107 | conf.update_pulses() |
|
108 | 108 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) |
|
109 | 109 | return redirect(conf.get_absolute_url_edit()) |
|
110 | 110 | |
|
111 | 111 | except Exception as e: |
|
112 | 112 | messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], repr(e))) |
|
113 | 113 | else: |
|
114 | 114 | messages.warning(request, 'Your current configuration will be replaced') |
|
115 | 115 | form = USRPRXImportForm() |
|
116 | 116 | |
|
117 | 117 | kwargs = {} |
|
118 | 118 | kwargs['form'] = form |
|
119 | 119 | kwargs['title'] = 'USRP Rx Configuration' |
|
120 | 120 | kwargs['suptitle'] = 'Import file' |
|
121 | 121 | kwargs['button'] = 'Upload' |
|
122 | 122 | kwargs['previous'] = conf.get_absolute_url() |
|
123 | 123 | |
|
124 | 124 | return render(request, 'usrp_rx_import.html', kwargs) |
|
125 | 125 | |
|
126 | 126 | def conf_raw(request, conf_id): |
|
127 | 127 | conf = get_object_or_404(USRPRXConfiguration, pk=conf_id) |
|
128 | 128 | raw = conf.write_device(raw=True) |
|
129 | 129 | return HttpResponse(raw, content_type='application/json') No newline at end of file |
@@ -1,158 +1,158 | |||
|
1 | 1 | import os |
|
2 | 2 | import json |
|
3 | 3 | from queue import Empty |
|
4 | 4 | |
|
5 | 5 | from django import forms |
|
6 | 6 | from django.utils.safestring import mark_safe |
|
7 | 7 | from apps.main.models import Device |
|
8 | 8 | from apps.main.forms import add_empty_choice |
|
9 | 9 | from .models import USRPTXConfiguration |
|
10 | 10 | |
|
11 | 11 | def create_choices_from_model(model, conf_id, all_choice=False): |
|
12 | 12 | |
|
13 | 13 | instance = globals()[model] |
|
14 | 14 | choices = instance.objects.all().values_list('pk', 'name') |
|
15 | 15 | |
|
16 | 16 | return choices |
|
17 | 17 | |
|
18 | 18 | def validators_pulse_2(self, data): |
|
19 | 19 | if data['enable_2'] is True and None in data.values(): |
|
20 | 20 | for key, value in data.items(): |
|
21 | 21 | if value is None: |
|
22 | 22 | self._errors[key] = ["Ensure this value isn't empty."] |
|
23 | 23 | |
|
24 | 24 | def validators_textarea(self,data): |
|
25 | 25 | if data['code_type_1'].pk == 19 and data['code_1'] == '': |
|
26 | 26 | self._errors['code_1'] = ["Ensure this value isn't empty."] |
|
27 | 27 | |
|
28 | 28 | try: |
|
29 | 29 | if data['code_type_2'].pk == 19 and data['code_2'] == '': |
|
30 | 30 | self._errors['code_2'] = ["Ensure this value isn't empty."] |
|
31 | 31 | except: |
|
32 | 32 | pass |
|
33 | 33 | |
|
34 | 34 | class USRPTXConfigurationForm(forms.ModelForm): |
|
35 | 35 | |
|
36 | 36 | def __init__(self, *args, **kwargs): |
|
37 | 37 | super(USRPTXConfigurationForm, self).__init__(*args, **kwargs) |
|
38 | 38 | |
|
39 | 39 | instance = getattr(self, 'instance', None) |
|
40 | 40 | |
|
41 | 41 | if instance and instance.pk: |
|
42 | 42 | |
|
43 | 43 | devices = Device.objects.filter(device_type__name='usrp_tx') |
|
44 | 44 | #if instance.experiment: |
|
45 | 45 | #self.fields['experiment'].widget.attrs['read_only'] = True |
|
46 | 46 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] |
|
47 | 47 | #self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
48 | 48 | |
|
49 | 49 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
50 | 50 | self.fields['experiment'].widget.attrs['readonly'] = True |
|
51 | 51 | |
|
52 | 52 | class Meta: |
|
53 | 53 | model = USRPTXConfiguration |
|
54 | 54 | exclude = ('type', 'parameters', 'status', 'total_units', 'author', 'hash') |
|
55 | 55 | |
|
56 | 56 | def clean(self): |
|
57 | 57 | form_data = super(USRPTXConfigurationForm, self).clean() |
|
58 | 58 | |
|
59 | 59 | kwargs_check_pulse2 = {} |
|
60 | 60 | kwargs_check_pulse2['enable_2'] = form_data.get('enable_2') |
|
61 | 61 | kwargs_check_pulse2['pulse_2'] = form_data.get('pulse_2') |
|
62 | 62 | kwargs_check_pulse2['code_type_2'] = form_data.get('code_type_2') |
|
63 | 63 | kwargs_check_pulse2['repetitions_2'] = form_data.get('repetitions_2') |
|
64 | 64 | validators_pulse_2(self, kwargs_check_pulse2) |
|
65 | 65 | |
|
66 | 66 | kwargs_check_textarea = {} |
|
67 | 67 | kwargs_check_textarea['code_type_1'] = form_data.get('code_type_1') |
|
68 | 68 | kwargs_check_textarea['code_1'] = form_data.get('code_1') |
|
69 | 69 | kwargs_check_textarea['code_type_2'] = form_data.get('code_type_2') |
|
70 | 70 | kwargs_check_textarea['code_2'] = form_data.get('code_2') |
|
71 | 71 | validators_textarea(self, kwargs_check_textarea) |
|
72 | 72 | |
|
73 | 73 | return form_data |
|
74 | 74 | |
|
75 | 75 | def save(self, *args, **kwargs): |
|
76 | 76 | conf = super(USRPTXConfigurationForm, self).save(*args, **kwargs) |
|
77 | 77 | conf.save() |
|
78 | 78 | return conf |
|
79 | 79 | |
|
80 | 80 | class USRPTXEditionForm(forms.ModelForm): |
|
81 | 81 | |
|
82 | 82 | def __init__(self, *args, **kwargs): |
|
83 | 83 | super(USRPTXEditionForm, self).__init__(*args, **kwargs) |
|
84 | 84 | |
|
85 | 85 | instance = getattr(self, 'instance', None) |
|
86 | 86 | |
|
87 | 87 | if instance and instance.pk: |
|
88 | 88 | |
|
89 | 89 | devices = Device.objects.filter(device_type__name='usrp_tx') |
|
90 | 90 | #if instance.experiment: |
|
91 | 91 | #self.fields['experiment'].widget.attrs['read_only'] = True |
|
92 | 92 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] |
|
93 | 93 | #self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
94 | 94 | |
|
95 | 95 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
96 | 96 | self.fields['experiment'].widget.attrs['readonly'] = True |
|
97 | 97 | |
|
98 | 98 | class Meta: |
|
99 | 99 | model = USRPTXConfiguration |
|
100 |
exclude = ('template', 'device', 'label |
|
|
100 | exclude = ('template', 'device', 'label', 'type', 'parameters', 'status', 'total_units', 'author', 'hash') | |
|
101 | 101 | |
|
102 | 102 | def clean(self): |
|
103 | 103 | form_data = super(USRPTXEditionForm, self).clean() |
|
104 | 104 | |
|
105 | 105 | kwargs_check_pulse2 = {} |
|
106 | 106 | kwargs_check_pulse2['enable_2'] = form_data.get('enable_2') |
|
107 | 107 | kwargs_check_pulse2['pulse_2'] = form_data.get('pulse_2') |
|
108 | 108 | kwargs_check_pulse2['code_type_2'] = form_data.get('code_type_2') |
|
109 | 109 | kwargs_check_pulse2['repetitions_2'] = form_data.get('repetitions_2') |
|
110 | 110 | validators_pulse_2(self, kwargs_check_pulse2) |
|
111 | 111 | |
|
112 | 112 | kwargs_check_textarea = {} |
|
113 | 113 | kwargs_check_textarea['code_type_1'] = form_data.get('code_type_1') |
|
114 | 114 | kwargs_check_textarea['code_1'] = form_data.get('code_1') |
|
115 | 115 | kwargs_check_textarea['code_type_2'] = form_data.get('code_type_2') |
|
116 | 116 | kwargs_check_textarea['code_2'] = form_data.get('code_2') |
|
117 | 117 | validators_textarea(self, kwargs_check_textarea) |
|
118 | 118 | |
|
119 | 119 | return form_data |
|
120 | 120 | |
|
121 | 121 | def save(self, *args, **kwargs): |
|
122 | 122 | conf = super(USRPTXEditionForm, self).save(*args, **kwargs) |
|
123 | 123 | conf.save() |
|
124 | 124 | return conf |
|
125 | 125 | |
|
126 | 126 | class ExtFileField(forms.FileField): |
|
127 | 127 | """ |
|
128 | 128 | Same as forms.FileField, but you can specify a file extension whitelist. |
|
129 | 129 | |
|
130 | 130 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |
|
131 | 131 | >>> |
|
132 | 132 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) |
|
133 | 133 | >>> |
|
134 | 134 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) |
|
135 | 135 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) |
|
136 | 136 | >>> |
|
137 | 137 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) |
|
138 | 138 | Traceback (most recent call last): |
|
139 | 139 | ... |
|
140 | 140 | ValidationError: [u'Not allowed filetype!'] |
|
141 | 141 | """ |
|
142 | 142 | def __init__(self, *args, **kwargs): |
|
143 | 143 | extensions = kwargs.pop("extensions") |
|
144 | 144 | self.extensions = [i.lower() for i in extensions] |
|
145 | 145 | |
|
146 | 146 | super(ExtFileField, self).__init__(*args, **kwargs) |
|
147 | 147 | |
|
148 | 148 | def clean(self, *args, **kwargs): |
|
149 | 149 | data = super(ExtFileField, self).clean(*args, **kwargs) |
|
150 | 150 | filename = data.name |
|
151 | 151 | ext = os.path.splitext(filename)[1] |
|
152 | 152 | ext = ext.lower() |
|
153 | 153 | if ext not in self.extensions: |
|
154 | 154 | raise forms.ValidationError('Not allowed file type: %s' % ext) |
|
155 | 155 | |
|
156 | 156 | class USRPTXImportForm(forms.Form): |
|
157 | 157 | |
|
158 | 158 | file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now