Retrieve Asset Consumption from ioThink
MODIFY
src/integration/asset/greencom/GreencomAssetIntegration.ts
src/integration/asset/greencom/GreencomAssetIntegration.ts
@@ -42,7 +42,7 @@ export default class GreencomAssetIntegration extends AssetIntegration<AssetSett
headers: this.buildAuthHeader(token)
}
);
- Logging.logDebug({
+ await Logging.logDebug({
tenantID: this.tenantID,
source: Constants.CENTRAL_SERVER,
action: ServerAction.RETRIEVE_ASSET_CONSUMPTION,

MODIFY
src/integration/asset/iothink/IothinkAssetIntegration.ts
src/integration/asset/iothink/IothinkAssetIntegration.ts
-import { AssetConnectionSetting, AssetSetting } from '../../../types/Setting';
+import Asset, { AssetType, IothinkProperty } from '../../../types/Asset';
+import { AssetConnectionSetting, AssetSetting, AssetSettingsType } from '../../../types/Setting';
import { AbstractCurrentConsumption } from '../../../types/Consumption';
-import Asset from '../../../types/Asset';
import AssetIntegration from '../AssetIntegration';
import AxiosFactory from '../../../utils/AxiosFactory';
import { AxiosInstance } from 'axios';
import BackendError from '../../../exception/BackendError';
+import Connection from '../../../types/Connection';
import Constants from '../../../utils/Constants';
import Cypher from '../../../utils/Cypher';
import Logging from '../../../utils/Logging';
import { ServerAction } from '../../../types/Server';
+import SettingStorage from '../../../storage/mongodb/SettingStorage';
import Utils from '../../../utils/Utils';
+import moment from 'moment';
const MODULE_NAME = 'IothinkAssetIntegration';
export default class IothinkAssetIntegration extends AssetIntegration<AssetSetting> {
private axiosInstance: AxiosInstance;
+ private initialDate = moment('20000101 00:00:00', 'YYYYMMDD HH:mm:ss');
public constructor(tenantID: string, settings: AssetSetting, connection: AssetConnectionSetting) {
super(tenantID, settings, connection);
@@ -27,9 +31,11 @@ export default class IothinkAssetIntegration extends AssetIntegration<AssetSetti
}
public async retrieveConsumptions(asset: Asset): Promise<AbstractCurrentConsumption[]> {
+ // To be discussed. How do we get local time of the asset? The requests needs local time
+ const timestampStart = -this.initialDate.diff(moment(asset.lastConsumption?.timestamp ? asset.lastConsumption.timestamp : moment().subtract(15, 'minutes')), 'seconds');
// Set new Token
const token = await this.connect();
- const request = `${this.connection.url}/${asset.meterID}`;
+ const request = `${this.connection.url}/${asset.meterID}&startTime=${timestampStart}`;
try {
// Get consumption
const response = await this.axiosInstance.get(
@@ -60,9 +66,38 @@ export default class IothinkAssetIntegration extends AssetIntegration<AssetSetti
}
- private filterConsumptionRequest(asset: Asset, data: any[]): AbstractCurrentConsumption[] {
+ private filterConsumptionRequest(asset: Asset, data: any): AbstractCurrentConsumption[] {
+ const consumptions: AbstractCurrentConsumption[] = [];
+ const energyDirection = asset.assetType === AssetType.PRODUCTION ? -1 : 1;
+ for (let i = 0; i < data.historics[0].logs.length; i++) {
const consumption = {} as AbstractCurrentConsumption;
- return [consumption];
+ consumption.currentInstantWatts = this.getPropertyValue(data.historics, IothinkProperty.Power_ACTIVE, i) * energyDirection;
+ consumption.currentInstantWattsL1 = this.getPropertyValue(data.historics, IothinkProperty.POWER_L1, i) * energyDirection;
+ consumption.currentInstantWattsL2 = this.getPropertyValue(data.historics, IothinkProperty.POWER_L2, i) * energyDirection;
+ consumption.currentInstantWattsL3 = this.getPropertyValue(data.historics, IothinkProperty.POWER_L3, i) * energyDirection;
+
+ if (asset.siteArea?.voltage) {
+ consumption.currentInstantAmps = consumption.currentInstantWatts / asset.siteArea.voltage;
+ consumption.currentInstantAmpsL1 = consumption.currentInstantWattsL1 / asset.siteArea.voltage;
+ consumption.currentInstantAmpsL2 = consumption.currentInstantWattsL2 / asset.siteArea.voltage;
+ consumption.currentInstantAmpsL3 = consumption.currentInstantWattsL3 / asset.siteArea.voltage;
+ }
+ consumption.lastConsumption = {
+ timestamp: moment(this.initialDate).add(data.historics[0].logs[i].timestamp, 'seconds').toDate(),
+ value: consumption.currentInstantWatts / 60
+ };
+ consumptions.push(consumption);
+ }
+ return consumptions;
+ }
+
+ private getPropertyValue(data: any[], propertyName: string, index: number): number {
+ for (const measure of data) {
+ if (measure.tagReference === propertyName) {
+ return Utils.convertToFloat(Utils.createDecimal(measure.logs[index].value)) * 1000;
+ }
+ }
+ return 0;
}

MODIFY
src/scheduler/tasks/AssetGetConsumptionTask.ts
src/scheduler/tasks/AssetGetConsumptionTask.ts
@@ -50,6 +50,10 @@ export default class AssetGetConsumptionTask extends SchedulerTask {
await OCPPUtils.addSiteLimitationToConsumption(tenant.id, asset.siteArea, siteAreaLimitConsumption);
// Create Consumptions
for (const consumption of assetConsumptions) {
+ // Check if last consumption already exists
+ if (asset.lastConsumption?.timestamp && moment(consumption.lastConsumption.timestamp).diff(moment(asset.lastConsumption.timestamp), 'seconds') < 50) {
+ continue;
+ }
// Create Consumption to save
const consumptionToSave: Consumption = {
startedAt: asset.lastConsumption?.timestamp ? asset.lastConsumption.timestamp : moment(consumption.lastConsumption.timestamp).subtract(1, 'minutes').toDate(),

MODIFY
src/types/Asset.ts
src/types/Asset.ts
@@ -42,3 +42,10 @@ export enum SchneiderProperty {
POWER_ACTIVE_L3 = 'PActive_Ph3',
POWER_ACTIVE = 'PActive_Tot',
}
+
+export enum IothinkProperty {
+ Power_ACTIVE = 'Puissance_active_totale',
+ POWER_L1 = 'Puissance_active_de_phase_1',
+ POWER_L2 = 'Puissance_active_de_phase_2',
+ POWER_L3 = 'Puissance_active_de_phase_3',
+}

Retrieve Asset Consumption from ioThink