Skip to content

Commit

Permalink
Merge pull request #5 from IPG-Automotive-UK/Bugfix/Post-Processor-wi…
Browse files Browse the repository at this point in the history
…ll-stop-working-if-it-receives-a-bad-erg.info-file

Improved reader error handling and exposed validation function for ERG infofile.
  • Loading branch information
andnygIPG authored Oct 3, 2023
2 parents a146f3c + 740d73c commit 9662492
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ Read the data for all quantities
```
let data = erg.read(ergFile, infoFile);
```

Read quantity definitions - no data values are returned, just the name and unit of each quantity

```
let quants = erg.readInfoQuants(infoFile);
```

Read header information - Date, Testrun Name, CarMaker Version

```
let header = erg.readInfoHeader(infoFile);
```

Validate ERG InfoFile

```
if(erg.validateInfoHeader(infoFile)){
console.log("Valid ERG InfoFile);
}
else{
console.log("Invalid ERG InfoFile);
}
```
60 changes: 54 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
import fs from "fs";

// returns header information from info file
/**
* Validates a CarMaker erg Infofile
* @param infoFile Path to CarMaker erg InfoFile
* @returns true if valid erg infofile, false if invalid
*/
function validateInfoHeader(infoFile: string) {
// read infofile
const info: string[] = fs
.readFileSync(infoFile, "utf8")
.replace(/\r/g, "")
.split("\n");

let validFormat = false;
let validInfofile = false;

// check that first line of infofile is valid
if (info[0].startsWith("#INFOFILE1.1")) {
validInfofile = true;
}

// Check that file format is erg
for (const line of info) {
if (line.startsWith("File.Format") && line.includes("erg")) {
validFormat = true;
break;
}
}

return validFormat && validInfofile;
}

/**
* Read the erg infofile header.
* @param infoFile Path to CarMaker erg InfoFile.
* @returns Object containing erg file metadata.
*/
function readInfoHeader(infoFile: string) {
// read the info file
const info: string[] = fs
Expand Down Expand Up @@ -29,7 +64,11 @@ function readInfoHeader(infoFile: string) {
return headerInfo;
}

// returns quantity information from info file
/**
* Read list of quantities from erg infofile.
* @param infoFile Path to CarMaker erg InfoFile.
* @returns Array of objects containing erg file quantity names.
*/
function readInfoQuants(infoFile: string) {
// read the info file
const info: string[] = fs
Expand Down Expand Up @@ -69,12 +108,21 @@ function readInfoQuants(infoFile: string) {
});
return quants;
}

// read erg and info file and return data values for each quantity
/**
* Read erg and info file and return data values for each quantity.
* @param ergFile Path to CarMaker erg file.
* @param infoFile Path to CarMaker erg InfoFile.
* @returns Array of CarMaker quantities
*/
function read(ergFile: string, infoFile: string) {
// read info file
let quants = readInfoQuants(infoFile);

// error if there are no quantities in erg infofile
if (quants.length < 1) {
throw new Error("ERG File does not contain any quantities");
}

// read erg file
let ergBuffer = fs.readFileSync(ergFile);

Expand Down Expand Up @@ -213,5 +261,5 @@ function read(ergFile: string, infoFile: string) {
return quants;
}

export { read, readInfoHeader, readInfoQuants };
export default { read, readInfoHeader, readInfoQuants };
export { read, readInfoHeader, readInfoQuants, validateInfoHeader };
export default { read, readInfoHeader, readInfoQuants, validateInfoHeader };

0 comments on commit 9662492

Please sign in to comment.