Source: bemap-services-v2/ev/bemap-charging-time-request.js

/**
 * BeNomad BeMap JavaScript API - ChargingTimeRequest (v1.5)
 *
 * Strongly-typed request builder for `POST service/chargingTime/1.0` —
 * computes the time required to charge an EV from one battery level to
 * another at a given charger.
 *
 * Wire shape cross-checked against
 * `bemap_idea/.../service/v1_0_0/model/chargingTime/ChargingTimeRequest.java`.
 *
 * Required:
 *   - `vehicle` — UUID from the EV catalogue.
 *   - `chargingPointPower` — kW, positive.
 *   - `chargingCurrentType` — one of `bemap.ChargingCurrentType.*`.
 *
 * Constraint: `chargingBatteryLevel` must be strictly greater than
 * `remainingBatteryLevel` (charging the battery down makes no sense).
 *
 * Example:
 *
 *     var req = new bemap.ChargingTimeRequest({
 *         vehicle: 'a204c816-823c-4db5-a2b0-385033af54e6',
 *         chargingPointPower: 22,
 *         chargingCurrentType: bemap.ChargingCurrentType.AC_THREE_PHASES,
 *         remainingBatteryLevel: 20,
 *         chargingBatteryLevel: 80,
 *         temperature: 18
 *     });
 *
 * @since 1.5.0
 * @public
 * @constructor
 * @param {Object} [options]
 * @param {String} options.vehicle Vehicle UUID key.
 * @param {Number} options.chargingPointPower kW (positive).
 * @param {String} options.chargingCurrentType `bemap.ChargingCurrentType.*`.
 * @param {Number} [options.remainingBatteryLevel=0] Current SOC, 0-100.
 * @param {Number} [options.chargingBatteryLevel=100] Target SOC, 0-100.
 * @param {Number} [options.temperature=20] Ambient °C.
 * @param {String} [options.connectorType] Connector type ID (constrains effective power).
 * @param {Array.<String>} [options.options]
 * @param {String} [options.geoserver]
 */
bemap.ChargingTimeRequest = function(options) {
    var opts = options || {};
    this.vehicle = opts.vehicle || null;
    this.chargingPointPower = (typeof opts.chargingPointPower === 'number') ? opts.chargingPointPower : null;
    this.chargingCurrentType = opts.chargingCurrentType || null;
    this.remainingBatteryLevel = (typeof opts.remainingBatteryLevel === 'number') ? opts.remainingBatteryLevel : 0;
    this.chargingBatteryLevel = (typeof opts.chargingBatteryLevel === 'number') ? opts.chargingBatteryLevel : 100;
    this.temperature = (typeof opts.temperature === 'number') ? opts.temperature : 20;
    this.connectorType = opts.connectorType || null;
    this.options = Array.isArray(opts.options) ? opts.options.slice() : null;
    this.geoserver = opts.geoserver || null;
};

/**
 * Serialise to the wire body. Suppresses defaults so the payload stays
 * minimal. Caller-side validation is performed by
 * `bemap.ChargingTime.estimate()` BEFORE the network call.
 *
 * @public
 * @return {Object}
 */
bemap.ChargingTimeRequest.prototype.toJson = function() {
    var body = {
        chargingPointPower: this.chargingPointPower,
        chargingCurrentType: this.chargingCurrentType
    };
    if (this.vehicle) body.vehicle = this.vehicle;
    if (this.remainingBatteryLevel !== 0) body.remainingBatteryLevel = this.remainingBatteryLevel;
    if (this.chargingBatteryLevel !== 100) body.chargingBatteryLevel = this.chargingBatteryLevel;
    if (this.temperature !== 20) body.temperature = this.temperature;
    if (this.connectorType) body.connectorType = this.connectorType;
    if (this.options && this.options.length) body.options = this.options.slice();
    if (this.geoserver) body.geoserver = this.geoserver;
    return body;
};