Commit | Line | Data |
---|---|---|
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 | |
12 | path_garmintools = "/home/ataylor/bin/garmintools" | |
13 | # path to gmn2tcx and support files (saxon, etc) | |
14 | path_gmn2tcx = "/home/ataylor/bin/gmn2tcx" | |
15 | # path to save gmn data files | |
16 | path_save_gmn = "/mnt/documents/Bicycling/logged_data" | |
17 | # path to save tcx files | |
18 | path_save_tcx = "/mnt/documents/Bicycling/logged_data/temp_tcx" | |
19 | # path to log file for garmintools output | |
20 | path_log = "/tmp/garmintools.log" | |
21 | # UID/GID to set on new files | |
22 | uid = 1000 | |
23 | gid = 1000 | |
24 | ||
25 | import 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). | |
31 | my_env = os.environ | |
32 | try: | |
33 | my_env["PATH"] | |
34 | except: | |
35 | my_env["PATH"] = path_garmintools + ":" + path_gmn2tcx | |
36 | else: | |
37 | my_env["PATH"] = my_env["PATH"] + ":" + path_garmintools + ":" + path_gmn2tcx | |
38 | try: | |
39 | my_env["LD_LIBRARY_PATH"] | |
40 | except: | |
41 | my_env["LD_LIBRARY_PATH"] = path_garmintools | |
42 | else: | |
43 | my_env["LD_LIBRARY_PATH"] = my_env["LD_LIBRARY_PATH"] + ":" + path_garmintools | |
44 | my_env["GARMIN_SAVE_RUNS"] = path_save_gmn | |
45 | ||
46 | # Export the data from the GPS as binary gmn files and store STDOUT in result. | |
47 | result = 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. | |
52 | line_start = 0 | |
53 | written_files = [] | |
54 | n = 0 | |
55 | for 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 | |
72 | file_log = open(path_log, 'w') | |
73 | file_log.write(result) | |
74 | file_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. | |
78 | devnull = open(os.devnull, 'w') | |
79 | if not written_files: | |
80 | print "No new files to process." | |
81 | else: | |
82 | print "New TCX files:" | |
83 | for 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) | |
90 | devnull.close() |