Calculate asset consumptions from the past
MODIFY
src/integration/asset/lacroix/LacroixAssetIntegration.ts
src/integration/asset/lacroix/LacroixAssetIntegration.ts
@@ -7,13 +7,16 @@ import AxiosFactory from '../../../utils/AxiosFactory';
import { AxiosInstance } from 'axios';
import BackendError from '../../../exception/BackendError';
import Constants from '../../../utils/Constants';
+import ConsumptionStorage from '../../../storage/mongodb/ConsumptionStorage';
import Cypher from '../../../utils/Cypher';
import Logging from '../../../utils/Logging';
import { ServerAction } from '../../../types/Server';
import Tenant from '../../../types/Tenant';
import TransactionStorage from '../../../storage/mongodb/TransactionStorage';
import Utils from '../../../utils/Utils';
+import { constant } from 'lodash';
import moment from 'moment';
+import { time } from 'console';
const MODULE_NAME = 'LacroixAssetIntegration';
@@ -92,8 +95,27 @@ export default class LacroixAssetIntegration extends AssetIntegration<AssetSetti
};
consumptions.push(consumption);
}
+ if ((moment().diff(moment(asset.lastConsumption?.timestamp), 'minutes')) > 1) {
+ const consumptionsDB = await ConsumptionStorage.getSiteAreaChargingStationConsumptions(this.tenant,
+ { siteAreaID: asset.siteAreaID, startDate: asset.lastConsumption.timestamp, endDate: consumptions[consumptions.length - 1].lastConsumption.timestamp },
+ Constants.DB_PARAMS_MAX_LIMIT, ['instantWatts', 'instantWattsL1','instantWattsL2','instantWattsL3', 'endedAt']);
+ for (const consumption of consumptions) {
+ const timestamp = consumption.lastConsumption.timestamp;
+ timestamp.setSeconds(0);
+ timestamp.setMilliseconds(0);
+ const consumptionToSubtract = consumptionsDB.result.find((consumptionDB) => {
+ consumptionDB.endedAt === timestamp;
+ });
+ if (consumptionToSubtract && (moment().diff(moment(consumption.lastConsumption?.timestamp), 'minutes')) > 1) {
+ consumption.currentInstantWatts = -consumptionToSubtract.instantWatts;
+ consumption.currentInstantWattsL1 = -consumptionToSubtract.instantWattsL1;
+ consumption.currentInstantWattsL2 = -consumptionToSubtract.instantWattsL2;
+ consumption.currentInstantWattsL3 = -consumptionToSubtract.instantWattsL3;
+ }
+ }
+ }
const transactionResponse = await TransactionStorage.getTransactions(this.tenant.id, { siteAreaIDs:[asset.siteAreaID],
- stop: { $exists: false } }, Constants.DB_PARAMS_MAX_LIMIT);
+ stop: { $exists: false } }, Constants.DB_PARAMS_MAX_LIMIT, ['currentInstantWatts', 'currentInstantWattsL1','currentInstantWattsL2','currentInstantWattsL3']);
for (const transaction of transactionResponse.result) {
consumptions[consumptions.length - 1].currentInstantWatts -= transaction.currentInstantWatts;
consumptions[consumptions.length - 1].currentInstantWattsL1 -= transaction.currentInstantWattsL1;

MODIFY
src/storage/mongodb/ConsumptionStorage.ts
src/storage/mongodb/ConsumptionStorage.ts
@@ -265,6 +265,103 @@ export default class ConsumptionStorage {
return consumptionsMDB;
}
+ static async getSiteAreaChargingStationConsumptions(tenant: Tenant,
+ params: { siteAreaID: string; startDate: Date; endDate: Date }, dbParams: DbParams = Constants.DB_PARAMS_MAX_LIMIT,
+ projectFields?: string[]): Promise<DataResult<Consumption>> {
+ // Debug
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getSiteAreaConsumptions');
+ // Check
+ DatabaseUtils.checkTenantObject(tenant);
+ // Clone before updating the values
+ dbParams = Utils.cloneObject(dbParams);
+ // Check Limit
+ dbParams.limit = Utils.checkRecordLimit(dbParams.limit);
+ // Check Skip
+ dbParams.skip = Utils.checkRecordSkip(dbParams.skip);
+ // Sort
+ if (!dbParams.sort) {
+ dbParams.sort = { endedAt: 1 };
+ }
+ // Create Aggregation
+ const aggregation = [];
+ aggregation.push({
+ $sort: dbParams.sort
+ });
+ // Skip
+ aggregation.push({
+ $skip: dbParams.skip
+ });
+ // Limit
+ aggregation.push({
+ $limit: dbParams.limit
+ });
+ // Create filters
+ const filters: FilterParams = {};
+ // ID
+ if (params.siteAreaID) {
+ filters.siteAreaID = DatabaseUtils.convertToObjectID(params.siteAreaID);
+ }
+ // Date provided?
+ if (params.startDate || params.endDate) {
+ filters.endedAt = {};
+ }
+ // Start date
+ if (params.startDate) {
+ filters.endedAt.$gte = Utils.convertToDate(params.startDate);
+ }
+ // End date
+ if (params.endDate) {
+ filters.endedAt.$lte = Utils.convertToDate(params.endDate);
+ }
+ // Check that charging station is set
+ filters.chargeBoxID = { '$ne': null };
+ // Filters
+ if (filters) {
+ aggregation.push({
+ $match: filters
+ });
+ }
+ // Group consumption values per minute
+ aggregation.push({
+ $group: {
+ _id: {
+ year: { '$year': '$endedAt' },
+ month: { '$month': '$endedAt' },
+ day: { '$dayOfMonth': '$endedAt' },
+ hour: { '$hour': '$endedAt' },
+ minute: { '$minute': '$endedAt' }
+ },
+ instantWatts: { $sum: '$instantWatts' },
+ instantWattsL1: { $sum: '$instantWattsL1' },
+ instantWattsL2: { $sum: '$instantWattsL2' },
+ instantWattsL3: { $sum: '$instantWattsL3' },
+ instantAmps: { $sum: '$instantAmps' },
+ limitWatts: { $last: '$limitSiteAreaWatts' },
+ limitAmps: { $last: '$limitSiteAreaAmps' }
+ }
+ });
+ // Rebuild the date
+ aggregation.push({
+ $addFields: {
+ endedAt: {
+ $dateFromParts: { 'year': '$_id.year', 'month': '$_id.month', 'day': '$_id.day', 'hour': '$_id.hour', 'minute': '$_id.minute' }
+ }
+ }
+ });
+ // Project
+ DatabaseUtils.projectFields(aggregation, projectFields, ['_id']);
+ // Read DB
+ const consumptionsMDB = await global.database.getCollection<Consumption>(tenant.id, 'consumptions')
+ .aggregate(aggregation, { allowDiskUse: true })
+ .toArray();
+ // Debug
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getSiteAreaConsumptions', uniqueTimerID, consumptionsMDB);
+ return {
+ count: consumptionsMDB.length,
+ result: consumptionsMDB
+ };
+ }
+
static async getTransactionConsumptions(tenantID: string, params: { transactionId: number },
dbParams: DbParams = Constants.DB_PARAMS_MAX_LIMIT, projectFields?: string[]): Promise<DataResult<Consumption>> {
// Debug

Calculate asset consumptions from the past