Fixed push CDR
MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -992,7 +992,7 @@ export default class OCPPService {
// Notify admins
await this.notifyStatusNotification(tenantID, chargingStation, statusNotification);
// Send new status to IOP
- await this.updateOCPIStatus(tenantID, chargingStation, foundConnector);
+ await this.updateOCPIConnectorStatus(tenantID, chargingStation, foundConnector);
// Save
await ChargingStationStorage.saveChargingStation(tenantID, chargingStation);
// Trigger Smart Charging
@@ -1021,7 +1021,7 @@ export default class OCPPService {
Utils.objectHasProperty(statusNotification, 'timestamp')) {
// Get the last transaction
const lastTransaction = await TransactionStorage.getLastTransaction(
- tenantID, chargingStation.id, connector.connectorId);
+ tenantID, chargingStation.id, connector.connectorId, { withChargingStation: true, withUser: true });
// Session is finished
if (lastTransaction && lastTransaction.stop && !lastTransaction.stop.extraInactivityComputed) {
const transactionStopTimestamp = Utils.convertToDate(lastTransaction.stop.timestamp);
@@ -1067,7 +1067,7 @@ export default class OCPPService {
}
}
- private async updateOCPIStatus(tenantID: string, chargingStation: ChargingStation, connector: Connector) {
+ private async updateOCPIConnectorStatus(tenantID: string, chargingStation: ChargingStation, connector: Connector) {
const tenant: Tenant = await TenantStorage.getTenant(tenantID);
if (chargingStation.issuer && chargingStation.public && Utils.isTenantComponentActive(tenant, TenantComponents.OCPI)) {
try {
@@ -1079,7 +1079,7 @@ export default class OCPPService {
Logging.logError({
tenantID: tenantID,
source: chargingStation.id,
- module: MODULE_NAME, method: 'updateOCPIStatus',
+ module: MODULE_NAME, method: 'updateOCPIConnectorStatus',
action: ServerAction.OCPI_PATCH_STATUS,
message: `An error occurred while patching the charging station status of ${chargingStation.id}`,
detailedMessages: { error: error.message, stack: error.stack }

MODIFY
src/storage/mongodb/TransactionStorage.ts
src/storage/mongodb/TransactionStorage.ts
@@ -939,7 +939,8 @@ export default class TransactionStorage {
return transactionsMDB.length === 1 ? transactionsMDB[0] : null;
}
- public static async getLastTransaction(tenantID: string, chargeBoxID: string, connectorId: number): Promise<Transaction> {
+ public static async getLastTransaction(tenantID: string, chargeBoxID: string, connectorId: number,
+ params: { withChargingStation?: boolean; withUser?: boolean; }): Promise<Transaction> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getLastTransaction');
// Check
@@ -952,6 +953,30 @@ export default class TransactionStorage {
'connectorId': Utils.convertToInt(connectorId)
}
});
+ // Sort
+ aggregation.push({
+ $sort: {
+ timestamp: -1
+ }
+ });
+ // The last one
+ aggregation.push({
+ $limit: 1
+ });
+ // Add Charging Station
+ if (params.withChargingStation) {
+ DatabaseUtils.pushChargingStationLookupInAggregation({
+ tenantID, aggregation: aggregation, localField: 'chargeBoxID', foreignField: '_id',
+ asField: 'chargeBox', oneToOneCardinality: true, oneToOneCardinalityNotNull: false
+ });
+ }
+ // Add User
+ if (params.withUser) {
+ DatabaseUtils.pushUserLookupInAggregation({
+ tenantID, aggregation: aggregation, localField: 'userID', foreignField: '_id',
+ asField: 'user', oneToOneCardinality: true, oneToOneCardinalityNotNull: false
+ });
+ }
// Rename ID
DatabaseUtils.pushRenameDatabaseIDToNumber(aggregation);
// Convert Object ID to string
@@ -960,20 +985,14 @@ export default class TransactionStorage {
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'siteAreaID');
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'stop.userID');
DatabaseUtils.pushConvertObjectIDToString(aggregation, 'remotestop.userID');
+ DatabaseUtils.pushConvertObjectIDToString(aggregation, 'chargeBox.siteAreaID');
// Set to null
DatabaseUtils.clearFieldValueIfSubFieldIsNull(aggregation, 'stop', 'timestamp');
DatabaseUtils.clearFieldValueIfSubFieldIsNull(aggregation, 'remotestop', 'timestamp');
- // Sort
- aggregation.push({ $sort: { timestamp: -1 } });
- // The last one
- aggregation.push({ $limit: 1 });
- // Add Charge Box
- DatabaseUtils.pushChargingStationLookupInAggregation({
- tenantID, aggregation: aggregation, localField: 'chargeBoxID', foreignField: '_id',
- asField: 'chargeBox', oneToOneCardinality: true, oneToOneCardinalityNotNull: false
- });
- DatabaseUtils.pushConvertObjectIDToString(aggregation, 'chargeBox.siteAreaID');
// Read DB
+ console.log('====================================');
+ console.log(JSON.stringify(aggregation, null, ' '));
+ console.log('====================================');
const transactionsMDB = await global.database.getCollection<Transaction>(tenantID, 'transactions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();

Fixed push CDR