@@ -1,440 +1,442 | |||
|
1 | 1 | from django.shortcuts import render_to_response |
|
2 | 2 | from django.template import RequestContext |
|
3 | 3 | from django.shortcuts import redirect, render, get_object_or_404 |
|
4 | 4 | from django.contrib import messages |
|
5 | 5 | from django.conf import settings |
|
6 | 6 | from django.http import HttpResponse |
|
7 | 7 | from django.core.urlresolvers import reverse |
|
8 | 8 | |
|
9 | 9 | from datetime import datetime |
|
10 | 10 | from time import sleep |
|
11 | 11 | import os |
|
12 | 12 | |
|
13 | 13 | from apps.main.models import Device, Configuration, Experiment |
|
14 | 14 | from apps.main.views import sidebar |
|
15 | 15 | |
|
16 | 16 | from .models import ABSConfiguration, ABSBeam |
|
17 | 17 | from .forms import ABSConfigurationForm, ABSBeamEditForm, ABSBeamAddForm, ABSImportForm |
|
18 | 18 | |
|
19 | 19 | from .utils.overJroShow import overJroShow |
|
20 | 20 | from .utils.OverJRO import OverJRO |
|
21 | 21 | # Create your views here. |
|
22 | 22 | import json, ast |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | def get_values_from_form(form_data): |
|
26 | 26 | |
|
27 | 27 | sublistup = [] |
|
28 | 28 | sublistdown = [] |
|
29 | 29 | subtxlistup = [] |
|
30 | 30 | subtxlistdown = [] |
|
31 | 31 | subrxlistup = [] |
|
32 | 32 | subrxlistdown = [] |
|
33 | 33 | |
|
34 | 34 | up_values_list = [] |
|
35 | 35 | down_values_list = [] |
|
36 | 36 | up_txvalues_list = [] |
|
37 | 37 | down_txvalues_list = [] |
|
38 | 38 | up_rxvalues_list = [] |
|
39 | 39 | down_rxvalues_list = [] |
|
40 | 40 | |
|
41 | 41 | values_list = {} |
|
42 | 42 | cont = 1 |
|
43 | 43 | |
|
44 | 44 | for i in range(1,65): |
|
45 | 45 | x = float(form_data['abs_up'+str(i)]) |
|
46 | 46 | y = float(form_data['abs_down'+str(i)]) |
|
47 | 47 | sublistup.append(x) |
|
48 | 48 | sublistdown.append(y) |
|
49 | 49 | |
|
50 | 50 | if str(i) in form_data.getlist('uptx_checks'): |
|
51 | 51 | subtxlistup.append(1) |
|
52 | 52 | else: |
|
53 | 53 | subtxlistup.append(0) |
|
54 | 54 | if str(i) in form_data.getlist('downtx_checks'): |
|
55 | 55 | subtxlistdown.append(1) |
|
56 | 56 | else: |
|
57 | 57 | subtxlistdown.append(0) |
|
58 | 58 | |
|
59 | 59 | if str(i) in form_data.getlist('uprx_checks'): |
|
60 | 60 | subrxlistup.append(1) |
|
61 | 61 | else: |
|
62 | 62 | subrxlistup.append(0) |
|
63 | 63 | if str(i) in form_data.getlist('downrx_checks'): |
|
64 | 64 | subrxlistdown.append(1) |
|
65 | 65 | else: |
|
66 | 66 | subrxlistdown.append(0) |
|
67 | 67 | |
|
68 | 68 | cont = cont+1 |
|
69 | 69 | |
|
70 | 70 | if cont == 9: |
|
71 | 71 | up_values_list.append(sublistup) |
|
72 | 72 | down_values_list.append(sublistdown) |
|
73 | 73 | sublistup = [] |
|
74 | 74 | sublistdown = [] |
|
75 | 75 | |
|
76 | 76 | up_txvalues_list.append(subtxlistup) |
|
77 | 77 | down_txvalues_list.append(subtxlistdown) |
|
78 | 78 | subtxlistup = [] |
|
79 | 79 | subtxlistdown = [] |
|
80 | 80 | up_rxvalues_list.append(subrxlistup) |
|
81 | 81 | down_rxvalues_list.append(subrxlistdown) |
|
82 | 82 | subrxlistup = [] |
|
83 | 83 | subrxlistdown = [] |
|
84 | 84 | cont = 1 |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | list_uesup = [] |
|
88 | 88 | list_uesdown = [] |
|
89 | 89 | for i in range(1,5): |
|
90 | 90 | if form_data['ues_up'+str(i)] == '': |
|
91 | 91 | list_uesup.append(0.0) |
|
92 | 92 | else: |
|
93 | 93 | list_uesup.append(float(form_data['ues_up'+str(i)])) |
|
94 | 94 | |
|
95 | 95 | if form_data['ues_down'+str(i)] == '': |
|
96 | 96 | list_uesdown.append(0.0) |
|
97 | 97 | else: |
|
98 | 98 | list_uesdown.append(float(form_data['ues_down'+str(i)])) |
|
99 | 99 | |
|
100 | 100 | onlyrx_list = form_data.getlist('onlyrx') |
|
101 | 101 | only_rx = {} |
|
102 | 102 | if '1' in onlyrx_list: |
|
103 | 103 | only_rx['up'] = True |
|
104 | 104 | else: |
|
105 | 105 | only_rx['up'] = False |
|
106 | 106 | if '2' in onlyrx_list: |
|
107 | 107 | only_rx['down'] = True |
|
108 | 108 | else: |
|
109 | 109 | only_rx['down'] = False |
|
110 | 110 | |
|
111 | 111 | antenna = {'antenna_up': up_values_list, 'antenna_down': down_values_list} |
|
112 | 112 | tx = {'up': up_txvalues_list, 'down': down_txvalues_list} |
|
113 | 113 | rx = {'up': up_rxvalues_list, 'down': down_rxvalues_list} |
|
114 | 114 | ues = {'up': list_uesup, 'down': list_uesdown} |
|
115 | 115 | name = str(form_data['beam_name']) |
|
116 | 116 | |
|
117 | 117 | beam_data = {'name': name, 'antenna': antenna, 'tx': tx, 'rx': rx, 'ues': ues, 'only_rx': only_rx} |
|
118 | 118 | |
|
119 | 119 | return beam_data |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | |
|
123 | 123 | def abs_conf(request, id_conf, status_request=None): |
|
124 | 124 | |
|
125 | 125 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
126 | 126 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
127 | 127 | active_beam_id = json.loads(conf.active_beam) |
|
128 | 128 | |
|
129 | 129 | #------------Colors for Active Beam:------------- |
|
130 | 130 | modules_status = json.loads(conf.module_status) |
|
131 | 131 | module_messages = json.loads(conf.module_messages) |
|
132 | 132 | |
|
133 | 133 | color_status = {} |
|
134 | 134 | for status in modules_status: |
|
135 | 135 | if modules_status[status] == 3: #Running background-color: #00cc00; |
|
136 | 136 | color_status[status] = 'class=text-success'#'bgcolor=#00cc00' |
|
137 | 137 | elif modules_status[status] == 1: #Connected background-color: #ee902c; |
|
138 | 138 | color_status[status] = 'class=text-warning'#'bgcolor=#ee902c' |
|
139 | 139 | else: #Disconnected background-color: #ff0000; |
|
140 | 140 | color_status[status] = 'class=text-danger'#'bgcolor=#FF0000' |
|
141 | 141 | #------------------------------------------------ |
|
142 | 142 | |
|
143 | 143 | kwargs = {} |
|
144 | 144 | #kwargs['status'] = conf.device.get_status_display() |
|
145 | 145 | kwargs['connected_modules'] = str(conf.connected_modules())+'/64' |
|
146 | 146 | |
|
147 | 147 | kwargs['dev_conf'] = conf |
|
148 | 148 | if conf.operation_mode == 0: |
|
149 | 149 | kwargs['dev_conf_keys'] = ['name', 'operation_mode'] |
|
150 | 150 | else: |
|
151 | 151 | kwargs['dev_conf_keys'] = ['name', 'operation_mode', 'operation_value'] |
|
152 | 152 | |
|
153 | 153 | kwargs['title'] = 'ABS Configuration' |
|
154 | 154 | kwargs['suptitle'] = 'Details' |
|
155 | 155 | #kwargs['no_play'] = True |
|
156 | 156 | |
|
157 | 157 | kwargs['button'] = 'Edit Configuration' |
|
158 | 158 | #------------------Active Beam----------------------- |
|
159 | 159 | try: |
|
160 | 160 | active_beam_id = active_beam_id['active_beam'] |
|
161 | 161 | active_beam = ABSBeam.objects.get(pk=active_beam_id) |
|
162 | 162 | kwargs['active_beam'] = active_beam |
|
163 | 163 | for beam in beams: |
|
164 | 164 | if beam.id == active_beam.id: |
|
165 | 165 | beam.color_status = color_status |
|
166 | 166 | beam.module_messages = module_messages |
|
167 | 167 | except: |
|
168 | 168 | active_beam = '' |
|
169 | 169 | #---------------------------------------------------- |
|
170 | 170 | kwargs['beams'] = beams |
|
171 | 171 | kwargs['modules_status'] = modules_status |
|
172 | 172 | kwargs['color_status'] = color_status |
|
173 | 173 | kwargs['module_messages'] = module_messages |
|
174 | 174 | |
|
175 | 175 | #if conf.device.status in [0,1]: |
|
176 | 176 | if conf.connected_modules() == 0: |
|
177 | conf.device.status = 0 | |
|
177 | 178 | messages.error(request, 'No ABS module is connected.')#conf.message) |
|
179 | conf.device.save() | |
|
178 | 180 | else: |
|
179 | 181 | messages.success(request, 'ABS modules are connected.')#conf.message) |
|
180 | 182 | |
|
181 | 183 | ###### SIDEBAR ###### |
|
182 | 184 | kwargs.update(sidebar(conf=conf)) |
|
183 | 185 | |
|
184 | 186 | if status_request: |
|
185 | 187 | conf.status_device() |
|
186 | 188 | kwargs['status_request'] = True |
|
187 | 189 | return render(request, 'abs_conf.html', kwargs) |
|
188 | 190 | |
|
189 | 191 | return render(request, 'abs_conf.html', kwargs) |
|
190 | 192 | |
|
191 | 193 | |
|
192 | 194 | def abs_conf_edit(request, id_conf): |
|
193 | 195 | |
|
194 | 196 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
195 | 197 | |
|
196 | 198 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
197 | 199 | print beams |
|
198 | 200 | |
|
199 | 201 | if request.method=='GET': |
|
200 | 202 | form = ABSConfigurationForm(instance=conf) |
|
201 | 203 | |
|
202 | 204 | if request.method=='POST': |
|
203 | 205 | form = ABSConfigurationForm(request.POST, instance=conf) |
|
204 | 206 | |
|
205 | 207 | if form.is_valid(): |
|
206 | 208 | conf = form.save(commit=False) |
|
207 | 209 | conf.save() |
|
208 | 210 | return redirect('url_abs_conf', id_conf=conf.id) |
|
209 | 211 | |
|
210 | 212 | ###### SIDEBAR ###### |
|
211 | 213 | kwargs = {} |
|
212 | 214 | |
|
213 | 215 | kwargs['dev_conf'] = conf |
|
214 | 216 | #kwargs['id_dev'] = conf.id |
|
215 | 217 | kwargs['id_conf'] = conf.id |
|
216 | 218 | kwargs['form'] = form |
|
217 | 219 | kwargs['abs_beams'] = beams |
|
218 | 220 | kwargs['title'] = 'Device Configuration' |
|
219 | 221 | kwargs['suptitle'] = 'Edit' |
|
220 | 222 | kwargs['button'] = 'Save' |
|
221 | 223 | |
|
222 | 224 | kwargs['edit'] = True |
|
223 | 225 | |
|
224 | 226 | return render(request, 'abs_conf_edit.html', kwargs) |
|
225 | 227 | |
|
226 | 228 | |
|
227 | 229 | def import_file(request, id_conf): |
|
228 | 230 | |
|
229 | 231 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
230 | 232 | if request.method=='POST': |
|
231 | 233 | form = ABSImportForm(request.POST, request.FILES) |
|
232 | 234 | if form.is_valid(): |
|
233 | 235 | try: |
|
234 | 236 | parms = conf.import_from_file(request.FILES['file_name']) |
|
235 | 237 | |
|
236 | 238 | if parms: |
|
237 | 239 | conf.update_from_file(parms) |
|
238 | 240 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) |
|
239 | 241 | return redirect(conf.get_absolute_url_edit()) |
|
240 | 242 | |
|
241 | 243 | except Exception as e: |
|
242 | 244 | messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) |
|
243 | 245 | |
|
244 | 246 | else: |
|
245 | 247 | messages.warning(request, 'Your current configuration will be replaced') |
|
246 | 248 | form = ABSImportForm() |
|
247 | 249 | |
|
248 | 250 | kwargs = {} |
|
249 | 251 | kwargs['form'] = form |
|
250 | 252 | kwargs['title'] = 'ABS Configuration' |
|
251 | 253 | kwargs['suptitle'] = 'Import file' |
|
252 | 254 | kwargs['button'] = 'Upload' |
|
253 | 255 | kwargs['previous'] = conf.get_absolute_url() |
|
254 | 256 | |
|
255 | 257 | return render(request, 'abs_import.html', kwargs) |
|
256 | 258 | |
|
257 | 259 | |
|
258 | 260 | def send_beam(request, id_conf, id_beam): |
|
259 | 261 | |
|
260 | 262 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
261 | 263 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
262 | 264 | beams_list = ABSBeam.objects.filter(abs_conf=conf) |
|
263 | 265 | #To set this beam as an Active Beam |
|
264 | 266 | beam.set_as_activebeam() |
|
265 | 267 | #To send beam position to abs-modules |
|
266 | 268 | i = 0 |
|
267 | 269 | for b in beams_list: |
|
268 | 270 | if b.id == int(id_beam): |
|
269 | 271 | break |
|
270 | 272 | else: |
|
271 | 273 | i += 1 |
|
272 | 274 | beam_pos = i + 1 #Estandarizar |
|
273 | 275 | print 'Position: ',beam_pos |
|
274 | 276 | conf.send_beam_num(beam_pos) |
|
275 | 277 | |
|
276 | 278 | return redirect('url_abs_conf', conf.id) |
|
277 | 279 | |
|
278 | 280 | |
|
279 | 281 | def add_beam(request, id_conf): |
|
280 | 282 | |
|
281 | 283 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
282 | 284 | confs = Configuration.objects.all() |
|
283 | 285 | |
|
284 | 286 | if request.method=='GET': |
|
285 | 287 | #form = ABSBeamEditForm() |
|
286 | 288 | form = ABSBeamAddForm() |
|
287 | 289 | |
|
288 | 290 | if request.method=='POST': |
|
289 | 291 | form = ABSBeamAddForm(request.POST) |
|
290 | 292 | |
|
291 | 293 | beam_data = get_values_from_form(request.POST) |
|
292 | 294 | |
|
293 | 295 | new_beam = ABSBeam( |
|
294 | 296 | name =beam_data['name'], |
|
295 | 297 | antenna =json.dumps(beam_data['antenna']), |
|
296 | 298 | abs_conf=conf, |
|
297 | 299 | tx =json.dumps(beam_data['tx']), |
|
298 | 300 | rx =json.dumps(beam_data['rx']), |
|
299 | 301 | ues =json.dumps(beam_data['ues']), |
|
300 | 302 | only_rx =json.dumps(beam_data['only_rx']) |
|
301 | 303 | ) |
|
302 | 304 | new_beam.save() |
|
303 | 305 | #---Update 6bits configuration and add beam to abs configuration beams list. |
|
304 | 306 | new_beam.modules_6bits() |
|
305 | 307 | #new_beam.add_beam2list() |
|
306 | 308 | messages.success(request, 'Beam: "%s" has been added.' % new_beam.name) |
|
307 | 309 | |
|
308 | 310 | return redirect('url_edit_abs_conf', conf.id) |
|
309 | 311 | |
|
310 | 312 | ###### SIDEBAR ###### |
|
311 | 313 | kwargs = {} |
|
312 | 314 | |
|
313 | 315 | #kwargs['dev_conf'] = conf.device |
|
314 | 316 | #kwargs['id_dev'] = conf.device |
|
315 | 317 | kwargs['id_conf'] = conf.id |
|
316 | 318 | kwargs['form'] = form |
|
317 | 319 | kwargs['title'] = 'ABS Beams' |
|
318 | 320 | kwargs['suptitle'] = 'Add Beam' |
|
319 | 321 | kwargs['button'] = 'Add' |
|
320 | 322 | kwargs['no_sidebar'] = True |
|
321 | 323 | |
|
322 | 324 | #kwargs['previous'] = conf.get_absolute_url_edit() |
|
323 | 325 | kwargs['edit'] = True |
|
324 | 326 | |
|
325 | 327 | return render(request, 'abs_add_beam.html', kwargs) |
|
326 | 328 | |
|
327 | 329 | |
|
328 | 330 | def edit_beam(request, id_conf, id_beam): |
|
329 | 331 | |
|
330 | 332 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
331 | 333 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
332 | 334 | |
|
333 | 335 | if request.method=='GET': |
|
334 | 336 | form = ABSBeamEditForm(initial={'beam': beam}) |
|
335 | 337 | |
|
336 | 338 | if request.method=='POST': |
|
337 | 339 | form = ABSBeamEditForm(request.POST) |
|
338 | 340 | |
|
339 | 341 | beam_data = get_values_from_form(request.POST) |
|
340 | 342 | |
|
341 | 343 | beam.dict_to_parms(beam_data) |
|
342 | 344 | beam.save() |
|
343 | 345 | |
|
344 | 346 | messages.success(request, 'Beam: "%s" has been updated.' % beam.name) |
|
345 | 347 | |
|
346 | 348 | return redirect('url_edit_abs_conf', conf.id) |
|
347 | 349 | |
|
348 | 350 | ###### SIDEBAR ###### |
|
349 | 351 | kwargs = {} |
|
350 | 352 | |
|
351 | 353 | kwargs['id_conf'] = conf.id |
|
352 | 354 | kwargs['form'] = form |
|
353 | 355 | kwargs['title'] = 'ABS Beams' |
|
354 | 356 | kwargs['suptitle'] = 'Edit Beam' |
|
355 | 357 | kwargs['button'] = 'Save' |
|
356 | 358 | kwargs['no_sidebar'] = True |
|
357 | 359 | |
|
358 | 360 | #kwargs['previous'] = conf.get_absolute_url_edit() |
|
359 | 361 | kwargs['edit'] = True |
|
360 | 362 | |
|
361 | 363 | return render(request, 'abs_edit_beam.html', kwargs) |
|
362 | 364 | |
|
363 | 365 | |
|
364 | 366 | |
|
365 | 367 | def remove_beam(request, id_conf, id_beam): |
|
366 | 368 | |
|
367 | 369 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
368 | 370 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
369 | 371 | |
|
370 | 372 | if request.method=='POST': |
|
371 | 373 | if beam: |
|
372 | 374 | try: |
|
373 | 375 | beam.remove_beamfromlist() |
|
374 | 376 | beam.delete() |
|
375 | 377 | messages.success(request, 'Beam: "%s" has been deleted.' % beam) |
|
376 | 378 | except: |
|
377 | 379 | messages.error(request, 'Unable to delete beam: "%s".' % beam) |
|
378 | 380 | |
|
379 | 381 | return redirect('url_edit_abs_conf', conf.id) |
|
380 | 382 | |
|
381 | 383 | ###### SIDEBAR ###### |
|
382 | 384 | kwargs = {} |
|
383 | 385 | |
|
384 | 386 | kwargs['object'] = beam |
|
385 | 387 | kwargs['delete'] = True |
|
386 | 388 | kwargs['title'] = 'Delete' |
|
387 | 389 | kwargs['suptitle'] = 'Beam' |
|
388 | 390 | kwargs['previous'] = conf.get_absolute_url_edit() |
|
389 | 391 | return render(request, 'confirm.html', kwargs) |
|
390 | 392 | |
|
391 | 393 | |
|
392 | 394 | |
|
393 | 395 | def plot_patterns(request, id_conf, id_beam=None): |
|
394 | 396 | |
|
395 | 397 | kwargs = {} |
|
396 | 398 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
397 | 399 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
398 | 400 | |
|
399 | 401 | if id_beam: |
|
400 | 402 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
401 | 403 | kwargs['beam'] = beam |
|
402 | 404 | |
|
403 | 405 | |
|
404 | 406 | ###### SIDEBAR ###### |
|
405 | 407 | |
|
406 | 408 | kwargs['dev_conf'] = conf.device |
|
407 | 409 | kwargs['id_dev'] = conf.device |
|
408 | 410 | kwargs['id_conf'] = conf.id |
|
409 | 411 | kwargs['abs_beams'] = beams |
|
410 | 412 | kwargs['title'] = 'ABS Patterns' |
|
411 | 413 | kwargs['suptitle'] = conf.name |
|
412 | 414 | kwargs['no_sidebar'] = True |
|
413 | 415 | |
|
414 | 416 | return render(request, 'abs_patterns.html', kwargs) |
|
415 | 417 | |
|
416 | 418 | |
|
417 | 419 | def plot_pattern(request, id_conf, id_beam, antenna): |
|
418 | 420 | |
|
419 | 421 | if antenna=='down': |
|
420 | 422 | sleep(3) |
|
421 | 423 | |
|
422 | 424 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
423 | 425 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
424 | 426 | |
|
425 | 427 | name = conf.experiment.name |
|
426 | 428 | |
|
427 | 429 | just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0 |
|
428 | 430 | phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)] |
|
429 | 431 | gain_tx = json.loads(beam.tx)[antenna] |
|
430 | 432 | gain_rx = json.loads(beam.rx)[antenna] |
|
431 | 433 | ues = json.loads(beam.ues)[antenna] |
|
432 | 434 | |
|
433 | 435 | newOverJro = overJroShow(name) |
|
434 | 436 | fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx) |
|
435 | 437 | |
|
436 | 438 | response=HttpResponse(content_type='image/png') |
|
437 | 439 | |
|
438 | 440 | fig.canvas.print_png(response) |
|
439 | 441 | |
|
440 | 442 | return response |
General Comments 0
You need to be logged in to leave comments.
Login now