Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Simplify logic in createStationInfoHash() CS method
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Apr 27, 2022
1 parent 94ec7e9 commit 3ead11b
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ export default class ChargingStation {
private getStationInfoFromTemplate(): ChargingStationInfo {
const stationInfo: ChargingStationInfo = this.getTemplateFromFile();
if (Utils.isNullOrUndefined(stationInfo)) {
const logMsg = 'Fail to read charging station template file';
const logMsg = 'Failed to read charging station template file';
logger.error(`${this.logPrefix()} ${logMsg}`);
throw new BaseError(logMsg);
}
Expand Down Expand Up @@ -1001,19 +1001,21 @@ export default class ChargingStation {
}

private createStationInfoHash(stationInfo: ChargingStationInfo): ChargingStationInfo {
const previousInfoHash = stationInfo?.infoHash ?? '';
delete stationInfo.infoHash;
const currentInfoHash = crypto
.createHash(Constants.DEFAULT_HASH_ALGORITHM)
.update(JSON.stringify(stationInfo))
.digest('hex');
if (
(!Utils.isEmptyObject(stationInfo) && Utils.isEmptyString(previousInfoHash)) ||
(!Utils.isEmptyString(previousInfoHash) && currentInfoHash !== previousInfoHash)
) {
stationInfo.infoHash = currentInfoHash;
} else if (!Utils.isEmptyObject(stationInfo)) {
stationInfo.infoHash = previousInfoHash;
if (!Utils.isEmptyObject(stationInfo)) {
const previousInfoHash = stationInfo?.infoHash ?? '';
delete stationInfo.infoHash;
const currentInfoHash = crypto
.createHash(Constants.DEFAULT_HASH_ALGORITHM)
.update(JSON.stringify(stationInfo))
.digest('hex');
if (
Utils.isEmptyString(previousInfoHash) ||
(!Utils.isEmptyString(previousInfoHash) && currentInfoHash !== previousInfoHash)
) {
stationInfo.infoHash = currentInfoHash;
} else {
stationInfo.infoHash = previousInfoHash;
}
}
return stationInfo;
}
Expand Down

0 comments on commit 3ead11b

Please sign in to comment.