Skip to main content
16-Pearl
September 12, 2018
Solved

for loop issue

  • September 12, 2018
  • 2 replies
  • 3844 views

Hi all,

 

could somebody show me what is wrong in my code?? I wanted to go thru all the rows in infotable to add them to stream. but unfortunately only last row is added, despite the fact that first part of code reads all *.csv file.


var params = {
path: "ptc.csv" /* STRING */,
columnMappings: undefined /* STRING */,
hasHeader: true /* BOOLEAN */,
longitudeField: undefined /* NUMBER */,
dateFormat: "dd.MM.yyyy HH:mm:ss" /* STRING */,
fileRepository: "HYDAC_FR" /* THINGNAME */,
latitudeField: undefined /* NUMBER */,
fieldDelimiter: undefined /* STRING */,
stringDelimiter: "\"" /* STRING */,
dataShape: "PTC_DATASHAPE" /* DATASHAPENAME */
};

var myInfoTable = Resources["CSVParserFunctions"].ReadCSVFile(params);
var tableLength = myInfoTable.rows.length;

for (var x = 0; x < tableLength; x++) {
var row = myInfoTable.rows[x];
var tags = new Array();
var timestamp = new Date();
var values = Things["PTC_STREAM"].CreateValues();

values.Par2 = row.Par2; //NUMBER
values.Par1 = row.Par1; //NUMBER
values.Par3 = row.Par3; //NUMBER
values.Date = row.Date; //DATETIME

var location = new Object();
location.latitude = 0;
location.longitude = 0;
location.elevation = 0;
location.units ="WGS84";

var params = {
tags : tags,
timestamp : timestamp,
source : me.name,
values : values,
location : location
};

Things["PTC_STREAM"].AddStreamEntry(params);
}

 

thx in advance

gucio

This topic has been closed for replies.
Best answer by CarlesColl

You can't add two stream entries at the exact same timestamp (they will be overwritten), with new Date() you have millisecond precision and the for loop goes faster than 1 millisecond 😉

2 replies

5-Regular Member
September 12, 2018

Well I think you are adding the row in stream after the loop .So basically it is iterating through the infotable and the last row is getting added in the stream .Try adding the stream entry inside the loop so that for every iteration it adds the values to the stream .

 

 

Thanks ,

Mukul Narang

1-Visitor
September 12, 2018

You can't add two stream entries at the exact same timestamp (they will be overwritten), with new Date() you have millisecond precision and the for loop goes faster than 1 millisecond 😉

Gucio16-PearlAuthor
16-Pearl
September 12, 2018

hi Carles,

 

Probably the timestamp could be the topic.

Is there any sleep/wait method/function in TX to wait for a while before new row will be read??

1-Visitor
September 12, 2018

pause(1);

1-> Milliseconds