Snippets

Zenduit Team KML Export

Created by Shane Graham

File device.js Added

  • Ignore whitespace
  • Hide word diff
+window.Devices = class Devices {
+
+	constructor(geotab, filter, date) {
+    this.date = date;
+		var util = new Util();        
+		util.bindAll(this);
+    this.groupSearch = filter;
+		this.geotab = geotab;
+	}  
+
+  setGroupSearch(filter) {
+    this.groupSearch = filter;
+  }
+
+	getDevice(i) {
+		return this.Devices[i];
+	}
+
+	getDeviceById(DeviceID) {
+		for(var i = 0; i < this.Devices.length; i++) {
+		    if(DeviceID.toString() == this.Devices[i].id)
+		    	return this.Devices[i];
+		};
+	}
+
+	getLogRecords(i) {
+		return this.Devices[i].logRecords;
+	}
+
+	getLogRecordsById(DeviceID) {
+		for(var i = 0; i < this.Devices.length; i++) {
+		    if(DeviceID.toString() == this.Devices[i].id)
+		    	return this.Devices[i].logRecords;
+		};
+	}
+
+	getDevices() {
+		return this.Devices;
+	}
+
+  pullGroups() {
+    return this.geotab.call("Get", {typeName: "Group"});
+  }
+
+  buildGroupCache() {
+    window.changeLoadingMessage("Groups")
+    if (this.geoGroups !== undefined && this.geoGroups.length > 0) {
+      this.groupCache = {};
+      for (var i = 0; i < this.geoGroups.length; i++) {
+        if (this.geoGroups[i].name === undefined) {
+          this.groupCache[this.geoGroups[i].id] = this.geoGroups[i].id;
+        } else {
+          this.groupCache[this.geoGroups[i].id] = this.geoGroups[i].name;
+        }
+      }
+    }
+  }
+
+  getGroupNameById(id) {
+    return this.groupCache[id];
+  }
+
+	pullDevices() {
+    window.changeLoadingMessage("Devices")
+    var calls = [];
+    var _this = this;
+    return this.geotab.call("Get", {
+      typeName: "Device",
+      search: {
+        groups: this.groupSearch,
+        fromDate: this.date.start.toISOString()
+      }
+    })
+    .then(Devices => this.Devices = Devices)
+    .then(this.pullGroups)
+    .then(groups => this.geoGroups = groups)
+    .then(this.buildGroupCache)
+    .then(this.pullRouteProgression)
+    .then(this.pullLogRecords)
+    .then(function(logRecords) {
+      for(var i = 0; i < logRecords.length; i++) {
+          if(logRecords[i].length > 0)
+            _this.Devices[i].logRecords = _this.filter(logRecords[i], logRecords[i][0].device.id);
+      }
+    });
+	}
+
+	pullLogRecords() {
+    window.changeLoadingMessage("LogRecords")
+  	var calls = this.Devices.map(device => ({
+  		typeName: 'LogRecord',
+  		search: {
+        fromDate: this.date.start.toISOString(),
+        toDate: this.date.end.toISOString(),
+  			deviceSearch: {id: device.id}
+  		}
+  	}));
+  	return this.geotab.multiPromise("Get", calls);
+	}
+
+	filter(record, vId) {
+    var device = this.getDeviceById(vId);
+    device.selected = false;
+    return record;
+	}
+
+  getGroups(groups) {
+    var tmpGroups = [];
+    var self = this;
+    groups.forEach(function(value) {
+      tmpGroups.push(value.id);
+      if(value.children.length > 0)
+        tmpGroups.concat(self.getGroups(value.children));
+    });
+    return tmpGroups;
+  }
+
+  getGroupNames(groups) {
+    var tmpGroups = [];
+    var self = this;
+    groups.forEach(function(value) {
+      tmpGroups.push(self.getGroupNameById(value.id));
+      if(value.children.length > 0)
+        tmpGroups.concat(self.getGroups(value.children));
+    });
+    return tmpGroups;
+  }
+
+  exportCSV() {
+    this.exportKML(); return;
+    var data = [],
+        csvContent = "data:text/csv;charset=utf-8,",
+        dateString = this.date.getFullYear() + "-" + (this.date.getMonth() + 1) + "-" + this.date.getDate();
+
+    data.push(["Device", "Latitude", "Longitude","Date"]);
+
+    var devices = this.getDevices();
+    devices.forEach((device, deviceId) => {
+      device.logRecords.forEach((logRecord, key) => {
+        data.push([
+          device.name, 
+          logRecord.latitude,
+          logRecord.longitude,
+          new Date(this.date).toISOString() 
+        ]);
+      });    
+    });
+
+    data.forEach(function(infoArray, index){
+       var dataString = infoArray.join(",");
+       csvContent += index < data.length ? dataString+ "\n" : dataString;
+    }); 
+
+    var encodedUri = encodeURI(csvContent);
+    var link = document.createElement("a");
+    link.setAttribute("href", encodedUri);
+    link.setAttribute("download", "LatReport"+dateString+".csv");
+    link.click();
+  }
+
+  exportKML() {
+    var devices = this.getDevices();
+    var xw = new XMLWriter('UTF-8');
+    xw.formatting = 'indented';
+    xw.indentChar = ' ';
+    xw.indentation = 2;
+    xw.writeStartDocument( );
+      xw.writeStartElement( 'kml' );
+        xw.writeAttributeString( "xmlns", "http://www.opengis.net/kml/2.2" );
+        xw.writeStartElement('Document');
+        console.log(devices);
+        devices.forEach((device, deviceId) => {
+          console.log(device);
+          if(device.logRecords && device.logRecords.length > 0 && device.selected) {
+            xw.writeStartElement('Placemark');     
+              xw.writeStartElement('name');
+                xw.writeCDATA( device.name );
+              xw.writeEndElement();  
+              xw.writeStartElement('Style');
+                xw.writeStartElement('LineStyle');
+                  xw.writeElementString('color', '7fffffff');
+                  /*xw.writeElementString('width', '4');*/
+                xw.writeEndElement(); 
+              xw.writeEndElement(); 
+              xw.writeStartElement('description');
+                xw.writeCDATA( "" );
+              xw.writeEndElement();    
+              xw.writeStartElement('LineString');
+                xw.writeElementString('extrude', '1');
+                xw.writeElementString('altitudeMode', 'relativeToGround');  
+                xw.writeStartElement( "coordinates" );
+                device.logRecords.forEach((logRecord, key) => {
+                    xw.writeString( logRecord.longitude + "," + logRecord.latitude + ",0" );
+                });
+                xw.writeEndElement();
+              xw.writeEndElement();
+            xw.writeEndElement();
+          }
+        });
+        xw.writeEndElement();
+      xw.writeEndElement();
+    xw.writeEndDocument();
+    var xml = xw.flush();
+    xw.close();
+    xw = undefined;
+
+    var kmlData = new Blob([xml], { type: 'text/kml' }); 
+    var kmlURL = URL.createObjectURL(kmlData);
+
+    var element = document.createElement('a');
+
+    element.href =  kmlURL;
+
+    element.setAttribute('download', 'logrecords.kml');
+
+    element.style.display = 'none';
+    document.body.appendChild(element);
+
+    element.click();
+
+    document.body.removeChild(element);
+  }
+
+	getFromDate(DeviceID) {
+    var date;
+    var device = this.getDeviceById(DeviceID);
+		date = new Date(this.date).setHours(0);
+    return date;
+	}
+
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.