-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
174 lines (152 loc) · 5.73 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<script src="https://cdn.jsdelivr.net/npm/mg-api-js"></script>
<style>
#map {
height: 100vh;
}
</style>
</head>
<body>
<!-- leaflet map -->
<div id="map"></div>
<script type="text/javascript">
// Create authentication object
const authentication = {
credentials: {
database: '',
userName: '',
password: ''
},
path: 'https://preview.geotab.com'
};
// Authentication to create the GeotabApi
const api = new GeotabApi(authentication);
// Authenticate and log the result
api.authenticate()
.then(response => {
console.log('I have authenticated');
initMap(); // Initialize the map after authentication
loopWithPause(); // Start the loop after authentication
})
.catch(error => {
console.error('Authentication failed:', error);
});
let map;
let layerGroup;
// Function to initialize the map
async function initMap() {
const serial = "G90000000001";
const limoIcon = L.icon({
iconUrl: '/images/limo-hero.png',
iconSize: [116.5, 55.8],
});
try {
const result = await api.call("Get", {
typeName: "Device",
search: {
serialNumber: serial
}
});
if (result.length > 0) {
const device = result[0];
const location = await initDeviceLocation(device);
map = L.map('map').setView(location, 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
layerGroup = L.layerGroup().addTo(map);
L.marker(location, { icon: limoIcon }).addTo(layerGroup);
} else {
console.error('Error: empty data');
}
} catch (error) {
alert(error);
}
}
// Function to get device location
async function initDeviceLocation(device) {
return new Promise((resolve, reject) => {
api.call("Get", {
typeName: "DeviceStatusInfo",
search: {
deviceSearch: {
id: device.id
}
}
}, function (result) {
if (result.length > 0) {
const location = new L.LatLng(41, 41);
resolve(location);
} else {
reject("Location information not available for the inputted device");
}
}, function (error) {
reject(error);
});
});
}
// Function to pause execution for a given number of milliseconds
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Function to retrieve and display device location
async function retrieveAndDisplayDeviceLocation(serial, limoIcon) {
try {
const result = await api.call("Get", {
typeName: "Device",
search: {
serialNumber: serial
}
});
if (result.length > 0) {
showDeviceLocation(result[0], limoIcon);
} else {
console.error('Error: empty data');
}
} catch (error) {
alert(error);
}
}
// Function to show device location on the map
function showDeviceLocation(device, limoIcon) {
api.call("Get", {
typeName: "DeviceStatusInfo",
search: {
deviceSearch: {
id: device.id
}
}
}, function (result) {
if (result.length > 0) {
const location = new L.LatLng(result[0].latitude, result[0].longitude);
const marker = L.marker(location, { icon: limoIcon });
layerGroup.clearLayers().addLayer(marker);
map.setView(location);
} else {
alert("Location information not available for the inputted device");
}
}, function (error) {
alert(error);
});
}
// Loop to retrieve and display device location every 5 seconds
async function loopWithPause() {
const serial = "G90000000001";
const icon = L.icon({
iconUrl: '/images/limo-hero.png',
iconSize: [116.5, 55.8],
});
while (true) {
await retrieveAndDisplayDeviceLocation(serial, icon);
await delay(5000); // Pause for 5 seconds
}
}
</script>
</body>
</html>