##// END OF EJS Templates
Starting work with celery...
Fiorella Quino -
r202:deddc337364a
parent child
Show More
@@ -0,0 +1,19
1 from __future__ import absolute_import
2 from celery import task
3 from .models import Experiment
4
5 #@task
6 #def task_start(id_exp):
7
8 # exp = Experiment.objects.get(pk=id_exp)
9
10 # return exp.start()
11
12
13
14 #@task
15 #def task_stop(id_exp):
16
17 # exp = Experiment.objects.get(pk=id_exp)
18
19 # return exp.stop()
@@ -1,1103 +1,1103
1 1 from django.db import models
2 2 from apps.main.models import Configuration
3 3 from django.core.urlresolvers import reverse
4 4 # Create your models here.
5 5
6 6 import ast
7 7 import socket
8 8 import json
9 9 import requests
10 10 import struct
11 11 import sys, time
12 12
13 13 import multiprocessing
14 14
15 15
16 16 antenna_default = json.dumps({
17 17 "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
18 18 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
19 19 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
20 20 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
21 21 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
22 22 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
23 23 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
24 24 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0]
25 25 ]
26 26 ,
27 27 "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
28 28 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
29 29 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
30 30 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
31 31 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
32 32 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
33 33 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
34 34 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]],
35 35 })
36 36
37 37
38 38 tx_default = json.dumps({
39 39 "up": [[1,1,1,1,0,0,0,0],
40 40 [1,1,1,1,0,0,0,0],
41 41 [1,1,1,1,0,0,0,0],
42 42 [1,1,1,1,0,0,0,0],
43 43 [0,0,0,0,1,1,1,1],
44 44 [0,0,0,0,1,1,1,1],
45 45 [0,0,0,0,1,1,1,1],
46 46 [0,0,0,0,1,1,1,1]],
47 47
48 48 "down": [[1,1,1,1,0,0,0,0],
49 49 [1,1,1,1,0,0,0,0],
50 50 [1,1,1,1,0,0,0,0],
51 51 [1,1,1,1,0,0,0,0],
52 52 [0,0,0,0,1,1,1,1],
53 53 [0,0,0,0,1,1,1,1],
54 54 [0,0,0,0,1,1,1,1],
55 55 [0,0,0,0,1,1,1,1]],
56 56 })
57 57
58 58 rx_default = json.dumps({
59 59 "up": [[1,1,1,1,0,0,0,0],
60 60 [1,1,1,1,0,0,0,0],
61 61 [1,1,1,1,0,0,0,0],
62 62 [1,1,1,1,0,0,0,0],
63 63 [0,0,0,0,1,1,1,1],
64 64 [0,0,0,0,1,1,1,1],
65 65 [0,0,0,0,1,1,1,1],
66 66 [0,0,0,0,1,1,1,1]],
67 67
68 68 "down": [[1,1,1,1,0,0,0,0],
69 69 [1,1,1,1,0,0,0,0],
70 70 [1,1,1,1,0,0,0,0],
71 71 [1,1,1,1,0,0,0,0],
72 72 [0,0,0,0,1,1,1,1],
73 73 [0,0,0,0,1,1,1,1],
74 74 [0,0,0,0,1,1,1,1],
75 75 [0,0,0,0,1,1,1,1]],
76 76 })
77 77
78 78 conf_default = {}
79 79 status_default = {}
80 80 for i in range(1,65):
81 81 conf_default[str(i)] = ""
82 82 status_default[str(i)] = 0
83 83
84 84 ues_default = json.dumps({
85 85 "up": [0.533333,0.00000,1.06667,0.00000],
86 86 "down": [0.533333,0.00000,1.06667,0.00000]
87 87 })
88 88
89 89 onlyrx_default = json.dumps({
90 90 "up": False,
91 91 "down": False
92 92 })
93 93
94 94 def up_convertion(cadena):
95 95 valores = []
96 96 for c in cadena:
97 97 if c == 1.0: valores=valores+['000']
98 98 if c == 2.0: valores=valores+['001']
99 99 if c == 3.0: valores=valores+['010']
100 100 if c == 0.0: valores=valores+['011']
101 101 if c == 0.5: valores=valores+['100']
102 102 if c == 1.5: valores=valores+['101']
103 103 if c == 2.5: valores=valores+['110']
104 104 if c == 3.5: valores=valores+['111']
105 105
106 106 return valores
107 107
108 108 def up_conv_bits(value):
109 109
110 110 if value == 1.0: bits="000"
111 111 if value == 2.0: bits="001"
112 112 if value == 3.0: bits="010"
113 113 if value == 0.0: bits="011"
114 114 if value == 0.5: bits="100"
115 115 if value == 1.5: bits="101"
116 116 if value == 2.5: bits="110"
117 117 if value == 3.5: bits="111"
118 118
119 119 return bits
120 120
121 121 def down_convertion(cadena):
122 122 valores = []
123 123 for c in cadena:
124 124 if c == 1.0: valores=valores+['000']
125 125 if c == 2.0: valores=valores+['001']
126 126 if c == 3.0: valores=valores+['010']
127 127 if c == 0.0: valores=valores+['011']
128 128 if c == 0.5: valores=valores+['100']
129 129 if c == 1.5: valores=valores+['101']
130 130 if c == 2.5: valores=valores+['110']
131 131 if c == 3.5: valores=valores+['111']
132 132
133 133 return valores
134 134
135 135 def down_conv_bits(value):
136 136
137 137 if value == 1.0: bits="000"
138 138 if value == 2.0: bits="001"
139 139 if value == 3.0: bits="010"
140 140 if value == 0.0: bits="011"
141 141 if value == 0.5: bits="100"
142 142 if value == 1.5: bits="101"
143 143 if value == 2.5: bits="110"
144 144 if value == 3.5: bits="111"
145 145
146 146 return bits
147 147
148 148 def ip2position(module_number):
149 149 j=0
150 150 i=0
151 151 for x in range(0,module_number-1):
152 152 j=j+1
153 153 if j==8:
154 154 i=i+1
155 155 j=0
156 156
157 157 pos = [i,j]
158 158 return pos
159 159
160 160
161 161 def fromBinary2Char(binary_string):
162 162 number = int(binary_string, 2)
163 163 #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f'
164 164 number = number + 33
165 165 char = chr(number)
166 166 return char
167 167
168 168 def fromChar2Binary(char):
169 169 number = ord(char) - 33
170 170 #Minus 33 to get the real value
171 171 bits = bin(number)[2:]
172 172 #To ensure we have a string with 6bits
173 173 if len(bits) < 6:
174 174 bits = bits.zfill(6)
175 175 return bits
176 176
177 177 OPERATION_MODES = (
178 178 (0, 'Manual'),
179 179 (1, 'Automatic'),
180 180 )
181 181
182 182
183 183 class ABSConfiguration(Configuration):
184 184 active_beam = models.CharField(verbose_name='Active Beam', max_length=20000, default="{}")
185 185 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default))
186 186 operation_mode = models.PositiveSmallIntegerField(verbose_name='Operation Mode', choices=OPERATION_MODES, default = 0)
187 187 operation_value = models.FloatField(verbose_name='Periodic (seconds)', default="10", null=True, blank=True)
188 188
189 189 class Meta:
190 190 db_table = 'abs_configurations'
191 191
192 192 def get_absolute_url_plot(self):
193 193 return reverse('url_plot_abs_patterns', args=[str(self.id)])
194 194
195 195
196 196 def parms_to_dict(self):
197 197
198 198 parameters = {}
199 199
200 200 parameters['device_id'] = self.device.id
201 201 parameters['name'] = self.name
202 202 parameters['beams'] = {}
203 203
204 204 beams = ABSBeam.objects.filter(abs_conf=self)
205 205 b=1
206 206 for beam in beams:
207 207 #absbeam = ABSBeam.objects.get(pk=beams[beam])
208 208 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
209 209 b+=1
210 210
211 211 return parameters
212 212
213 213 def dict_to_parms(self, parameters):
214 214
215 215 self.name = parameters['name']
216 216
217 217 absbeams = ABSBeam.objects.filter(abs_conf=self)
218 218 beams = parameters['beams']
219 219
220 220 if absbeams:
221 221 beams_number = len(beams)
222 222 absbeams_number = len(absbeams)
223 223 if beams_number==absbeams_number:
224 224 i = 1
225 225 for absbeam in absbeams:
226 226 absbeam.dict_to_parms(beams['beam'+str(i)])
227 227 i = i+1
228 228 elif beams_number > absbeams_number:
229 229 i = 1
230 230 for absbeam in absbeams:
231 231 absbeam.dict_to_parms(beams['beam'+str(i)])
232 232 i=i+1
233 233 for x in range(i,beams_number+1):
234 234 new_beam = ABSBeam(
235 235 name =beams['beam'+str(i)]['name'],
236 236 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
237 237 abs_conf = self,
238 238 tx =json.dumps(beams['beam'+str(i)]['tx']),
239 239 rx =json.dumps(beams['beam'+str(i)]['rx']),
240 240 ues =json.dumps(beams['beam'+str(i)]['ues']),
241 241 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
242 242 )
243 243 new_beam.save()
244 244 i=i+1
245 245 else: #beams_number < absbeams_number:
246 246 i = 1
247 247 for absbeam in absbeams:
248 248 if i <= beams_number:
249 249 absbeam.dict_to_parms(beams['beam'+str(i)])
250 250 i=i+1
251 251 else:
252 252 absbeam.delete()
253 253 else:
254 254 for beam in beams:
255 255 new_beam = ABSBeam(
256 256 name =beams[beam]['name'],
257 257 antenna =json.dumps(beams[beam]['antenna']),
258 258 abs_conf = self,
259 259 tx =json.dumps(beams[beam]['tx']),
260 260 rx =json.dumps(beams[beam]['rx']),
261 261 ues =json.dumps(beams[beam]['ues']),
262 262 only_rx =json.dumps(beams[beam]['only_rx'])
263 263 )
264 264 new_beam.save()
265 265
266 266
267 267
268 268 def update_from_file(self, parameters):
269 269
270 270 self.dict_to_parms(parameters)
271 271 self.save()
272 272
273 273
274 274 def get_beams(self, **kwargs):
275 275 '''
276 276 This function returns ABS Configuration beams
277 277 '''
278 278 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
279 279
280 280 def clone(self, **kwargs):
281 281
282 282 beams = self.get_beams()
283 283 self.pk = None
284 284 self.id = None
285 285 for attr, value in kwargs.items():
286 286 setattr(self, attr, value)
287 287 self.save()
288 288
289 289 for beam in beams:
290 290 beam.clone(abs_conf=self)
291 291
292 292 #-----For Active Beam-----
293 293 active_beam = json.loads(self.active_beam)
294 294 new_beams = ABSBeam.objects.filter(abs_conf=self)
295 295 active_beam['active_beam'] = new_beams[0].id
296 296 self.active_beam = json.dumps(active_beam)
297 297 self.save()
298 298 #-----For Active Beam-----
299 299
300 300 return self
301 301
302 302
303 303 def module_conf(self, module_num, beams):
304 304 """
305 305 This function creates beam configurations for one abs module.
306 306 """
307 307 ip_address = self.device.ip_address
308 308 ip_address = ip_address.split('.')
309 309 module_seq = (ip_address[0],ip_address[1],ip_address[2])
310 310 dot = '.'
311 311 module_ip = dot.join(module_seq)+'.'+str(module_num)
312 312 module_port = self.device.port_address
313 313 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
314 314
315 315 #header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
316 316 #module = 'ABS_'+str(module_num)
317 317 bs = '' #{}
318 318 i=1
319 319
320 320 for beam in beams:
321 321 #bs[i] = fromBinary2Char(beam.module_6bits(module_num))
322 322 bs = bs + fromBinary2Char(beam.module_6bits(module_num))
323 323 i=i+1
324 324
325 325 beams = bs
326 326
327 327 parameters = {}
328 328 parameters['beams'] = beams #json.dumps(beams)
329 329 print parameters
330 330 answer = ''
331 331
332 332 try:
333 333 #r_write = requests.post(write_route, parameters, timeout=0.5)
334 334 r_write = requests.post(write_route, json = parameters, timeout=0.5)
335 335 answer = r_write.json()
336 336 #self.message = answer['message']
337 337 except:
338 338 #self.message = "Could not write ABS parameters"
339 339 return False
340 340
341 341 return answer
342 342
343 343
344 344
345 345 def write_device(self):
346 346 """
347 347 This function sends the beams list to every abs module.
348 348 It needs 'module_conf' function
349 349 """
350 350
351 351 beams = ABSBeam.objects.filter(abs_conf=self)
352 352
353 353 #-------------Write each abs module-----------
354 354 if beams:
355 355 beams_status = ast.literal_eval(self.module_status)
356 356 disconnected_modules = 0
357 357 for i in range(1,65): #(62,65)
358 358 #--------------JUEVES-------------
359 359 if beams_status[str(i)] != 0:
360 360 try:
361 361 answer = self.module_conf(i,beams)
362 362 if answer:
363 363 if answer['status']:
364 364 beams_status[str(i)] = 3
365 365
366 366 except:
367 367 beams_status[str(i)] = 1
368 368
369 369 pass
370 370 else:
371 371 disconnected_modules += 1
372 372 #--------------JUEVES-------------
373 373 #try:
374 374 # answer = self.module_conf(i, beams)
375 375 # beams_status[str(i)] = 1
376 376 # self.module_status = json.dumps(beams_status)
377 377 # self.save()
378 378
379 379 #except:
380 380 # beams_status[str(i)] = 0
381 381 # self.module_status = json.dumps(beams_status)
382 382 # self.save()
383 383 # answer = 0
384 384 # return 0
385 385 else:
386 386 self.message = "ABS Configuration does not have beams"
387 387 return False
388 388
389 389 #self.device.status = 1
390 390 ##
391 391 #-------------Jueves-------------
392 392 if disconnected_modules == 64:
393 393 self.message = "Could not write ABS Modules"
394 394 return False
395 395 else:
396 396 self.message = "ABS Beams List have been sent to ABS Modules"
397 397 beams[0].set_as_activebeam()
398 398
399 399 self.module_status = json.dumps(beams_status)
400 400 self.save()
401 401
402 402 #-------------Jueves-------------
403 403 #if answer==1:
404 404 # self.message = "ABS Beams List have been sent to ABS Modules"
405 405 # beams[0].set_as_activebeam()
406 406 #else:
407 407 # self.message = "Could not read ABS parameters"
408 408 ##
409 409 self.save()
410 410 return True
411 411
412 412
413 413
414 414
415 415 def read_module(self, module):
416 416
417 417 """
418 418 Read out-bits (up-down) of 1 abs module NOT for Configuration
419 419 """
420 420
421 421 parameters = {}
422 422 ip_address = self.device.ip_address
423 423 ip_address = ip_address.split('.')
424 424 module_seq = (ip_address[0],ip_address[1],ip_address[2])
425 425 dot = '.'
426 426 module_ip = dot.join(module_seq)+'.'+str(module)
427 427 module_port = self.device.port_address
428 428 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
429 429
430 430 print(read_route)
431 431
432 432 answer = ''
433 433 module_bits = ''
434 434
435 435 try:
436 436 r_write = requests.get(read_route, timeout=0.7)
437 437 answer = r_write.json()
438 438 self.message = answer['message']
439 439 module_bits = answer['allbits']
440 440 except:
441 441 #message = "Could not read ABS parameters"
442 442 answer = r_write.json()
443 443 return 0
444 444
445 445 return module_bits
446 446
447 447
448 448 def absmodule_status(self):
449 449 """
450 450 This function gets the status of each abs module. It sends GET method to Web Application
451 451 in Python Bottle.
452 452 This function updates "module_status" field from ABSconfiguration.
453 453 """
454 454 ip_address = self.device.ip_address
455 455 ip_address = ip_address.split('.')
456 456 module_seq = (ip_address[0],ip_address[1],ip_address[2])
457 457 dot = '.'
458 458 module_port = self.device.port_address
459 459
460 460 modules_status = json.loads(self.module_status)
461 461
462 462 for i in range(1,65):
463 463 module_ip = dot.join(module_seq)+'.'+str(i)
464 464 print module_ip
465 465
466 466 route = 'http://'+module_ip+':'+str(module_port)+'/hello'
467 467
468 468 try:
469 469 r = requests.get(route, timeout=0.7)
470 470 modules_status[str(i)] = 1
471 471 except:
472 472 modules_status[str(i)] = 0
473 473 pass
474 474
475 475
476 476 self.module_status=json.dumps(modules_status)
477 477 self.save()
478 478
479 479 return
480 480
481 481
482 482 def connected_modules(self):
483 483 """
484 484 This function returns the number of connected abs-modules without updating.
485 485 """
486 486 modules_status = json.loads(self.module_status)
487 487 num = 0
488 488 for status in modules_status:
489 489 if modules_status[status] != 0:
490 490 num += 1
491 491
492 492 return num
493 493
494 494
495 495 def status_device(self):
496 496 """
497 497 This function returns the status of all abs-modules as one.
498 498 If at least one module is connected, its answer is "1"
499 499 """
500 500 self.absmodule_status()
501 501 connected_modules = self.connected_modules()
502 502 if connected_modules>0:
503 503 self.message = 'ABS modules Status have been updated.'
504 504 return 1
505 505 self.message = 'No ABS module is connected.'
506 506 return 0
507 507
508 508
509 509
510 510 def write_module(self, module):
511 511
512 512 """
513 513 Send configuration to one abs module
514 514 """
515 515
516 516 parameters = {}
517 517 ip_address = self.abs_conf.device.ip_address
518 518 ip_address = ip_address.split('.')
519 519 module_seq = (ip_address[0],ip_address[1],ip_address[2])
520 520 dot = '.'
521 521 module_ip = dot.join(module_seq)+'.'+str(module)
522 522 module_port = self.abs_conf.device.port_address
523 523 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
524 524
525 525 #print write_route
526 526
527 header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
528 module = 'ABS_'+str(module)
529 beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
530 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
527 #header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
528 #module = 'ABS_'+str(module)
529 beams = '!`*3<ENW'#{1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
530 # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
531 531
532 parameters['header'] = header
532 #parameters['header'] = header
533 533 parameters['module'] = module
534 534 parameters['beams'] = json.dumps(beams)
535 535
536 536 answer = ''
537 537
538 538 try:
539 539 r_write = requests.post(write_route, parameters, timeout=0.5)
540 540 answer = r_write.json()
541 541 self.message = answer['message']
542 542 except:
543 543 self.message = "Could not write ABS parameters"
544 544 return 0
545 545
546 546
547 547 #self.device.status = int(answer['status'])
548 548
549 549 return 1
550 550
551 551
552 552 def beam_selector(self, module, beam_pos):
553 553 """
554 554 This function selects the beam number for one absmodule.
555 555 """
556 556
557 557 if beam_pos > 0:
558 558 beam_pos = beam_pos - 1
559 559 else:
560 560 beam_pos = 0
561 561
562 562 #El indice del apunte debe ser menor que el numero total de apuntes
563 563 #El servidor tcp en el embebido comienza a contar desde 0
564 564 beams_list = ABSBeam.objects.filter(abs_conf=self)
565 565 if len(beams_list) < beam_pos:
566 566 return 0
567 567
568 568 flag = 1
569 569 if beam_pos>9:
570 570 flag = 2
571 571
572 572 module_address = ('192.168.1.'+str(module), 5500)
573 573 header = 'JROABSCeCnModCnMod0100000'
574 574 numbers = len(str(beam_pos))
575 575 function = 'CHGB'
576 576
577 577 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
578 578
579 579 # Create the datagram socket
580 580 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
581 581 #sock.connect(module_address)
582 582 try:
583 583 sock.connect(module_address)
584 584 sock.send(message_tx)
585 585 sock.close()
586 586 print("Writing abs module:"+module_address[0]+"...")
587 587 except:
588 588 sock = None
589 589 print("Problem writing abs module:"+module_address[0])
590 590 return 0
591 591
592 592 return 1
593 593
594 594
595 595 def change_beam(self, beam_pos):
596 596 """
597 597 This function selects the beam number for all absmodules.
598 598 """
599 599 for i in range(1,65):
600 600 try:
601 601 self.beam_selector(i,beam_pos)
602 602 except:
603 603 print("Problem with module: 192.168.1."+str(i))
604 604 self.message = "Problem with module: 192.168.1."+str(i)
605 605 #return 0
606 606 return 1
607 607
608 608
609 609 def send_beam_num(self, beam_pos):
610 610 """
611 611 This function connects to a multicast group and sends the beam number
612 612 to all abs modules.
613 613 """
614 614
615 615 if beam_pos > 0:
616 616 beam_pos = beam_pos - 1
617 617 else:
618 618 beam_pos = 0
619 619
620 620 #El indice del apunte debe ser menor que el numero total de apuntes
621 621 #El servidor tcp en el embebido comienza a contar desde 0
622 622 beams_list = ABSBeam.objects.filter(abs_conf=self)
623 623 if len(beams_list) < beam_pos:
624 624 return 0
625 625
626 626 flag = 1
627 627 if beam_pos>9:
628 628 flag = 2
629 629
630 630 header = 'JROABSCeCnModCnMod0100000'
631 631 flag = str(flag)
632 632 function = 'CHGB'
633 633 message_tx = header+flag+function+str(beam_pos)+'0'
634 634
635 635 multicast_group = '224.3.29.71'
636 636 server_address = ('',10000)
637 637
638 638 # Create the socket
639 639 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
640 640 # Bind to the server address
641 641 sock.bind(server_address)
642 642 # Telling the OS add the socket to the multicast on all interfaces
643 643 group = socket.inet_aton(multicast_group)
644 644 mreq = struct.pack('4sL', group, socket.INADDR_ANY)
645 645 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
646 646
647 647 #print 'sending acknowledgement to all: \n' + message_tx
648 648 sock.sendto(message_tx, (multicast_group, 10000))
649 649 sock.close()
650 650 sock = None
651 651
652 652 return 1
653 653
654 654 def test1(self):
655 655 t1 = time.time()
656 656 t2 = 0
657 657 while (t2-t1)<100:#300
658 658 t2 = time.time()
659 659 self.send_beam_num(2)
660 660 time.sleep(0.04)
661 661 self.send_beam_num(1)
662 662 time.sleep(0.04)
663 663 return
664 664
665 665 def change_procs_test1(self, module):
666 666
667 667 for i in range (1,300):#300
668 668 beam_pos = 1
669 669 module_address = ('192.168.1.'+str(module), 5500)
670 670 header = 'JROABSCeCnModCnMod0100000'
671 671 numbers = len(str(beam_pos))
672 672 function = 'CHGB'
673 673
674 674 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
675 675
676 676 # Create the datagram socket
677 677 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
678 678 sock.connect(module_address)
679 679
680 680 sock.send(message_tx)
681 681 #t = sock.recv(1024)
682 682 sock.close()
683 683 sock = None
684 684
685 685
686 686 time.sleep(0.04)
687 687
688 688
689 689 beam_pos = 0
690 690 numbers = len(str(beam_pos))
691 691
692 692 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
693 693
694 694 # Create the datagram socket
695 695 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
696 696 sock.connect(module_address)
697 697 sock.send(message_tx)
698 698 sock.close()
699 699 sock = None
700 700
701 701 time.sleep(0.04)
702 702
703 703
704 704 def multi_procs_test1(self):
705 705
706 706 """
707 707 This function sends the beam number to all abs modules using multiprocessing.
708 708 """
709 709
710 710 #if __name__ == "__main__":
711 711 size = 10000000 # Number of random numbers to add
712 712 procs = 65 # (Number-1) of processes to create (absmodule)
713 713
714 714 # Create a list of jobs and then iterate through
715 715 # the number of processes appending each process to
716 716 # the job list
717 717 jobs = []
718 718 for i in range(1, procs):
719 719
720 720 process = multiprocessing.Process(target=self.change_procs_test1,args=(i,))
721 721 jobs.append(process)
722 722 #print jobs
723 723
724 724 # Start the processes (i.e. calculate the random number lists)
725 725 for j in jobs:
726 726 #time.sleep(0.4)
727 727 #print j
728 728 j.start()
729 729
730 730 # Ensure all of the processes have finished
731 731 for j in jobs:
732 732 j.join()
733 733
734 734 print("List processing complete.")
735 735 return 1
736 736
737 737
738 738
739 739 def multi_procs_test2(self):
740 740 """
741 741 This function use multiprocessing python library. Importing Pool we can select
742 742 the number of cores we want to use
743 743 After 'nproc' linux command, we know how many cores computer has.
744 744 NOT WORKING
745 745 """
746 746 import multiprocessing
747 747 pool = multiprocessing.Pool(3) # cores
748 748
749 749 tasks = []
750 750 procs = 65
751 751 for i in range(62, procs):
752 752 tasks.append( (i,))
753 753 #print tasks
754 754 #pool.apply_async(self.change_procs_test1, 62)
755 755 results = [pool.apply( self.change_procs_test1, t ) for t in tasks]
756 756 #for result in results:
757 757 #result.get()
758 758 #(plotNum, plotFilename) = result.get()
759 759 #print("Result: plot %d written to %s" % (plotNum, plotFilename) )
760 760
761 761 return 1
762 762
763 763
764 764 def multi_procs_test3(self):
765 765 """
766 766 This function use multiprocessing python library. Importing Pool we can select
767 767 the number of cores we want to use
768 768 After 'nproc' linux command, we know how many cores computer has.
769 769 """
770 770 from multiprocessing import Pool
771 771 import time
772 772
773 773 def f(x):
774 774 return x*x
775 775
776 776 tasks = []
777 777 procs = 65
778 778 pool = Pool(processes=4)
779 779
780 780 #for i in range(62, procs):
781 781 #result = pool.map(f, range(62,65))
782 782 it = pool.imap(self.change_procs_test1, range(62,65))
783 783 #result.get(timeout=5)
784 784
785 785 return 1
786 786
787 787
788 788 def f(x):
789 789 print x
790 790 return
791 791
792 792 def multi_procs_test4(self):
793 793 import multiprocessing as mp
794 794
795 795
796 796 num_workers = mp.cpu_count()
797 797
798 798 pool = mp.Pool(num_workers)
799 799 procs = 65
800 800 for i in range(62, procs):
801 801 #for task in tasks:
802 802 pool.apply_async(self.f, args = (i,))
803 803
804 804 pool.close()
805 805 pool.join()
806 806
807 807 return 1
808 808
809 809
810 810 def get_absolute_url_import(self):
811 811 return reverse('url_import_abs_conf', args=[str(self.id)])
812 812
813 813
814 814
815 815
816 816 class ABSBeam(models.Model):
817 817
818 818 name = models.CharField(max_length=60, default='Beam')
819 819 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
820 820 abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration')
821 821 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
822 822 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
823 823 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
824 824 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
825 825 modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default))
826 826 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
827 827 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
828 828
829 829 class Meta:
830 830 db_table = 'abs_beams'
831 831
832 832 def __unicode__(self):
833 833 return u'%s' % (self.name)
834 834
835 835 def parms_to_dict(self):
836 836
837 837 #Update data
838 838 self.modules_6bits()
839 839
840 840 parameters = {}
841 841
842 842 parameters['name'] = self.name
843 843 parameters['antenna'] = ast.literal_eval(self.antenna)
844 844 parameters['abs_conf'] = self.abs_conf.name
845 845 parameters['tx'] = ast.literal_eval(self.tx)
846 846 parameters['rx'] = ast.literal_eval(self.rx)
847 847 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
848 848 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
849 849 parameters['configuration'] = ast.literal_eval(self.modules_conf)
850 850 parameters['ues'] = ast.literal_eval(self.ues)
851 851 parameters['only_rx'] = json.loads(self.only_rx)
852 852
853 853 return parameters
854 854
855 855 def dict_to_parms(self, parameters):
856 856
857 857 self.name = parameters['name']
858 858 self.antenna = json.dumps(parameters['antenna'])
859 859 #self.abs_conf = parameters['abs_conf']
860 860 self.tx = json.dumps(parameters['tx'])
861 861 self.rx = json.dumps(parameters['rx'])
862 862 #self.s_time = parameters['s_time']
863 863 #self.e_time = parameters['e_time']
864 864 self.ues = json.dumps(parameters['ues'])
865 865 self.only_rx = json.dumps(parameters['only_rx'])
866 866
867 867 self.modules_6bits()
868 868 self.save()
869 869
870 870
871 871 def clone(self, **kwargs):
872 872
873 873 self.pk = None
874 874 self.id = None
875 875 for attr, value in kwargs.items():
876 876 setattr(self, attr, value)
877 877
878 878 self.save()
879 879
880 880 return self
881 881
882 882
883 883
884 884 def module_6bits(self, module):
885 885 """
886 886 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
887 887 """
888 888 if module > 64:
889 889 beam_bits = ""
890 890 return beam_bits
891 891
892 892 data = ast.literal_eval(self.antenna)
893 893 up_data = data['antenna_up']
894 894 down_data = data['antenna_down']
895 895
896 896 pos = ip2position(module)
897 897 up_value = up_data[pos[0]][pos[1]]
898 898 down_value = down_data[pos[0]][pos[1]]
899 899
900 900 up_bits = up_conv_bits(up_value)
901 901 down_bits = down_conv_bits(down_value)
902 902 beam_bits = up_bits+down_bits
903 903
904 904 return beam_bits
905 905
906 906 def modules_6bits(self):
907 907 """
908 908 This function returns 6bits from every abs module (1-64) in a dict
909 909 """
910 910 modules_configuration = ast.literal_eval(self.modules_conf)
911 911
912 912 for i in range(1,65):
913 913 modules_configuration[str(i)] = self.module_6bits(i)
914 914
915 915 self.modules_conf = json.dumps(modules_configuration)
916 916 self.save()
917 917
918 918 return self.modules_conf
919 919
920 920
921 921 def set_as_activebeam(self):
922 922 """
923 923 This function set this beam as the active beam of its ABS Configuration.
924 924 """
925 925 self.abs_conf.active_beam = json.dumps({'active_beam': self.id})
926 926 self.abs_conf.save()
927 927
928 928 return
929 929
930 930
931 931 @property
932 932 def get_upvalues(self):
933 933 """
934 934 This function reads antenna pattern and show the up-value of one abs module
935 935 """
936 936
937 937 data = ast.literal_eval(self.antenna)
938 938 up_data = data['antenna_up']
939 939
940 940 up_values = []
941 941 for data in up_data:
942 942 for i in range(0,8):
943 943 up_values.append(data[i])
944 944
945 945 return up_values
946 946
947 947 @property
948 948 def antenna_upvalues(self):
949 949 """
950 950 This function reads antenna pattern and show the up - values of one abs beam
951 951 in a particular order
952 952 """
953 953 data = ast.literal_eval(self.antenna)
954 954 up_data = data['antenna_up']
955 955
956 956 return up_data
957 957
958 958 @property
959 959 def antenna_downvalues(self):
960 960 """
961 961 This function reads antenna pattern and show the down - values of one abs beam
962 962 in a particular order
963 963 """
964 964 data = ast.literal_eval(self.antenna)
965 965 down_data = data['antenna_down']
966 966
967 967 return down_data
968 968
969 969 @property
970 970 def get_downvalues(self):
971 971 """
972 972 This function reads antenna pattern and show the down-value of one abs module
973 973 """
974 974
975 975 data = ast.literal_eval(self.antenna)
976 976 down_data = data['antenna_down']
977 977
978 978 down_values = []
979 979 for data in down_data:
980 980 for i in range(0,8):
981 981 down_values.append(data[i])
982 982
983 983 return down_values
984 984
985 985 @property
986 986 def get_up_ues(self):
987 987 """
988 988 This function shows the up-ues-value of one beam
989 989 """
990 990 data = ast.literal_eval(self.ues)
991 991 up_ues = data['up']
992 992
993 993 return up_ues
994 994
995 995 @property
996 996 def get_down_ues(self):
997 997 """
998 998 This function shows the down-ues-value of one beam
999 999 """
1000 1000 data = ast.literal_eval(self.ues)
1001 1001 down_ues = data['down']
1002 1002
1003 1003 return down_ues
1004 1004
1005 1005 @property
1006 1006 def get_up_onlyrx(self):
1007 1007 """
1008 1008 This function shows the up-onlyrx-value of one beam
1009 1009 """
1010 1010 data = json.loads(self.only_rx)
1011 1011 up_onlyrx = data['up']
1012 1012
1013 1013 return up_onlyrx
1014 1014
1015 1015 @property
1016 1016 def get_down_onlyrx(self):
1017 1017 """
1018 1018 This function shows the down-onlyrx-value of one beam
1019 1019 """
1020 1020 data = json.loads(self.only_rx)
1021 1021 down_onlyrx = data['down']
1022 1022
1023 1023 return down_onlyrx
1024 1024
1025 1025 @property
1026 1026 def get_tx(self):
1027 1027 """
1028 1028 This function shows the tx-values of one beam
1029 1029 """
1030 1030 data = json.loads(self.tx)
1031 1031
1032 1032 return data
1033 1033
1034 1034 @property
1035 1035 def get_uptx(self):
1036 1036 """
1037 1037 This function shows the up-tx-values of one beam
1038 1038 """
1039 1039 data = json.loads(self.tx)
1040 1040 up_data = data['up']
1041 1041
1042 1042 up_values = []
1043 1043 for data in up_data:
1044 1044 for i in range(0,8):
1045 1045 up_values.append(data[i])
1046 1046
1047 1047 return up_values
1048 1048
1049 1049 @property
1050 1050 def get_downtx(self):
1051 1051 """
1052 1052 This function shows the down-tx-values of one beam
1053 1053 """
1054 1054 data = json.loads(self.tx)
1055 1055 down_data = data['down']
1056 1056
1057 1057 down_values = []
1058 1058 for data in down_data:
1059 1059 for i in range(0,8):
1060 1060 down_values.append(data[i])
1061 1061
1062 1062 return down_values
1063 1063
1064 1064
1065 1065
1066 1066 @property
1067 1067 def get_rx(self):
1068 1068 """
1069 1069 This function shows the rx-values of one beam
1070 1070 """
1071 1071 data = json.loads(self.rx)
1072 1072
1073 1073 return data
1074 1074
1075 1075 @property
1076 1076 def get_uprx(self):
1077 1077 """
1078 1078 This function shows the up-rx-values of one beam
1079 1079 """
1080 1080 data = json.loads(self.rx)
1081 1081 up_data = data['up']
1082 1082
1083 1083 up_values = []
1084 1084 for data in up_data:
1085 1085 for i in range(0,8):
1086 1086 up_values.append(data[i])
1087 1087
1088 1088 return up_values
1089 1089
1090 1090 @property
1091 1091 def get_downrx(self):
1092 1092 """
1093 1093 This function shows the down-rx-values of one beam
1094 1094 """
1095 1095 data = json.loads(self.rx)
1096 1096 down_data = data['down']
1097 1097
1098 1098 down_values = []
1099 1099 for data in down_data:
1100 1100 for i in range(0,8):
1101 1101 down_values.append(data[i])
1102 1102
1103 1103 return down_values
General Comments 0
You need to be logged in to leave comments. Login now