Message boards : Number crunching : USB port reset script
Author | Message |
---|---|
Here is a Python script named dm.py that monitors the % progress (% complete) of your R@H task. If the task is running (not suspended) and the % progress stops increasing, dm.py plays a sound then attempts to reset the USB port the detector is connected to. logg = u'' # all platforms Insert the path to wherever you want the log file to be saved between the two single-quotes, for example: logg = u'c:\mydocuments\logs\dm.log' # all platforms The path must exist which means you must create the path if it does not exist now. If you don't insert a path between the single-quotes the script will not save a log to disk. It will always log to stdout which will likely be the terminal in which you run the script unless redirected. Linux users must configure the line that says: sound_file = u'' # Linux only Insert the path to a sound file between the 2 single-quotes. If you don't insert a path the script will not play a warning sound when the % progress stops. Wav, ogg and several other sound file formats are acceptable and are found in /usr/share/sounds/ on many distros. Warning: The Python interpreter is very fussy about line indents. It use line indents to determine start/end of compound statements and other things. Yes it's wierd but it removes the need for ; to mark the end of statements and all those brackets other languages can't live without. But if you screw up the indents the script won't run. The point is this ---> Before you go bashing around on the keyboard with your big thick fingers and screw up the indents, be sure to make a note of how the lines are indented immediately before and after the line you want to edit. Make sure those indents are the same before saving your edit. Copy all the lines between the start start start start and stop stop stop stop lines but do not include those 2 lines. start start start start start start start start
import usb
from sys import platform
from os import walk
from os.path import join, isfile, exists
from subprocess import check_output
from time import sleep, strftime
if platform.startswith(u'win'):import _winreg, winsound
elif platform.startswith(u'linux'): from pygame import mixer
# START CONFIGURE HERE ############################################################################
winks = 150 # all platforms
logg = u'dm.log' # all platforms
sound_file = u'/usr/share/sounds/KDE-Im-Connection-Lost.ogg' # Linux only
# END CONFIGURE ###################################################################################
plat = ''
###################################################################################################
def log_it(x):
if not logg == '':
log_file = open(logg, 'a')
try:
log_file.write(strftime('%c') + ' ' + x + '\n')
except:
print (strftime('%c') + ' exception in log_it()\n')
log_file.close()
print(x)
###################################################################################################
def search_paths():
global plat
# searches the system for paths to BOINC data dir and BOINC binary dir, in the case of Windows the
# info is read from the registry, for other OSs the file system is searched
plat = platform
if plat.startswith(u'win'):
plat = u'win'
reg_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, u'Software\\Space Sciences Laboratory, U.C. Berkeley\\BOINC Setup\\')
boinc_data = _winreg.QueryValueEx(reg_key, u'DATADIR')[0]
boinc_binary = _winreg.QueryValueEx(reg_key, u'INSTALLDIR')[0]
_winreg.CloseKey(reg_key)
elif plat.startswith(u'linux'):
plat = u'linux'
boinc_data = u'/'
elif plat.startswith(u'darwin'):
plat = u'darwin'
boinc_data = u'/'
else:
plat = u'dunno'
boinc_data = u'/'
if boinc_data == u'/': # if / then it's Linux or OS X, find the BOINC binary dir
boinc_binary = ''
for root, dirs, files in walk(u'/'):
for name in files:
if name == u'boinccmd':
boinc_binary = root
break
if not boinc_binary == '': break
for root, dirs, files in walk(boinc_data):
for name in files:
if name == u'gui_rpc_auth.cfg':
boinc_data = root
break
if not boinc_data == u'/': break
return (boinc_data, boinc_binary)
###################################################################################################
def dev_reset():
global plat
if plat == u'win':
winsound.MessageBeep(winsound.MB_ICONHAND)
elif plat == u'linux':
if exists(sound_file):
mixer.init()
alert = mixer.Sound(sound_file)
alert.play()
for bus in usb.busses():
for dev in bus.devices:
if dev.idVendor == u'5824' and dev.idProduct == u'1503':
dev.reset()
###################################################################################################
frac = 0.0
old_frac = 0.0
b_dirs = search_paths()
b_dirs = ('/home/kim/BOINC/', '/home/kim/BOINC/')
infile = open(join(b_dirs[1], u'gui_rpc_auth.cfg' ), u'r')
passwd = infile.read().strip('\n')
infile.close()
log_it('')
log_it(u'Starting dm.py')
#print(b_dirs, passwd)
while True:
tasks = check_output([join(b_dirs[0], u'boinccmd'), u'--host', u'localhost', u'--passwd', passwd, u'--get_tasks']).split(u'-------')[1:]
for task in tasks:
if u'http://radioactiveathome.org/boinc/' in task:
break
task = task.splitlines()
old_frac = frac
for line in task:
if line.startswith(u' state: '):
state = line.split(':')[1].strip(' ')
elif line.startswith(u' scheduler state: '):
sched_state = line.split(':')[1].strip(' ')
elif line.startswith(u' fraction done: '):
frac = float(line.split(':')[1].strip(' ').strip(' '))
if state == u'2' and sched_state == u'2':
if frac == old_frac:
log_it(state + u' ' + sched_state + u' ' + str(frac) + u' ' + u'possible disconnect')
dev_reset()
else:
count = 0
log_it(state + ' ' + sched_state + ' ' + str(frac))
else: # not state == '2' or not sched_state == '2':
count = 0
log_it(state + ' ' + sched_state + u' task is not running')
sleep(winks)
stop stop stop stop stop stop stop stop stop ____________ | |
ID: 1010 | Rating: 0 | rate: / Reply Quote | |
Why so difficult? | |
ID: 1054 | Rating: 0 | rate: / Reply Quote | |
That's not so easy, the sensor would have to monitor the connection at hardware level (not the actual data transfer) to avoid constantly reconnecting if connected to USB but without app running. | |
ID: 1056 | Rating: 0 | rate: / Reply Quote | |
I realized - a check on the data and if there was no transfer of the connection to usb again initiated. | |
ID: 1060 | Rating: 0 | rate: / Reply Quote | |
Message boards :
Number crunching :
USB port reset script