Source: bemap-services-v2/ev/bemap-ev-reachable-area-request.js

/**
 * BeNomad BeMap JavaScript API - EvReachableAreaRequest (v1.5)
 *
 * Strongly-typed request builder for `POST service/evreachablearea/1.0`.
 * Returns a polygon describing the area an EV can reach from a start
 * coordinate given its battery and ambient conditions.
 *
 * Wire shape cross-checked against
 * `bemap_idea/.../service/v1_0_0/model/evReachableArea/EvReachableAreaRequest.java`.
 *
 * Required:
 *   - `start` — `bemap.Coordinate` or `{lon, lat}`.
 *   - `vehicle` — UUID key.
 *   - `temperature` — int °C (server marks this `required = true`).
 *
 * Example:
 *
 *     var req = new bemap.EvReachableAreaRequest({
 *         start: new bemap.Coordinate(2.35, 48.85),
 *         vehicle: 'a204c816-...',
 *         temperature: 18,
 *         initBatteryLevel: 80,
 *         payload: 75,
 *         criterias: [bemap.EvCriterion.AVOID_TOLLS]
 *     });
 *
 * @since 1.5.0
 * @public
 * @constructor
 * @param {Object} [options]
 * @param {bemap.Coordinate|{lon:Number, lat:Number}} options.start
 * @param {String} options.vehicle Vehicle UUID key.
 * @param {Number} options.temperature Ambient °C.
 * @param {Number} [options.initBatteryLevel=100]
 * @param {Number} [options.payload=75] kg.
 * @param {Array.<String>} [options.criterias] `bemap.EvCriterion.*`
 * @param {Boolean} [options.weather=false]
 * @param {String} [options.weatherProvider]
 * @param {bemap.Coordinate|{lon:Number, lat:Number}} [options.stop]
 *        Optional — when set, the polygon is the area encompassing both
 *        start and stop within the battery budget.
 * @param {Boolean} [options.polyline=true]
 * @param {Boolean} [options.encodedPolyline=false]
 * @param {String} [options.geoserver]
 */
bemap.EvReachableAreaRequest = function(options) {
    var opts = options || {};
    this.start = opts.start || null;
    this.stop = opts.stop || null;
    this.vehicle = opts.vehicle || null;
    this.initBatteryLevel = (typeof opts.initBatteryLevel === 'number') ? opts.initBatteryLevel : 100;
    this.temperature = (typeof opts.temperature === 'number') ? opts.temperature : null;
    this.payload = (typeof opts.payload === 'number') ? opts.payload : 75;
    this.criterias = Array.isArray(opts.criterias) ? opts.criterias.slice() : null;
    this.weather = !!opts.weather;
    this.weatherProvider = opts.weatherProvider || null;
    this.polyline = (opts.polyline !== false);
    this.encodedPolyline = !!opts.encodedPolyline;
    this.geoserver = opts.geoserver || null;
};

bemap.EvReachableAreaRequest._lonOf = function(c) {
    if (!c) return null;
    if (typeof c.getLon === 'function') return c.getLon();
    if (c.lon !== undefined) return c.lon;
    if (c.longitude !== undefined) return c.longitude;
    return null;
};
bemap.EvReachableAreaRequest._latOf = function(c) {
    if (!c) return null;
    if (typeof c.getLat === 'function') return c.getLat();
    if (c.lat !== undefined) return c.lat;
    if (c.latitude !== undefined) return c.latitude;
    return null;
};

/**
 * Serialise to the wire body. Suppresses defaults.
 * @public
 * @return {Object}
 */
bemap.EvReachableAreaRequest.prototype.toJson = function() {
    var body = {};
    var lonOf = bemap.EvReachableAreaRequest._lonOf;
    var latOf = bemap.EvReachableAreaRequest._latOf;

    if (this.start) {
        body.startLon = lonOf(this.start);
        body.startLat = latOf(this.start);
    }
    if (this.stop) {
        body.stopLon = lonOf(this.stop);
        body.stopLat = latOf(this.stop);
    }
    if (this.vehicle) body.vehicle = this.vehicle;
    if (this.initBatteryLevel !== 100) body.initBatLvl = this.initBatteryLevel;
    if (this.temperature !== null) body.temperature = this.temperature;
    if (this.payload !== 75) body.payload = this.payload;
    if (this.criterias && this.criterias.length) body.criterias = this.criterias.slice();
    if (this.weather) body.weather = true;
    if (this.weatherProvider) body.wp = this.weatherProvider;
    if (!this.polyline) body.geo = false;
    if (this.encodedPolyline) body.egeo = true;
    if (this.geoserver) body.geoserver = this.geoserver;
    return body;
};