Extracting and Parsing Data from vNode Logs to Update a Tag

Created by Jose Fabian Solano, Modified on Wed, 3 Apr at 1:50 PM by Jose Fabian Solano

The main purpose of this code is to extract a specific piece of data from the logs generated by vNode. This data is only available in text format within the logs. To make it more useful and accessible within our system, we extract it, parse it, and store it in a tag within vNode. This way, we convert the raw text data into a structured and accessible form that can be used elsewhere in the system.


const fs = require('fs');
const path = require('path');

const dirPath = 'C:\\Program Files\\vNode\\log\\104';

// Get the list of files in the folder
const files = fs.readdirSync(dirPath).filter(file => file.endsWith('.log'));

if (files.length === 0) {
$.logger.debug('No TXT files found in the specified folder.');
return;
}

// Sort the list of files by modification date (most recent first)
files.sort((a, b) => {
return fs.statSync(path.join(dirPath, b)).mtime.getTime() - fs.statSync(path.join(dirPath, a)).mtime.getTime();
});

// Select the last file from the list
const lastFile = files[0];
const lastFilePath = path.join(dirPath, lastFile);
$.logger.debug("This is the last file: " + lastFilePath);

// Read the content of the last file
const lastFileContent = fs.readFileSync(lastFilePath, 'utf-8');
//$.logger.debug("This is the content of the file: " + lastFileContent);


//***********************************************************************************************************************************************************//

const lines = lastFileContent.split('\n');
$.logger.debug("This is the content of the file: " + lines + "Here ends the log: ");

let lastValue = null; // Will store the last found value for "Value"

for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i];
const matches = line.match(/Value=(false|true)/); // Search for the value "false" or "true" in the line

if (matches) {
lastValue = matches[1]; // The first captured group is the value "false" or "true"
break; // Stop searching after finding the last value
}
}
$.logger.debug("//************************ The value of value is: " + lastValue + " *******************************************//");
$.api.tag.update('/104/Reading/C_SC_NA_Reading', lastValue, 192, new Date().getTime(), true);



//***********************************************************************************************************************************************************//

Created by JF Solano


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article