# Wrapper script to dump files from Garmin Edge 305 and convert to TCX for import into GoldenCheetah
# Requires garmintools and garmin-dev-master packages
# Needs root permissions for USB access
# ataylor@subgeniuskitty.com
# path to garmin_save_runs, garmin_dump and libgarmintools.so
path_garmintools
= "/home/ataylor/bin/garmintools"
# path to gmn2tcx and support files (saxon, etc)
path_gmn2tcx
= "/home/ataylor/bin/gmn2tcx"
# path to save gmn data files
path_save_gmn
= "/mnt/documents/Bicycling/logged_data"
path_save_tcx
= "/mnt/documents/Bicycling/logged_data/temp_tcx"
# path to log file for garmintools output
path_log
= "/tmp/garmintools.log"
# UID/GID to set on new files
# Set up the environment. We want to append to PATH and LD_LIBRARY_PATH
# in case some system files are in a funky place. However, we must first
# check that the variables exist since they might be blank (like
# LD_LIBRARY_PATH on my system).
my_env
["PATH"] = path_garmintools
+ ":" + path_gmn2tcx
my_env
["PATH"] = my_env
["PATH"] + ":" + path_garmintools
+ ":" + path_gmn2tcx
my_env
["LD_LIBRARY_PATH"]
my_env
["LD_LIBRARY_PATH"] = path_garmintools
my_env
["LD_LIBRARY_PATH"] = my_env
["LD_LIBRARY_PATH"] + ":" + path_garmintools
my_env
["GARMIN_SAVE_RUNS"] = path_save_gmn
# Export the data from the GPS as binary gmn files and store STDOUT in result.
result
= subprocess
.check_output(['/home/ataylor/bin/garmintools/garmin_save_runs'])
# Newly written files will have a line starting with "Wrote: " and then the full
# path to the new file. We want to extract only these lines and add them to the
first_chars
+= result
[n
+k
]
if first_chars
== b
'Wrote: ':
while result
[n
+k
] != b
'\n':
temp_string
+= result
[n
+k
]
written_files
.append(temp_string
)
# Dump output of garmintools run for analysis in case something goes wrong
file_log
= open(path_log
, 'w')
# For any newly written gmn files, save a copy as tcx format in directory specified
# by path_save_tcx so I can import to GoldenCheetah next time I start it.
devnull
= open(os
.devnull
, 'w')
print "No new files to process."
for path_gmn
in written_files
:
path_tcx
= path_save_tcx
+ "/" + os
.path
.split(path_gmn
)[1].replace(".gmn", ".tcx")
file_tcx
= open(path_tcx
, 'w')
subprocess
.call(["gmn2tcx", path_gmn
], stdout
=file_tcx
, stderr
=devnull
)
os
.chown(path_tcx
, uid
, gid
)