Importing a bunch of pages from old websites.
[website_subgeniuskitty.com] / data / development / misc / garmin_edge_305_linux.files / Edge305.py
diff --git a/data/development/misc/garmin_edge_305_linux.files/Edge305.py b/data/development/misc/garmin_edge_305_linux.files/Edge305.py
new file mode 100644 (file)
index 0000000..d50f8ae
--- /dev/null
@@ -0,0 +1,90 @@
+#!/usr/bin/python
+
+# 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
+
+# Aaron Taylor
+# ataylor@subgeniuskitty.com
+
+# Configuration
+# 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 to save tcx files
+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
+uid = 1000
+gid = 1000
+
+import subprocess, os
+
+# 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 = os.environ
+try:
+    my_env["PATH"]
+except:
+    my_env["PATH"] = path_garmintools + ":" + path_gmn2tcx
+else: 
+    my_env["PATH"] = my_env["PATH"] + ":" + path_garmintools + ":" + path_gmn2tcx
+try:
+    my_env["LD_LIBRARY_PATH"]
+except: 
+    my_env["LD_LIBRARY_PATH"] = path_garmintools
+else:
+    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 
+# written_files list.
+line_start = 0
+written_files = []
+n = 0
+for i in result:
+    if i == b'\n':
+        line_start = n+1
+    elif line_start == n:
+        first_chars = "" 
+        for k in range(9):
+            first_chars += result[n+k]
+        if first_chars == b'Wrote:   ':
+            k = 9
+            temp_string = ""
+            while result[n+k] != b'\n':
+                temp_string += result[n+k]
+                k += 1
+            written_files.append(temp_string)
+    n += 1
+
+# Dump output of garmintools run for analysis in case something goes wrong
+file_log = open(path_log, 'w')
+file_log.write(result)
+file_log.close()
+
+# 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')
+if not written_files:
+    print "No new files to process."
+else:
+    print "New TCX files:"
+for path_gmn in written_files:
+    path_tcx = path_save_tcx + "/" + os.path.split(path_gmn)[1].replace(".gmn", ".tcx")
+    print path_tcx
+    file_tcx = open(path_tcx, 'w')
+    subprocess.call(["gmn2tcx", path_gmn], stdout=file_tcx, stderr=devnull)
+    file_tcx.close()
+    os.chown(path_tcx, uid, gid)
+devnull.close()