Commits

Adam Bird committed fe9838c

Support reading files, tcx and fit from devices via readFile() method

  • Participants
  • Parent commits 817cffa

Comments (0)

Files changed (1)

       return devices[currentDevice];
     };
             
-    var baseHandler = function(readWrite, dataType, pluginMethod, name, callback, progress) {
+    var baseHandler = function(readWrite, dataType, pluginMethod, name, param, callback, progress) {
       if (devices.length === 0) {
         throw new Error("No devices found");
       }
         readWrite = "Read";
       }
       inUse = readWrite.toLowerCase() + name;
-      plugin['Start' + readWrite + pluginMethod](currentDevice, dataType);
+      plugin['Start' + readWrite + pluginMethod](currentDevice, dataType, param);
       
       // This handler will set a timeout and execute again if an interaction is in progress.
       var finishHandler = function finishHandler() {
         var status = plugin['Finish' + readWrite + pluginMethod]();
         switch (status) {
           case Garmin.STATUS.working:
+            console.log("working");
             if (progress) {
               progress(getProgress());
             };
       finishHandler();
     };
     
-    var readHandler = function(dataType, pluginMethod, type) {
+    var readHandler = function(dataType, pluginMethod, type, param) {
       return function(callback){
         // Empty the data store, so we don't get bogus data if the event cannot complete.
         plugin.TcdXml = "";
-        baseHandler('Read', dataType, pluginMethod, type, callback);
+        baseHandler('Read', dataType, pluginMethod, type, param, callback);
       };
     };
     
-    var writeHandler = function(dataType, pluginMethod, type) {
+    var writeHandler = function(dataType, pluginMethod, type, param) {
       return function(data, callback) {
         // Strip out any newlines, as they break things.
         plugin.TcdXml = data.replace(/[\n\r]+/gm, '');
         plugin.FileName = "";
-        baseHandler('Write', dataType, pluginMethod, type, callback);
+        baseHandler('Write', dataType, pluginMethod, type, param, callback);
       };
     };
     
       this['write' + type] = writeHandler(data[0], data[1], type);
     }, this);
 
+    // simple interface to reading FIT or TCX files, switching of base methods
+    // performed according to current device support
+    this.readFile = function (file_id, callback) {
+      if (this.activeDevice().canReadFITActivities()) {
+        var data = plugin.GetBinaryFile(currentDevice, file_id, false);
+        var binary_data = decodeBinaryFile(data);
+        if (callback) {
+          callback(binary_data);
+        }
+      } else {
+        readHandler('FitnessHistory', 'FitnessDetail', 'FitnessHistory', file_id)(callback);
+      }
+    }
+
+    decodeBinaryFile = function (input) {
+      var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+      var output = [];
+      var chr1, chr2, chr3 = 0;
+      var enc1, enc2, enc3, enc4 = 0;
+      var i = 0;
+   
+      // strip header
+      input = input.replace(/^.+\r*\n/, '')
+      // strip any invalid characters, inc new lines
+      input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
+   
+      do {
+        enc1 = keyStr.indexOf(input.charAt(i++));
+        enc2 = keyStr.indexOf(input.charAt(i++));
+        enc3 = keyStr.indexOf(input.charAt(i++));
+        enc4 = keyStr.indexOf(input.charAt(i++));
+        chr1 = (enc1 << 2) | (enc2 >> 4);
+        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
+        chr3 = ((enc3 & 3) << 6) | enc4;
+
+        output.push(chr1);
+
+        if (enc3 != 64) {
+          output.push(chr2);
+        }
+        if (enc4 != 64) {
+          output.push(chr3);
+        }
+        chr1 = chr2 = chr3 = 0;
+        enc1 = enc2 = enc3 = enc4 = 0;
+      } while (i < input.length);
+
+      return new Uint8Array(output);
+    }
+
     // See if the plugin is actually installed.
     if (!plugin.Unlock) {
       this.installed = false;