55
66import re
77import time
8+ import random
89import inspect
910import logging
1011import threading
1112import subprocess
1213from collections import deque
1314
14- __version__ = '0.6 '
15+ __version__ = '0.7 '
1516__all__ = ['spiCountBoards' , 'SPICommandCallback' , 'SPIProcessingThread' ,
1617 'psuSend' , 'psuRead' , 'psuCountTemperature' , 'psuTemperature' ,
1718 'rs485CountBoards' , 'rs485Reset' , 'rs485Sleep' , 'rs485Wake' , 'rs485Check' ,
3132
3233
3334# SPI control
34- MAX_SPI_RETRY = 2
35- WAIT_SPI_RETRY = 0.2
35+ MAX_SPI_RETRY = 4
36+ WAIT_SPI_RETRY = 0.25
3637
3738# SPI constants
3839SPI_cfg_normal = 0x0104
8889SPI_NoOp = 0x0000
8990
9091# I2C control
91- MAX_I2C_RETRY = 2
92- WAIT_I2C_RETRY = 0.2
92+ MAX_I2C_RETRY = 4
93+ WAIT_I2C_RETRY = 0.25
9394
9495# RS485 control
95- MAX_RS485_RETRY = 2
96- WAIT_RS485_RETRY = 0.2
96+ MAX_RS485_RETRY = 4
97+ WAIT_RS485_RETRY = 0.25
98+
99+
100+ def _sleep (interval , margin_percent = 5 ):
101+ """
102+ Helper function to give a slightly random sleep time to help break things
103+ up.
104+ """
105+
106+ time .sleep (interval * random .uniform (1 - margin_percent / 100. , 1 + margin_percent / 100. ))
97107
98108
99109def spiCountBoards (sub20Mapper , maxRetry = MAX_SPI_RETRY , waitRetry = WAIT_SPI_RETRY ):
@@ -108,7 +118,7 @@ def spiCountBoards(sub20Mapper, maxRetry=MAX_SPI_RETRY, waitRetry=WAIT_SPI_RETRY
108118 status = False
109119 while ((not status ) and (attempt <= maxRetry )):
110120 if attempt != 0 :
111- time . sleep (waitRetry )
121+ _sleep (waitRetry )
112122
113123 p = subprocess .Popen (['/usr/local/bin/countBoards' , str (sub20SN )],
114124 stdout = subprocess .PIPE , stderr = subprocess .PIPE ,
@@ -192,7 +202,7 @@ def _run_command(sub20SN, device_count, devices, spi_commands, maxRetry=MAX_SPI_
192202 status = False
193203 while ((not status ) and (attempt <= maxRetry )):
194204 if attempt != 0 :
195- time . sleep (waitRetry )
205+ _sleep (waitRetry )
196206
197207 try :
198208 subprocess .check_call (command )
@@ -284,7 +294,7 @@ def _read_register(sub20SN, device_count, devices, spi_registers, maxRetry=MAX_S
284294 data = {}
285295 while ((not status ) and (attempt <= maxRetry )):
286296 if attempt != 0 :
287- time . sleep (waitRetry )
297+ _sleep (waitRetry )
288298
289299 try :
290300 resp = subprocess .check_output (command , text = True )
@@ -343,7 +353,7 @@ def psuSend(sub20SN, psuAddress, state, maxRetry=MAX_I2C_RETRY, waitRetry=WAIT_I
343353 status = False
344354 for attempt in range (maxRetry + 1 ):
345355 if attempt != 0 :
346- time . sleep (waitRetry )
356+ _sleep (waitRetry )
347357
348358 try :
349359 p = subprocess .Popen (['/usr/local/bin/onoffPSU' , str (sub20SN ), '0x%02X' % psuAddress , str (state )],
@@ -372,7 +382,7 @@ def psuRead(sub20SN, psuAddress, maxRetry=MAX_I2C_RETRY, waitRetry=WAIT_I2C_RETR
372382 data = {}
373383 for attempt in range (maxRetry + 1 ):
374384 if attempt != 0 :
375- time . sleep (waitRetry )
385+ _sleep (waitRetry )
376386
377387 try :
378388 p = subprocess .Popen (['/usr/local/bin/readPSU' , str (sub20SN ), '0x%02X' % psuAddress ],
@@ -408,7 +418,7 @@ def psuCountTemperature(sub20SN, maxRetry=MAX_I2C_RETRY, waitRetry=WAIT_I2C_RETR
408418 ntemp = 0
409419 for attempt in range (maxRetry + 1 ):
410420 if attempt != 0 :
411- time . sleep (waitRetry )
421+ _sleep (waitRetry )
412422
413423 try :
414424 p = subprocess .Popen (['/usr/local/bin/countThermometers' , str (sub20SN )],
@@ -436,7 +446,7 @@ def psuTemperature(sub20SN, maxRetry=MAX_I2C_RETRY, waitRetry=WAIT_I2C_RETRY):
436446 temps = []
437447 for attempt in range (maxRetry + 1 ):
438448 if attempt == 0 :
439- time . sleep (waitRetry )
449+ _sleep (waitRetry )
440450
441451 try :
442452 p = subprocess .Popen (['/usr/local/bin/readThermometers' , str (sub20SN )],
@@ -475,7 +485,7 @@ def rs485CountBoards(sub20Mapper, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485
475485 status = False
476486 while ((not status ) and (attempt <= maxRetry )):
477487 if attempt != 0 :
478- time . sleep (waitRetry )
488+ _sleep (waitRetry )
479489
480490 p = subprocess .Popen (['/usr/local/bin/countPICs' , str (sub20SN )],
481491 stdout = subprocess .PIPE , stderr = subprocess .PIPE ,
@@ -524,7 +534,7 @@ def rs485Reset(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RETR
524534
525535 except Exception as e :
526536 aspSUB20Logger .warning ("Could not reset board %s: %s" , board_key , str (e ))
527- time . sleep (waitRetry )
537+ _sleep (waitRetry )
528538 success &= board_success
529539
530540 # Check for completion of reset
@@ -561,7 +571,7 @@ def rs485Sleep(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RETR
561571
562572 except Exception as e :
563573 aspSUB20Logger .warning ("Could not sleep board %s: %s" , board_key , str (e ))
564- time . sleep (waitRetry )
574+ _sleep (waitRetry )
565575 success &= board_success
566576
567577 return success
@@ -593,7 +603,7 @@ def rs485Wake(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RETRY
593603
594604 except Exception as e :
595605 aspSUB20Logger .warning ("Could not wake board %s: %s" , board_key , str (e ))
596- time . sleep (waitRetry )
606+ _sleep (waitRetry )
597607 success &= board_success
598608
599609 # Check for completion of wake
@@ -636,7 +646,7 @@ def rs485Check(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RETR
636646 except Exception as e :
637647 if verbose :
638648 aspSUB20Logger .warning ("Could not echo '%s' to board %s: %s" , data , board_key , str (e ))
639- time . sleep (waitRetry )
649+ _sleep (waitRetry )
640650 success &= board_success
641651 if not board_success :
642652 failed .append (antennaMapping [board_key ])
@@ -676,7 +686,7 @@ def rs485SetTime(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RE
676686 except Exception as e :
677687 if verbose :
678688 aspSUB20Logger .warning ("Could not set time to '%s' on board %s: %s" , data , board_key , str (e ))
679- time . sleep (waitRetry )
689+ _sleep (waitRetry )
680690 success &= board_success
681691 if not board_success :
682692 failed .append (antennaMapping [board_key ])
@@ -721,7 +731,7 @@ def rs485GetTime(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RE
721731 except Exception as e :
722732 if verbose :
723733 aspSUB20Logger .warning ("Could not get time from board %s: %s" , board_key , str (e ))
724- time . sleep (waitRetry )
734+ _sleep (waitRetry )
725735 success &= board_success
726736 if not board_success :
727737 data .append (0 )
@@ -766,7 +776,7 @@ def rs485Power(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RETR
766776
767777 except Exception as e :
768778 aspSUB20Logger .warning ("Could not get power info. for board %s: %s" , board_key , str (e ))
769- time . sleep (waitRetry )
779+ _sleep (waitRetry )
770780 success &= board_success
771781
772782 return success , fees
@@ -809,7 +819,7 @@ def rs485RFPower(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS485_RE
809819
810820 except Exception as e :
811821 aspSUB20Logger .warning ("Could not get RF power info. for board %s: %s" , board_key , str (e ))
812- time . sleep (waitRetry )
822+ _sleep (waitRetry )
813823 success &= board_success
814824
815825 return success , rf_powers
@@ -852,7 +862,7 @@ def rs485Temperature(sub20Mapper2, maxRetry=MAX_RS485_RETRY, waitRetry=WAIT_RS48
852862
853863 except Exception as e :
854864 aspSUB20Logger .warning ("Could not get temperature info. for board %s: %s" , board_key , str (e ))
855- time . sleep (waitRetry )
865+ _sleep (waitRetry )
856866 success &= board_success
857867
858868 return success , temps
0 commit comments