@@ -158,6 +158,23 def ip2position(module_number): | |||||
158 | return pos |
|
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 | def change_beam_for_multiprocessing(module): |
|
178 | def change_beam_for_multiprocessing(module): | |
162 |
|
179 | |||
163 | for i in range (1,50): |
|
180 | for i in range (1,50): | |
@@ -226,6 +243,26 class ABSConfiguration(Configuration): | |||||
226 |
|
243 | |||
227 | return parameters |
|
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 | def module_conf(self, module_num, beams): |
|
267 | def module_conf(self, module_num, beams): | |
231 | """ |
|
268 | """ | |
@@ -241,12 +278,13 class ABSConfiguration(Configuration): | |||||
241 |
|
278 | |||
242 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
279 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' | |
243 | module = 'ABS_'+str(module_num) |
|
280 | module = 'ABS_'+str(module_num) | |
244 | bs = {} |
|
281 | bs = '' #{} | |
245 | i=1 |
|
282 | i=1 | |
246 | #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
283 | #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', | |
247 | # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
284 | # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} | |
248 | for beam in beams: |
|
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 | i=i+1 |
|
288 | i=i+1 | |
251 |
|
289 | |||
252 | beams = bs |
|
290 | beams = bs | |
@@ -254,8 +292,8 class ABSConfiguration(Configuration): | |||||
254 | parameters = {} |
|
292 | parameters = {} | |
255 | parameters['header'] = header |
|
293 | parameters['header'] = header | |
256 | parameters['module'] = module |
|
294 | parameters['module'] = module | |
257 |
parameters['beams'] = |
|
295 | parameters['beams'] = beams #json.dumps(beams) | |
258 |
|
296 | print parameters['beams'] | ||
259 | answer = '' |
|
297 | answer = '' | |
260 |
|
298 | |||
261 | try: |
|
299 | try: | |
@@ -267,7 +305,6 class ABSConfiguration(Configuration): | |||||
267 | return 0 |
|
305 | return 0 | |
268 | return 1 |
|
306 | return 1 | |
269 |
|
307 | |||
270 |
|
||||
271 | def read_module(self, module): |
|
308 | def read_module(self, module): | |
272 |
|
309 | |||
273 | """ |
|
310 | """ | |
@@ -291,10 +328,10 class ABSConfiguration(Configuration): | |||||
291 | try: |
|
328 | try: | |
292 | r_write = requests.get(read_route, timeout=0.7) |
|
329 | r_write = requests.get(read_route, timeout=0.7) | |
293 | answer = r_write.json() |
|
330 | answer = r_write.json() | |
294 | message = answer['message'] |
|
331 | self.message = answer['message'] | |
295 | module_bits = answer['allbits'] |
|
332 | module_bits = answer['allbits'] | |
296 | except: |
|
333 | except: | |
297 | message = "Could not read ABS parameters" |
|
334 | #message = "Could not read ABS parameters" | |
298 | return 0 |
|
335 | return 0 | |
299 |
|
336 | |||
300 | return module_bits |
|
337 | return module_bits | |
@@ -303,33 +340,46 class ABSConfiguration(Configuration): | |||||
303 | def write_device(self): |
|
340 | def write_device(self): | |
304 | """ |
|
341 | """ | |
305 | This function sends the beams list to every abs module. |
|
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) |
|
346 | ###beams_list = ast.literal_eval(self.beams) | |
309 | beams = [] |
|
347 | ###beams = [] | |
310 |
|
348 | beams = ABSBeam.objects.filter(abs_conf=self) | ||
311 | for bl in range(1,len(beams_list)+1): |
|
349 | ###for bl in range(1,len(beams_list)+1): | |
312 | b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)]) |
|
350 | ### b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)]) | |
313 | beams.append(b) |
|
351 | ### beams.append(b) | |
314 |
|
352 | |||
315 | #---Write each abs module--- |
|
353 | #---Write each abs module--- | |
316 | beams_status = ast.literal_eval(self.module_status) |
|
354 | if beams: | |
317 | for i in range(62,65): |
|
355 | beams_status = ast.literal_eval(self.module_status) | |
318 | try: |
|
356 | for i in range(62,65): #(62,65) | |
319 | self.module_conf(i, beams) |
|
357 | try: | |
320 | beams_status[str(i)] = 1 |
|
358 | answer = self.module_conf(i, beams) | |
321 | self.module_status = json.dumps(beams_status) |
|
359 | beams_status[str(i)] = 1 | |
322 | self.save() |
|
360 | self.module_status = json.dumps(beams_status) | |
323 | #self.module_conf(63,beams) |
|
361 | self.save() | |
324 | #beams_status[str(63)] = 1 |
|
362 | #self.module_conf(63,beams) | |
325 | #self.module_status = json.dumps(beams_status) |
|
363 | #beams_status[str(63)] = 1 | |
326 | except: |
|
364 | #self.module_status = json.dumps(beams_status) | |
327 | beams_status[str(i)] = 0 |
|
365 | except: | |
328 | self.module_status = json.dumps(beams_status) |
|
366 | beams_status[str(i)] = 0 | |
329 | self.save() |
|
367 | self.module_status = json.dumps(beams_status) | |
330 |
|
|
368 | self.save() | |
|
369 | answer = 0 | |||
|
370 | return 0 | |||
|
371 | else: | |||
|
372 | self.message = "ABS Configuration does not have beams" | |||
|
373 | return 0 | |||
331 |
|
374 | |||
332 | #self.device.status = 1 |
|
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 | self.save() |
|
383 | self.save() | |
334 | return 1 |
|
384 | return 1 | |
335 |
|
385 | |||
@@ -481,18 +531,18 class ABSConfiguration(Configuration): | |||||
481 | def test1(self): |
|
531 | def test1(self): | |
482 | t1 = time.time() |
|
532 | t1 = time.time() | |
483 | t2 = 0 |
|
533 | t2 = 0 | |
484 |
while (t2-t1)< |
|
534 | while (t2-t1)<100:#300 | |
485 | t2 = time.time() |
|
535 | t2 = time.time() | |
486 | self.send_beam_num(1) |
|
|||
487 | time.sleep(0.04) |
|
|||
488 | self.send_beam_num(2) |
|
536 | self.send_beam_num(2) | |
489 | time.sleep(0.04) |
|
537 | time.sleep(0.04) | |
|
538 | self.send_beam_num(1) | |||
|
539 | time.sleep(0.04) | |||
490 | return |
|
540 | return | |
491 |
|
541 | |||
492 | def change_procs_test1(self, module): |
|
542 | def change_procs_test1(self, module): | |
493 |
|
543 | |||
494 | for i in range (1,300): |
|
544 | for i in range (1,300):#300 | |
495 |
beam_pos = |
|
545 | beam_pos = 1 | |
496 | module_address = ('192.168.1.'+str(module), 5500) |
|
546 | module_address = ('192.168.1.'+str(module), 5500) | |
497 | header = 'JROABSCeCnModCnMod0100000' |
|
547 | header = 'JROABSCeCnModCnMod0100000' | |
498 | numbers = len(str(beam_pos)) |
|
548 | numbers = len(str(beam_pos)) | |
@@ -510,10 +560,10 class ABSConfiguration(Configuration): | |||||
510 | sock = None |
|
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 | numbers = len(str(beam_pos)) |
|
567 | numbers = len(str(beam_pos)) | |
518 |
|
568 | |||
519 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
569 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |
@@ -525,20 +575,24 class ABSConfiguration(Configuration): | |||||
525 | sock.close() |
|
575 | sock.close() | |
526 | sock = None |
|
576 | sock = None | |
527 |
|
577 | |||
528 |
time.sleep(0. |
|
578 | time.sleep(0.04) | |
529 |
|
579 | |||
530 |
|
580 | |||
531 | def multi_procs_test1(self): |
|
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 | #if __name__ == "__main__": |
|
587 | #if __name__ == "__main__": | |
534 | size = 10000000 # Number of random numbers to add |
|
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 | # Create a list of jobs and then iterate through |
|
591 | # Create a list of jobs and then iterate through | |
538 | # the number of processes appending each process to |
|
592 | # the number of processes appending each process to | |
539 | # the job list |
|
593 | # the job list | |
540 | jobs = [] |
|
594 | jobs = [] | |
541 |
for i in range( |
|
595 | for i in range(1, procs): | |
542 |
|
596 | |||
543 | process = multiprocessing.Process(target=self.change_procs_test1,args=(i,)) |
|
597 | process = multiprocessing.Process(target=self.change_procs_test1,args=(i,)) | |
544 | jobs.append(process) |
|
598 | jobs.append(process) | |
@@ -558,6 +612,78 class ABSConfiguration(Configuration): | |||||
558 | return 1 |
|
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 | def status_device(self): |
|
687 | def status_device(self): | |
562 |
|
688 | |||
563 | return 1 |
|
689 | return 1 | |
@@ -620,6 +746,17 class ABSBeam(models.Model): | |||||
620 |
|
746 | |||
621 | return parameters |
|
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 | def change_beam(self, beam_pos=0): |
|
761 | def change_beam(self, beam_pos=0): | |
625 |
|
762 |
@@ -2,7 +2,7 | |||||
2 |
|
2 | |||
3 | {% block extra-menu-actions %} |
|
3 | {% block extra-menu-actions %} | |
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> |
|
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 | {% endblock %} |
|
6 | {% endblock %} | |
7 |
|
7 | |||
8 | {% block extra-content %} |
|
8 | {% block extra-content %} | |
@@ -193,7 +193,7 | |||||
193 | </style> |
|
193 | </style> | |
194 |
|
194 | |||
195 | <h4>Beams:</h4> |
|
195 | <h4>Beams:</h4> | |
196 |
{% if beams |
|
196 | {% if beams %} | |
197 |
|
197 | |||
198 | <div class="container"> |
|
198 | <div class="container"> | |
199 | <ul class="nav nav-pills"> |
|
199 | <ul class="nav nav-pills"> |
@@ -122,14 +122,14 def get_values_from_form(form_data): | |||||
122 | def abs_conf(request, id_conf): |
|
122 | def abs_conf(request, id_conf): | |
123 |
|
123 | |||
124 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
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_ |
|
132 | #beams_id = 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) |
|
|||
131 |
|
||||
132 | beams_id = ast.literal_eval(conf.beams) |
|
|||
133 |
|
133 | |||
134 | ip=conf.device.ip_address |
|
134 | ip=conf.device.ip_address | |
135 | port=conf.device.port_address |
|
135 | port=conf.device.port_address | |
@@ -148,7 +148,7 def abs_conf(request, id_conf): | |||||
148 | kwargs['button'] = 'Edit Configuration' |
|
148 | kwargs['button'] = 'Edit Configuration' | |
149 |
|
149 | |||
150 | #kwargs['no_play'] = True |
|
150 | #kwargs['no_play'] = True | |
151 | kwargs['beams_id'] = beams_id |
|
151 | #kwargs['beams_id'] = beams_id | |
152 | kwargs['beams'] = beams |
|
152 | kwargs['beams'] = beams | |
153 | kwargs['beam_selector'] = 0 |
|
153 | kwargs['beam_selector'] = 0 | |
154 | #kwargs['my_data'] = simplejson.dumps(beams) |
|
154 | #kwargs['my_data'] = simplejson.dumps(beams) | |
@@ -163,13 +163,16 def abs_conf(request, id_conf): | |||||
163 | def abs_conf_edit(request, id_conf): |
|
163 | def abs_conf_edit(request, id_conf): | |
164 |
|
164 | |||
165 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
165 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) | |
166 | beams_list = ast.literal_eval(conf.beams) |
|
166 | #beams_list = ast.literal_eval(conf.beams) | |
167 | i = 1 |
|
167 | #i = 1 | |
168 | beams = [] |
|
168 | #beams = [] | |
169 | for b in beams_list: |
|
169 | #for b in beams_list: | |
170 | beam = ABSBeam.objects.get(pk=beams_list['beam'+str(i)]) |
|
170 | # beam = ABSBeam.objects.get(pk=beams_list['beam'+str(i)]) | |
171 | beams.append(beam) |
|
171 | # beams.append(beam) | |
172 | i=i+1 |
|
172 | # i=i+1 | |
|
173 | ||||
|
174 | beams = ABSBeam.objects.filter(abs_conf=conf) | |||
|
175 | print beams | |||
173 |
|
176 | |||
174 | if request.method=='GET': |
|
177 | if request.method=='GET': | |
175 | form = ABSConfigurationForm(instance=conf) |
|
178 | form = ABSConfigurationForm(instance=conf) |
General Comments 0
You need to be logged in to leave comments.
Login now