/**
* BeNomad BeMap JavaScript API - EvStepPoint (v1.5)
*
* One charging stop along an EV journey. Built from the v1 `StepPointFront`
* (`.../service/v1_0_0/.../response/StepPointFront.java`) or, via
* `fromChargeEvent()`, from a v2 `ChargeEventFront`
* (`.../service/v2_0_0/.../response/events/ChargeEventFront.java`).
*
* @public
* @constructor
* @param {Object} options Parsed JSON entry from the server response.
*/
bemap.EvStepPoint = function(options) {
var opts = options || {};
this.id = opts.id || null;
this.operatorId = opts.operatorId || null;
this.brand = opts.brand || null;
this.nameOfPool = opts.nameOfPool || null;
this.accessibility = opts.accessibility || null;
this.availabilityStatus = opts.availabilityStatus || null;
this.longitude = (typeof opts.longitude === 'number') ? opts.longitude : null;
this.latitude = (typeof opts.latitude === 'number') ? opts.latitude : null;
this.distance = (typeof opts.distance === 'number') ? opts.distance : 0;
this.duration = (typeof opts.duration === 'number') ? opts.duration : 0;
this.arrivalTime = (typeof opts.arrivalTime === 'number') ? opts.arrivalTime : null;
this.departureTime = (typeof opts.departureTime === 'number') ? opts.departureTime : null;
this.consumed = (typeof opts.consumed === 'number') ? opts.consumed : null;
this.arrivalBatteryLevel = (typeof opts.arrivalBatteryLevel === 'number') ? opts.arrivalBatteryLevel : null;
this.departureBatteryLevel = (typeof opts.departureBatteryLevel === 'number') ? opts.departureBatteryLevel : null;
this.chargingPower = opts.chargingPower || null;
this.chargingTime = (typeof opts.chargingTime === 'number') ? opts.chargingTime : 0;
this.chargingCost = opts.chargingCost || null;
this.weather = opts.weather || null;
this.countryCode = opts.countryCode || null;
this.country = opts.country || null;
this.postalCode = opts.postalCode || null;
this.city = opts.city || null;
this.street = opts.street || null;
this.streetNumber = opts.streetNumber || null;
this.phoneNumber = opts.phoneNumber || null;
this.open24x7 = (typeof opts.open24x7 === 'boolean') ? opts.open24x7 : null;
this.openingHours = Array.isArray(opts.openingHours) ? opts.openingHours.slice() : null;
this.comment = opts.comment || null;
this.images = Array.isArray(opts.images) ? opts.images.slice() : null;
this.chargingStations = Array.isArray(opts.chargingStations) ? opts.chargingStations.slice() : null;
this.maxSpeed = (typeof opts.maxSpeed === 'number') ? opts.maxSpeed : null;
// Full v2 `pool` object (set from a v2 CHARGE event by `fromChargeEvent`);
// null on a v1 step point. Exposed via `getPool()`.
this.pool = opts.pool || null;
};
bemap.EvStepPoint.prototype.getId = function() { return this.id; };
bemap.EvStepPoint.prototype.getOperatorId = function() { return this.operatorId; };
bemap.EvStepPoint.prototype.getBrand = function() { return this.brand; };
bemap.EvStepPoint.prototype.getName = function() { return this.nameOfPool; };
bemap.EvStepPoint.prototype.getAccessibility = function() { return this.accessibility; };
bemap.EvStepPoint.prototype.getAvailabilityStatus = function() { return this.availabilityStatus; };
bemap.EvStepPoint.prototype.getCoordinate = function() {
if (this.longitude === null || this.latitude === null) return null;
return new bemap.Coordinate(this.longitude, this.latitude);
};
bemap.EvStepPoint.prototype.getDistance = function() { return this.distance; };
bemap.EvStepPoint.prototype.getDuration = function() { return this.duration; };
bemap.EvStepPoint.prototype.getArrivalTime = function() { return this.arrivalTime; };
bemap.EvStepPoint.prototype.getDepartureTime = function() { return this.departureTime; };
bemap.EvStepPoint.prototype.getConsumed = function() { return this.consumed; };
bemap.EvStepPoint.prototype.getArrivalBatteryLevel = function() { return this.arrivalBatteryLevel; };
bemap.EvStepPoint.prototype.getDepartureBatteryLevel = function() { return this.departureBatteryLevel; };
bemap.EvStepPoint.prototype.getChargingPower = function() { return this.chargingPower; };
bemap.EvStepPoint.prototype.getChargingTime = function() { return this.chargingTime; };
bemap.EvStepPoint.prototype.getChargingCost = function() { return this.chargingCost; };
bemap.EvStepPoint.prototype.getWeather = function() { return this.weather; };
bemap.EvStepPoint.prototype.getCountryCode = function() { return this.countryCode; };
bemap.EvStepPoint.prototype.getCity = function() { return this.city; };
bemap.EvStepPoint.prototype.getMaxSpeed = function() { return this.maxSpeed; };
/** Full v2 `pool` object (all provider/station detail) when built from a v2 CHARGE event. @public @since 2.0.0 @return {Object|null} */
bemap.EvStepPoint.prototype.getPool = function() { return this.pool || null; };
/**
* Build an `EvStepPoint` from a REST **v2.0.0** `CHARGE` event (`ChargeEventFront`).
*
* v2 nests the station/provider detail under `pool` (`PoolFront`) and the
* coordinate under `coord`; the charging figures (battery in/out, charging time,
* power, cost) are on the event itself. `leg` (optional) carries the preceding
* `ROUTE` segment's `distance`/`duration`/`consumed` so the v1 getters
* (`getDistance()`/`getDuration()`/`getConsumed()`) keep returning per-leg data.
* The complete raw pool is preserved via `getPool()`.
*
* @public
* @since 2.0.0
* @param {Object} evt a v2 `CHARGE` event
* @param {Object} [leg] `{ distance, duration, consumed }` from the preceding ROUTE event
* @return {bemap.EvStepPoint}
*/
bemap.EvStepPoint.fromChargeEvent = function(evt, leg) {
evt = evt || {};
var pool = evt.pool || {};
var coord = evt.coord || {};
var addr = pool.address || {};
var l = leg || {};
var sp = new bemap.EvStepPoint({
id: pool.id || null,
operatorId: pool.operatorId || null,
brand: pool.brand || null,
nameOfPool: pool.name || null,
accessibility: pool.accessibility || null,
availabilityStatus: pool.availabilityStatus || null,
longitude: (typeof coord.lon === 'number') ? coord.lon : null,
latitude: (typeof coord.lat === 'number') ? coord.lat : null,
distance: (typeof l.distance === 'number') ? l.distance : 0,
duration: (typeof l.duration === 'number') ? l.duration : 0,
arrivalTime: (typeof evt.arrivalTime === 'number') ? evt.arrivalTime : null,
departureTime: (typeof evt.departureTime === 'number') ? evt.departureTime : null,
consumed: (typeof l.consumed === 'number') ? l.consumed : null,
arrivalBatteryLevel: (typeof evt.arrivalBatteryLevel === 'number') ? evt.arrivalBatteryLevel : null,
departureBatteryLevel: (typeof evt.departureBatteryLevel === 'number') ? evt.departureBatteryLevel : null,
chargingPower: evt.chargingPower || null,
chargingTime: (typeof evt.chargingTime === 'number') ? evt.chargingTime : 0,
chargingCost: evt.chargingCost || null,
weather: evt.weather || null,
countryCode: pool.countryCode || null,
phoneNumber: pool.phoneNumber || null,
open24x7: (typeof pool.open24x7 === 'boolean') ? pool.open24x7 : null,
openingHours: Array.isArray(pool.openingHours) ? pool.openingHours : null,
country: addr.country || null,
postalCode: addr.postalCode || null,
city: addr.city || null,
street: addr.street || null,
streetNumber: addr.streetNumber || null
});
// Keep the full v2 pool for anything the flat EvStepPoint doesn't expose.
sp.pool = evt.pool || null;
return sp;
};