Importing a bunch of pages from old websites.
[website_subgeniuskitty.com] / data / development / misc / garmin_edge_305_linux.files / Edge305.py
CommitLineData
f6e94cb2
AT
1#!/usr/bin/python
2
3# Wrapper script to dump files from Garmin Edge 305 and convert to TCX for import into GoldenCheetah
4# Requires garmintools and garmin-dev-master packages
5# Needs root permissions for USB access
6
7# Aaron Taylor
8# ataylor@subgeniuskitty.com
9
10# Configuration
11# path to garmin_save_runs, garmin_dump and libgarmintools.so
12path_garmintools = "/home/ataylor/bin/garmintools"
13# path to gmn2tcx and support files (saxon, etc)
14path_gmn2tcx = "/home/ataylor/bin/gmn2tcx"
15# path to save gmn data files
16path_save_gmn = "/mnt/documents/Bicycling/logged_data"
17# path to save tcx files
18path_save_tcx = "/mnt/documents/Bicycling/logged_data/temp_tcx"
19# path to log file for garmintools output
20path_log = "/tmp/garmintools.log"
21# UID/GID to set on new files
22uid = 1000
23gid = 1000
24
25import subprocess, os
26
27# Set up the environment. We want to append to PATH and LD_LIBRARY_PATH
28# in case some system files are in a funky place. However, we must first
29# check that the variables exist since they might be blank (like
30# LD_LIBRARY_PATH on my system).
31my_env = os.environ
32try:
33 my_env["PATH"]
34except:
35 my_env["PATH"] = path_garmintools + ":" + path_gmn2tcx
36else:
37 my_env["PATH"] = my_env["PATH"] + ":" + path_garmintools + ":" + path_gmn2tcx
38try:
39 my_env["LD_LIBRARY_PATH"]
40except:
41 my_env["LD_LIBRARY_PATH"] = path_garmintools
42else:
43 my_env["LD_LIBRARY_PATH"] = my_env["LD_LIBRARY_PATH"] + ":" + path_garmintools
44my_env["GARMIN_SAVE_RUNS"] = path_save_gmn
45
46# Export the data from the GPS as binary gmn files and store STDOUT in result.
47result = subprocess.check_output(['/home/ataylor/bin/garmintools/garmin_save_runs'])
48
49# Newly written files will have a line starting with "Wrote: " and then the full
50# path to the new file. We want to extract only these lines and add them to the
51# written_files list.
52line_start = 0
53written_files = []
54n = 0
55for i in result:
56 if i == b'\n':
57 line_start = n+1
58 elif line_start == n:
59 first_chars = ""
60 for k in range(9):
61 first_chars += result[n+k]
62 if first_chars == b'Wrote: ':
63 k = 9
64 temp_string = ""
65 while result[n+k] != b'\n':
66 temp_string += result[n+k]
67 k += 1
68 written_files.append(temp_string)
69 n += 1
70
71# Dump output of garmintools run for analysis in case something goes wrong
72file_log = open(path_log, 'w')
73file_log.write(result)
74file_log.close()
75
76# For any newly written gmn files, save a copy as tcx format in directory specified
77# by path_save_tcx so I can import to GoldenCheetah next time I start it.
78devnull = open(os.devnull, 'w')
79if not written_files:
80 print "No new files to process."
81else:
82 print "New TCX files:"
83for path_gmn in written_files:
84 path_tcx = path_save_tcx + "/" + os.path.split(path_gmn)[1].replace(".gmn", ".tcx")
85 print path_tcx
86 file_tcx = open(path_tcx, 'w')
87 subprocess.call(["gmn2tcx", path_gmn], stdout=file_tcx, stderr=devnull)
88 file_tcx.close()
89 os.chown(path_tcx, uid, gid)
90devnull.close()