Added changes for Wit Asset integration and also replaced connection type with id.
MODIFY
src/integration/asset/iothink/IothinkAssetIntegration.ts
src/integration/asset/iothink/IothinkAssetIntegration.ts
@@ -149,13 +149,13 @@ export default class IothinkAssetIntegration extends AssetIntegration<AssetSetti
private async connect(): Promise<string> {
// Get token from cache
- let token = AssetTokenCache.getInstanceForTenant(this.tenant).getToken(AssetConnectionType.IOTHINK);
+ let token = AssetTokenCache.getInstanceForTenant(this.tenant).getToken(this.connection.id);
if (!token) {
this.checkConnectionIsProvided();
// Get a fresh token (if not found or expired)
token = await this.fetchAssetProviderToken(await this.getCredentialURLParams());
// Cache it for better performance
- AssetTokenCache.getInstanceForTenant(this.tenant).setToken(AssetConnectionType.IOTHINK, token);
+ AssetTokenCache.getInstanceForTenant(this.tenant).setToken(this.connection.id, token);
}
return token.accessToken;
}
@@ -172,14 +172,13 @@ export default class IothinkAssetIntegration extends AssetIntegration<AssetSetti
}),
`Time out error (5s) when getting the token with the connection URL '${this.connection.url}/token'`
);
- const data = response.data;
return {
- accessToken: data.access_token,
- tokenType: data.token_type,
- expiresIn: data.expires_in,
- userName: data.userName,
- issued: data['.issued'],
- expires: data['.expires']
+ accessToken: response.data.access_token,
+ tokenType: response.data.token_type,
+ expiresIn: response.data.expires_in,
+ userName: response.data.userName,
+ issued: response.data['.issued'],
+ expires: response.data['.expires']
};
}

MODIFY
src/integration/asset/wit/WitAssetIntegration.ts
src/integration/asset/wit/WitAssetIntegration.ts
-import Asset, { AssetType, WitDataSet } from '../../../types/Asset';
+import Asset, { AssetConnectionToken, AssetType, WitDataSet } from '../../../types/Asset';
import { AssetConnectionSetting, AssetSetting } from '../../../types/Setting';
import { AbstractCurrentConsumption } from '../../../types/Consumption';
import AssetIntegration from '../AssetIntegration';
+import AssetTokenCache from '../../../utils/AssetTokenCache';
import AxiosFactory from '../../../utils/AxiosFactory';
import { AxiosInstance } from 'axios';
import BackendError from '../../../exception/BackendError';
@@ -105,10 +106,21 @@ export default class WitAssetIntegration extends AssetIntegration<AssetSetting>
}
private async connect(): Promise<string> {
+ let token = AssetTokenCache.getInstanceForTenant(this.tenant).getToken(this.connection.id);
+ if (!token) {
// Check if connection is initialized
this.checkConnectionIsProvided();
// Get credential params
const credentials = await this.getCredentialURLParams();
+ token = await this.fetchAssetProviderToken(credentials);
+ // Cache it for better performance
+ AssetTokenCache.getInstanceForTenant(this.tenant).setToken(this.connection.id, token);
+ }
+ return token.accessToken;
+ }
+
+ private async fetchAssetProviderToken(credentials: URLSearchParams): Promise<AssetConnectionToken> {
+ const now = new Date();
// Send credentials to get the token
const response = await Utils.executePromiseWithTimeout(5000,
this.axiosInstance.post(`${this.connection.witConnection.authenticationUrl}/token`,
@@ -121,8 +133,14 @@ export default class WitAssetIntegration extends AssetIntegration<AssetSetting>
}),
`Time out error (5s) when getting the token with the connection URL '${this.connection.witConnection.authenticationUrl}/token'`
);
- // Return the Token
- return response.data.access_token;
+ const expireTime = moment().add(response.data.expires_in, 'seconds').toDate();
+ return {
+ accessToken: response.data.access_token,
+ tokenType: response.data.token_type,
+ expiresIn: response.data.expires_in,
+ issued: now,
+ expires: expireTime,
+ };
}
private async getCredentialURLParams(): Promise<URLSearchParams> {

MODIFY
src/utils/AssetTokenCache.ts
src/utils/AssetTokenCache.ts
@@ -5,7 +5,7 @@ import moment from 'moment';
export default class AssetTokenCache {
private static instances = new Map<string, AssetTokenCache>();
- private tokens = new Map<AssetConnectionType, AssetConnectionToken>();
+ private tokens = new Map<string, AssetConnectionToken>();
public static getInstanceForTenant(tenant: Tenant): AssetTokenCache {
if (!AssetTokenCache.instances.has(tenant.id)) {
@@ -14,15 +14,15 @@ export default class AssetTokenCache {
return AssetTokenCache.instances.get(tenant.id);
}
- public getToken(assetConnectionType: AssetConnectionType): AssetConnectionToken {
- const token = this.tokens.get(assetConnectionType);
+ public getToken(assetConnectionId: string): AssetConnectionToken {
+ const token = this.tokens.get(assetConnectionId);
if (token && !moment(new Date(token.expires)).subtract(60, 'seconds').isBefore()) {
return token;
}
return null;
}
- public setToken(assetConnectionType: AssetConnectionType, token: AssetConnectionToken): void {
- this.tokens.set(assetConnectionType, token);
+ public setToken(assetConnectionId: string, token: AssetConnectionToken): void {
+ this.tokens.set(assetConnectionId, token);
}
}

Added changes for Wit Asset integration and also replaced connection type with id.