From: Jérôme Benoit Date: Wed, 25 Aug 2021 22:13:33 +0000 (+0200) Subject: Storage: storage records in JSON file as an array to ease import. X-Git-Tag: v1.0.44~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b652b0c3a8be2600bd603a7744141088de97b666;p=e-mobility-charging-stations-simulator.git Storage: storage records in JSON file as an array to ease import. Signed-off-by: Jérôme Benoit --- diff --git a/README.md b/README.md index 2aed8173..84d9736b 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ make SUBMODULES_INIT=false #### Firmware Management Profile -- :x: GetDiagnostics +- :white_check_mark: GetDiagnostics - :x: DiagnosticsStatusNotification - :x: FirmwareStatusNotification - :x: UpdateFirmware diff --git a/src/utils/performance-storage/JSONFileStorage.ts b/src/utils/performance-storage/JSONFileStorage.ts index 4b5085cb..60e54437 100644 --- a/src/utils/performance-storage/JSONFileStorage.ts +++ b/src/utils/performance-storage/JSONFileStorage.ts @@ -11,10 +11,29 @@ export class JSONFileStorage extends Storage { public storePerformanceStatistics(performanceStatistics: Statistics): void { const performanceJSONFilePath = path.join(path.resolve(__dirname, '../../../'), this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '')); - fs.appendFile(performanceJSONFilePath, JSON.stringify(performanceStatistics, null, 2), 'utf8', (err) => { - if (err) { - FileUtils.handleFileException(this.logPrefix, 'Performance measurements', performanceJSONFilePath, err); + if (!fs.existsSync(performanceJSONFilePath)) { + this.open(performanceJSONFilePath); + } + fs.readFile(performanceJSONFilePath, 'utf-8', (error, data) => { + if (error) { + FileUtils.handleFileException(this.logPrefix, 'Performance measurements', performanceJSONFilePath, error); + } else { + const performanceRecords: Statistics[] = data ? JSON.parse(data.toString()) as Statistics[] : []; + performanceRecords.push(performanceStatistics); + fs.writeFile(performanceJSONFilePath, JSON.stringify(performanceRecords, null, 2), 'utf-8', (err) => { + if (err) { + FileUtils.handleFileException(this.logPrefix, 'Performance measurements', performanceJSONFilePath, err); + } + }); } }); } + + private open(filePath: string): void { + try { + fs.openSync(filePath, 'w+'); + } catch (error) { + FileUtils.handleFileException(this.logPrefix, 'Performance measurements', filePath, error); + } + } }