Added retrieving wit consumption
MODIFY
src/integration/asset/wit/WitAssetIntegration.ts
src/integration/asset/wit/WitAssetIntegration.ts
+import Asset, { AssetType } from '../../../types/Asset';
import { AssetConnectionSetting, AssetSetting } 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';
@@ -11,6 +11,7 @@ import Cypher from '../../../utils/Cypher';
import Logging from '../../../utils/Logging';
import { ServerAction } from '../../../types/Server';
import Utils from '../../../utils/Utils';
+import moment from 'moment';
const MODULE_NAME = 'WitAssetIntegration';
@@ -27,18 +28,78 @@ export default class WitAssetIntegration extends AssetIntegration<AssetSetting>
}
public async retrieveConsumptions(asset: Asset, manualCall: boolean): Promise<AbstractCurrentConsumption[]> {
-
- throw Error('Function is not implemented for this connection type');
-
- // Function is not implemented yet, call async function to
- await Logging.logError({
+ if (asset.lastConsumption?.timestamp && moment(asset.lastConsumption.timestamp).add(this.connection.witConnection.refreshInterval, 'minutes') > moment()) {
+ return [];
+ }
+ // Set new Token
+ const token = await this.connect();
+ const request = manualCall ?
+ `${this.connection.url}/${asset.meterID}?From=${moment().subtract(this.connection.witConnection.refreshInterval, 'minutes').toISOString()}` :
+ // Check if it is first consumption for this asset
+ `${this.connection.url}/${asset.meterID}?From=${(asset.lastConsumption?.timestamp) ? asset.lastConsumption.timestamp.toISOString() : moment().startOf('day').toISOString()}`;
+ try {
+ // Get consumption
+ const response = await this.axiosInstance.get(
+ request,
+ {
+ headers: this.buildAuthHeader(token)
+ }
+ );
+ await Logging.logDebug({
tenantID: this.tenantID,
+ source: Constants.CENTRAL_SERVER,
+ action: ServerAction.RETRIEVE_ASSET_CONSUMPTION,
+ message: `${asset.name} > WIT web service has been called successfully`,
+ module: MODULE_NAME, method: 'retrieveConsumption',
+ detailedMessages: { response: response.data }
+ });
+ return this.filterConsumptionRequest(asset, response.data, manualCall);
+ } catch (error) {
+ throw new BackendError({
+ source: Constants.CENTRAL_SERVER,
+ module: MODULE_NAME,
+ method: 'retrieveConsumption',
action: ServerAction.RETRIEVE_ASSET_CONSUMPTION,
- module: MODULE_NAME, method: 'retrieveConsumptions',
- message: 'Error while retrieving consumption. Function is not implemented for this connection type',
+ message: 'Error while retrieving the asset consumption',
+ detailedMessages: { request, token, error: error.message, stack: error.stack, asset }
});
+ }
+ }
- return [];
+ private filterConsumptionRequest(asset: Asset, data: any, manualCall: boolean): AbstractCurrentConsumption[] {
+ const consumptions: AbstractCurrentConsumption[] = [];
+ if (!Utils.isEmptyArray(data)) {
+ for (const dataset of data) {
+ const consumption = {} as AbstractCurrentConsumption;
+ switch (asset.assetType) {
+ case AssetType.CONSUMPTION:
+ consumption.currentInstantWatts = Utils.convertToFloat(dataset.V);
+ consumption.currentConsumptionWh = Utils.createDecimal(consumption.currentInstantWatts).
+ mul(Utils.createDecimal(this.connection.witConnection.refreshInterval / 60)).toNumber() ;
+ consumption.lastConsumption = {
+ timestamp: dataset.T,
+ value: consumption.currentConsumptionWh
+ };
+ break;
+ case AssetType.PRODUCTION:
+ consumption.currentInstantWatts = Utils.createDecimal(dataset.V).mul(-1).toNumber();
+ consumption.currentConsumptionWh = Utils.createDecimal(consumption.currentInstantWatts).
+ mul(Utils.createDecimal(this.connection.witConnection.refreshInterval / 60)).toNumber() ;
+ consumption.lastConsumption = {
+ timestamp: dataset.T,
+ value: consumption.currentConsumptionWh
+ };
+ break;
+ case AssetType.CONSUMPTION_AND_PRODUCTION:
+ throw new Error('Asset connection does not support producing and consuming assets');
+ }
+ consumptions.push(consumption);
+ }
+ }
+ if (manualCall) {
+ return !Utils.isEmptyArray(consumptions) ? [consumptions[consumptions.length - 1]] : [];
+ }
+ return consumptions;
}
private async connect(): Promise<string> {

Added retrieving wit consumption