@@ -1,1655 +1,1663 | |||
|
1 | 1 | import ast |
|
2 | 2 | import json |
|
3 | 3 | from datetime import datetime |
|
4 | 4 | |
|
5 | 5 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
6 | 6 | from django.utils.safestring import mark_safe |
|
7 | 7 | from django.http import HttpResponseRedirect |
|
8 | 8 | from django.core.urlresolvers import reverse |
|
9 | 9 | from django.db.models import Q |
|
10 | 10 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
|
11 | 11 | from django.contrib import messages |
|
12 | 12 | from django.http.request import QueryDict |
|
13 | 13 | |
|
14 | 14 | try: |
|
15 | 15 | from urllib.parse import urlencode |
|
16 | 16 | except ImportError: |
|
17 | 17 | from urllib import urlencode |
|
18 | 18 | |
|
19 | 19 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
20 | 20 | from .forms import OperationSearchForm, FilterForm, ChangeIpForm |
|
21 | 21 | |
|
22 | 22 | from .tasks import task_start, task_stop |
|
23 | 23 | |
|
24 | 24 | from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm |
|
25 | 25 | from apps.dds.forms import DDSConfigurationForm |
|
26 | 26 | from apps.jars.forms import JARSConfigurationForm |
|
27 | 27 | from apps.cgs.forms import CGSConfigurationForm |
|
28 | 28 | from apps.abs.forms import ABSConfigurationForm |
|
29 | 29 | from apps.usrp.forms import USRPConfigurationForm |
|
30 | 30 | |
|
31 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment | |
|
31 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment, DEV_STATES | |
|
32 | 32 | from apps.cgs.models import CGSConfiguration |
|
33 | 33 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE |
|
34 | 34 | from apps.usrp.models import USRPConfiguration |
|
35 | 35 | from apps.abs.models import ABSConfiguration |
|
36 | 36 | from apps.rc.models import RCConfiguration, RCLine, RCLineType |
|
37 | 37 | from apps.dds.models import DDSConfiguration |
|
38 | 38 | |
|
39 | 39 | from django.contrib.auth.decorators import login_required |
|
40 | 40 | from django.contrib.auth.decorators import user_passes_test |
|
41 | 41 | from django.contrib.admin.views.decorators import staff_member_required |
|
42 | 42 | |
|
43 | 43 | CONF_FORMS = { |
|
44 | 44 | 'rc': RCConfigurationForm, |
|
45 | 45 | 'dds': DDSConfigurationForm, |
|
46 | 46 | 'jars': JARSConfigurationForm, |
|
47 | 47 | 'cgs': CGSConfigurationForm, |
|
48 | 48 | 'abs': ABSConfigurationForm, |
|
49 | 49 | 'usrp': USRPConfigurationForm, |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | CONF_MODELS = { |
|
53 | 53 | 'rc': RCConfiguration, |
|
54 | 54 | 'dds': DDSConfiguration, |
|
55 | 55 | 'jars': JARSConfiguration, |
|
56 | 56 | 'cgs': CGSConfiguration, |
|
57 | 57 | 'abs': ABSConfiguration, |
|
58 | 58 | 'usrp': USRPConfiguration, |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | MIX_MODES = { |
|
62 | 62 | '0': 'P', |
|
63 | 63 | '1': 'S', |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | MIX_OPERATIONS = { |
|
67 | 67 | '0': 'OR', |
|
68 | 68 | '1': 'XOR', |
|
69 | 69 | '2': 'AND', |
|
70 | 70 | '3': 'NAND', |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | def index(request): |
|
74 | 74 | kwargs = {'no_sidebar':True} |
|
75 | 75 | |
|
76 | 76 | return render(request, 'index.html', kwargs) |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | def locations(request): |
|
80 | 80 | |
|
81 | 81 | page = request.GET.get('page') |
|
82 | 82 | order = ('name',) |
|
83 | 83 | |
|
84 | 84 | kwargs = get_paginator(Location, page, order) |
|
85 | 85 | |
|
86 | 86 | kwargs['keys'] = ['name', 'description'] |
|
87 | 87 | kwargs['title'] = 'Radar System' |
|
88 | 88 | kwargs['suptitle'] = 'List' |
|
89 | 89 | kwargs['no_sidebar'] = True |
|
90 | 90 | |
|
91 | 91 | return render(request, 'base_list.html', kwargs) |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | def location(request, id_loc): |
|
95 | 95 | |
|
96 | 96 | location = get_object_or_404(Location, pk=id_loc) |
|
97 | 97 | |
|
98 | 98 | kwargs = {} |
|
99 | 99 | kwargs['location'] = location |
|
100 | 100 | kwargs['location_keys'] = ['name', 'description'] |
|
101 | 101 | |
|
102 | 102 | kwargs['title'] = 'Location' |
|
103 | 103 | kwargs['suptitle'] = 'Details' |
|
104 | 104 | |
|
105 | 105 | return render(request, 'location.html', kwargs) |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | @user_passes_test(lambda u:u.is_staff) |
|
109 | 109 | def location_new(request): |
|
110 | 110 | |
|
111 | 111 | if request.method == 'GET': |
|
112 | 112 | form = LocationForm() |
|
113 | 113 | |
|
114 | 114 | if request.method == 'POST': |
|
115 | 115 | form = LocationForm(request.POST) |
|
116 | 116 | |
|
117 | 117 | if form.is_valid(): |
|
118 | 118 | form.save() |
|
119 | 119 | return redirect('url_locations') |
|
120 | 120 | |
|
121 | 121 | kwargs = {} |
|
122 | 122 | kwargs['form'] = form |
|
123 | 123 | kwargs['title'] = 'Radar System' |
|
124 | 124 | kwargs['suptitle'] = 'New' |
|
125 | 125 | kwargs['button'] = 'Create' |
|
126 | 126 | |
|
127 | 127 | return render(request, 'base_edit.html', kwargs) |
|
128 | 128 | |
|
129 | 129 | |
|
130 | 130 | @user_passes_test(lambda u:u.is_staff) |
|
131 | 131 | def location_edit(request, id_loc): |
|
132 | 132 | |
|
133 | 133 | location = get_object_or_404(Location, pk=id_loc) |
|
134 | 134 | |
|
135 | 135 | if request.method=='GET': |
|
136 | 136 | form = LocationForm(instance=location) |
|
137 | 137 | |
|
138 | 138 | if request.method=='POST': |
|
139 | 139 | form = LocationForm(request.POST, instance=location) |
|
140 | 140 | |
|
141 | 141 | if form.is_valid(): |
|
142 | 142 | form.save() |
|
143 | 143 | return redirect('url_locations') |
|
144 | 144 | |
|
145 | 145 | kwargs = {} |
|
146 | 146 | kwargs['form'] = form |
|
147 | 147 | kwargs['title'] = 'Location' |
|
148 | 148 | kwargs['suptitle'] = 'Edit' |
|
149 | 149 | kwargs['button'] = 'Update' |
|
150 | 150 | |
|
151 | 151 | return render(request, 'base_edit.html', kwargs) |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | @user_passes_test(lambda u:u.is_staff) |
|
155 | 155 | def location_delete(request, id_loc): |
|
156 | 156 | |
|
157 | 157 | location = get_object_or_404(Location, pk=id_loc) |
|
158 | 158 | |
|
159 | 159 | if request.method=='POST': |
|
160 | 160 | |
|
161 | 161 | if request.user.is_staff: |
|
162 | 162 | location.delete() |
|
163 | 163 | return redirect('url_locations') |
|
164 | 164 | |
|
165 | 165 | messages.error(request, 'Not enough permission to delete this object') |
|
166 | 166 | return redirect(location.get_absolute_url()) |
|
167 | 167 | |
|
168 | 168 | kwargs = { |
|
169 | 169 | 'title': 'Delete', |
|
170 | 170 | 'suptitle': 'Location', |
|
171 | 171 | 'object': location, |
|
172 | 172 | 'previous': location.get_absolute_url(), |
|
173 | 173 | 'delete': True |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | return render(request, 'confirm.html', kwargs) |
|
177 | 177 | |
|
178 | 178 | |
|
179 | 179 | def devices(request): |
|
180 | 180 | |
|
181 | 181 | page = request.GET.get('page') |
|
182 | 182 | order = ('device_type', 'name') |
|
183 | 183 | |
|
184 | 184 | kwargs = get_paginator(Device, page, order) |
|
185 | 185 | kwargs['keys'] = ['name', 'ip_address', 'port_address', 'device_type'] |
|
186 | 186 | kwargs['title'] = 'Device' |
|
187 | 187 | kwargs['suptitle'] = 'List' |
|
188 | 188 | kwargs['no_sidebar'] = True |
|
189 | 189 | |
|
190 | 190 | return render(request, 'base_list.html', kwargs) |
|
191 | 191 | |
|
192 | 192 | |
|
193 | 193 | def device(request, id_dev): |
|
194 | 194 | |
|
195 | 195 | device = get_object_or_404(Device, pk=id_dev) |
|
196 | 196 | |
|
197 | 197 | kwargs = {} |
|
198 | 198 | kwargs['device'] = device |
|
199 | 199 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
200 | 200 | |
|
201 | 201 | kwargs['title'] = 'Device' |
|
202 | 202 | kwargs['suptitle'] = 'Details' |
|
203 | 203 | |
|
204 | 204 | return render(request, 'device.html', kwargs) |
|
205 | 205 | |
|
206 | 206 | |
|
207 | 207 | @user_passes_test(lambda u:u.is_staff) |
|
208 | 208 | def device_new(request): |
|
209 | 209 | |
|
210 | 210 | if request.method == 'GET': |
|
211 | 211 | form = DeviceForm() |
|
212 | 212 | |
|
213 | 213 | if request.method == 'POST': |
|
214 | 214 | form = DeviceForm(request.POST) |
|
215 | 215 | |
|
216 | 216 | if form.is_valid(): |
|
217 | 217 | form.save() |
|
218 | 218 | return redirect('url_devices') |
|
219 | 219 | |
|
220 | 220 | kwargs = {} |
|
221 | 221 | kwargs['form'] = form |
|
222 | 222 | kwargs['title'] = 'Device' |
|
223 | 223 | kwargs['suptitle'] = 'New' |
|
224 | 224 | kwargs['button'] = 'Create' |
|
225 | 225 | |
|
226 | 226 | return render(request, 'base_edit.html', kwargs) |
|
227 | 227 | |
|
228 | 228 | |
|
229 | 229 | @user_passes_test(lambda u:u.is_staff) |
|
230 | 230 | def device_edit(request, id_dev): |
|
231 | 231 | |
|
232 | 232 | device = get_object_or_404(Device, pk=id_dev) |
|
233 | 233 | |
|
234 | 234 | if request.method=='GET': |
|
235 | 235 | form = DeviceForm(instance=device) |
|
236 | 236 | |
|
237 | 237 | if request.method=='POST': |
|
238 | 238 | form = DeviceForm(request.POST, instance=device) |
|
239 | 239 | |
|
240 | 240 | if form.is_valid(): |
|
241 | 241 | form.save() |
|
242 | 242 | return redirect(device.get_absolute_url()) |
|
243 | 243 | |
|
244 | 244 | kwargs = {} |
|
245 | 245 | kwargs['form'] = form |
|
246 | 246 | kwargs['title'] = 'Device' |
|
247 | 247 | kwargs['suptitle'] = 'Edit' |
|
248 | 248 | kwargs['button'] = 'Update' |
|
249 | 249 | |
|
250 | 250 | return render(request, 'base_edit.html', kwargs) |
|
251 | 251 | |
|
252 | 252 | |
|
253 | 253 | @user_passes_test(lambda u:u.is_staff) |
|
254 | 254 | def device_delete(request, id_dev): |
|
255 | 255 | |
|
256 | 256 | device = get_object_or_404(Device, pk=id_dev) |
|
257 | 257 | |
|
258 | 258 | if request.method=='POST': |
|
259 | 259 | |
|
260 | 260 | if request.user.is_staff: |
|
261 | 261 | device.delete() |
|
262 | 262 | return redirect('url_devices') |
|
263 | 263 | |
|
264 | 264 | messages.error(request, 'Not enough permission to delete this object') |
|
265 | 265 | return redirect(device.get_absolute_url()) |
|
266 | 266 | |
|
267 | 267 | kwargs = { |
|
268 | 268 | 'title': 'Delete', |
|
269 | 269 | 'suptitle': 'Device', |
|
270 | 270 | 'object': device, |
|
271 | 271 | 'previous': device.get_absolute_url(), |
|
272 | 272 | 'delete': True |
|
273 | 273 | } |
|
274 | 274 | |
|
275 | 275 | return render(request, 'confirm.html', kwargs) |
|
276 | 276 | |
|
277 | 277 | |
|
278 | 278 | @user_passes_test(lambda u:u.is_staff) |
|
279 | 279 | def device_change_ip(request, id_dev): |
|
280 | 280 | |
|
281 | 281 | device = get_object_or_404(Device, pk=id_dev) |
|
282 | 282 | |
|
283 | 283 | if request.method=='POST': |
|
284 | 284 | |
|
285 | 285 | if request.user.is_staff: |
|
286 | 286 | device.change_ip(**request.POST.dict()) |
|
287 | 287 | level, message = device.message.split('|') |
|
288 | 288 | messages.add_message(request, level, message) |
|
289 | 289 | else: |
|
290 | 290 | messages.error(request, 'Not enough permission to delete this object') |
|
291 | 291 | return redirect(device.get_absolute_url()) |
|
292 | 292 | |
|
293 | 293 | kwargs = { |
|
294 | 294 | 'title': 'Device', |
|
295 | 295 | 'suptitle': 'Change IP', |
|
296 | 296 | 'object': device, |
|
297 | 297 | 'previous': device.get_absolute_url(), |
|
298 | 298 | 'form': ChangeIpForm(initial={'ip_address':device.ip_address}), |
|
299 | 299 | 'message' : ' ', |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | return render(request, 'confirm.html', kwargs) |
|
303 | 303 | |
|
304 | 304 | |
|
305 | 305 | def campaigns(request): |
|
306 | 306 | |
|
307 | 307 | page = request.GET.get('page') |
|
308 | 308 | order = ('start_date',) |
|
309 | 309 | filters = request.GET.copy() |
|
310 | 310 | |
|
311 | 311 | kwargs = get_paginator(Campaign, page, order, filters) |
|
312 | 312 | |
|
313 | 313 | form = FilterForm(initial=request.GET, extra_fields=['range_date', 'tags','template']) |
|
314 | 314 | kwargs['keys'] = ['name', 'start_date', 'end_date'] |
|
315 | 315 | kwargs['title'] = 'Campaign' |
|
316 | 316 | kwargs['suptitle'] = 'List' |
|
317 | 317 | kwargs['no_sidebar'] = True |
|
318 | 318 | kwargs['form'] = form |
|
319 | 319 | filters.pop('page', None) |
|
320 | 320 | kwargs['q'] = urlencode(filters) |
|
321 | 321 | |
|
322 | 322 | return render(request, 'base_list.html', kwargs) |
|
323 | 323 | |
|
324 | 324 | |
|
325 | 325 | def campaign(request, id_camp): |
|
326 | 326 | |
|
327 | 327 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
328 | 328 | experiments = Experiment.objects.filter(campaign=campaign) |
|
329 | 329 | |
|
330 | 330 | form = CampaignForm(instance=campaign) |
|
331 | 331 | |
|
332 | 332 | kwargs = {} |
|
333 | 333 | kwargs['campaign'] = campaign |
|
334 | 334 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] |
|
335 | 335 | |
|
336 | 336 | kwargs['experiments'] = experiments |
|
337 | 337 | kwargs['experiment_keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
338 | 338 | |
|
339 | 339 | kwargs['title'] = 'Campaign' |
|
340 | 340 | kwargs['suptitle'] = 'Details' |
|
341 | 341 | |
|
342 | 342 | kwargs['form'] = form |
|
343 | 343 | kwargs['button'] = 'Add Experiment' |
|
344 | 344 | |
|
345 | 345 | return render(request, 'campaign.html', kwargs) |
|
346 | 346 | |
|
347 | 347 | |
|
348 | 348 | @user_passes_test(lambda u:u.is_staff) |
|
349 | 349 | def campaign_new(request): |
|
350 | 350 | |
|
351 | 351 | kwargs = {} |
|
352 | 352 | |
|
353 | 353 | if request.method == 'GET': |
|
354 | 354 | |
|
355 | 355 | if 'template' in request.GET: |
|
356 | 356 | if request.GET['template']=='0': |
|
357 | 357 | form = NewForm(initial={'create_from':2}, |
|
358 | 358 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
359 | 359 | else: |
|
360 | 360 | kwargs['button'] = 'Create' |
|
361 | 361 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
362 | 362 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
363 | 363 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
364 | 364 | form = CampaignForm(instance=camp, |
|
365 | 365 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), |
|
366 | 366 | 'template':False}) |
|
367 | 367 | elif 'blank' in request.GET: |
|
368 | 368 | kwargs['button'] = 'Create' |
|
369 | 369 | form = CampaignForm() |
|
370 | 370 | else: |
|
371 | 371 | form = NewForm() |
|
372 | 372 | |
|
373 | 373 | if request.method == 'POST': |
|
374 | 374 | kwargs['button'] = 'Create' |
|
375 | 375 | post = request.POST.copy() |
|
376 | 376 | experiments = [] |
|
377 | 377 | |
|
378 | 378 | for id_exp in post.getlist('experiments'): |
|
379 | 379 | exp = Experiment.objects.get(pk=id_exp) |
|
380 | 380 | new_exp = exp.clone(template=False) |
|
381 | 381 | experiments.append(new_exp) |
|
382 | 382 | |
|
383 | 383 | post.setlist('experiments', []) |
|
384 | 384 | |
|
385 | 385 | form = CampaignForm(post) |
|
386 | 386 | |
|
387 | 387 | if form.is_valid(): |
|
388 | 388 | campaign = form.save() |
|
389 | 389 | for exp in experiments: |
|
390 | 390 | campaign.experiments.add(exp) |
|
391 | 391 | campaign.save() |
|
392 | 392 | return redirect('url_campaign', id_camp=campaign.id) |
|
393 | 393 | |
|
394 | 394 | kwargs['form'] = form |
|
395 | 395 | kwargs['title'] = 'Campaign' |
|
396 | 396 | kwargs['suptitle'] = 'New' |
|
397 | 397 | |
|
398 | 398 | return render(request, 'campaign_edit.html', kwargs) |
|
399 | 399 | |
|
400 | 400 | |
|
401 | 401 | @user_passes_test(lambda u:u.is_staff) |
|
402 | 402 | def campaign_edit(request, id_camp): |
|
403 | 403 | |
|
404 | 404 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
405 | 405 | |
|
406 | 406 | if request.method=='GET': |
|
407 | 407 | form = CampaignForm(instance=campaign) |
|
408 | 408 | |
|
409 | 409 | if request.method=='POST': |
|
410 | 410 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
411 | 411 | post = request.POST.copy() |
|
412 | 412 | new_exps = post.getlist('experiments') |
|
413 | 413 | post.setlist('experiments', []) |
|
414 | 414 | form = CampaignForm(post, instance=campaign) |
|
415 | 415 | |
|
416 | 416 | if form.is_valid(): |
|
417 | 417 | camp = form.save() |
|
418 | 418 | for id_exp in new_exps: |
|
419 | 419 | if int(id_exp) in exps: |
|
420 | 420 | exps.pop(id_exp) |
|
421 | 421 | else: |
|
422 | 422 | exp = Experiment.objects.get(pk=id_exp) |
|
423 | 423 | if exp.template: |
|
424 | 424 | camp.experiments.add(exp.clone(template=False)) |
|
425 | 425 | else: |
|
426 | 426 | camp.experiments.add(exp) |
|
427 | 427 | |
|
428 | 428 | for id_exp in exps: |
|
429 | 429 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
430 | 430 | |
|
431 | 431 | return redirect('url_campaign', id_camp=id_camp) |
|
432 | 432 | |
|
433 | 433 | kwargs = {} |
|
434 | 434 | kwargs['form'] = form |
|
435 | 435 | kwargs['title'] = 'Campaign' |
|
436 | 436 | kwargs['suptitle'] = 'Edit' |
|
437 | 437 | kwargs['button'] = 'Update' |
|
438 | 438 | |
|
439 | 439 | return render(request, 'campaign_edit.html', kwargs) |
|
440 | 440 | |
|
441 | 441 | |
|
442 | 442 | @user_passes_test(lambda u:u.is_staff) |
|
443 | 443 | def campaign_delete(request, id_camp): |
|
444 | 444 | |
|
445 | 445 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
446 | 446 | |
|
447 | 447 | if request.method=='POST': |
|
448 | 448 | if request.user.is_staff: |
|
449 | 449 | |
|
450 | 450 | for exp in campaign.experiments.all(): |
|
451 | 451 | for conf in Configuration.objects.filter(experiment=exp): |
|
452 | 452 | conf.delete() |
|
453 | 453 | exp.delete() |
|
454 | 454 | campaign.delete() |
|
455 | 455 | |
|
456 | 456 | return redirect('url_campaigns') |
|
457 | 457 | |
|
458 | 458 | messages.error(request, 'Not enough permission to delete this object') |
|
459 | 459 | return redirect(campaign.get_absolute_url()) |
|
460 | 460 | |
|
461 | 461 | kwargs = { |
|
462 | 462 | 'title': 'Delete', |
|
463 | 463 | 'suptitle': 'Campaign', |
|
464 | 464 | 'object': campaign, |
|
465 | 465 | 'previous': campaign.get_absolute_url(), |
|
466 | 466 | 'delete': True |
|
467 | 467 | } |
|
468 | 468 | |
|
469 | 469 | return render(request, 'confirm.html', kwargs) |
|
470 | 470 | |
|
471 | 471 | |
|
472 | 472 | @user_passes_test(lambda u:u.is_staff) |
|
473 | 473 | def campaign_export(request, id_camp): |
|
474 | 474 | |
|
475 | 475 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
476 | 476 | content = campaign.parms_to_dict() |
|
477 | 477 | content_type = 'application/json' |
|
478 | 478 | filename = '%s_%s.json' %(campaign.name, campaign.id) |
|
479 | 479 | |
|
480 | 480 | response = HttpResponse(content_type=content_type) |
|
481 | 481 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
482 | 482 | response.write(content) |
|
483 | 483 | |
|
484 | 484 | return response |
|
485 | 485 | |
|
486 | 486 | |
|
487 | 487 | @user_passes_test(lambda u:u.is_staff) |
|
488 | 488 | def campaign_import(request, id_camp): |
|
489 | 489 | |
|
490 | 490 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
491 | 491 | |
|
492 | 492 | if request.method == 'GET': |
|
493 | 493 | file_form = UploadFileForm() |
|
494 | 494 | |
|
495 | 495 | if request.method == 'POST': |
|
496 | 496 | file_form = UploadFileForm(request.POST, request.FILES) |
|
497 | 497 | |
|
498 | 498 | if file_form.is_valid(): |
|
499 | 499 | |
|
500 | 500 | parms = campaign.import_from_file(request.FILES['file']) |
|
501 | 501 | |
|
502 | 502 | if parms: |
|
503 | 503 | parms['name'] = parms['campaign'] |
|
504 | 504 | |
|
505 | 505 | new_camp = campaign.dict_to_parms(parms, CONF_MODELS) |
|
506 | 506 | |
|
507 | 507 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
508 | 508 | |
|
509 | 509 | return redirect(new_camp.get_absolute_url_edit()) |
|
510 | 510 | |
|
511 | 511 | messages.error(request, "Could not import parameters from file") |
|
512 | 512 | |
|
513 | 513 | kwargs = {} |
|
514 | 514 | kwargs['title'] = 'Campaign' |
|
515 | 515 | kwargs['form'] = file_form |
|
516 | 516 | kwargs['suptitle'] = 'Importing file' |
|
517 | 517 | kwargs['button'] = 'Import' |
|
518 | 518 | |
|
519 | 519 | return render(request, 'campaign_import.html', kwargs) |
|
520 | 520 | |
|
521 | 521 | |
|
522 | 522 | def experiments(request): |
|
523 | 523 | |
|
524 | 524 | page = request.GET.get('page') |
|
525 | 525 | order = ('location',) |
|
526 | 526 | filters = request.GET.copy() |
|
527 | 527 | |
|
528 | 528 | kwargs = get_paginator(Experiment, page, order, filters) |
|
529 | 529 | |
|
530 | 530 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
531 | 531 | |
|
532 | 532 | kwargs['keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
533 | 533 | kwargs['title'] = 'Experiment' |
|
534 | 534 | kwargs['suptitle'] = 'List' |
|
535 | 535 | kwargs['no_sidebar'] = True |
|
536 | 536 | kwargs['form'] = form |
|
537 | 537 | filters.pop('page', None) |
|
538 | 538 | kwargs['q'] = urlencode(filters) |
|
539 | 539 | |
|
540 | 540 | return render(request, 'base_list.html', kwargs) |
|
541 | 541 | |
|
542 | 542 | |
|
543 | 543 | def experiment(request, id_exp): |
|
544 | 544 | |
|
545 | 545 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
546 | experiment.get_status() | |
|
546 | #experiment.get_status() | |
|
547 | 547 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
548 | 548 | |
|
549 | 549 | kwargs = {} |
|
550 | 550 | |
|
551 | 551 | kwargs['experiment_keys'] = ['radar_system', 'name', 'start_time', 'end_time'] |
|
552 | 552 | kwargs['experiment'] = experiment |
|
553 | 553 | |
|
554 | 554 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
555 | 555 | kwargs['configurations'] = configurations |
|
556 | 556 | |
|
557 | 557 | kwargs['title'] = 'Experiment' |
|
558 | 558 | kwargs['suptitle'] = 'Details' |
|
559 | 559 | |
|
560 | 560 | kwargs['button'] = 'Add Configuration' |
|
561 | 561 | |
|
562 | 562 | ###### SIDEBAR ###### |
|
563 | 563 | kwargs.update(sidebar(experiment=experiment)) |
|
564 | 564 | |
|
565 | 565 | return render(request, 'experiment.html', kwargs) |
|
566 | 566 | |
|
567 | 567 | |
|
568 | 568 | @user_passes_test(lambda u:u.is_staff) |
|
569 | 569 | def experiment_new(request, id_camp=None): |
|
570 | 570 | |
|
571 | 571 | kwargs = {} |
|
572 | 572 | |
|
573 | 573 | if request.method == 'GET': |
|
574 | 574 | if 'template' in request.GET: |
|
575 | 575 | if request.GET['template']=='0': |
|
576 | 576 | form = NewForm(initial={'create_from':2}, |
|
577 | 577 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
578 | 578 | else: |
|
579 | 579 | kwargs['button'] = 'Create' |
|
580 | 580 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
581 | 581 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
582 | 582 | exp=Experiment.objects.get(pk=request.GET['template']) |
|
583 | 583 | form = ExperimentForm(instance=exp, |
|
584 | 584 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), |
|
585 | 585 | 'template': False}) |
|
586 | 586 | elif 'blank' in request.GET: |
|
587 | 587 | kwargs['button'] = 'Create' |
|
588 | 588 | form = ExperimentForm() |
|
589 | 589 | else: |
|
590 | 590 | form = NewForm() |
|
591 | 591 | |
|
592 | 592 | if request.method == 'POST': |
|
593 | 593 | form = ExperimentForm(request.POST) |
|
594 | 594 | if form.is_valid(): |
|
595 | 595 | experiment = form.save() |
|
596 | 596 | |
|
597 | 597 | if 'template' in request.GET: |
|
598 | 598 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
599 | 599 | for conf in configurations: |
|
600 | 600 | conf.clone(experiment=experiment, template=False) |
|
601 | 601 | |
|
602 | 602 | return redirect('url_experiment', id_exp=experiment.id) |
|
603 | 603 | |
|
604 | 604 | kwargs['form'] = form |
|
605 | 605 | kwargs['title'] = 'Experiment' |
|
606 | 606 | kwargs['suptitle'] = 'New' |
|
607 | 607 | |
|
608 | 608 | return render(request, 'experiment_edit.html', kwargs) |
|
609 | 609 | |
|
610 | 610 | |
|
611 | 611 | @user_passes_test(lambda u:u.is_staff) |
|
612 | 612 | def experiment_edit(request, id_exp): |
|
613 | 613 | |
|
614 | 614 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
615 | 615 | |
|
616 | 616 | if request.method == 'GET': |
|
617 | 617 | form = ExperimentForm(instance=experiment) |
|
618 | 618 | |
|
619 | 619 | if request.method=='POST': |
|
620 | 620 | form = ExperimentForm(request.POST, instance=experiment) |
|
621 | 621 | |
|
622 | 622 | if form.is_valid(): |
|
623 | 623 | experiment = form.save() |
|
624 | 624 | return redirect('url_experiment', id_exp=experiment.id) |
|
625 | 625 | |
|
626 | 626 | kwargs = {} |
|
627 | 627 | kwargs['form'] = form |
|
628 | 628 | kwargs['title'] = 'Experiment' |
|
629 | 629 | kwargs['suptitle'] = 'Edit' |
|
630 | 630 | kwargs['button'] = 'Update' |
|
631 | 631 | |
|
632 | 632 | return render(request, 'experiment_edit.html', kwargs) |
|
633 | 633 | |
|
634 | 634 | |
|
635 | 635 | @user_passes_test(lambda u:u.is_staff) |
|
636 | 636 | def experiment_delete(request, id_exp): |
|
637 | 637 | |
|
638 | 638 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
639 | 639 | |
|
640 | 640 | if request.method=='POST': |
|
641 | 641 | if request.user.is_staff: |
|
642 | 642 | for conf in Configuration.objects.filter(experiment=experiment): |
|
643 | 643 | conf.delete() |
|
644 | 644 | experiment.delete() |
|
645 | 645 | return redirect('url_experiments') |
|
646 | 646 | |
|
647 | 647 | messages.error(request, 'Not enough permission to delete this object') |
|
648 | 648 | return redirect(experiment.get_absolute_url()) |
|
649 | 649 | |
|
650 | 650 | kwargs = { |
|
651 | 651 | 'title': 'Delete', |
|
652 | 652 | 'suptitle': 'Experiment', |
|
653 | 653 | 'object': experiment, |
|
654 | 654 | 'previous': experiment.get_absolute_url(), |
|
655 | 655 | 'delete': True |
|
656 | 656 | } |
|
657 | 657 | |
|
658 | 658 | return render(request, 'confirm.html', kwargs) |
|
659 | 659 | |
|
660 | 660 | |
|
661 | 661 | @user_passes_test(lambda u:u.is_staff) |
|
662 | 662 | def experiment_export(request, id_exp): |
|
663 | 663 | |
|
664 | 664 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
665 | 665 | content = experiment.parms_to_dict() |
|
666 | 666 | content_type = 'application/json' |
|
667 | 667 | filename = '%s_%s.json' %(experiment.name, experiment.id) |
|
668 | 668 | |
|
669 | 669 | response = HttpResponse(content_type=content_type) |
|
670 | 670 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
671 | 671 | response.write(content) |
|
672 | 672 | |
|
673 | 673 | return response |
|
674 | 674 | |
|
675 | 675 | |
|
676 | 676 | @user_passes_test(lambda u:u.is_staff) |
|
677 | 677 | def experiment_import(request, id_exp): |
|
678 | 678 | |
|
679 | 679 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
680 | 680 | configurations = Configuration.objects.filter(experiment=experiment) |
|
681 | 681 | |
|
682 | 682 | if request.method == 'GET': |
|
683 | 683 | file_form = UploadFileForm() |
|
684 | 684 | |
|
685 | 685 | if request.method == 'POST': |
|
686 | 686 | file_form = UploadFileForm(request.POST, request.FILES) |
|
687 | 687 | |
|
688 | 688 | if file_form.is_valid(): |
|
689 | 689 | |
|
690 | 690 | parms = experiment.import_from_file(request.FILES['file']) |
|
691 | 691 | |
|
692 | 692 | if parms: |
|
693 | 693 | |
|
694 | 694 | new_exp = experiment.dict_to_parms(parms, CONF_MODELS) |
|
695 | 695 | |
|
696 | 696 | if new_exp.__class__.__name__=='dict': |
|
697 | 697 | messages.error(request, new_exp['Error'] ) |
|
698 | 698 | return redirect(experiment.get_absolute_url_import()) |
|
699 | 699 | |
|
700 | 700 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
701 | 701 | |
|
702 | 702 | return redirect(new_exp.get_absolute_url_edit()) |
|
703 | 703 | |
|
704 | 704 | messages.error(request, "Could not import parameters from file") |
|
705 | 705 | |
|
706 | 706 | kwargs = {} |
|
707 | 707 | kwargs['title'] = 'Experiment' |
|
708 | 708 | kwargs['form'] = file_form |
|
709 | 709 | kwargs['suptitle'] = 'Importing file' |
|
710 | 710 | kwargs['button'] = 'Import' |
|
711 | 711 | |
|
712 | 712 | kwargs.update(sidebar(experiment=experiment)) |
|
713 | 713 | |
|
714 | 714 | return render(request, 'experiment_import.html', kwargs) |
|
715 | 715 | |
|
716 | 716 | |
|
717 | 717 | @user_passes_test(lambda u:u.is_staff) |
|
718 | 718 | def experiment_start(request, id_exp): |
|
719 | 719 | |
|
720 | 720 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
721 | 721 | |
|
722 | if exp.status == 2: | |
|
723 | messages.warning(request, 'Experiment {} already running'.format(exp)) | |
|
722 | confs = Configuration.objects.filter(experiment=exp, type=0) | |
|
723 | if not confs: | |
|
724 | messages.error(request, 'Experiment not running. No Device Configurations detected.') | |
|
725 | return redirect(exp.get_absolute_url()) | |
|
724 | 726 | |
|
725 | elif exp.status == 3: | |
|
726 | messages.warning(request, 'Experiment {} already programmed'.format(exp)) | |
|
727 | for conf in confs: | |
|
728 | conf.status_device() | |
|
729 | if conf.device.status != 2: | |
|
730 | messages.error(request, 'Experiment not running. Configuration {} state: '.format(conf) + DEV_STATES[conf.device.status][1]) | |
|
731 | return redirect(exp.get_absolute_url()) | |
|
727 | 732 | |
|
728 | if not (exp.start_time < datetime.now().time() < exp.end_time): | |
|
729 | exp.status==1 | |
|
730 | exp.save() | |
|
731 | messages.warning(request, 'Experiment {} is out of time'.format(exp)) | |
|
732 | return redirect(exp.get_absolute_url()) | |
|
733 | exp.get_status() | |
|
733 | 734 | |
|
735 | if exp.status == 2: | |
|
736 | messages.warning(request, 'Experiment {} already running'.format(exp)) | |
|
734 | 737 | else: |
|
735 | 738 | task = task_start.delay(exp.pk) |
|
736 | 739 | exp.status = task.wait() |
|
737 | 740 | if exp.status==0: |
|
738 | 741 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
739 | 742 | if exp.status==2: |
|
740 | 743 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
741 | ||
|
742 | exp.save() | |
|
744 | exp.save() | |
|
743 | 745 | |
|
744 | 746 | return redirect(exp.get_absolute_url()) |
|
745 | 747 | |
|
746 | 748 | |
|
747 | 749 | @user_passes_test(lambda u:u.is_staff) |
|
748 | 750 | def experiment_stop(request, id_exp): |
|
749 | 751 | |
|
750 | 752 | exp = get_object_or_404(Experiment, pk=id_exp) |
|
753 | confs = Configuration.objects.filter(experiment=exp, type=0) | |
|
754 | if not confs: | |
|
755 | messages.error(request, 'No Device Configurations detected.') | |
|
756 | return redirect(exp.get_absolute_url()) | |
|
751 | 757 | |
|
758 | exp.get_status() | |
|
759 | ||
|
752 | 760 | if exp.status == 2: |
|
753 | 761 | task = task_stop.delay(exp.pk) |
|
754 | 762 | exp.status = task.wait() |
|
755 | 763 | messages.warning(request, 'Experiment {} stopped'.format(exp)) |
|
756 | 764 | exp.save() |
|
757 | 765 | else: |
|
758 | 766 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
759 | 767 | |
|
760 | 768 | return redirect(exp.get_absolute_url()) |
|
761 | 769 | |
|
762 | 770 | |
|
763 | 771 | @user_passes_test(lambda u:u.is_staff) |
|
764 | 772 | def experiment_mix(request, id_exp): |
|
765 | 773 | |
|
766 | 774 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
767 | 775 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, |
|
768 | 776 | mix=False)] |
|
769 | 777 | |
|
770 | 778 | if len(rc_confs)<2: |
|
771 | 779 | messages.warning(request, 'You need at least two RC Configurations to make a mix') |
|
772 | 780 | return redirect(experiment.get_absolute_url()) |
|
773 | 781 | |
|
774 | 782 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) |
|
775 | 783 | |
|
776 | 784 | if mix_confs: |
|
777 | 785 | mix = mix_confs[0] |
|
778 | 786 | else: |
|
779 | 787 | mix = RCConfiguration(experiment=experiment, |
|
780 | 788 | device=rc_confs[0].device, |
|
781 | 789 | ipp=rc_confs[0].ipp, |
|
782 | 790 | clock_in=rc_confs[0].clock_in, |
|
783 | 791 | clock_divider=rc_confs[0].clock_divider, |
|
784 | 792 | mix=True, |
|
785 | 793 | parameters='') |
|
786 | 794 | mix.save() |
|
787 | 795 | |
|
788 | 796 | line_type = RCLineType.objects.get(name='mix') |
|
789 | 797 | for i in range(len(rc_confs[0].get_lines())): |
|
790 | 798 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
791 | 799 | line.save() |
|
792 | 800 | |
|
793 | 801 | initial = {'name': mix.name, |
|
794 | 802 | 'result': parse_mix_result(mix.parameters), |
|
795 | 803 | 'delay': 0, |
|
796 | 804 | 'mask': [0,1,2,3,4,5,6,7] |
|
797 | 805 | } |
|
798 | 806 | |
|
799 | 807 | if request.method=='GET': |
|
800 | 808 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
801 | 809 | |
|
802 | 810 | if request.method=='POST': |
|
803 | 811 | result = mix.parameters |
|
804 | 812 | |
|
805 | 813 | if '{}|'.format(request.POST['experiment']) in result: |
|
806 | 814 | messages.error(request, 'Configuration already added') |
|
807 | 815 | else: |
|
808 | 816 | if 'operation' in request.POST: |
|
809 | 817 | operation = MIX_OPERATIONS[request.POST['operation']] |
|
810 | 818 | else: |
|
811 | 819 | operation = ' ' |
|
812 | 820 | |
|
813 | 821 | mode = MIX_MODES[request.POST['mode']] |
|
814 | 822 | |
|
815 | 823 | if result: |
|
816 | 824 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, |
|
817 | 825 | request.POST['experiment'], |
|
818 | 826 | mode, |
|
819 | 827 | operation, |
|
820 | 828 | float(request.POST['delay']), |
|
821 | 829 | parse_mask(request.POST.getlist('mask')) |
|
822 | 830 | ) |
|
823 | 831 | else: |
|
824 | 832 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], |
|
825 | 833 | mode, |
|
826 | 834 | operation, |
|
827 | 835 | float(request.POST['delay']), |
|
828 | 836 | parse_mask(request.POST.getlist('mask')) |
|
829 | 837 | ) |
|
830 | 838 | |
|
831 | 839 | mix.parameters = result |
|
832 | 840 | mix.name = request.POST['name'] |
|
833 | 841 | mix.save() |
|
834 | 842 | mix.update_pulses() |
|
835 | 843 | |
|
836 | 844 | initial['result'] = parse_mix_result(result) |
|
837 | 845 | initial['name'] = mix.name |
|
838 | 846 | |
|
839 | 847 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
840 | 848 | |
|
841 | 849 | |
|
842 | 850 | kwargs = { |
|
843 | 851 | 'title': 'Experiment', |
|
844 | 852 | 'suptitle': 'Mix Configurations', |
|
845 | 853 | 'form' : form, |
|
846 | 854 | 'extra_button': 'Delete', |
|
847 | 855 | 'button': 'Add', |
|
848 | 856 | 'cancel': 'Back', |
|
849 | 857 | 'previous': experiment.get_absolute_url(), |
|
850 | 858 | 'id_exp':id_exp, |
|
851 | 859 | |
|
852 | 860 | } |
|
853 | 861 | |
|
854 | 862 | return render(request, 'experiment_mix.html', kwargs) |
|
855 | 863 | |
|
856 | 864 | |
|
857 | 865 | @user_passes_test(lambda u:u.is_staff) |
|
858 | 866 | def experiment_mix_delete(request, id_exp): |
|
859 | 867 | |
|
860 | 868 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) |
|
861 | 869 | values = conf.parameters.split('-') |
|
862 | 870 | conf.parameters = '-'.join(values[:-1]) |
|
863 | 871 | conf.save() |
|
864 | 872 | |
|
865 | 873 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
866 | 874 | |
|
867 | 875 | |
|
868 | 876 | def experiment_summary(request, id_exp): |
|
869 | 877 | |
|
870 | 878 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
871 | 879 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
872 | 880 | |
|
873 | 881 | kwargs = {} |
|
874 | 882 | |
|
875 | 883 | kwargs['experiment_keys'] = ['radar_system', 'name', 'start_time', 'end_time'] |
|
876 | 884 | kwargs['experiment'] = experiment |
|
877 | 885 | |
|
878 | 886 | kwargs['configurations'] = [] |
|
879 | 887 | |
|
880 | 888 | kwargs['title'] = 'Experiment Summary' |
|
881 | 889 | kwargs['suptitle'] = 'Details' |
|
882 | 890 | |
|
883 | 891 | kwargs['button'] = 'Verify Parameters' |
|
884 | 892 | |
|
885 | 893 | jars_conf = False |
|
886 | 894 | rc_conf = False |
|
887 | 895 | |
|
888 | 896 | for configuration in configurations: |
|
889 | 897 | |
|
890 | 898 | #--------------------- RC ----------------------: |
|
891 | 899 | if configuration.device.device_type.name == 'rc': |
|
892 | 900 | if configuration.mix: |
|
893 | 901 | continue |
|
894 | 902 | rc_conf = True |
|
895 | 903 | ipp = configuration.ipp |
|
896 | 904 | lines = configuration.get_lines(line_type__name='tx') |
|
897 | 905 | configuration.tx_lines = [] |
|
898 | 906 | |
|
899 | 907 | for tx_line in lines: |
|
900 | 908 | line = {'name':tx_line.get_name()} |
|
901 | 909 | tx_params = json.loads(tx_line.params) |
|
902 | 910 | line['width'] = tx_params['pulse_width'] |
|
903 | 911 | if line['width'] in (0, '0'): |
|
904 | 912 | continue |
|
905 | 913 | delays = tx_params['delays'] |
|
906 | 914 | |
|
907 | 915 | if delays not in ('', '0'): |
|
908 | 916 | n = len(delays.split(',')) |
|
909 | 917 | line['taus'] = '{} Taus: {}'.format(n, delays) |
|
910 | 918 | else: |
|
911 | 919 | line['taus'] = '-' |
|
912 | 920 | |
|
913 | 921 | for code_line in configuration.get_lines(line_type__name='codes'): |
|
914 | 922 | code_params = json.loads(code_line.params) |
|
915 | 923 | if tx_line.pk==int(code_params['TX_ref']): |
|
916 | 924 | line['codes'] = '{}:{}'.format(RCLineCode.objects.get(pk=code_params['code']), |
|
917 | 925 | '-'.join(code_params['codes'])) |
|
918 | 926 | |
|
919 | 927 | for windows_line in configuration.get_lines(line_type__name='windows'): |
|
920 | 928 | win_params = json.loads(windows_line.params) |
|
921 | 929 | if tx_line.pk==int(win_params['TX_ref']): |
|
922 | 930 | windows = '' |
|
923 | 931 | for i, params in enumerate(win_params['params']): |
|
924 | 932 | windows += 'W{}: Ho={first_height} km DH={resolution} km NSA={number_of_samples}<br>'.format(i, **params) |
|
925 | 933 | line['windows'] = mark_safe(windows) |
|
926 | 934 | |
|
927 | 935 | configuration.tx_lines.append(line) |
|
928 | 936 | |
|
929 | 937 | #-------------------- JARS -----------------------: |
|
930 | 938 | if configuration.device.device_type.name == 'jars': |
|
931 | 939 | jars_conf = True |
|
932 | 940 | kwargs['exp_type'] = EXPERIMENT_TYPE[configuration.exp_type][1] |
|
933 | 941 | channels_number = configuration.channels_number |
|
934 | 942 | exp_type = configuration.exp_type |
|
935 | 943 | fftpoints = configuration.fftpoints |
|
936 | 944 | filter_parms = configuration.filter_parms |
|
937 | 945 | filter_parms = ast.literal_eval(filter_parms) |
|
938 | 946 | spectral_number = configuration.spectral_number |
|
939 | 947 | |
|
940 | 948 | |
|
941 | 949 | kwargs['configurations'].append(configuration) |
|
942 | 950 | |
|
943 | 951 | |
|
944 | 952 | #------ RC & JARS ------: |
|
945 | 953 | ipp = 937.5 # |
|
946 | 954 | nsa = 200# |
|
947 | 955 | dh = 1.5 # |
|
948 | 956 | channels_number = 5 # |
|
949 | 957 | |
|
950 | 958 | if rc_conf and jars_conf: |
|
951 | 959 | if exp_type == 0: #Short |
|
952 | 960 | bytes = 2 |
|
953 | 961 | b = nsa*2*bytes*channels_number |
|
954 | 962 | else: #Float |
|
955 | 963 | bytes = 4 |
|
956 | 964 | channels = channels_number + spectral_number |
|
957 | 965 | b = nsa*2*bytes*fftpoints*channels |
|
958 | 966 | |
|
959 | 967 | ipps = (ipp*pow(10,-6))/0.15 |
|
960 | 968 | GB = 1048576.0*1024.0 |
|
961 | 969 | Hour = 3600 |
|
962 | 970 | rate = b/ipps |
|
963 | 971 | rate = rate *(1/GB)*(Hour) |
|
964 | 972 | kwargs['rate'] = str(rate)+" GB/h" |
|
965 | 973 | else: |
|
966 | 974 | kwargs['rate'] = '' |
|
967 | 975 | |
|
968 | 976 | ###### SIDEBAR ###### |
|
969 | 977 | kwargs.update(sidebar(experiment=experiment)) |
|
970 | 978 | |
|
971 | 979 | return render(request, 'experiment_summary.html', kwargs) |
|
972 | 980 | |
|
973 | 981 | |
|
974 | 982 | @user_passes_test(lambda u:u.is_staff) |
|
975 | 983 | def experiment_verify(request, id_exp): |
|
976 | 984 | |
|
977 | 985 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
978 | 986 | experiment_data = json.loads(experiment.parms_to_dict()) |
|
979 | 987 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
980 | 988 | |
|
981 | 989 | kwargs = {} |
|
982 | 990 | |
|
983 | 991 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'start_time', 'end_time'] |
|
984 | 992 | kwargs['experiment'] = experiment |
|
985 | 993 | |
|
986 | 994 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
987 | 995 | kwargs['configurations'] = configurations |
|
988 | 996 | kwargs['experiment_data'] = experiment_data |
|
989 | 997 | |
|
990 | 998 | kwargs['title'] = 'Verify Experiment' |
|
991 | 999 | kwargs['suptitle'] = 'Parameters' |
|
992 | 1000 | |
|
993 | 1001 | kwargs['button'] = 'Update' |
|
994 | 1002 | |
|
995 | 1003 | jars_conf = False |
|
996 | 1004 | rc_conf = False |
|
997 | 1005 | dds_conf = False |
|
998 | 1006 | |
|
999 | 1007 | for configuration in configurations: |
|
1000 | 1008 | #-------------------- JARS -----------------------: |
|
1001 | 1009 | if configuration.device.device_type.name == 'jars': |
|
1002 | 1010 | jars_conf = True |
|
1003 | 1011 | jars = configuration |
|
1004 | 1012 | kwargs['jars_conf'] = jars_conf |
|
1005 | 1013 | filter_parms = configuration.filter_parms |
|
1006 | 1014 | filter_parms = ast.literal_eval(filter_parms) |
|
1007 | 1015 | kwargs['filter_parms'] = filter_parms |
|
1008 | 1016 | #--Sampling Frequency |
|
1009 | 1017 | clock = filter_parms['clock'] |
|
1010 | 1018 | filter_2 = filter_parms['filter_2'] |
|
1011 | 1019 | filter_5 = filter_parms['filter_5'] |
|
1012 | 1020 | filter_fir = filter_parms['filter_fir'] |
|
1013 | 1021 | samp_freq_jars = clock/filter_2/filter_5/filter_fir |
|
1014 | 1022 | |
|
1015 | 1023 | kwargs['samp_freq_jars'] = samp_freq_jars |
|
1016 | 1024 | kwargs['jars'] = configuration |
|
1017 | 1025 | |
|
1018 | 1026 | #--------------------- RC ----------------------: |
|
1019 | 1027 | if configuration.device.device_type.name == 'rc' and not configuration.mix: |
|
1020 | 1028 | rc_conf = True |
|
1021 | 1029 | rc = configuration |
|
1022 | 1030 | |
|
1023 | 1031 | rc_parms = configuration.parms_to_dict() |
|
1024 | 1032 | |
|
1025 | 1033 | win_lines = rc.get_lines(line_type__name='windows') |
|
1026 | 1034 | if win_lines: |
|
1027 | 1035 | dh = json.loads(win_lines[0].params)['params'][0]['resolution'] |
|
1028 | 1036 | #--Sampling Frequency |
|
1029 | 1037 | samp_freq_rc = 0.15/dh |
|
1030 | 1038 | kwargs['samp_freq_rc'] = samp_freq_rc |
|
1031 | 1039 | |
|
1032 | 1040 | kwargs['rc_conf'] = rc_conf |
|
1033 | 1041 | kwargs['rc'] = configuration |
|
1034 | 1042 | |
|
1035 | 1043 | #-------------------- DDS ----------------------: |
|
1036 | 1044 | if configuration.device.device_type.name == 'dds': |
|
1037 | 1045 | dds_conf = True |
|
1038 | 1046 | dds = configuration |
|
1039 | 1047 | dds_parms = configuration.parms_to_dict() |
|
1040 | 1048 | |
|
1041 | 1049 | kwargs['dds_conf'] = dds_conf |
|
1042 | 1050 | kwargs['dds'] = configuration |
|
1043 | 1051 | |
|
1044 | 1052 | |
|
1045 | 1053 | #------------Validation------------: |
|
1046 | 1054 | #Clock |
|
1047 | 1055 | if dds_conf and rc_conf and jars_conf: |
|
1048 | 1056 | if float(filter_parms['clock']) != float(rc_parms['clock_in']) and float(rc_parms['clock_in']) != float(dds_parms['clock']): |
|
1049 | 1057 | messages.warning(request, "Devices don't have the same clock.") |
|
1050 | 1058 | elif rc_conf and jars_conf: |
|
1051 | 1059 | if float(filter_parms['clock']) != float(rc_parms['clock_in']): |
|
1052 | 1060 | messages.warning(request, "Devices don't have the same clock.") |
|
1053 | 1061 | elif rc_conf and dds_conf: |
|
1054 | 1062 | if float(rc_parms['clock_in']) != float(dds_parms['clock']): |
|
1055 | 1063 | messages.warning(request, "Devices don't have the same clock.") |
|
1056 | 1064 | if float(samp_freq_rc) != float(dds_parms['frequencyA']): |
|
1057 | 1065 | messages.warning(request, "Devices don't have the same Frequency A.") |
|
1058 | 1066 | |
|
1059 | 1067 | |
|
1060 | 1068 | #------------POST METHOD------------: |
|
1061 | 1069 | if request.method == 'POST': |
|
1062 | 1070 | if request.POST['suggest_clock']: |
|
1063 | 1071 | try: |
|
1064 | 1072 | suggest_clock = float(request.POST['suggest_clock']) |
|
1065 | 1073 | except: |
|
1066 | 1074 | messages.warning(request, "Invalid value in CLOCK IN.") |
|
1067 | 1075 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1068 | 1076 | else: |
|
1069 | 1077 | suggest_clock = "" |
|
1070 | 1078 | if suggest_clock: |
|
1071 | 1079 | if rc_conf: |
|
1072 | 1080 | rc.clock_in = suggest_clock |
|
1073 | 1081 | rc.save() |
|
1074 | 1082 | if jars_conf: |
|
1075 | 1083 | filter_parms = jars.filter_parms |
|
1076 | 1084 | filter_parms = ast.literal_eval(filter_parms) |
|
1077 | 1085 | filter_parms['clock'] = suggest_clock |
|
1078 | 1086 | jars.filter_parms = json.dumps(filter_parms) |
|
1079 | 1087 | jars.save() |
|
1080 | 1088 | kwargs['filter_parms'] = filter_parms |
|
1081 | 1089 | if dds_conf: |
|
1082 | 1090 | dds.clock = suggest_clock |
|
1083 | 1091 | dds.save() |
|
1084 | 1092 | |
|
1085 | 1093 | if request.POST['suggest_frequencyA']: |
|
1086 | 1094 | try: |
|
1087 | 1095 | suggest_frequencyA = float(request.POST['suggest_frequencyA']) |
|
1088 | 1096 | except: |
|
1089 | 1097 | messages.warning(request, "Invalid value in FREQUENCY A.") |
|
1090 | 1098 | return redirect('url_verify_experiment', id_exp=experiment.id) |
|
1091 | 1099 | else: |
|
1092 | 1100 | suggest_frequencyA = "" |
|
1093 | 1101 | if suggest_frequencyA: |
|
1094 | 1102 | if jars_conf: |
|
1095 | 1103 | filter_parms = jars.filter_parms |
|
1096 | 1104 | filter_parms = ast.literal_eval(filter_parms) |
|
1097 | 1105 | filter_parms['fch'] = suggest_frequencyA |
|
1098 | 1106 | jars.filter_parms = json.dumps(filter_parms) |
|
1099 | 1107 | jars.save() |
|
1100 | 1108 | kwargs['filter_parms'] = filter_parms |
|
1101 | 1109 | if dds_conf: |
|
1102 | 1110 | dds.frequencyA_Mhz = request.POST['suggest_frequencyA'] |
|
1103 | 1111 | dds.save() |
|
1104 | 1112 | |
|
1105 | 1113 | ###### SIDEBAR ###### |
|
1106 | 1114 | kwargs.update(sidebar(experiment=experiment)) |
|
1107 | 1115 | |
|
1108 | 1116 | |
|
1109 | 1117 | |
|
1110 | 1118 | |
|
1111 | 1119 | |
|
1112 | 1120 | return render(request, 'experiment_verify.html', kwargs) |
|
1113 | 1121 | |
|
1114 | 1122 | |
|
1115 | 1123 | #@user_passes_test(lambda u:u.is_staff) |
|
1116 | 1124 | def parse_mix_result(s): |
|
1117 | 1125 | |
|
1118 | 1126 | values = s.split('-') |
|
1119 | 1127 | html = 'EXP MOD OPE DELAY MASK\r\n' |
|
1120 | 1128 | |
|
1121 | 1129 | if not values or values[0] in ('', ' '): |
|
1122 | 1130 | return mark_safe(html) |
|
1123 | 1131 | |
|
1124 | 1132 | for i, value in enumerate(values): |
|
1125 | 1133 | if not value: |
|
1126 | 1134 | continue |
|
1127 | 1135 | pk, mode, operation, delay, mask = value.split('|') |
|
1128 | 1136 | conf = RCConfiguration.objects.get(pk=pk) |
|
1129 | 1137 | if i==0: |
|
1130 | 1138 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1131 | 1139 | conf.name, |
|
1132 | 1140 | mode, |
|
1133 | 1141 | ' ', |
|
1134 | 1142 | delay, |
|
1135 | 1143 | mask) |
|
1136 | 1144 | else: |
|
1137 | 1145 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
1138 | 1146 | conf.name, |
|
1139 | 1147 | mode, |
|
1140 | 1148 | operation, |
|
1141 | 1149 | delay, |
|
1142 | 1150 | mask) |
|
1143 | 1151 | |
|
1144 | 1152 | return mark_safe(html) |
|
1145 | 1153 | |
|
1146 | 1154 | def parse_mask(l): |
|
1147 | 1155 | |
|
1148 | 1156 | values = [] |
|
1149 | 1157 | |
|
1150 | 1158 | for x in range(8): |
|
1151 | 1159 | if '{}'.format(x) in l: |
|
1152 | 1160 | values.append(1) |
|
1153 | 1161 | else: |
|
1154 | 1162 | values.append(0) |
|
1155 | 1163 | |
|
1156 | 1164 | values.reverse() |
|
1157 | 1165 | |
|
1158 | 1166 | return int(''.join([str(x) for x in values]), 2) |
|
1159 | 1167 | |
|
1160 | 1168 | |
|
1161 | 1169 | def dev_confs(request): |
|
1162 | 1170 | |
|
1163 | 1171 | |
|
1164 | 1172 | page = request.GET.get('page') |
|
1165 | 1173 | order = ('type', 'device__device_type', 'experiment') |
|
1166 | 1174 | filters = request.GET.copy() |
|
1167 | 1175 | |
|
1168 | 1176 | kwargs = get_paginator(Configuration, page, order, filters) |
|
1169 | 1177 | |
|
1170 | 1178 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
1171 | 1179 | kwargs['keys'] = ['name', 'experiment', 'type', 'programmed_date'] |
|
1172 | 1180 | kwargs['title'] = 'Configuration' |
|
1173 | 1181 | kwargs['suptitle'] = 'List' |
|
1174 | 1182 | kwargs['no_sidebar'] = True |
|
1175 | 1183 | kwargs['form'] = form |
|
1176 | 1184 | filters.pop('page', None) |
|
1177 | 1185 | kwargs['q'] = urlencode(filters) |
|
1178 | 1186 | |
|
1179 | 1187 | return render(request, 'base_list.html', kwargs) |
|
1180 | 1188 | |
|
1181 | 1189 | |
|
1182 | 1190 | def dev_conf(request, id_conf): |
|
1183 | 1191 | |
|
1184 | 1192 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1185 | 1193 | |
|
1186 | 1194 | return redirect(conf.get_absolute_url()) |
|
1187 | 1195 | |
|
1188 | 1196 | |
|
1189 | 1197 | @user_passes_test(lambda u:u.is_staff) |
|
1190 | 1198 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
1191 | 1199 | |
|
1192 | 1200 | initial = {} |
|
1193 | 1201 | kwargs = {} |
|
1194 | 1202 | |
|
1195 | 1203 | if id_exp!=0: |
|
1196 | 1204 | initial['experiment'] = id_exp |
|
1197 | 1205 | |
|
1198 | 1206 | if id_dev!=0: |
|
1199 | 1207 | initial['device'] = id_dev |
|
1200 | 1208 | |
|
1201 | 1209 | if request.method == 'GET': |
|
1202 | 1210 | |
|
1203 | 1211 | if id_dev: |
|
1204 | 1212 | kwargs['button'] = 'Create' |
|
1205 | 1213 | device = Device.objects.get(pk=id_dev) |
|
1206 | 1214 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1207 | 1215 | initial['name'] = request.GET['name'] |
|
1208 | 1216 | form = DevConfForm(initial=initial) |
|
1209 | 1217 | else: |
|
1210 | 1218 | if 'template' in request.GET: |
|
1211 | 1219 | if request.GET['template']=='0': |
|
1212 | 1220 | choices = [(conf.pk, '{}'.format(conf)) for conf in Configuration.objects.filter(template=True)] |
|
1213 | 1221 | form = NewForm(initial={'create_from':2}, |
|
1214 | 1222 | template_choices=choices) |
|
1215 | 1223 | else: |
|
1216 | 1224 | kwargs['button'] = 'Create' |
|
1217 | 1225 | conf = Configuration.objects.get(pk=request.GET['template']) |
|
1218 | 1226 | id_dev = conf.device.pk |
|
1219 | 1227 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1220 | 1228 | form = DevConfForm(instance=conf, |
|
1221 | 1229 | initial={'name': '{} [{:%Y/%m/%d}]'.format(conf.name, datetime.now()), |
|
1222 | 1230 | 'template': False, |
|
1223 | 1231 | 'experiment':id_exp}) |
|
1224 | 1232 | elif 'blank' in request.GET: |
|
1225 | 1233 | kwargs['button'] = 'Create' |
|
1226 | 1234 | form = ConfigurationForm(initial=initial) |
|
1227 | 1235 | else: |
|
1228 | 1236 | form = NewForm() |
|
1229 | 1237 | |
|
1230 | 1238 | if request.method == 'POST': |
|
1231 | 1239 | |
|
1232 | 1240 | device = Device.objects.get(pk=request.POST['device']) |
|
1233 | 1241 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1234 | 1242 | |
|
1235 | 1243 | form = DevConfForm(request.POST) |
|
1236 | 1244 | kwargs['button'] = 'Create' |
|
1237 | 1245 | if form.is_valid(): |
|
1238 | 1246 | conf = form.save() |
|
1239 | 1247 | |
|
1240 | 1248 | if 'template' in request.GET and conf.device.device_type.name=='rc': |
|
1241 | 1249 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) |
|
1242 | 1250 | for line in lines: |
|
1243 | 1251 | line.clone(rc_configuration=conf) |
|
1244 | 1252 | |
|
1245 | 1253 | if conf.device.device_type.name=='jars': |
|
1246 | 1254 | conf.add_parms_to_filter() |
|
1247 | 1255 | |
|
1248 | 1256 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
1249 | 1257 | |
|
1250 | 1258 | kwargs['id_exp'] = id_exp |
|
1251 | 1259 | kwargs['form'] = form |
|
1252 | 1260 | kwargs['title'] = 'Configuration' |
|
1253 | 1261 | kwargs['suptitle'] = 'New' |
|
1254 | 1262 | |
|
1255 | 1263 | |
|
1256 | 1264 | if id_dev != 0: |
|
1257 | 1265 | device = Device.objects.get(pk=id_dev) |
|
1258 | 1266 | kwargs['device'] = device.device_type.name |
|
1259 | 1267 | |
|
1260 | 1268 | return render(request, 'dev_conf_edit.html', kwargs) |
|
1261 | 1269 | |
|
1262 | 1270 | |
|
1263 | 1271 | @user_passes_test(lambda u:u.is_staff) |
|
1264 | 1272 | def dev_conf_edit(request, id_conf): |
|
1265 | 1273 | |
|
1266 | 1274 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1267 | 1275 | |
|
1268 | 1276 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1269 | 1277 | |
|
1270 | 1278 | if request.method=='GET': |
|
1271 | 1279 | form = DevConfForm(instance=conf) |
|
1272 | 1280 | |
|
1273 | 1281 | if request.method=='POST': |
|
1274 | 1282 | form = DevConfForm(request.POST, instance=conf) |
|
1275 | 1283 | |
|
1276 | 1284 | if form.is_valid(): |
|
1277 | 1285 | form.save() |
|
1278 | 1286 | return redirect('url_dev_conf', id_conf=id_conf) |
|
1279 | 1287 | |
|
1280 | 1288 | kwargs = {} |
|
1281 | 1289 | kwargs['form'] = form |
|
1282 | 1290 | kwargs['title'] = 'Device Configuration' |
|
1283 | 1291 | kwargs['suptitle'] = 'Edit' |
|
1284 | 1292 | kwargs['button'] = 'Update' |
|
1285 | 1293 | |
|
1286 | 1294 | ###### SIDEBAR ###### |
|
1287 | 1295 | kwargs.update(sidebar(conf=conf)) |
|
1288 | 1296 | |
|
1289 | 1297 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1290 | 1298 | |
|
1291 | 1299 | |
|
1292 | 1300 | @user_passes_test(lambda u:u.is_staff) |
|
1293 | 1301 | def dev_conf_start(request, id_conf): |
|
1294 | 1302 | |
|
1295 | 1303 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1296 | 1304 | |
|
1297 | 1305 | if conf.start_device(): |
|
1298 | 1306 | messages.success(request, conf.message) |
|
1299 | 1307 | else: |
|
1300 | 1308 | messages.error(request, conf.message) |
|
1301 | 1309 | |
|
1302 | 1310 | #conf.status_device() |
|
1303 | 1311 | |
|
1304 | 1312 | return redirect(conf.get_absolute_url()) |
|
1305 | 1313 | |
|
1306 | 1314 | |
|
1307 | 1315 | @user_passes_test(lambda u:u.is_staff) |
|
1308 | 1316 | def dev_conf_stop(request, id_conf): |
|
1309 | 1317 | |
|
1310 | 1318 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1311 | 1319 | |
|
1312 | 1320 | if conf.stop_device(): |
|
1313 | 1321 | messages.success(request, conf.message) |
|
1314 | 1322 | else: |
|
1315 | 1323 | messages.error(request, conf.message) |
|
1316 | 1324 | |
|
1317 | 1325 | #conf.status_device() |
|
1318 | 1326 | |
|
1319 | 1327 | return redirect(conf.get_absolute_url()) |
|
1320 | 1328 | |
|
1321 | 1329 | |
|
1322 | 1330 | def dev_conf_status(request, id_conf): |
|
1323 | 1331 | |
|
1324 | 1332 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1325 | 1333 | |
|
1326 | 1334 | if conf.status_device(): |
|
1327 | 1335 | messages.success(request, conf.message) |
|
1328 | 1336 | else: |
|
1329 | 1337 | messages.error(request, conf.message) |
|
1330 | 1338 | |
|
1331 | 1339 | return redirect(conf.get_absolute_url()) |
|
1332 | 1340 | |
|
1333 | 1341 | |
|
1334 | 1342 | @user_passes_test(lambda u:u.is_staff) |
|
1335 | 1343 | def dev_conf_write(request, id_conf): |
|
1336 | 1344 | |
|
1337 | 1345 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1338 | 1346 | |
|
1339 | 1347 | if conf.write_device(): |
|
1340 | 1348 | messages.success(request, conf.message) |
|
1341 | 1349 | conf.clone(type=1, template=False) |
|
1342 | 1350 | else: |
|
1343 | 1351 | messages.error(request, conf.message) |
|
1344 | 1352 | |
|
1345 | 1353 | return redirect(conf.get_absolute_url()) |
|
1346 | 1354 | |
|
1347 | 1355 | |
|
1348 | 1356 | @user_passes_test(lambda u:u.is_staff) |
|
1349 | 1357 | def dev_conf_read(request, id_conf): |
|
1350 | 1358 | |
|
1351 | 1359 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1352 | 1360 | |
|
1353 | 1361 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1354 | 1362 | |
|
1355 | 1363 | if request.method=='GET': |
|
1356 | 1364 | |
|
1357 | 1365 | parms = conf.read_device() |
|
1358 | 1366 | #conf.status_device() |
|
1359 | 1367 | |
|
1360 | 1368 | if not parms: |
|
1361 | 1369 | messages.error(request, conf.message) |
|
1362 | 1370 | return redirect(conf.get_absolute_url()) |
|
1363 | 1371 | |
|
1364 | 1372 | form = DevConfForm(initial=parms, instance=conf) |
|
1365 | 1373 | |
|
1366 | 1374 | if request.method=='POST': |
|
1367 | 1375 | form = DevConfForm(request.POST, instance=conf) |
|
1368 | 1376 | |
|
1369 | 1377 | if form.is_valid(): |
|
1370 | 1378 | form.save() |
|
1371 | 1379 | return redirect(conf.get_absolute_url()) |
|
1372 | 1380 | |
|
1373 | 1381 | messages.error(request, "Parameters could not be saved") |
|
1374 | 1382 | |
|
1375 | 1383 | kwargs = {} |
|
1376 | 1384 | kwargs['id_dev'] = conf.id |
|
1377 | 1385 | kwargs['form'] = form |
|
1378 | 1386 | kwargs['title'] = 'Device Configuration' |
|
1379 | 1387 | kwargs['suptitle'] = 'Parameters read from device' |
|
1380 | 1388 | kwargs['button'] = 'Save' |
|
1381 | 1389 | |
|
1382 | 1390 | ###### SIDEBAR ###### |
|
1383 | 1391 | kwargs.update(sidebar(conf=conf)) |
|
1384 | 1392 | |
|
1385 | 1393 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
1386 | 1394 | |
|
1387 | 1395 | |
|
1388 | 1396 | @user_passes_test(lambda u:u.is_staff) |
|
1389 | 1397 | def dev_conf_import(request, id_conf): |
|
1390 | 1398 | |
|
1391 | 1399 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1392 | 1400 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1393 | 1401 | |
|
1394 | 1402 | if request.method == 'GET': |
|
1395 | 1403 | file_form = UploadFileForm() |
|
1396 | 1404 | |
|
1397 | 1405 | if request.method == 'POST': |
|
1398 | 1406 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1399 | 1407 | |
|
1400 | 1408 | if file_form.is_valid(): |
|
1401 | 1409 | |
|
1402 | 1410 | parms = conf.import_from_file(request.FILES['file']) |
|
1403 | 1411 | |
|
1404 | 1412 | if parms: |
|
1405 | 1413 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
1406 | 1414 | form = DevConfForm(initial=parms, instance=conf) |
|
1407 | 1415 | |
|
1408 | 1416 | kwargs = {} |
|
1409 | 1417 | kwargs['id_dev'] = conf.id |
|
1410 | 1418 | kwargs['form'] = form |
|
1411 | 1419 | kwargs['title'] = 'Device Configuration' |
|
1412 | 1420 | kwargs['suptitle'] = 'Parameters imported' |
|
1413 | 1421 | kwargs['button'] = 'Save' |
|
1414 | 1422 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1415 | 1423 | kwargs['previous'] = conf.get_absolute_url() |
|
1416 | 1424 | |
|
1417 | 1425 | ###### SIDEBAR ###### |
|
1418 | 1426 | kwargs.update(sidebar(conf=conf)) |
|
1419 | 1427 | |
|
1420 | 1428 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1421 | 1429 | |
|
1422 | 1430 | messages.error(request, "Could not import parameters from file") |
|
1423 | 1431 | |
|
1424 | 1432 | kwargs = {} |
|
1425 | 1433 | kwargs['id_dev'] = conf.id |
|
1426 | 1434 | kwargs['title'] = 'Device Configuration' |
|
1427 | 1435 | kwargs['form'] = file_form |
|
1428 | 1436 | kwargs['suptitle'] = 'Importing file' |
|
1429 | 1437 | kwargs['button'] = 'Import' |
|
1430 | 1438 | |
|
1431 | 1439 | kwargs.update(sidebar(conf=conf)) |
|
1432 | 1440 | |
|
1433 | 1441 | return render(request, 'dev_conf_import.html', kwargs) |
|
1434 | 1442 | |
|
1435 | 1443 | |
|
1436 | 1444 | @user_passes_test(lambda u:u.is_staff) |
|
1437 | 1445 | def dev_conf_export(request, id_conf): |
|
1438 | 1446 | |
|
1439 | 1447 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1440 | 1448 | |
|
1441 | 1449 | if request.method == 'GET': |
|
1442 | 1450 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1443 | 1451 | |
|
1444 | 1452 | if request.method == 'POST': |
|
1445 | 1453 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
1446 | 1454 | |
|
1447 | 1455 | if file_form.is_valid(): |
|
1448 | 1456 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
1449 | 1457 | |
|
1450 | 1458 | response = HttpResponse(content_type=fields['content_type']) |
|
1451 | 1459 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
1452 | 1460 | response.write(fields['content']) |
|
1453 | 1461 | |
|
1454 | 1462 | return response |
|
1455 | 1463 | |
|
1456 | 1464 | messages.error(request, "Could not export parameters") |
|
1457 | 1465 | |
|
1458 | 1466 | kwargs = {} |
|
1459 | 1467 | kwargs['id_dev'] = conf.id |
|
1460 | 1468 | kwargs['title'] = 'Device Configuration' |
|
1461 | 1469 | kwargs['form'] = file_form |
|
1462 | 1470 | kwargs['suptitle'] = 'Exporting file' |
|
1463 | 1471 | kwargs['button'] = 'Export' |
|
1464 | 1472 | |
|
1465 | 1473 | return render(request, 'dev_conf_export.html', kwargs) |
|
1466 | 1474 | |
|
1467 | 1475 | |
|
1468 | 1476 | @user_passes_test(lambda u:u.is_staff) |
|
1469 | 1477 | def dev_conf_delete(request, id_conf): |
|
1470 | 1478 | |
|
1471 | 1479 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1472 | 1480 | |
|
1473 | 1481 | if request.method=='POST': |
|
1474 | 1482 | if request.user.is_staff: |
|
1475 | 1483 | conf.delete() |
|
1476 | 1484 | return redirect('url_dev_confs') |
|
1477 | 1485 | |
|
1478 | 1486 | messages.error(request, 'Not enough permission to delete this object') |
|
1479 | 1487 | return redirect(conf.get_absolute_url()) |
|
1480 | 1488 | |
|
1481 | 1489 | kwargs = { |
|
1482 | 1490 | 'title': 'Delete', |
|
1483 | 1491 | 'suptitle': 'Experiment', |
|
1484 | 1492 | 'object': conf, |
|
1485 | 1493 | 'previous': conf.get_absolute_url(), |
|
1486 | 1494 | 'delete': True |
|
1487 | 1495 | } |
|
1488 | 1496 | |
|
1489 | 1497 | return render(request, 'confirm.html', kwargs) |
|
1490 | 1498 | |
|
1491 | 1499 | |
|
1492 | 1500 | def sidebar(**kwargs): |
|
1493 | 1501 | |
|
1494 | 1502 | side_data = {} |
|
1495 | 1503 | |
|
1496 | 1504 | conf = kwargs.get('conf', None) |
|
1497 | 1505 | experiment = kwargs.get('experiment', None) |
|
1498 | 1506 | |
|
1499 | 1507 | if not experiment: |
|
1500 | 1508 | experiment = conf.experiment |
|
1501 | 1509 | |
|
1502 | 1510 | if experiment: |
|
1503 | 1511 | side_data['experiment'] = experiment |
|
1504 | 1512 | campaign = experiment.campaign_set.all() |
|
1505 | 1513 | if campaign: |
|
1506 | 1514 | side_data['campaign'] = campaign[0] |
|
1507 | 1515 | experiments = campaign[0].experiments.all() |
|
1508 | 1516 | else: |
|
1509 | 1517 | experiments = [experiment] |
|
1510 | 1518 | configurations = experiment.configuration_set.filter(type=0) |
|
1511 | 1519 | side_data['side_experiments'] = experiments |
|
1512 | 1520 | side_data['side_configurations'] = configurations |
|
1513 | 1521 | |
|
1514 | 1522 | return side_data |
|
1515 | 1523 | |
|
1516 | 1524 | def get_paginator(model, page, order, filters={}, n=10): |
|
1517 | 1525 | |
|
1518 | 1526 | kwargs = {} |
|
1519 | 1527 | query = Q() |
|
1520 | 1528 | if isinstance(filters, QueryDict): |
|
1521 | 1529 | filters = filters.dict() |
|
1522 | 1530 | [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] |
|
1523 | 1531 | filters.pop('page', None) |
|
1524 | 1532 | |
|
1525 | 1533 | if 'template' in filters: |
|
1526 | 1534 | filters['template'] = True |
|
1527 | 1535 | if 'start_date' in filters: |
|
1528 | 1536 | filters['start_date__gte'] = filters.pop('start_date') |
|
1529 | 1537 | if 'end_date' in filters: |
|
1530 | 1538 | filters['start_date__lte'] = filters.pop('end_date') |
|
1531 | 1539 | if 'tags' in filters: |
|
1532 | 1540 | tags = filters.pop('tags') |
|
1533 | 1541 | fields = [f.name for f in model._meta.get_fields()] |
|
1534 | 1542 | |
|
1535 | 1543 | if 'tags' in fields: |
|
1536 | 1544 | query = query | Q(tags__icontains=tags) |
|
1537 | 1545 | if 'name' in fields: |
|
1538 | 1546 | query = query | Q(name__icontains=tags) |
|
1539 | 1547 | if 'location' in fields: |
|
1540 | 1548 | query = query | Q(location__name__icontains=tags) |
|
1541 | 1549 | if 'device' in fields: |
|
1542 | 1550 | query = query | Q(device__device_type__name__icontains=tags) |
|
1543 | 1551 | |
|
1544 | 1552 | object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1545 | 1553 | paginator = Paginator(object_list, n) |
|
1546 | 1554 | |
|
1547 | 1555 | try: |
|
1548 | 1556 | objects = paginator.page(page) |
|
1549 | 1557 | except PageNotAnInteger: |
|
1550 | 1558 | objects = paginator.page(1) |
|
1551 | 1559 | except EmptyPage: |
|
1552 | 1560 | objects = paginator.page(paginator.num_pages) |
|
1553 | 1561 | |
|
1554 | 1562 | kwargs['objects'] = objects |
|
1555 | 1563 | kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1556 | 1564 | |
|
1557 | 1565 | return kwargs |
|
1558 | 1566 | |
|
1559 | 1567 | def operation(request, id_camp=None): |
|
1560 | 1568 | |
|
1561 | 1569 | kwargs = {} |
|
1562 | 1570 | kwargs['title'] = 'Radars Operation' |
|
1563 | 1571 | kwargs['no_sidebar'] = True |
|
1564 | 1572 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), |
|
1565 | 1573 | end_date__gte=datetime.now()).order_by('-start_date') |
|
1566 | 1574 | |
|
1567 | 1575 | |
|
1568 | 1576 | if id_camp: |
|
1569 | 1577 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1570 | 1578 | form = OperationForm(initial={'campaign': campaign.id}, campaigns=campaigns) |
|
1571 | 1579 | kwargs['campaign'] = campaign |
|
1572 | 1580 | else: |
|
1573 | 1581 | form = OperationForm(campaigns=campaigns) |
|
1574 | 1582 | kwargs['form'] = form |
|
1575 | 1583 | return render(request, 'operation.html', kwargs) |
|
1576 | 1584 | |
|
1577 | 1585 | #---Experiment |
|
1578 | 1586 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1579 | 1587 | kwargs['experiment_keys'] = keys[1:] |
|
1580 | 1588 | kwargs['experiments'] = experiments |
|
1581 | 1589 | #---Radar |
|
1582 | 1590 | kwargs['locations'] = campaign.get_experiments_by_radar() |
|
1583 | 1591 | kwargs['form'] = form |
|
1584 | 1592 | |
|
1585 | 1593 | return render(request, 'operation.html', kwargs) |
|
1586 | 1594 | |
|
1587 | 1595 | |
|
1588 | 1596 | @login_required |
|
1589 | 1597 | def radar_start(request, id_camp, id_radar): |
|
1590 | 1598 | |
|
1591 | 1599 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1592 | 1600 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1593 | 1601 | now = datetime.utcnow() |
|
1594 | 1602 | |
|
1595 | 1603 | for exp in experiments: |
|
1596 | 1604 | date = datetime.combine(datetime.now().date(), exp.start_time) |
|
1597 | 1605 | |
|
1598 | 1606 | if exp.status == 2: |
|
1599 | 1607 | messages.warning(request, 'Experiment {} already running'.format(exp)) |
|
1600 | 1608 | continue |
|
1601 | 1609 | |
|
1602 | 1610 | if exp.status == 3: |
|
1603 | 1611 | messages.warning(request, 'Experiment {} already programmed'.format(exp)) |
|
1604 | 1612 | continue |
|
1605 | 1613 | |
|
1606 | 1614 | if date>campaign.end_date or date<campaign.start_date: |
|
1607 | 1615 | messages.warning(request, 'Experiment {} out of date'.format(exp)) |
|
1608 | 1616 | continue |
|
1609 | 1617 | |
|
1610 | 1618 | if now>=date: |
|
1611 | 1619 | task = task_start.delay(exp.pk) |
|
1612 | 1620 | exp.status = task.wait() |
|
1613 | 1621 | if exp.status==0: |
|
1614 | 1622 | messages.error(request, 'Experiment {} not start'.format(exp)) |
|
1615 | 1623 | if exp.status==2: |
|
1616 | 1624 | messages.success(request, 'Experiment {} started'.format(exp)) |
|
1617 | 1625 | else: |
|
1618 | 1626 | task = task_start.apply_async((exp.pk,), eta=date) |
|
1619 | 1627 | exp.status = 3 |
|
1620 | 1628 | messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, date)) |
|
1621 | 1629 | |
|
1622 | 1630 | exp.save() |
|
1623 | 1631 | |
|
1624 | 1632 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1625 | 1633 | |
|
1626 | 1634 | |
|
1627 | 1635 | @login_required |
|
1628 | 1636 | def radar_stop(request, id_camp, id_radar): |
|
1629 | 1637 | |
|
1630 | 1638 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1631 | 1639 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1632 | 1640 | |
|
1633 | 1641 | for exp in experiments: |
|
1634 | 1642 | |
|
1635 | 1643 | if exp.status == 2: |
|
1636 | 1644 | task = task_stop.delay(exp.pk) |
|
1637 | 1645 | exp.status = task.wait() |
|
1638 | 1646 | messages.warning(request, 'Experiment {} stopped'.format(exp)) |
|
1639 | 1647 | exp.save() |
|
1640 | 1648 | else: |
|
1641 | 1649 | messages.error(request, 'Experiment {} not running'.format(exp)) |
|
1642 | 1650 | |
|
1643 | 1651 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1644 | 1652 | |
|
1645 | 1653 | |
|
1646 | 1654 | @login_required |
|
1647 | 1655 | def radar_refresh(request, id_camp, id_radar): |
|
1648 | 1656 | |
|
1649 | 1657 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1650 | 1658 | experiments = campaign.get_experiments_by_radar(id_radar)[0]['experiments'] |
|
1651 | 1659 | |
|
1652 | 1660 | for exp in experiments: |
|
1653 | 1661 | exp.get_status() |
|
1654 | 1662 | |
|
1655 | 1663 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
General Comments 0
You need to be logged in to leave comments.
Login now