typed-ocpp
Version:
A library for type-aware parsing, serialization and validation of OCPP 1.6, OCPP 2.0 and OCPP 2.1 messages
80 lines • 3.25 kB
JavaScript
/*
* This module builds upon the primitives in ./schedules.ts and implements
* scheduling types and functions mapping to a subset of the OCPP data model
* shared by OCPP 1.6, 2.0.1 and 2.1.
*/
/**
* Cloning function for use with scheduling methods in `./schedule.ts` that
* returns a deep copy of the provided `ExchangeLimits` object.
*/
export const cloneChargingLimits = (l) => ({
...l,
charging: { ...l.charging },
discharging: { ...l.discharging },
});
/**
* Merging function for use with scheduling methods in `./schedule.ts` that
* returns a deep copy of the provided `right` instance of `ExchangeLimits`.
* Semantically, this can be used to merge two exchange schedules in a manner
* that sets the limits of overlapping periods to those from the `right`
* schedule.
*/
export const mergeChargingLimitsRight = (l, r) => {
if (l.unit !== r.unit) {
throw new Error('cannot merge limits with different units');
}
return cloneChargingLimits(r);
};
/**
* Merging function for use with scheduling methods in `./schedule.ts` that
* returns a new `ExchangeLimits` object computed as the convervative minimum
* between `left` and `right`. Semantically, this can be used to merge two
* schedules in a manner that sets the limits of overlapping periods to the
* minimum values between both.
*/
export const mergeChargingLimitsMin = (l, r) => {
if (l.unit !== r.unit) {
throw new Error('cannot merge limits with different units');
}
return {
shouldDischarge: l.shouldDischarge === r.shouldDischarge ? l.shouldDischarge : false,
charging: {
max: Math.min(l.charging.max, r.charging.max),
min: Math.max(l.charging.min, r.charging.min),
phases: { qty: Math.min(l.charging.phases.qty, r.charging.phases.qty) },
},
discharging: {
max: Math.min(l.discharging.max, r.discharging.max),
min: Math.max(l.discharging.min, r.discharging.min),
phases: { qty: Math.min(l.discharging.phases.qty, r.discharging.phases.qty) },
},
unit: l.unit,
};
};
/**
* Merging function for use with scheduling methods in `./schedule.ts` that
* returns a new `ExchangeLimits` object computed as the logical sum of the
* limits in `left` and `right`. Semantically, this can be used to merge two
* exchange schedules in a manner that composes limits of overlapping periods
* via addition.
*/
export const mergeChargingLimitsAdd = (l, r) => {
if (l.unit !== r.unit) {
throw new Error('cannot merge limits with different units');
}
return {
shouldDischarge: l.shouldDischarge === r.shouldDischarge ? l.shouldDischarge : false,
charging: {
max: l.charging.max + r.charging.max,
min: Math.max(l.charging.min, r.charging.min),
phases: { qty: Math.max(l.charging.phases.qty, r.charging.phases.qty) },
},
discharging: {
max: l.discharging.max + r.discharging.max,
min: Math.max(l.discharging.min, r.discharging.min),
phases: { qty: Math.max(l.discharging.phases.qty, r.discharging.phases.qty) },
},
unit: l.unit,
};
};
//# sourceMappingURL=utils.js.map