-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
160 lines (129 loc) · 5.56 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
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EPFL Salles Libres à Proximité</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body {
margin: 1rem;
}
.btn {
margin-bottom: 1rem;
}
.btn:focus,.btn:active {
outline: none !important;
box-shadow: none;
}
</style>
</head>
<body>
<h3>EPFL</h3>
<h5>Less Walk, More Work.</h5>
<h5>Salles libres à proximité</h5>
<p>Toutes les salles ci-dessous sont disponibles pendant les 3 prochaines heures. Bon courage pour le travail :)</p>
<div>
<button onclick="startWatching()" class="btn btn-primary">Charger les salles à proximité</button>
</div>
<div>
<button onclick="showLoc()" class="btn btn-warning">[Debug] Afficher ma position</button>
</div>
<p id="pos"></p>
<p id="result"></p>
<div>
<br<
<p>Made by <a href="https://github.com/Androz2091">Simon Lefort</a> et hébergé par PolySource. Merci à <a href="https://github.com/antoninfaure">Antonin Faure</a> pour l'API Occupancy.</p>
</div>
<script src="./rooms-coordinates.js"></script>
<script>
function showLoc() {
if (!navigator.geolocation) {
alert("La géolocalisation n'est pas prise en charge par ce navigateur.");
return;
}
const options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(function (position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
document.getElementById('pos').innerText = `${latitude},${longitude}`;
}, function (error) {
alert("Erreur lors de la récupération de la position : " + error.message);
}, options);
}
</script>
<script>
let watchId;
let freeRooms = [];
fetchFreeRooms();
function startWatching() {
if (!navigator.geolocation) {
alert("La géolocalisation n'est pas prise en charge par ce navigateur.");
return;
}
document.getElementById('result').innerText = 'Chargement...';
watchId = navigator.geolocation.watchPosition(onPositionReceived, onError, {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
}
function fetchFreeRooms(hourCount) {
const DEFAULT_HOUR_COUNT = 3;
const start = new Date();
const end = new Date();
end.setHours(end.getHours() + (hourCount || DEFAULT_HOUR_COUNT));
end.setMinutes(0, 0, 0);
start.setMinutes(0, 0, 0);
fetch('https://occupancy-backend-e150a8daef31.herokuapp.com/api/rooms/find_free_rooms', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify([{
start: start.toISOString(),
end: end.toISOString(),
}])
})
.then((res) => res.json())
.then((data) => freeRooms = data.map((r) => r.name));
}
function onPositionReceived(position) {
const userLat = position.coords.latitude;
const userLng = position.coords.longitude;
const sortedRooms = rooms
.map((room) => {
const coordinates = room.geometry?.coordinates[0][0];
const distance = getUserRoomDistance(userLat, userLng, coordinates[0], coordinates[1]);
return { distance, name: room.name, type: room.type };
})
.sort((a, b) => a.distance - b.distance);
document.getElementById('pos').innerText = `${userLat},${userLng}`;
const filteredRooms = sortedRooms
.filter((room) => room.type === "SALLE DE COURS")
.filter((room) => freeRooms.includes(room.name));
document.getElementById('result').innerHTML = filteredRooms.map((room) => '[<a href="https://plan.epfl.ch/?room='+room.name+'" target="_blank">map</a>]'+ ' <a href="https://occupancy.epfl.ch/'+room.name+'" target="_blank">'+room.name + '</a> (' + Math.ceil(room.distance * 1000) + 'm)').join('<br>');
}
function onError(error) {
alert("Erreur lors de la récupération de la position : " + error.message);
}
function getUserRoomDistance(userLatitude, userLongitude, salleLatitude, salleLongitude) {
const R = 6371;
const deltaLatitude = deg2rad(salleLatitude - userLatitude);
const deltaLongitude = deg2rad(salleLongitude - userLongitude);
const a = Math.sin(deltaLatitude / 2) ** 2
+ Math.cos(deg2rad(userLongitude)) * Math.cos(deg2rad(salleLatitude)) * Math.sin(deltaLongitude / 2) ** 2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
</script>
</body>
</html>