I'm working on a custom widget that exports data from Exosite IoT Platform on .csv and .xlsx files. It's working really fine, I can do all the selection, export data but I'd like to ask if you can help me to change the way I get and show it on the sheet:
Today, it goes like this:
name | value | date | unit
Channel 1 | 20.5 | 5/3/2017 10:41 AM | ºC
Channel 1 | 22.5 | 5/3/2017 10:42 AM | ºC
Channel 1 | 19.5 | 5/3/2017 10:43 AM | ºC
Channel 2 | 18.5 | 5/3/2017 10:41 AM | ºC
Channel 2 | 23.5 | 5/3/2017 10:42 AM | ºC
I want to show like this:
Channel 1 | Channel 2 | date | unit
20.5 | 18.5 | 5/3/2017 10:41 AM | ºC
22.5 | 23.5 | 5/3/2017 10:42 AM | ºC
19.5 | 20.5 | 5/3/2017 10:43 AM | ºC
20.5 | 20.5 | 5/3/2017 10:44 AM | ºC
20.5 | 20.5 | 5/3/2017 10:45 AM | ºC
My .js function that edits this is:
processData:function(){
this.dataToExport=[];
var dataToExport=this.dataToExport;
var i,
j,
k,
date,
dateFormat,
value,
unit,
data;
// collect output data
k = 0;
for (i = 0; i < resources.length; i++) {
if (resources[i].isEnabled()) {
for (j = 0; j < resources[i].data.length; j++) {
date = new Date(resources[i].data[j][0] * 1000);
dateFormat = '';
dateFormat += date.toLocaleTimeString() + ' ';
dateFormat += date.toDateString();
value = resources[i].data[j][1];
try {
if (typeof JSON.parse(value) === 'object') {
value = $('<span/>')
.attr('title', value)
.text('More data');
}
} catch (e) {}
unit = JSON.parse(resources[i].info.description.meta).datasource.unit;
dataToExport[k] = {
date: dateFormat,
value: value,
name: resources[i].info.description.name,
unit: unit
};
k++;
}
}
}
},
dataToExport:[],
exportData:function(fileExtension){
alasql("SELECT * INTO " + fileExtension + "('exportedData." + fileExtension + "',{headers:true}) FROM ?", [this.dataToExport]);
}
I'm working with alaSQL to do this, but any help would be really great! I'm working with javascript recently and I feel some difficulties yet...
I believe that I need to filter and order the timestamps of the datasources, like a list on C++ and apply TList, for example (don't know if there's a command like that on JS). It should create a table with number of lines equal to the size of this list and number of columns equal to channel quantity + 1 (timestamp). On each line I put a timestamp, I have to look for an equal timestamp and insert the value of the channel related to this.