Fixed ocpp storage
MODIFY
src/storage/mongodb/OCPPStorage.ts
src/storage/mongodb/OCPPStorage.ts
+import DbParams from '../../types/database/DbParams';
+import { DataResult } from '../../types/DataResult';
+import global from '../../types/GlobalType';
import { OCPPAuthorizeRequestExtended, OCPPBootNotificationRequestExtended, OCPPDataTransferRequestExtended, OCPPDiagnosticsStatusNotificationRequestExtended, OCPPFirmwareStatusNotificationRequestExtended, OCPPHeartbeatRequestExtended, OCPPNormalizedMeterValues, OCPPStatusNotificationRequestExtended } from '../../types/ocpp/OCPPServer';
-
+import { ServerAction } from '../../types/Server';
import Constants from '../../utils/Constants';
import Cypher from '../../utils/Cypher';
-import { DataResult } from '../../types/DataResult';
-import DatabaseUtils from './DatabaseUtils';
-import DbParams from '../../types/database/DbParams';
import Logging from '../../utils/Logging';
import Utils from '../../utils/Utils';
-import global from '../../types/GlobalType';
+import DatabaseUtils from './DatabaseUtils';
+
const MODULE_NAME = 'OCPPStorage';
@@ -447,9 +448,9 @@ export default class OCPPStorage {
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'saveMeterValues');
// Check
await Utils.checkTenant(tenantID);
- const meterValuesMDB = [];
// Save all
for (const meterValueToSave of meterValuesToSave.values) {
+ try {
const timestamp = Utils.convertToDate(meterValueToSave.timestamp);
const meterValueMDB = {
_id: Cypher.hash(`${meterValueToSave.chargeBoxID}~${meterValueToSave.connectorId}~${timestamp.toISOString()}~${meterValueToSave.value}~${JSON.stringify(meterValueToSave.attribute)}`),
@@ -460,11 +461,19 @@ export default class OCPPStorage {
value: meterValueToSave.attribute.format === 'SignedData' ? meterValueToSave.value : Utils.convertToInt(meterValueToSave.value),
attribute: meterValueToSave.attribute,
};
- // Add
- meterValuesMDB.push(meterValueMDB);
- }
// Execute
- await global.database.getCollection<any>(tenantID, 'metervalues').insertMany(meterValuesMDB);
+ await global.database.getCollection<any>(tenantID, 'metervalues').insertOne(meterValueMDB);
+ } catch (error) {
+ Logging.logError({
+ tenantID,
+ source: meterValueToSave.chargeBoxID,
+ module: MODULE_NAME, method: 'saveMeterValues',
+ action: ServerAction.METER_VALUES,
+ message: 'An error occurred while trying to save the meter value',
+ detailedMessages: { error: error.message, stack: error.stack, meterValue: meterValueToSave }
+ });
+ }
+ }
// Debug
Logging.traceEnd(MODULE_NAME, 'saveMeterValues', uniqueTimerID, { meterValuesToSave });
}

Fixed ocpp storage