Skip to content

Commit acc63ee

Browse files
committed
Clean up error handling on the RS485 calls.
1 parent 0db5e66 commit acc63ee

File tree

1 file changed

+146
-111
lines changed

1 file changed

+146
-111
lines changed

aspSUB20.py

Lines changed: 146 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -369,19 +369,23 @@ def rs485Reset(sub20Mapper2, maxRetry=0, waitRetry=0.2):
369369
board = int(board_key)
370370
board_success = False
371371
for attempt in range(maxRetry+1):
372-
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s RSET' % (sub20SN, board), shell=True,
373-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
374-
output, output2 = p.communicate()
375372
try:
376-
output = output.decode('ascii')
377-
output2 = output2.decode('ascii')
378-
except AttributeError:
379-
pass
380-
381-
if p.returncode == 0:
382-
board_success = True
383-
break
384-
else:
373+
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s RSET' % (sub20SN, board), shell=True,
374+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
375+
output, output2 = p.communicate()
376+
try:
377+
output = output.decode('ascii')
378+
output2 = output2.decode('ascii')
379+
except AttributeError:
380+
pass
381+
382+
if p.returncode == 0:
383+
board_success = True
384+
break
385+
else:
386+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
387+
388+
except Exception as e:
385389
aspSUB20Logger.warning("Could not reset board %s: %s", board_key, str(e))
386390
time.sleep(waitRetry)
387391
success &= board_success
@@ -407,19 +411,23 @@ def rs485Sleep(sub20Mapper2, maxRetry=0, waitRetry=0.2):
407411
board = int(board_key)
408412
board_success = False
409413
for attempt in range(maxRetry+1):
410-
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s SLEP' % (sub20SN, board), shell=True,
411-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
412-
output, output2 = p.communicate()
413414
try:
414-
output = output.decode('ascii')
415-
output2 = output2.decode('ascii')
416-
except AttributeError:
417-
pass
418-
419-
if p.returncode == 0:
420-
board_success = True
421-
break
422-
else:
415+
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s SLEP' % (sub20SN, board), shell=True,
416+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
417+
output, output2 = p.communicate()
418+
try:
419+
output = output.decode('ascii')
420+
output2 = output2.decode('ascii')
421+
except AttributeError:
422+
pass
423+
424+
if p.returncode == 0:
425+
board_success = True
426+
break
427+
else:
428+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
429+
430+
except Exception as e:
423431
aspSUB20Logger.warning("Could not sleep board %s: %s", board_key, str(e))
424432
time.sleep(waitRetry)
425433
success &= board_success
@@ -440,19 +448,23 @@ def rs485Wake(sub20Mapper2, maxRetry=0, waitRetry=0.2):
440448
board = int(board_key)
441449
board_success = False
442450
for attempt in range(maxRetry+1):
443-
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s WAKE' % (sub20SN, board), shell=True,
444-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
445-
output, output2 = p.communicate()
446451
try:
447-
output = output.decode('ascii')
448-
output2 = output2.decode('ascii')
449-
except AttributeError:
450-
pass
451-
452-
if p.returncode == 0:
453-
board_success = True
454-
break
455-
else:
452+
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s WAKE' % (sub20SN, board), shell=True,
453+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
454+
output, output2 = p.communicate()
455+
try:
456+
output = output.decode('ascii')
457+
output2 = output2.decode('ascii')
458+
except AttributeError:
459+
pass
460+
461+
if p.returncode == 0:
462+
board_success = True
463+
break
464+
else:
465+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
466+
467+
except Exception as e:
456468
aspSUB20Logger.warning("Could not wake board %s: %s", board_key, str(e))
457469
time.sleep(waitRetry)
458470
success &= board_success
@@ -483,19 +495,23 @@ def rs485Check(sub20Mapper2, maxRetry=0, waitRetry=0.2, verbose=False):
483495
board = int(board_key)
484496
board_success = False
485497
for attempt in range(maxRetry+1):
486-
p = subprocess.Popen('/usr/local/bin/sendPICDevice -v -d %s %s ECHO%s' % (sub20SN, board, data), shell=True,
487-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
488-
output, output2 = p.communicate()
489498
try:
490-
output = output.decode('ascii')
491-
output2 = output2.decode('ascii')
492-
except AttributeError:
493-
pass
494-
495-
if p.returncode == 0 and output.find():
496-
board_success = True
497-
break
498-
else:
499+
p = subprocess.Popen('/usr/local/bin/sendPICDevice -v -d %s %s ECHO%s' % (sub20SN, board, data), shell=True,
500+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
501+
output, output2 = p.communicate()
502+
try:
503+
output = output.decode('ascii')
504+
output2 = output2.decode('ascii')
505+
except AttributeError:
506+
pass
507+
508+
if p.returncode == 0 and output.find():
509+
board_success = True
510+
break
511+
else:
512+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
513+
514+
except Exception as e:
499515
if verbose:
500516
aspSUB20Logger.warning("Could not echo '%s' to board %s: %s", data, board_key, str(e))
501517
time.sleep(waitRetry)
@@ -524,19 +540,23 @@ def rs485SetTime(sub20Mapper2, maxRetry=0, waitRetry=0.2, verbose=False):
524540
board = int(board_key)
525541
board_success = False
526542
for attempt in range(maxRetry+1):
527-
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s STIM%s' % (sub20SN, board, data), shell=True,
528-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
529-
output, output2 = p.communicate()
530543
try:
531-
output = output.decode('ascii')
532-
output2 = output2.decode('ascii')
533-
except AttributeError:
534-
pass
535-
536-
if p.returncode == 0:
537-
board_success = True
538-
break
539-
else:
544+
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s STIM%s' % (sub20SN, board, data), shell=True,
545+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
546+
output, output2 = p.communicate()
547+
try:
548+
output = output.decode('ascii')
549+
output2 = output2.decode('ascii')
550+
except AttributeError:
551+
pass
552+
553+
if p.returncode == 0:
554+
board_success = True
555+
break
556+
else:
557+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
558+
559+
except Exception as e:
540560
if verbose:
541561
aspSUB20Logger.warning("Could not set time to '%s' on board %s: %s", data, board_key, str(e))
542562
time.sleep(waitRetry)
@@ -567,22 +587,26 @@ def rs485GetTime(sub20Mapper2, maxRetry=0, waitRetry=0.2, verbose=False):
567587
board = int(board_key)
568588
board_success = False
569589
for attempt in range(maxRetry+1):
570-
p = subprocess.Popen('/usr/local/bin/sendPICDevice -v -d %s %s GTIM' % (sub20SN, board), shell=True,
571-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
572-
output, output2 = p.communicate()
573590
try:
574-
output = output.decode('ascii')
575-
output2 = output2.decode('ascii')
576-
except AttributeError:
577-
pass
578-
579-
mtch = gtimRE.search(output)
580-
if mtch is not None:
581-
gtim_data = mtch.group('gtim')
582-
data.append(int(gtim_data, 10))
583-
board_success = True
584-
break
585-
else:
591+
p = subprocess.Popen('/usr/local/bin/sendPICDevice -v -d %s %s GTIM' % (sub20SN, board), shell=True,
592+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
593+
output, output2 = p.communicate()
594+
try:
595+
output = output.decode('ascii')
596+
output2 = output2.decode('ascii')
597+
except AttributeError:
598+
pass
599+
600+
mtch = gtimRE.search(output)
601+
if mtch is not None:
602+
gtim_data = mtch.group('gtim')
603+
data.append(int(gtim_data, 10))
604+
board_success = True
605+
break
606+
else:
607+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
608+
609+
except Exception as e:
586610
if verbose:
587611
aspSUB20Logger.warning("Could not get time from board %s: %s", board_key, str(e))
588612
time.sleep(waitRetry)
@@ -611,25 +635,29 @@ def rs485Power(sub20Mapper2, maxRetry=0, waitRetry=0.2):
611635
board = int(board_key)
612636
board_success = False
613637
for attempt in range(maxRetry+1):
614-
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s CURA' % (sub20SN, board), shell=True,
615-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
616-
output, output2 = p.communicate()
617638
try:
618-
output = output.decode('ascii')
619-
output2 = output2.decode('ascii')
620-
except AttributeError:
621-
pass
622-
623-
if p.returncode == 0:
624-
for line in filter(lambda x: x.find(' mA') != -1, output.split('\n')):
625-
mtch = curaRE.search(line)
626-
if mtch is not None:
627-
fees.append(float(mtch.group('curr')))
628-
else:
629-
fees.append(-1.0)
630-
board_success = True
631-
break
632-
else:
639+
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s CURA' % (sub20SN, board), shell=True,
640+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
641+
output, output2 = p.communicate()
642+
try:
643+
output = output.decode('ascii')
644+
output2 = output2.decode('ascii')
645+
except AttributeError:
646+
pass
647+
648+
if p.returncode == 0:
649+
for line in filter(lambda x: x.find(' mA') != -1, output.split('\n')):
650+
mtch = curaRE.search(line)
651+
if mtch is not None:
652+
fees.append(float(mtch.group('curr')))
653+
else:
654+
fees.append(-1.0)
655+
board_success = True
656+
break
657+
else:
658+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
659+
660+
except Exception as e:
633661
aspSUB20Logger.warning("Could not get power info. for board %s: %s", board_key, str(e))
634662
time.sleep(waitRetry)
635663
success &= board_success
@@ -674,6 +702,9 @@ def rs485RFPower(sub20Mapper2, maxRetry=0, waitRetry=0.2):
674702
rf_powers.append(-1.0)
675703
board_success = True
676704
break
705+
else:
706+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
707+
677708
except Exception as e:
678709
aspSUB20Logger.warning("Could not get RF power info. for board %s: %s", board_key, str(e))
679710
time.sleep(waitRetry)
@@ -700,25 +731,29 @@ def rs485Temperature(sub20Mapper2, maxRetry=0, waitRetry=0.2):
700731
board = int(board_key)
701732
board_success = False
702733
for attempt in range(maxRetry+1):
703-
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s OWTE' % (sub20SN, board), shell=True,
704-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
705-
output, output2 = p.communicate()
706734
try:
707-
output = output.decode('ascii')
708-
output2 = output2.decode('ascii')
709-
except AttributeError:
710-
pass
711-
712-
if p.returncode == 0:
713-
for line in filter(lambda x: x.find(' C') != -1, output.split('\n')):
714-
mtch = owteRE.search(line)
715-
if mtch is not None:
716-
temps.append(float(mtch.group('temp')))
717-
else:
718-
temps.append(-99.0)
719-
board_success = True
720-
break
721-
else:
735+
p = subprocess.Popen('/usr/local/bin/sendPICDevice %s %s OWTE' % (sub20SN, board), shell=True,
736+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
737+
output, output2 = p.communicate()
738+
try:
739+
output = output.decode('ascii')
740+
output2 = output2.decode('ascii')
741+
except AttributeError:
742+
pass
743+
744+
if p.returncode == 0:
745+
for line in filter(lambda x: x.find(' C') != -1, output.split('\n')):
746+
mtch = owteRE.search(line)
747+
if mtch is not None:
748+
temps.append(float(mtch.group('temp')))
749+
else:
750+
temps.append(-99.0)
751+
board_success = True
752+
break
753+
else:
754+
raise RuntimeError("Non-zero return code: %s" % output2.replace('\n', ' - '))
755+
756+
except Exception as e:
722757
aspSUB20Logger.warning("Could not get temperature info. for board %s: %s", board_key, str(e))
723758
time.sleep(waitRetry)
724759
success &= board_success

0 commit comments

Comments
 (0)