Table of Contents

Storing Event and Report values to a file in TransSECS Devices

Whenever an Event is received the script associated with that event is called.

When an event is received, the value of the incoming variable (incoming value) is a JSON representation of the event, including all reports and values. For example, the script shown may print something like:

Report Received { "COMPLETED": [  { "RPTID0": [ {"CLOCK":"2024032014462593", "type":20 },{"GasFlow":"1.883", "type":44 },{"LOTID":"", "type":20 } ], "rptid":100} ], "ceid":7502, "timestamp":"2024-03-20 14:46:25.955"}

Similarly, when a Report is received a script is called on the report. This will be called if the report is part of any event or requested manually.

The value here is slightly different. The value itself is the report Id, for example, 100. The data associated with the report is retrieved by calling

var map = incomingValue.getProperty("MAP");

You can get string representation of the map with

var mapString = map.toString();

If you print that string you might get something like:

{CLOCK=2024032015172190, GasFlow=1.955, LOTID=, CEID=7502, EventName=COMPLETED}

We can save either the Event JSON or the map to a file, appending a line as each new value is received. If we want to save the event, then our string is:

var stringToSave = incomingValue.getStringValue(); 

and the script is placed in the script block for the CEID.

For the Report, the string to save is:

var stringToSave = incomingValue.getProperty("MAP").toString();

The code then goes in the Report Script block.

The code is about the same for both blocks:

// Import the Java FileWriter and BufferedWriter classes
var FileWriter = Java.type("java.io.FileWriter");
var BufferedWriter = Java.type("java.io.BufferedWriter");

/**
 * Appends the provided string to a file, followed by a newline.
 * @param {String} stringToSave The string representation of the data to append.
 */
function appendMapStringToFile(stringToSave) {
    // Specify the file path where you want to save or append the map's string representation
    var filePath = "path/to/your/file.txt";

    try {
        // Create a FileWriter in append mode (the boolean true enables appending)
        var writer = new BufferedWriter(new FileWriter(filePath, true));

        // Write the map's string representation to the file, and append a new line
        writer.write(stringToSave + "\n"); // Adding "\n" to ensure the string ends with a newline

        // Always close the writer to flush and release system resources
        writer.close();

        print("Successfully wrote to the file.");
    } catch (e) {
        print("An error occurred: " + e);
    }
}

// Example usage of the function

// This is for a report
var stringToSave = incomingValue.getProperty("MAP").toString();
// for an event the code would be:
//var stringToSave = incomingValue.getStringValue(); 
appendMapStringToFile(stringToSave);
incomingValue;  // always "return" a value 

Each time a report, or event is received a new line is added to the log file. So after a few reports the file may look like this:

{CLOCK=2024032015392720, GasFlow=2.004, LOTID=, CEID=7502, EventName=COMPLETED}
{CLOCK=2024032015392774, GasFlow=1.974, LOTID=, CEID=7502, EventName=COMPLETED}
{CLOCK=2024032015392799, GasFlow=1.974, LOTID=, CEID=7502, EventName=COMPLETED}