0

I'm receiving some coordinates from a RFID sensor and saving it in a .txt file. I need to read these coordinates and apply some calculation (matrix calculus).

How can I read it in real-time? I noticed that I can't use FS since my .txt file is never finished, my sensor keeps saving data every second.

These are my coordinates in a text file:

{"coordinates":{"x":-775,"y":-1217,"z":780},"tagId":"26459"}
{"coordinates":{"x":-152,"y":-113,"z":-1327},"tagId":"26398"}
{"coordinates":{"x":-770,"y":-1185,"z":731},"tagId":"26459"}
{"coordinates":{"x":-137,"y":-104,"z":-1337},"tagId":"26398"}
ford04
  • 66,267
  • 20
  • 199
  • 171
  • You might find your answer [here](https://stackoverflow.com/questions/11225001/reading-a-file-in-real-time-using-node-js). – Huzaifa Aug 22 '19 at 16:42

2 Answers2

0

I believe you can still use FS as long as you're dealing with streams.

For example, you could do the following:

const textFilePath = 'path/to/txt/file';
let stream = fs.createReadStream(textFilePath);

stream.on("data", (data) => {
  let chunk = data.toString();
  // Do something here with the data chunk ...
});

In the same way, you can stream writes to your file to ensure consistent chunk transactions.

stream.write(coordinateObject);
0

You need to use the node-red-node-tail node.

This node does specifically what you are looking for.

You will need to run the message through JSON node to convert the string to a JSON object so you can access the fields in each line.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • It does seems like what I am looking for, since I'm using Node-red to obtain the data via MQTT from my RFID sensors. What I did: used the mqtt + function (to filter my payload and get just the coordinates and tagIds) + converted to a JSON string + saved it to a file .log Since my coordinates are already a JSON object, I just need to use this node-tail in my node.js server and it will work? Thankss – Natan Pereira Dorneles Aug 22 '19 at 18:23
  • If the messages are already coming in via MQTT there is no point writing them to a file just to read them back in to do something else, just wire what ever flow you want to the same node you have feeding the file writer. (Also https://stackoverflow.com/help/someone-answers) – hardillb Aug 22 '19 at 18:36
  • What I want to do with my coordinates is to apply some comparison and some calculation with it. Since node-red can't do that, I thought about sending it to Node.js (using the .log) and use Node.js math library to do what I want. (Did it!) – Natan Pereira Dorneles Aug 22 '19 at 18:58
  • Why can't Node-RED do it (You can load other modules)? and if it can't, just subscribe to the MQTT data directly in the Node.JS app absolutely no need for the file. – hardillb Aug 22 '19 at 19:03