##// END OF EJS Templates
Se hicieron correciones en el módulo de ABS para que funcione en un entorno virtual, no funciona en Docker como era antes. Si funcionó con el DDS
Renato-TUF -
r361:670b47ba4b67
parent child
Show More
@@ -1,1007 +1,1013
1 1 from django.db import models
2 2 from apps.main.models import Configuration , User
3 3 from django.urls import reverse
4 4 from celery.execute import send_task
5 5 from datetime import datetime
6 6 import ast
7 7 import socket
8 8 import json
9 9 import requests
10 10 import struct
11 11 import os, sys, time
12 12
13 13 antenna_default = json.dumps({
14 14 "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
15 15 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
16 16 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
17 17 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
18 18 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
19 19 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
20 20 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
21 21 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0]
22 22 ]
23 23 ,
24 24 "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
25 25 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
26 26 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
27 27 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
28 28 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
29 29 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
30 30 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
31 31 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]],
32 32 })
33 33
34 34
35 35 tx_default = json.dumps({
36 36 "up": [[1,1,1,1,0,0,0,0],
37 37 [1,1,1,1,0,0,0,0],
38 38 [1,1,1,1,0,0,0,0],
39 39 [1,1,1,1,0,0,0,0],
40 40 [0,0,0,0,1,1,1,1],
41 41 [0,0,0,0,1,1,1,1],
42 42 [0,0,0,0,1,1,1,1],
43 43 [0,0,0,0,1,1,1,1]],
44 44
45 45 "down": [[1,1,1,1,0,0,0,0],
46 46 [1,1,1,1,0,0,0,0],
47 47 [1,1,1,1,0,0,0,0],
48 48 [1,1,1,1,0,0,0,0],
49 49 [0,0,0,0,1,1,1,1],
50 50 [0,0,0,0,1,1,1,1],
51 51 [0,0,0,0,1,1,1,1],
52 52 [0,0,0,0,1,1,1,1]],
53 53 })
54 54
55 55 rx_default = json.dumps({
56 56 "up": [[1,1,1,1,0,0,0,0],
57 57 [1,1,1,1,0,0,0,0],
58 58 [1,1,1,1,0,0,0,0],
59 59 [1,1,1,1,0,0,0,0],
60 60 [0,0,0,0,1,1,1,1],
61 61 [0,0,0,0,1,1,1,1],
62 62 [0,0,0,0,1,1,1,1],
63 63 [0,0,0,0,1,1,1,1]],
64 64
65 65 "down": [[1,1,1,1,0,0,0,0],
66 66 [1,1,1,1,0,0,0,0],
67 67 [1,1,1,1,0,0,0,0],
68 68 [1,1,1,1,0,0,0,0],
69 69 [0,0,0,0,1,1,1,1],
70 70 [0,0,0,0,1,1,1,1],
71 71 [0,0,0,0,1,1,1,1],
72 72 [0,0,0,0,1,1,1,1]],
73 73 })
74 74
75 75 status_default = '0000000000000000000000000000000000000000000000000000000000000000'
76 76 default_messages = {}
77 77
78 78 for i in range(1,65):
79 79 default_messages[str(i)] = "Module "+str(i)
80 80
81 81
82 82 ues_default = json.dumps({
83 83 "up": [0.533333,0.00000,1.06667,0.00000],
84 84 "down": [0.533333,0.00000,1.06667,0.00000]
85 85 })
86 86
87 87 onlyrx_default = json.dumps({
88 88 "up": False,
89 89 "down": False
90 90 })
91 91
92 92 def up_convertion(cadena):
93 93 valores = []
94 94 for c in cadena:
95 95 if c == 1.0: valores=valores+['000']
96 96 if c == 2.0: valores=valores+['001']
97 97 if c == 3.0: valores=valores+['010']
98 98 if c == 0.0: valores=valores+['011']
99 99 if c == 0.5: valores=valores+['100']
100 100 if c == 1.5: valores=valores+['101']
101 101 if c == 2.5: valores=valores+['110']
102 102 if c == 3.5: valores=valores+['111']
103 103
104 104 return valores
105 105
106 106 def up_conv_bits(value):
107 107
108 108 if value == 1.0: bits="000"
109 109 if value == 2.0: bits="001"
110 110 if value == 3.0: bits="010"
111 111 if value == 0.0: bits="011"
112 112 if value == 0.5: bits="100"
113 113 if value == 1.5: bits="101"
114 114 if value == 2.5: bits="110"
115 115 if value == 3.5: bits="111"
116 116
117 117 return bits
118 118
119 119 def down_convertion(cadena):
120 120 valores = []
121 121 for c in cadena:
122 122 if c == 1.0: valores=valores+['000']
123 123 if c == 2.0: valores=valores+['001']
124 124 if c == 3.0: valores=valores+['010']
125 125 if c == 0.0: valores=valores+['011']
126 126 if c == 0.5: valores=valores+['100']
127 127 if c == 1.5: valores=valores+['101']
128 128 if c == 2.5: valores=valores+['110']
129 129 if c == 3.5: valores=valores+['111']
130 130
131 131 return valores
132 132
133 133 def down_conv_bits(value):
134 134
135 135 if value == 1.0: bits="000"
136 136 if value == 2.0: bits="001"
137 137 if value == 3.0: bits="010"
138 138 if value == 0.0: bits="011"
139 139 if value == 0.5: bits="100"
140 140 if value == 1.5: bits="101"
141 141 if value == 2.5: bits="110"
142 142 if value == 3.5: bits="111"
143 143
144 144 return bits
145 145
146 146 def up_conv_value(bits):
147 147
148 148 if bits == "000": value=1.0
149 149 if bits == "001": value=2.0
150 150 if bits == "010": value=3.0
151 151 if bits == "011": value=0.0
152 152 if bits == "100": value=0.5
153 153 if bits == "101": value=1.5
154 154 if bits == "110": value=2.5
155 155 if bits == "111": value=3.5
156 156
157 157 return value
158 158
159 159 def down_conv_value(bits):
160 160
161 161 if bits == "000": value=1.0
162 162 if bits == "001": value=2.0
163 163 if bits == "010": value=3.0
164 164 if bits == "011": value=0.0
165 165 if bits == "100": value=0.5
166 166 if bits == "101": value=1.5
167 167 if bits == "110": value=2.5
168 168 if bits == "111": value=3.5
169 169
170 170 return value
171 171
172 172 def ip2position(module_number):
173 173 j=0
174 174 i=0
175 175 for x in range(0,module_number-1):
176 176 j=j+1
177 177 if j==8:
178 178 i=i+1
179 179 j=0
180 180
181 181 pos = [i,j]
182 182 return pos
183 183
184 184
185 185 def fromBinary2Char(binary_string):
186 186 number = int(binary_string, 2)
187 187 #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f'
188 188 number = number + 33
189 189 char = chr(number)
190 190 return char
191 191
192 192 def fromChar2Binary(char):
193 193 number = ord(char) - 33
194 194 #Minus 33 to get the real value
195 195 bits = bin(number)[2:]
196 196 #To ensure we have a string with 6bits
197 197 if len(bits) < 6:
198 198 bits = bits.zfill(6)
199 199 return bits
200 200
201 201 OPERATION_MODES = (
202 202 (0, 'Manual'),
203 203 (1, 'Automatic'),
204 204 )
205 205
206 206 class ABSConfiguration(Configuration):
207 207 active_beam = models.PositiveSmallIntegerField(verbose_name='Active Beam', default=0)
208 208 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=status_default)
209 209 operation_mode = models.PositiveSmallIntegerField(verbose_name='Operation Mode', choices=OPERATION_MODES, default = 0)
210 210 operation_value = models.FloatField(verbose_name='Periodic (seconds)', default="10", null=True, blank=True)
211 211 module_messages = models.CharField(verbose_name='Modules Messages', max_length=10000, default=json.dumps(default_messages))
212 212
213 213 class Meta:
214 214 db_table = 'abs_configurations'
215 215
216 216 def get_absolute_url_plot(self):
217 217 return reverse('url_plot_abs_patterns', args=[str(self.id)])
218 218
219 219
220 220 def parms_to_dict(self):
221 221
222 222 parameters = {}
223 223
224 224 parameters['device_id'] = self.device.id
225 225 parameters['label'] = self.label
226 226 parameters['device_type'] = self.device.device_type.name
227 227 parameters['beams'] = {}
228 228
229 229 beams = ABSBeam.objects.filter(abs_conf=self)
230 230 b=1
231 231 for beam in beams:
232 232 #absbeam = ABSBeam.objects.get(pk=beams[beam])
233 233 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
234 234 b+=1
235 235
236 236 return parameters
237 237
238 238
239 239 def dict_to_parms(self, parameters):
240 240
241 241 self.label = parameters['label']
242 242
243 243 absbeams = ABSBeam.objects.filter(abs_conf=self)
244 244 beams = parameters['beams']
245 245
246 246 if absbeams:
247 247 beams_number = len(beams)
248 248 absbeams_number = len(absbeams)
249 249 if beams_number==absbeams_number:
250 250 i = 1
251 251 for absbeam in absbeams:
252 252 absbeam.dict_to_parms(beams['beam'+str(i)])
253 253 i = i+1
254 254 elif beams_number > absbeams_number:
255 255 i = 1
256 256 for absbeam in absbeams:
257 257 absbeam.dict_to_parms(beams['beam'+str(i)])
258 258 i=i+1
259 259 for x in range(i,beams_number+1):
260 260 new_beam = ABSBeam(
261 261 name =beams['beam'+str(i)]['name'],
262 262 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
263 263 abs_conf = self,
264 264 tx =json.dumps(beams['beam'+str(i)]['tx']),
265 265 rx =json.dumps(beams['beam'+str(i)]['rx']),
266 266 ues =json.dumps(beams['beam'+str(i)]['ues']),
267 267 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
268 268 )
269 269 new_beam.save()
270 270 i=i+1
271 271 else: #beams_number < absbeams_number:
272 272 i = 1
273 273 for absbeam in absbeams:
274 274 if i <= beams_number:
275 275 absbeam.dict_to_parms(beams['beam'+str(i)])
276 276 i=i+1
277 277 else:
278 278 absbeam.delete()
279 279 else:
280 280 for beam in beams:
281 281 new_beam = ABSBeam(
282 282 name =beams[beam]['name'],
283 283 antenna =json.dumps(beams[beam]['antenna']),
284 284 abs_conf = self,
285 285 tx =json.dumps(beams[beam]['tx']),
286 286 rx =json.dumps(beams[beam]['rx']),
287 287 ues =json.dumps(beams[beam]['ues']),
288 288 only_rx =json.dumps(beams[beam]['only_rx'])
289 289 )
290 290 new_beam.save()
291 291
292 292
293 293
294 294 def update_from_file(self, parameters):
295 295
296 296 self.dict_to_parms(parameters)
297 297 self.save()
298 298
299 299
300 300 def get_beams(self, **kwargs):
301 301 '''
302 302 This function returns ABS Configuration beams
303 303 '''
304 304 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
305 305
306 306 def clone(self, **kwargs):
307 307
308 308 beams = self.get_beams()
309 309 self.pk = None
310 310 self.id = None
311 311 for attr, value in kwargs.items():
312 312 setattr(self, attr, value)
313 313 self.save()
314 314
315 315 for beam in beams:
316 316 beam.clone(abs_conf=self)
317 317
318 318 #-----For Active Beam-----
319 319 new_beams = ABSBeam.objects.filter(abs_conf=self)
320 320 self.active_beam = new_beams[0].id
321 321 self.save()
322 322 #-----For Active Beam-----
323 323 #-----For Device Status---
324 324 self.device.status = 3
325 325 self.device.save()
326 326 #-----For Device Status---
327 327
328 328 return self
329 329
330 330
331 331 def start_device(self):
332 332
333 333 if self.device.status == 3:
334 334
335 335 try:
336 336 #self.write_device()
337 337 send_task('task_change_beam', [self.id],)
338 338 self.message = 'ABS running'
339 339
340 340 except Exception as e:
341 341 self.message = str(e)
342 342 return False
343 343
344 344 return True
345 345
346 346 else:
347 347 self.message = 'Please, select Write ABS Device first.'
348 348 return False
349 349
350 350
351 351 def stop_device(self):
352 352
353 353 self.device.status = 2
354 354 self.device.save()
355 355 self.message = 'ABS has been stopped.'
356 356 self.save()
357 357
358 358 return True
359 359
360 360
361 361 def write_device(self):
362 362
363 363 """
364 364 This function sends the beams list to every abs module.
365 365 It needs 'module_conf' function
366 366 """
367 print("Write")
367 print("Write 3")
368
368 369 beams = ABSBeam.objects.filter(abs_conf=self)
369 370 nbeams = len(beams)
370
371
371 372 # Se manda a cero RC para poder realizar cambio de beam
372 373 if self.experiment is None:
373 374 confs = []
374 375 else:
375 376 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
376 377 confdds = ''
377 378 confjars = ''
378 379 confrc = ''
379 380 #TO STOP DEVICES: DDS-JARS-RC
380 381 for i in range(0,len(confs)):
381 382 if i==0:
382 383 for conf in confs:
383 384 if conf.device.device_type.name == 'dds':
384 385 confdds = conf
385 386 confdds.stop_device()
386 387 break
387 388 if i==1:
388 389 for conf in confs:
389 390 if conf.device.device_type.name == 'jars':
390 391 confjars = conf
391 392 confjars.stop_device()
392 393 break
393 394 if i==2:
394 395 for conf in confs:
395 396 if conf.device.device_type.name == 'rc':
396 397 confrc = conf
397 398 confrc.stop_device()
398 399 break
399 400
400 401 '''
401 402 if self.connected_modules() == 0 :
402 403 print("No encuentra modulos")
403 404 self.message = "No ABS Module detected."
404 405 return False
405 406 '''
406 407 #-------------Write each abs module-----------
407 408
408 409 if beams:
409 410 block_id = 0
410 411 message = 'SNDF{:03d}{:02d}{:02d}'.format(nbeams, nbeams, block_id)
411 412 for i, status in enumerate(self.module_status):
412 413 message += ''.join([fromBinary2Char(beam.module_6bits(i)) for beam in beams])
413 414 status = ['0'] * 64
414 415 n = 0
415 print("Llega una antes entrar a multicast")
416
417 print("Llega una antes entrar a multicast4")
418
416 419 sock = self.send_multicast(message)
417 420
418 421 while True:
419 422 #for i in range(32):
420 423 try:
421 424 data, address = sock.recvfrom(1024)
422 425 print (address, data)
423 426 data = data.decode("utf-8")
424 427 if data == '1':
425 428 status[int(address[0][10:])-1] = '3'
426 429 #print (int(address[0][10:])-1)
427 430 elif data == '0':
428 431 status[int(address[0][10:])-1] = '1'
429 432 except socket.timeout:
430 433 print('Timeout')
431 434 break
432 435 except Exception as e:
433 436 print ('Error {}'.format(e))
434 437 n += 1
435 438 sock.close()
436 439 else:
437 440 self.message = "ABS Configuration does not have beams"
438 441 print('No beams')
439 442 #Start DDS-RC-JARS
440 443 if confdds:
441 444 confdds.start_device()
442 445 if confrc:
443 446 #print confrc
444 447 confrc.start_device()
445 448 if confjars:
446 449 confjars.start_device()
447 450 return False
448 451
449 452 if n == 64:
450 453 self.message = "Could not write ABS Modules"
451 454 self.device.status = 0
452 455 self.module_status = ''.join(status)
453 456 self.save()
454 457 print('Could not write ABS')
455 458 #Start DDS-RC-JARS
456 459 if confdds:
457 460 confdds.start_device()
458 461 if confrc:
459 462 #print confrc
460 463 confrc.start_device()
461 464 if confjars:
462 465 confjars.start_device()
463 466 return False
464 467 else:
465 468 self.message = "ABS Beams List have been sent to ABS Modules"
466 469 print('ABS beams list sent')
467 470 self.active_beam = beams[0].pk
468 471
469 472 #Start DDS-RC-JARS
470 473 if confdds:
471 474 confdds.start_device()
472 475 if confrc:
473 476 #print confrc
474 477 confrc.start_device()
475 478 if confjars:
476 479 confjars.start_device()
477 480
478 481 print('Inicia intento de salvar device.status')
479 482 self.device.status = 3
480 483 self.module_status = ''.join(status)
481 484 #print(status)
482 485 self.save()
483 486 print('Estatus salvado')
484 487 conf_active, __ = ABSActive.objects.get_or_create(pk=1)
485 488 conf_active.conf = self
486 489 conf_active.save()
487 490 return True
488 491
489 492
490 493 def read_module(self, module):
491 494
492 495 """
493 496 Read out-bits (up-down) of 1 abs module NOT for Configuration
494 497 """
495 498
496 499 ip_address = self.device.ip_address
497 500 ip_address = ip_address.split('.')
498 501 module_seq = (ip_address[0],ip_address[1],ip_address[2])
499 502 dot = '.'
500 503 module_ip = dot.join(module_seq)+'.'+str(module)
501 504 module_port = self.device.port_address
502 505 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
503 506
504 507 module_status = json.loads(self.module_status)
505 508 print(read_route)
506 509
507 510 module_bits = ''
508 511
509 512 try:
510 513 r_read = requests.get(read_route, timeout=0.5)
511 514 answer = r_read.json()
512 515 module_bits = answer['allbits']
513 516 except:
514 517 return {}
515 518
516 519 return module_bits
517 520
518 521 def read_device(self):
519 522
520 523 parms = {}
521 524 # Reads active modules.
522 525 module_status = json.loads(self.module_status)
523 526 total = 0
524 527 for status in module_status:
525 528 if module_status[status] != 0:
526 529 module_bits = self.read_module(int(status))
527 530 bits={}
528 531 if module_bits:
529 532 bits = (str(module_bits['um2']) + str(module_bits['um1']) + str(module_bits['um0']) +
530 533 str(module_bits['dm2']) + str(module_bits['dm1']) + str(module_bits['dm0']) )
531 534 parms[str(status)] = bits
532 535
533 536 total +=1
534 537
535 538 if total==0:
536 539 self.message = "No ABS Module detected. Please select 'Status'."
537 540 return False
538 541
539 542
540 543
541 544 self.message = "ABS Modules have been read"
542 545 #monitoreo_tx = JROABSClnt_01CeCnMod000000MNTR10
543 546 return parms
544 547
545 548
546 549 def connected_modules(self):
547 550 """
548 551 This function returns the number of connected abs-modules without updating.
549 552 """
550 553 num = 0
551 554 print(self.module_status)
552 555 for i, status in enumerate(self.module_status):
553 556 if status != '0':
554 557 num += 1
555 558 #print('status {}:{}'.format(i+1, status))
556 559 return num
557 560
558 561 def send_multicast(self, message):
559 562 #print("Send multicast")
560 563 multicast_group = ('224.3.29.71', 10000)
561 564 # Create the datagram socket
562 565 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
563 566 sock.settimeout(1)
564 local_ip = os.environ.get('LOCAL_IP', '192.168.2.128')
567 local_ip = os.environ.get('LOCAL_IP', '0.0.0.0')
568 local_ip = '0.0.0.0'
569 print("He llegado a IP local")
570
565 571 sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(local_ip))
566 572 sock.sendto(message.encode(), multicast_group)
567 573 print('Sending ' + message)
568 574 return sock
569 575
570 576 def status_device(self):
571 577 """
572 578 This function returns the status of all abs-modules as one.
573 579 If at least one module is connected, its answer is "1"
574 580 """
575 581 print ('Status device')
576 582 print (self.active_beam)
577 583 beams = ABSBeam.objects.filter(abs_conf=self)
578 584 #print beams[self.active_beam-1].module_6bits(0)
579 585 active = ABSActive.objects.get(pk=1)
580 586 if active.conf != self:
581 587 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url()
582 588 self.message += "\n"
583 589 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
584 590
585 591 return False
586 592
587 593 sock = self.send_multicast('MNTR')
588 594
589 595 n = 0
590 596 status = ['0'] * 64
591 597
592 598 while True:
593 599 #for i in range(32):
594 600 #if True:
595 601 try:
596 602 print("Recibiendo")
597 603 address = None
598 604 data, address = sock.recvfrom(2)
599 605 print (address, data)
600 606 print("!!!!")
601 607 data = data.decode()
602 608 aux_mon = "1"
603 609 aux_expected = aux_mon
604 610 if(len(data)==2):
605 611 print ("data[1]: ")
606 612 print (data[1])
607 613 aux_mon = fromChar2Binary(data[1])
608 614 print (aux_mon)
609 615 aux_i = (str(address[0]).split('.'))[3]
610 616 print (aux_i)
611 617 print ('Active beam')
612 618 beam_active = ABSBeam.objects.get(pk=self.active_beam)
613 619 print (beam_active)
614 620 aux_expected = beam_active.module_6bits(int(aux_i)-1)
615 621 print (aux_expected)
616 622
617 623 print ("data[0]: ")
618 624 print (data[0])
619 625
620 626 if data[0] == '1':
621 627 status[int(address[0][10:])-1] = '3'
622 628 if aux_mon == aux_expected:
623 629 print ('Es igual')
624 630 else:
625 631 print ('Es diferente')
626 632 status[int(address[0][10:])-1] = '2'
627 633
628 634 elif data[0] == '0':
629 635 status[int(address[0][10:])-1] = '1'
630 636 n += 1
631 637 print('Module: {} connected'.format(address))
632 638 except socket.timeout:
633 639 print('Timeout')
634 640 break
635 641 except:
636 642 print('Module: {} error'.format(address))
637 643 pass
638 644
639 645 sock.close()
640 646
641 647 if n > 0:
642 648 self.message = 'ABS modules Status have been updated.'
643 649 self.device.status = 1
644 650 else:
645 651 self.device.status = 0
646 652 self.message = 'No ABS module is connected.'
647 653 self.module_status = ''.join(status)
648 654 self.save()
649 655
650 656 return self.device.status
651 657
652 658
653 659 def send_beam(self, beam_pos):
654 660 """
655 661 This function connects to a multicast group and sends the beam number
656 662 to all abs modules.
657 663 """
658 664 print ('Send beam')
659 665 print (self.active_beam)
660 666 beams = ABSBeam.objects.filter(abs_conf=self)
661 667 #print beams[self.active_beam-1].module_6bits(0)
662 668 active = ABSActive.objects.get(pk=1)
663 669 if active.conf != self:
664 670 self.message = 'La configuracion actual es la del siguiente enlace %s.' % active.conf.get_absolute_url()
665 671 self.message += "\n"
666 672 self.message += 'Se debe realizar un write en esta configuracion para luego obtener un status valido.'
667 673
668 674 return False
669 675
670 676 # Se manda a cero RC para poder realizar cambio de beam
671 677 if self.experiment is None:
672 678 confs = []
673 679 else:
674 680 confs = Configuration.objects.filter(experiment = self.experiment).filter(type=0)
675 681 confdds = ''
676 682 confjars = ''
677 683 confrc = ''
678 684 #TO STOP DEVICES: DDS-JARS-RC
679 685 for i in range(0,len(confs)):
680 686 if i==0:
681 687 for conf in confs:
682 688 if conf.device.device_type.name == 'dds':
683 689 confdds = conf
684 690 confdds.stop_device()
685 691 break
686 692 if i==1:
687 693 for conf in confs:
688 694 if conf.device.device_type.name == 'jars':
689 695 confjars = conf
690 696 confjars.stop_device()
691 697 break
692 698 if i==2:
693 699 for conf in confs:
694 700 if conf.device.device_type.name == 'rc':
695 701 confrc = conf
696 702 confrc.stop_device()
697 703 break
698 704 if beam_pos > 0:
699 705 beam_pos = beam_pos - 1
700 706 else:
701 707 beam_pos = 0
702 708
703 709 #El indice del apunte debe ser menor que el numero total de apuntes
704 710 #El servidor tcp en el embebido comienza a contar desde 0
705 711 status = ['0'] * 64
706 712 message = 'CHGB{}'.format(beam_pos)
707 713 sock = self.send_multicast(message)
708 714 while True:
709 715 #for i in range(32):
710 716 try:
711 717 data, address = sock.recvfrom(1024)
712 718 print (address, data)
713 719 data = data.decode()
714 720 if data == '1':
715 721 status[int(address[0][10:])-1] = '3'
716 722 elif data == '0':
717 723 status[int(address[0][10:])-1] = '1'
718 724 except socket.timeout:
719 725 print('Timeout')
720 726 break
721 727 except Exception as e:
722 728 print ('Error {}'.format(e))
723 729 pass
724 730
725 731 sock.close()
726 732
727 733 #Start DDS-RC-JARS
728 734 if confdds:
729 735 confdds.start_device()
730 736 if confrc:
731 737 #print confrc
732 738 confrc.start_device()
733 739 if confjars:
734 740 confjars.start_device()
735 741
736 742 self.message = "ABS Beam has been changed"
737 743 self.module_status = ''.join(status)
738 744 self.save()
739 745 return True
740 746
741 747
742 748 def get_absolute_url_import(self):
743 749 return reverse('url_import_abs_conf', args=[str(self.id)])
744 750
745 751 class ABSActive(models.Model):
746 752 conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration', on_delete=models.CASCADE)
747 753
748 754 class Meta:
749 755 db_table = 'abs_absactive'
750 756
751 757 class ABSBeam(models.Model):
752 758
753 759 name = models.CharField(max_length=60, default='Beam')
754 760 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
755 761 abs_conf = models.ForeignKey('ABSConfiguration', null=True,
756 762 verbose_name='ABS Configuration', on_delete=models.CASCADE)
757 763 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
758 764 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
759 765 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
760 766 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
761 767 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
762 768 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
763 769
764 770 class Meta:
765 771 db_table = 'abs_beams'
766 772
767 773 def __unicode__(self):
768 774 return u'%s' % (self.name)
769 775
770 776 def parms_to_dict(self):
771 777
772 778 parameters = {}
773 779 parameters['name'] = self.name
774 780 parameters['antenna'] = ast.literal_eval(self.antenna)
775 781 parameters['abs_conf'] = self.abs_conf.name
776 782 parameters['tx'] = ast.literal_eval(self.tx)
777 783 parameters['rx'] = ast.literal_eval(self.rx)
778 784 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
779 785 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
780 786 parameters['ues'] = ast.literal_eval(self.ues)
781 787 parameters['only_rx'] = json.loads(self.only_rx)
782 788
783 789 return parameters
784 790
785 791 def dict_to_parms(self, parameters):
786 792
787 793 self.name = parameters['name']
788 794 self.antenna = json.dumps(parameters['antenna'])
789 795 #self.abs_conf = parameters['abs_conf']
790 796 self.tx = json.dumps(parameters['tx'])
791 797 self.rx = json.dumps(parameters['rx'])
792 798 #self.s_time = parameters['s_time']
793 799 #self.e_time = parameters['e_time']
794 800 self.ues = json.dumps(parameters['ues'])
795 801 self.only_rx = json.dumps(parameters['only_rx'])
796 802 self.save()
797 803
798 804
799 805 def clone(self, **kwargs):
800 806
801 807 self.pk = None
802 808 self.id = None
803 809 for attr, value in kwargs.items():
804 810 setattr(self, attr, value)
805 811
806 812 self.save()
807 813
808 814 return self
809 815
810 816
811 817 def module_6bits(self, module):
812 818 """
813 819 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
814 820 """
815 821 module += 1
816 822 if module > 64:
817 823 beam_bits = ""
818 824 return beam_bits
819 825
820 826 data = ast.literal_eval(self.antenna)
821 827 up_data = data['antenna_up']
822 828 down_data = data['antenna_down']
823 829
824 830 pos = ip2position(module)
825 831 up_value = up_data[pos[0]][pos[1]]
826 832 down_value = down_data[pos[0]][pos[1]]
827 833
828 834 up_bits = up_conv_bits(up_value)
829 835 down_bits = down_conv_bits(down_value)
830 836 beam_bits = up_bits+down_bits
831 837
832 838 return beam_bits
833 839
834 840
835 841 @property
836 842 def get_upvalues(self):
837 843 """
838 844 This function reads antenna pattern and show the up-value of one abs module
839 845 """
840 846
841 847 data = ast.literal_eval(self.antenna)
842 848 up_data = data['antenna_up']
843 849
844 850 up_values = []
845 851 for data in up_data:
846 852 for i in range(0,8):
847 853 up_values.append(data[i])
848 854
849 855 return up_values
850 856
851 857 @property
852 858 def antenna_upvalues(self):
853 859 """
854 860 This function reads antenna pattern and show the up - values of one abs beam
855 861 in a particular order
856 862 """
857 863 data = ast.literal_eval(self.antenna)
858 864 up_data = data['antenna_up']
859 865
860 866 return up_data
861 867
862 868 @property
863 869 def antenna_downvalues(self):
864 870 """
865 871 This function reads antenna pattern and show the down - values of one abs beam
866 872 in a particular order
867 873 """
868 874 data = ast.literal_eval(self.antenna)
869 875 down_data = data['antenna_down']
870 876
871 877 return down_data
872 878
873 879 @property
874 880 def get_downvalues(self):
875 881 """
876 882 This function reads antenna pattern and show the down-value of one abs module
877 883 """
878 884
879 885 data = ast.literal_eval(self.antenna)
880 886 down_data = data['antenna_down']
881 887
882 888 down_values = []
883 889 for data in down_data:
884 890 for i in range(0,8):
885 891 down_values.append(data[i])
886 892
887 893 return down_values
888 894
889 895 @property
890 896 def get_up_ues(self):
891 897 """
892 898 This function shows the up-ues-value of one beam
893 899 """
894 900 data = ast.literal_eval(self.ues)
895 901 up_ues = data['up']
896 902
897 903 return up_ues
898 904
899 905 @property
900 906 def get_down_ues(self):
901 907 """
902 908 This function shows the down-ues-value of one beam
903 909 """
904 910 data = ast.literal_eval(self.ues)
905 911 down_ues = data['down']
906 912
907 913 return down_ues
908 914
909 915 @property
910 916 def get_up_onlyrx(self):
911 917 """
912 918 This function shows the up-onlyrx-value of one beam
913 919 """
914 920 data = json.loads(self.only_rx)
915 921 up_onlyrx = data['up']
916 922
917 923 return up_onlyrx
918 924
919 925 @property
920 926 def get_down_onlyrx(self):
921 927 """
922 928 This function shows the down-onlyrx-value of one beam
923 929 """
924 930 data = json.loads(self.only_rx)
925 931 down_onlyrx = data['down']
926 932
927 933 return down_onlyrx
928 934
929 935 @property
930 936 def get_tx(self):
931 937 """
932 938 This function shows the tx-values of one beam
933 939 """
934 940 data = json.loads(self.tx)
935 941
936 942 return data
937 943
938 944 @property
939 945 def get_uptx(self):
940 946 """
941 947 This function shows the up-tx-values of one beam
942 948 """
943 949 data = json.loads(self.tx)
944 950 up_data = data['up']
945 951
946 952 up_values = []
947 953 for data in up_data:
948 954 for i in range(0,8):
949 955 up_values.append(data[i])
950 956
951 957 return up_values
952 958
953 959 @property
954 960 def get_downtx(self):
955 961 """
956 962 This function shows the down-tx-values of one beam
957 963 """
958 964 data = json.loads(self.tx)
959 965 down_data = data['down']
960 966
961 967 down_values = []
962 968 for data in down_data:
963 969 for i in range(0,8):
964 970 down_values.append(data[i])
965 971
966 972 return down_values
967 973
968 974
969 975
970 976 @property
971 977 def get_rx(self):
972 978 """
973 979 This function shows the rx-values of one beam
974 980 """
975 981 data = json.loads(self.rx)
976 982
977 983 return data
978 984
979 985 @property
980 986 def get_uprx(self):
981 987 """
982 988 This function shows the up-rx-values of one beam
983 989 """
984 990 data = json.loads(self.rx)
985 991 up_data = data['up']
986 992
987 993 up_values = []
988 994 for data in up_data:
989 995 for i in range(0,8):
990 996 up_values.append(data[i])
991 997
992 998 return up_values
993 999
994 1000 @property
995 1001 def get_downrx(self):
996 1002 """
997 1003 This function shows the down-rx-values of one beam
998 1004 """
999 1005 data = json.loads(self.rx)
1000 1006 down_data = data['down']
1001 1007
1002 1008 down_values = []
1003 1009 for data in down_data:
1004 1010 for i in range(0,8):
1005 1011 down_values.append(data[i])
1006 1012
1007 1013 return down_values
General Comments 0
You need to be logged in to leave comments. Login now