@@ -158,6 +158,23 def ip2position(module_number): | |||
|
158 | 158 | return pos |
|
159 | 159 | |
|
160 | 160 | |
|
161 | def fromBinary2Char(binary_string): | |
|
162 | number = int(binary_string, 2) | |
|
163 | #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f' | |
|
164 | number = number + 33 | |
|
165 | char = chr(number) | |
|
166 | return char | |
|
167 | ||
|
168 | def fromChar2Binary(char): | |
|
169 | number = ord(char) - 33 | |
|
170 | #Minus 33 to get the real value | |
|
171 | bits = bin(number)[2:] | |
|
172 | #To ensure we have a string with 6bits | |
|
173 | if len(bits) < 6: | |
|
174 | bits = bits.zfill(6) | |
|
175 | return bits | |
|
176 | ||
|
177 | ||
|
161 | 178 | def change_beam_for_multiprocessing(module): |
|
162 | 179 | |
|
163 | 180 | for i in range (1,50): |
@@ -226,6 +243,26 class ABSConfiguration(Configuration): | |||
|
226 | 243 | |
|
227 | 244 | return parameters |
|
228 | 245 | |
|
246 | def get_beams(self, **kwargs): | |
|
247 | ''' | |
|
248 | This function returns ABS Configuration beams | |
|
249 | ''' | |
|
250 | return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs) | |
|
251 | ||
|
252 | def clone(self, **kwargs): | |
|
253 | ||
|
254 | beams = self.get_beams() | |
|
255 | self.pk = None | |
|
256 | self.id = None | |
|
257 | for attr, value in kwargs.items(): | |
|
258 | setattr(self, attr, value) | |
|
259 | self.save() | |
|
260 | ||
|
261 | for beam in beams: | |
|
262 | beam.clone(abs_conf=self) | |
|
263 | ||
|
264 | return self | |
|
265 | ||
|
229 | 266 | |
|
230 | 267 | def module_conf(self, module_num, beams): |
|
231 | 268 | """ |
@@ -241,12 +278,13 class ABSConfiguration(Configuration): | |||
|
241 | 278 | |
|
242 | 279 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
243 | 280 | module = 'ABS_'+str(module_num) |
|
244 | bs = {} | |
|
281 | bs = '' #{} | |
|
245 | 282 | i=1 |
|
246 | 283 | #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
247 | 284 | # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
248 | 285 | for beam in beams: |
|
249 | bs[i] = beam.module_6bits(module_num) | |
|
286 | #bs[i] = fromBinary2Char(beam.module_6bits(module_num)) | |
|
287 | bs = bs + fromBinary2Char(beam.module_6bits(module_num)) | |
|
250 | 288 | i=i+1 |
|
251 | 289 | |
|
252 | 290 | beams = bs |
@@ -254,8 +292,8 class ABSConfiguration(Configuration): | |||
|
254 | 292 | parameters = {} |
|
255 | 293 | parameters['header'] = header |
|
256 | 294 | parameters['module'] = module |
|
257 |
parameters['beams'] = |
|
|
258 | ||
|
295 | parameters['beams'] = beams #json.dumps(beams) | |
|
296 | print parameters['beams'] | |
|
259 | 297 | answer = '' |
|
260 | 298 | |
|
261 | 299 | try: |
@@ -267,7 +305,6 class ABSConfiguration(Configuration): | |||
|
267 | 305 | return 0 |
|
268 | 306 | return 1 |
|
269 | 307 | |
|
270 | ||
|
271 | 308 | def read_module(self, module): |
|
272 | 309 | |
|
273 | 310 | """ |
@@ -291,10 +328,10 class ABSConfiguration(Configuration): | |||
|
291 | 328 | try: |
|
292 | 329 | r_write = requests.get(read_route, timeout=0.7) |
|
293 | 330 | answer = r_write.json() |
|
294 | message = answer['message'] | |
|
331 | self.message = answer['message'] | |
|
295 | 332 | module_bits = answer['allbits'] |
|
296 | 333 | except: |
|
297 | message = "Could not read ABS parameters" | |
|
334 | #message = "Could not read ABS parameters" | |
|
298 | 335 | return 0 |
|
299 | 336 | |
|
300 | 337 | return module_bits |
@@ -303,20 +340,22 class ABSConfiguration(Configuration): | |||
|
303 | 340 | def write_device(self): |
|
304 | 341 | """ |
|
305 | 342 | This function sends the beams list to every abs module. |
|
343 | It needs 'module_conf' function | |
|
306 | 344 | """ |
|
307 | 345 | |
|
308 | beams_list = ast.literal_eval(self.beams) | |
|
309 | beams = [] | |
|
310 | ||
|
311 | for bl in range(1,len(beams_list)+1): | |
|
312 | b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)]) | |
|
313 | beams.append(b) | |
|
346 | ###beams_list = ast.literal_eval(self.beams) | |
|
347 | ###beams = [] | |
|
348 | beams = ABSBeam.objects.filter(abs_conf=self) | |
|
349 | ###for bl in range(1,len(beams_list)+1): | |
|
350 | ### b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)]) | |
|
351 | ### beams.append(b) | |
|
314 | 352 | |
|
315 | 353 | #---Write each abs module--- |
|
354 | if beams: | |
|
316 | 355 | beams_status = ast.literal_eval(self.module_status) |
|
317 | for i in range(62,65): | |
|
356 | for i in range(62,65): #(62,65) | |
|
318 | 357 | try: |
|
319 | self.module_conf(i, beams) | |
|
358 | answer = self.module_conf(i, beams) | |
|
320 | 359 | beams_status[str(i)] = 1 |
|
321 | 360 | self.module_status = json.dumps(beams_status) |
|
322 | 361 | self.save() |
@@ -327,9 +366,20 class ABSConfiguration(Configuration): | |||
|
327 | 366 | beams_status[str(i)] = 0 |
|
328 | 367 | self.module_status = json.dumps(beams_status) |
|
329 | 368 | self.save() |
|
330 |
|
|
|
369 | answer = 0 | |
|
370 | return 0 | |
|
371 | else: | |
|
372 | self.message = "ABS Configuration does not have beams" | |
|
373 | return 0 | |
|
331 | 374 | |
|
332 | 375 | #self.device.status = 1 |
|
376 | ## | |
|
377 | if answer==1: | |
|
378 | self.message = "ABS Beams List have been sent to ABS Modules" | |
|
379 | else: | |
|
380 | self.message = "Could not read ABS parameters" | |
|
381 | ||
|
382 | ## | |
|
333 | 383 | self.save() |
|
334 | 384 | return 1 |
|
335 | 385 | |
@@ -481,18 +531,18 class ABSConfiguration(Configuration): | |||
|
481 | 531 | def test1(self): |
|
482 | 532 | t1 = time.time() |
|
483 | 533 | t2 = 0 |
|
484 |
while (t2-t1)< |
|
|
534 | while (t2-t1)<100:#300 | |
|
485 | 535 | t2 = time.time() |
|
486 | self.send_beam_num(1) | |
|
487 | time.sleep(0.04) | |
|
488 | 536 | self.send_beam_num(2) |
|
489 | 537 | time.sleep(0.04) |
|
538 | self.send_beam_num(1) | |
|
539 | time.sleep(0.04) | |
|
490 | 540 | return |
|
491 | 541 | |
|
492 | 542 | def change_procs_test1(self, module): |
|
493 | 543 | |
|
494 | for i in range (1,300): | |
|
495 |
beam_pos = |
|
|
544 | for i in range (1,300):#300 | |
|
545 | beam_pos = 1 | |
|
496 | 546 | module_address = ('192.168.1.'+str(module), 5500) |
|
497 | 547 | header = 'JROABSCeCnModCnMod0100000' |
|
498 | 548 | numbers = len(str(beam_pos)) |
@@ -510,10 +560,10 class ABSConfiguration(Configuration): | |||
|
510 | 560 | sock = None |
|
511 | 561 | |
|
512 | 562 | |
|
513 |
time.sleep(0. |
|
|
563 | time.sleep(0.04) | |
|
514 | 564 | |
|
515 | 565 | |
|
516 |
beam_pos = |
|
|
566 | beam_pos = 0 | |
|
517 | 567 | numbers = len(str(beam_pos)) |
|
518 | 568 | |
|
519 | 569 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
@@ -525,20 +575,24 class ABSConfiguration(Configuration): | |||
|
525 | 575 | sock.close() |
|
526 | 576 | sock = None |
|
527 | 577 | |
|
528 |
time.sleep(0. |
|
|
578 | time.sleep(0.04) | |
|
529 | 579 | |
|
530 | 580 | |
|
531 | 581 | def multi_procs_test1(self): |
|
532 | 582 | |
|
583 | """ | |
|
584 | This function sends the beam number to all abs modules using multiprocessing. | |
|
585 | """ | |
|
586 | ||
|
533 | 587 | #if __name__ == "__main__": |
|
534 | 588 | size = 10000000 # Number of random numbers to add |
|
535 | procs = 65 # (Number-1) of processes to create | |
|
589 | procs = 65 # (Number-1) of processes to create (absmodule) | |
|
536 | 590 | |
|
537 | 591 | # Create a list of jobs and then iterate through |
|
538 | 592 | # the number of processes appending each process to |
|
539 | 593 | # the job list |
|
540 | 594 | jobs = [] |
|
541 |
for i in range( |
|
|
595 | for i in range(1, procs): | |
|
542 | 596 | |
|
543 | 597 | process = multiprocessing.Process(target=self.change_procs_test1,args=(i,)) |
|
544 | 598 | jobs.append(process) |
@@ -558,6 +612,78 class ABSConfiguration(Configuration): | |||
|
558 | 612 | return 1 |
|
559 | 613 | |
|
560 | 614 | |
|
615 | ||
|
616 | def multi_procs_test2(self): | |
|
617 | """ | |
|
618 | This function use multiprocessing python library. Importing Pool we can select | |
|
619 | the number of cores we want to use | |
|
620 | After 'nproc' linux command, we know how many cores computer has. | |
|
621 | NOT WORKING | |
|
622 | """ | |
|
623 | import multiprocessing | |
|
624 | pool = multiprocessing.Pool(3) # cores | |
|
625 | ||
|
626 | tasks = [] | |
|
627 | procs = 65 | |
|
628 | for i in range(62, procs): | |
|
629 | tasks.append( (i,)) | |
|
630 | #print tasks | |
|
631 | #pool.apply_async(self.change_procs_test1, 62) | |
|
632 | results = [pool.apply( self.change_procs_test1, t ) for t in tasks] | |
|
633 | #for result in results: | |
|
634 | #result.get() | |
|
635 | #(plotNum, plotFilename) = result.get() | |
|
636 | #print("Result: plot %d written to %s" % (plotNum, plotFilename) ) | |
|
637 | ||
|
638 | return 1 | |
|
639 | ||
|
640 | ||
|
641 | def multi_procs_test3(self): | |
|
642 | """ | |
|
643 | This function use multiprocessing python library. Importing Pool we can select | |
|
644 | the number of cores we want to use | |
|
645 | After 'nproc' linux command, we know how many cores computer has. | |
|
646 | """ | |
|
647 | from multiprocessing import Pool | |
|
648 | import time | |
|
649 | ||
|
650 | def f(x): | |
|
651 | return x*x | |
|
652 | ||
|
653 | tasks = [] | |
|
654 | procs = 65 | |
|
655 | pool = Pool(processes=4) | |
|
656 | ||
|
657 | #for i in range(62, procs): | |
|
658 | #result = pool.map(f, range(62,65)) | |
|
659 | it = pool.imap(self.change_procs_test1, range(62,65)) | |
|
660 | #result.get(timeout=5) | |
|
661 | ||
|
662 | return 1 | |
|
663 | ||
|
664 | ||
|
665 | def f(x): | |
|
666 | print x | |
|
667 | return | |
|
668 | ||
|
669 | def multi_procs_test4(self): | |
|
670 | import multiprocessing as mp | |
|
671 | ||
|
672 | ||
|
673 | num_workers = mp.cpu_count() | |
|
674 | ||
|
675 | pool = mp.Pool(num_workers) | |
|
676 | procs = 65 | |
|
677 | for i in range(62, procs): | |
|
678 | #for task in tasks: | |
|
679 | pool.apply_async(self.f, args = (i,)) | |
|
680 | ||
|
681 | pool.close() | |
|
682 | pool.join() | |
|
683 | ||
|
684 | return 1 | |
|
685 | ||
|
686 | ||
|
561 | 687 | def status_device(self): |
|
562 | 688 | |
|
563 | 689 | return 1 |
@@ -620,6 +746,17 class ABSBeam(models.Model): | |||
|
620 | 746 | |
|
621 | 747 | return parameters |
|
622 | 748 | |
|
749 | def clone(self, **kwargs): | |
|
750 | ||
|
751 | self.pk = None | |
|
752 | self.id = None | |
|
753 | for attr, value in kwargs.items(): | |
|
754 | setattr(self, attr, value) | |
|
755 | ||
|
756 | self.save() | |
|
757 | ||
|
758 | return self | |
|
759 | ||
|
623 | 760 | |
|
624 | 761 | def change_beam(self, beam_pos=0): |
|
625 | 762 |
@@ -2,7 +2,7 | |||
|
2 | 2 | |
|
3 | 3 | {% block extra-menu-actions %} |
|
4 | 4 | <li><a href="{{ dev_conf.get_absolute_url_plot }}" target="_blank"><span class="glyphicon glyphicon-picture" aria-hidden="true"></span> View Patterns </a></li> |
|
5 | <li><a href="{{ dev_conf.get_absolute_url_write }}"><span class="glyphicon glyphicon-download" aria-hidden="true"></span> Write</a></li> | |
|
5 | <!--<li><a href="{{ dev_conf.get_absolute_url_write }}"><span class="glyphicon glyphicon-download" aria-hidden="true"></span> Write</a></li>--> | |
|
6 | 6 | {% endblock %} |
|
7 | 7 | |
|
8 | 8 | {% block extra-content %} |
@@ -193,7 +193,7 | |||
|
193 | 193 | </style> |
|
194 | 194 | |
|
195 | 195 | <h4>Beams:</h4> |
|
196 |
{% if beams |
|
|
196 | {% if beams %} | |
|
197 | 197 | |
|
198 | 198 | <div class="container"> |
|
199 | 199 | <ul class="nav nav-pills"> |
@@ -122,14 +122,14 def get_values_from_form(form_data): | |||
|
122 | 122 | def abs_conf(request, id_conf): |
|
123 | 123 | |
|
124 | 124 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
125 | beams = ABSBeam.objects.filter(abs_conf=conf) | |
|
126 | #beams_dict = ast.literal_eval(conf.beams) | |
|
127 | #beams = [] | |
|
128 | #for beam_id in range(1,len(beams_dict)+1): | |
|
129 | # beam = ABSBeam.objects.get(pk=beams_dict['beam'+str(beam_id)]) | |
|
130 | # beams.append(beam) | |
|
125 | 131 | |
|
126 |
beams_ |
|
|
127 | beams = [] | |
|
128 | for beam_id in range(1,len(beams_dict)+1): | |
|
129 | beam = ABSBeam.objects.get(pk=beams_dict['beam'+str(beam_id)]) | |
|
130 | beams.append(beam) | |
|
131 | ||
|
132 | beams_id = ast.literal_eval(conf.beams) | |
|
132 | #beams_id = ast.literal_eval(conf.beams) | |
|
133 | 133 | |
|
134 | 134 | ip=conf.device.ip_address |
|
135 | 135 | port=conf.device.port_address |
@@ -148,7 +148,7 def abs_conf(request, id_conf): | |||
|
148 | 148 | kwargs['button'] = 'Edit Configuration' |
|
149 | 149 | |
|
150 | 150 | #kwargs['no_play'] = True |
|
151 | kwargs['beams_id'] = beams_id | |
|
151 | #kwargs['beams_id'] = beams_id | |
|
152 | 152 | kwargs['beams'] = beams |
|
153 | 153 | kwargs['beam_selector'] = 0 |
|
154 | 154 | #kwargs['my_data'] = simplejson.dumps(beams) |
@@ -163,13 +163,16 def abs_conf(request, id_conf): | |||
|
163 | 163 | def abs_conf_edit(request, id_conf): |
|
164 | 164 | |
|
165 | 165 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
166 | beams_list = ast.literal_eval(conf.beams) | |
|
167 | i = 1 | |
|
168 | beams = [] | |
|
169 | for b in beams_list: | |
|
170 | beam = ABSBeam.objects.get(pk=beams_list['beam'+str(i)]) | |
|
171 | beams.append(beam) | |
|
172 | i=i+1 | |
|
166 | #beams_list = ast.literal_eval(conf.beams) | |
|
167 | #i = 1 | |
|
168 | #beams = [] | |
|
169 | #for b in beams_list: | |
|
170 | # beam = ABSBeam.objects.get(pk=beams_list['beam'+str(i)]) | |
|
171 | # beams.append(beam) | |
|
172 | # i=i+1 | |
|
173 | ||
|
174 | beams = ABSBeam.objects.filter(abs_conf=conf) | |
|
175 | print beams | |
|
173 | 176 | |
|
174 | 177 | if request.method=='GET': |
|
175 | 178 | form = ABSConfigurationForm(instance=conf) |
General Comments 0
You need to be logged in to leave comments.
Login now