-
Notifications
You must be signed in to change notification settings - Fork 12
/
simulate-driver.html
308 lines (263 loc) · 11.3 KB
/
simulate-driver.html
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<!--
Copyright 2017 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Delivery Driver Simulator</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.4/esri/css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
}
</style>
<script type="text/javascript">
var dojoConfig = {
packages: [{
name: "identity-manager-cacher",
location: "http://nixta.github.io/js-api-identity-manager-cacher/js/4.x"
},
{
name: "deliveries",
location: location.pathname.replace(/\/[^/]+$/, "/") + "./js/deliveries"
}
]
};
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.13.0.js"></script>
<script src="https://js.arcgis.com/4.4/"></script>
<script>
</script>
</head>
<body>
<button id="pauseSim" type="button" class="btn btn-primary">Pause</button>
<button id="resumeSim" type="button" class="btn btn-primary">Resume</button>
<div id="viewDiv"></div>
<script>
var currentSims = {};
var mapView, driversLayer;
var serviceLayers;
var simSpeed = 5,
simInterval = 100;
var pubNubPublishInterval = 1000;
var pubnub, serviceURL, simServiceURL, pubnubConfig;
///////////////////////////////
/// MAP + UI + PUBNUB
///////////////////////////////
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/layers/GraphicsLayer",
"esri/Graphic",
"esri/symbols/SimpleMarkerSymbol",
"deliveries/tools",
"deliveries/simtools",
"config.js",
"identity-manager-cacher/cacher",
"dojo/domReady!"
], function (Map, MapView, FeatureLayer, GraphicsLayer, Graphic, SimpleMarkerSymbol, deliveryTools,
deliverySimTools, config) {
initConfig();
serviceLayers = deliveryTools.getFeatureLayers(serviceURL);
deliverySimTools.getFeatureLayers(simServiceURL);
driversLayer = new GraphicsLayer();
var map = new Map({
basemap: 'dark-gray-vector',
layers: [
driversLayer
// serviceLayers.deliveryFence,
// serviceLayers.route,
// serviceLayers.customer,
// serviceLayers.driver,
// serviceLayers.delivery
]
});
var view = new MapView({
container: "viewDiv",
map: map,
center: [-73.986471073, 40.75961304],
zoom: 12,
constraints: { snapToZoom: false }
});
mapView = view;
view.on("click", function (event) {
var lat = Math.round(event.mapPoint.latitude * 1000) / 1000;
var lon = Math.round(event.mapPoint.longitude * 1000) / 1000;
console.log(`"lat": ${lat},\n "lng": ${lon}`);
});
pubnub = new PubNub(pubnubConfig);
// Add a listener to start simulating a route when the main app creates it
pubnub.addListener({
status: function (statusEvent) {
if (statusEvent.error) {
console.log("PubNub Status Event");
console.log(statusEvent);
}
},
message: function (m) {
console.log(`PubNub Message received on ${m.channel}`);
var channelName = m.channel;
switch (channelName) {
case 'route_created':
var routeId = m.message.routeId.toUpperCase();
var driverId = m.message.driverId.toUpperCase();
var customers = m.message.deliveries.map(function (delivery) {
return delivery.customerId;
});
var currentSimForDriver = currentSims[driverId];
if (currentSimForDriver !== undefined) {
currentSimForDriver.abort();
delete currentSims[driverId];
}
deliverySimTools.simulateDrive(driverId, routeId, locationUpdated,
simInterval /* ms */ , 5).then(
function (
result) {
result.speed = simSpeed;
currentSims[result.id] = result;
}).otherwise(function (error) {
console.log(`Error simulating Route ${routeId}`,
`Driver driverId`, error);
alert(`Error simulating route! ${error.message}`);
});
break;
}
// console.log(m);
}
});
pubnub.subscribe({
channels: [
'route_created'
]
});
var symbol = new SimpleMarkerSymbol({
style: "circle",
color: [0, 0, 255],
size: 12,
outline: {
color: [255, 255, 255],
width: 2
}
});
///////////////////////////////
/// CONFIGURATION
///////////////////////////////
function initConfig() {
// ArcGIS Configuration
serviceURL = config.serviceURL;
simServiceURL = config.simServiceURL;
// PubNub Configuration
pubnubConfig = {
publishKey: config.publishKey,
subscribeKey: config.subscribeKey
};
}
function locationUpdated(newLocation, driverSim, arrivedAtStop, departedStop) {
if (newLocation === undefined) {
console.log(`Simulation Complete for ${driverSim.id}`);
return;
}
publishNewLocation(newLocation, driverSim);
if (arrivedAtStop) {
console.log(`Just arrived at stop ${driverSim.sequence}`);
publishDeliveryCompleted(driverSim);
}
moveDriverGraphic(newLocation, driverSim);
}
function moveDriverGraphic(newLocation, driverSim) {
driversLayer.remove(driverSim.graphic);
driverSim.graphic = new Graphic({
attributes: {
driverId: driverSim.id
},
symbol: symbol,
geometry: newLocation
});
driversLayer.add(driverSim.graphic);
}
// Publish simulated driver location updates to PubNub as if a driver's device were moving.
function publishNewLocation(newLocation, driverSim) {
if (driverSim.lastPubNubPublish !== undefined && (performance.now() - driverSim.lastPubNubPublish) <
pubNubPublishInterval) {
return;
}
var pubnubPayload = {
channel: 'all_driver_updates',
message: {
driverId: driverSim.id.toUpperCase(),
lat: newLocation.latitude,
lng: newLocation.longitude
}
};
driverSim.lastPubNubPublish = performance.now();
if (driverSim.currentRoute != deliverySimTools.cruiseRouteId) {
pubnubPayload.message.routeId = driverSim.routeId;
pubnubPayload.message.sequence = driverSim.sequence;
}
pubnub.publish(pubnubPayload, function (status, response) {
if (status.error) {
console.log(status, response);
}
});
}
// Publish that the specific delivery is completed, as if the driver completed it in their app.
function publishDeliveryCompleted(driverSim) {
// General delivery notification for the route.
var pubnubPayload = {
channel: `deliveryComplete+${driverSim.routeId.toUpperCase()}`,
message: {
driverId: driverSim.id.toUpperCase(),
routeId: driverSim.routeId,
sequence: driverSim.sequence
}
};
console.log(`Pubnub message on delivery complete: ${pubnubPayload.channel}`);
pubnub.publish(pubnubPayload, function (status, response) {
if (status.error) {
console.log(status, response);
}
});
pubnubPayload.channel =
`deliveryComplete+${driverSim.routeId.toUpperCase()}+${driverSim.sequence}`;
console.log(`Pubnub message on delivery complete: ${pubnubPayload.channel}`);
pubnub.publish(pubnubPayload, function (status, response) {
if (status.error) {
console.log(status, response);
}
});
}
$('#pauseSim').on('click', function (e) {
for (var [key, sim] of Object.entries(currentSims)) {
sim.pause();
}
});
$('#resumeSim').on('click', function (e) {
for (var [key, sim] of Object.entries(currentSims)) {
sim.resume();
}
});
});
</script>
</body>
</html>