|
| 1 | +//************************************************************************************************ |
| 2 | +// Copyright © 2010 Steven M. Cohn. All Rights Reserved. |
| 3 | +//************************************************************************************************ |
| 4 | + |
| 5 | +namespace CatalogTcx |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.IO; |
| 10 | + using System.Linq; |
| 11 | + using System.Xml.Linq; |
| 12 | + |
| 13 | + |
| 14 | + class Program |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// blah blah blah |
| 18 | + /// </summary> |
| 19 | + /// <param name="args">blah blah blah</param> |
| 20 | + |
| 21 | + static void Main(string[] args) |
| 22 | + { |
| 23 | + var path = (args.Length == 0 ? "." : args[0]); |
| 24 | + |
| 25 | + // scan for tcx files in the current or specified path |
| 26 | + Console.WriteLine(); |
| 27 | + Console.WriteLine("... scanning files in current directory"); |
| 28 | + |
| 29 | + var count = ScanFiles(path, path, true); |
| 30 | + Console.WriteLine("... found: " + count.ToString()); |
| 31 | + |
| 32 | + // determine if a Garmin USB is mounted |
| 33 | + var disks = new UsbFactory().GetAvailableDisks(); |
| 34 | + if ((disks != null) && (disks.Count > 0)) |
| 35 | + { |
| 36 | + var disk = disks.FirstOrDefault( |
| 37 | + e => e.Model.StartsWith("Garmin", StringComparison.InvariantCulture)); |
| 38 | + |
| 39 | + if (disk != null) |
| 40 | + { |
| 41 | + // scan for tcx files on the Garmin, disk.Name would be simply like "G:" |
| 42 | + |
| 43 | + // Edge 820 stores fit files in Garmin\Activities |
| 44 | + var sourcePath = disk.Name + @"\Garmin\Activities"; |
| 45 | + if (!Directory.Exists(sourcePath)) |
| 46 | + { |
| 47 | + // Edge 705 stores tcx files in Garmin\History |
| 48 | + sourcePath = disk.Name + @"\Garmin\History"; |
| 49 | + } |
| 50 | + |
| 51 | + if (Directory.Exists(sourcePath)) |
| 52 | + { |
| 53 | + Console.WriteLine(); |
| 54 | + Console.WriteLine("... scanning files in " + sourcePath); |
| 55 | + count = ScanFiles(sourcePath, path, false); |
| 56 | + Console.WriteLine("... found: " + count); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + Console.WriteLine(); |
| 63 | + Console.WriteLine("... Garmin not found"); |
| 64 | + } |
| 65 | + |
| 66 | + // scan for Zwift files |
| 67 | + Console.WriteLine(); |
| 68 | + Console.WriteLine("... scanning for Zwift files"); |
| 69 | + count += ScanZwift(path, true); |
| 70 | + Console.WriteLine("... found: " + count); |
| 71 | + |
| 72 | + Console.WriteLine(); |
| 73 | + Console.Write("... Press any key: "); |
| 74 | + Console.ReadKey(); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private static int ScanFiles(string sourcePath, string targetPath, bool clean) |
| 79 | + { |
| 80 | + var count = 0; |
| 81 | + var dirnam = Path.GetDirectoryName(targetPath); |
| 82 | + if (dirnam == null) return 0; |
| 83 | + |
| 84 | + foreach (var filnam in GetFiles(sourcePath, new[] { ".fit", ".tcx" })) |
| 85 | + { |
| 86 | + var ext = Path.GetExtension(filnam); |
| 87 | + if (ext.Equals(".fit")) |
| 88 | + { |
| 89 | + // TODO: convert fit to tcx |
| 90 | + // TODO: set filnam to converted tcx file |
| 91 | + } |
| 92 | + |
| 93 | + // open each TCX file and look for its activity start time |
| 94 | + |
| 95 | + var root = XElement.Load(filnam); |
| 96 | + var ns = root.GetDefaultNamespace(); |
| 97 | + |
| 98 | + var lap = (from e in root |
| 99 | + .Element(ns + "Activities")? |
| 100 | + .Element(ns + "Activity")? |
| 101 | + .Elements(ns + "Lap") |
| 102 | + where e.Attribute("StartTime") != null |
| 103 | + select e).FirstOrDefault(); |
| 104 | + |
| 105 | + var startTime = lap?.Attribute("StartTime"); |
| 106 | + if (startTime == null) continue; |
| 107 | + |
| 108 | + // parse the start time so we can reformat into a filename |
| 109 | + var dttm = DateTime.Parse(startTime.Value); |
| 110 | + |
| 111 | + // the year is also used as a directory name |
| 112 | + var year = dttm.Year.ToString("0000"); |
| 113 | + |
| 114 | + // build the final file name |
| 115 | + var stamp = $"{year}{dttm.Month:00}{dttm.Day:00}_{dttm.Hour:00}{dttm.Minute:00}.tcx"; |
| 116 | + |
| 117 | + // ensure the archive directory (year name) exists |
| 118 | + var dirpath = Path.Combine(dirnam, year); |
| 119 | + if (!Directory.Exists(dirpath)) |
| 120 | + { |
| 121 | + Directory.CreateDirectory(dirpath); |
| 122 | + } |
| 123 | + |
| 124 | + var target = Path.Combine(dirpath, stamp); |
| 125 | + if (File.Exists(target)) |
| 126 | + { |
| 127 | + // delete target so there's no problem overwriting it |
| 128 | + File.Delete(target); |
| 129 | + } |
| 130 | + |
| 131 | + // save as formatted XML |
| 132 | + root.Save(target, SaveOptions.None); |
| 133 | + Console.WriteLine(" " + Path.GetFileName(filnam) + " --> " + Path.GetFileName(target)); |
| 134 | + count++; |
| 135 | + |
| 136 | + // set the timestamps of the file to the activity start time |
| 137 | + File.SetCreationTime(target, dttm); |
| 138 | + File.SetLastWriteTime(target, dttm); |
| 139 | + |
| 140 | + if (clean) |
| 141 | + { |
| 142 | + // delete the source |
| 143 | + File.Delete(filnam); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return count; |
| 148 | + } |
| 149 | + |
| 150 | + // gpsbabel -t -i garmin_fit |
| 151 | + // -f C:/Users/steven/Documents/Zwift/Activities/2016-12-08-18-33-29.fit |
| 152 | + // -o gtrnctr,course=0,sport=Biking |
| 153 | + // -F C:/Users/steven/Desktop/qwe.tcx |
| 154 | + |
| 155 | + private static int ScanZwift(string targetPath, bool clean) |
| 156 | + { |
| 157 | + var dirnam = Path.GetDirectoryName(targetPath); |
| 158 | + if (dirnam == null) return 0; |
| 159 | + |
| 160 | + var sourcePath = Path.Combine( |
| 161 | + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), |
| 162 | + @"Zwift\Activities"); |
| 163 | + |
| 164 | + if (!Directory.Exists(sourcePath)) return 0; |
| 165 | + |
| 166 | + // the year is also used as target directory name |
| 167 | + var year = DateTime.Now.Year.ToString("0000"); |
| 168 | + // ensure the archive directory (year name) exists |
| 169 | + var dirpath = Path.Combine(targetPath, year); |
| 170 | + if (!Directory.Exists(dirpath)) |
| 171 | + { |
| 172 | + Directory.CreateDirectory(dirpath); |
| 173 | + } |
| 174 | + |
| 175 | + var count = 0; |
| 176 | + |
| 177 | + foreach (var filnam in GetFiles(sourcePath, new[] { ".fit" })) |
| 178 | + { |
| 179 | + var name = Path.GetFileName(filnam); |
| 180 | + if (name != null) |
| 181 | + { |
| 182 | + var target = Path.Combine(dirpath, name); |
| 183 | + if (File.Exists(target)) |
| 184 | + { |
| 185 | + // delete target so there's no problem overwriting it |
| 186 | + File.Delete(target); |
| 187 | + } |
| 188 | + |
| 189 | + if (clean) |
| 190 | + { |
| 191 | + File.Move(filnam, target); |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + |
| 196 | + File.Copy(filnam, target); |
| 197 | + } |
| 198 | + |
| 199 | + count++; |
| 200 | + |
| 201 | + // set the timestamps of the file to the activity start time |
| 202 | + //File.SetCreationTime(target, dttm); |
| 203 | + //File.SetLastWriteTime(target, dttm); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return count; |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | + private static IEnumerable<string> GetFiles(string path, string[] types) |
| 212 | + { |
| 213 | + return Directory.GetFiles(path) |
| 214 | + .Where(file => types.Any(t => t.Equals(Path.GetExtension(file)))).ToList(); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments