-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcalculateLongDistanceEVRoute.js
More file actions
209 lines (166 loc) · 5.41 KB
/
Copy pathcalculateLongDistanceEVRoute.js
File metadata and controls
209 lines (166 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const calculateLongDistanceEVRouteUrl = 'https://api.tomtom.com/routing/1/calculateLongDistanceEVRoute/';
const requiredProperties = [ 'key', 'locations', 'chargingModes',
'constantSpeedConsumptionInkWhPerHundredkm', 'currentChargeInkWh',
'maxChargeInkWh', 'minChargeAtDestinationInkWh', 'minChargeAtChargingStopsInkWh' ];
function RouteData(obj) {
for(const property in obj)
this[property] = obj[property];
this.routes.forEach(function(route) {
route.legs.forEach(function(leg) {
const points = leg.points;
const length = points.length;
var index;
for(index = 0; index < length; index++) {
const point = points[index];
points[index] = new tt.LngLat(point.longitude, point.latitude);
}
});
});
}
RouteData.prototype.toGeoJson = function() {
const geoJson = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: []
}
}
]
};
const coordinates = geoJson.features[0].geometry.coordinates;
this.routes[0].legs.forEach(function(leg) {
leg.points.forEach(function(point) {
coordinates.push([point.lng, point.lat]);
});
});
return geoJson;
}
function CalculateLongDistanceEVRouteOptions(options) {
this.options = options;
}
CalculateLongDistanceEVRouteOptions.prototype.go = function() {
const options = this.options;
return new Promise(function(fulfill, reject) {
if (!hasOwnProperties(options, requiredProperties)) {
reject('calculateLongDistanceEVRoute call is missing required properties.');
return;
}
const url = formatUrl(options, reject);
if (url == null)
return;
const body = JSON.stringify({ chargingModes: options.chargingModes });
fetch(url, {
method: 'POST',
mode: 'cors',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: body
})
.then(function(response) {
response
.json()
.then(function(obj) {
if (!obj.hasOwnProperty('error'))
fulfill(new RouteData(obj));
else
reject(obj.error.description);
});
})
.catch(function(error) {
reject(error);
});
});
function addLocations(url, locations) {
if (locations == null)
return false;
var isFirstLocation = true;
for(const location of locations) {
if (!hasOwnProperties(location, ['lat', 'lng']))
return false;
if (isFirstLocation)
isFirstLocation = false;
else
url.text += ':';
url.text += location.lat + ',' + location.lng;
}
url += '/';
return true;
}
function addParameter(url, options, name, defaultValue, format) {
const hasProperty = options.hasOwnProperty(name);
if (!hasProperty && defaultValue == null)
return true;
if (url.hasParameters)
url.text += '&';
else {
url.text += '?';
url.hasParameters = true;
}
var value = hasProperty ? options[name] : defaultValue;
if (format != null) {
value = format(value);
if (value == null)
return false;
}
url.text += name + '=' + encodeURIComponent(value);
return true;
}
function addParameters(url, options, names) {
names.forEach(function(name) {
addParameter(url, options, name);
});
}
function formatConsumptionPairs(pairs) {
var text = '';
for(const pair of pairs) {
if (!Array.isArray(pair) || pair.length != 2)
return null;
if (text != '')
text += ':';
text += pair;
}
return text;
}
function formatUrl(options, reject) {
hasOwnProperties(options, ['key', 'locations', 'chargingModes']);
var url = { hasParameters: false, text: calculateLongDistanceEVRouteUrl };
if (!addLocations(url, options.locations)) {
reject(invalidProperty('locations'));
return null;
}
url.text += '/json';
addParameter(url, options, 'vehicleEngineType', 'electric');
if (!addParameter(url, options, 'constantSpeedConsumptionInkWhPerHundredkm',
null, formatConsumptionPairs)) {
reject(invalidProperty('constantSpeedConsumptionInkWhPerHundredkm'));
return null;
}
addParameters(url, options, [ 'currentChargeInkWh', 'maxChargeInkWh',
'minChargeAtDestinationInkWh', 'minChargeAtChargingStopsInkWh',
'vehicleHeading', 'sectionType', 'report', 'departAt', 'traffic', 'avoid',
'vehicleMaxSpeed', 'vehicleWeight', 'vehicleAxleWeight', 'vehicleLength',
'vehicleWidth', 'vehicleHeight', 'vehicleCommercial', 'vehicleLoadType',
'accelerationEfficiency', 'decelerationEfficiency', 'uphillEfficiency',
'downhillEfficiency', 'auxiliaryPowerInkW', 'key' ]);
return url.text;
}
function hasOwnProperties(options, properties) {
if (options == null)
return false;
for(const property of properties)
if (!options.hasOwnProperty(property))
return false;
return true;
}
function invalidProperty(name) {
return 'calculateLongDistanceEVRoute property (' + name + ') is invalid.';
}
}
function calculateLongDistanceEVRoute(options) {
return new CalculateLongDistanceEVRouteOptions(options);
}