forked from uniurbit/deviceMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceMap.inc.html
More file actions
233 lines (209 loc) · 8.88 KB
/
deviceMap.inc.html
File metadata and controls
233 lines (209 loc) · 8.88 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
width: 100%;
height: 600px;
}
.info {
width: 250px;
padding: 6px 8px;
font: 14px/18px Arial, Helvetica, sans-serif;
background: rgba(255, 255, 255, 1);
box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
border-radius: 5px;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 4px;
}
.legend-item img {
margin-right: 6px;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$.fn.bootstrapSwitch = $.fn.bootstrapSwitch || function () {};
$.fn.button = $.fn.button || function () {};
</script>
<script src="plugins/Map/leaflet-src.js"></script>
<script src="plugins/Map/leaflet-indoor.js"></script>
<script>
const deviceInfo = purpose;
const splitted_loc = deviceInfo.split(",");
const lat = splitted_loc[1];
const lng = splitted_loc[2];
const deviceType = splitted_loc[3];
const deviceLevel = splitted_loc[4];
function getIconSizeForZoom(zoom) {
const baseZoom = 19;
const baseSize = 20;
const scale = zoom / baseZoom;
return Math.max(12, Math.min(40, Math.round(baseSize * scale)));
}
const portMarkers = [];
let deviceMarker = null;
const osmUrl = '//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const osm = new L.TileLayer(osmUrl, {
maxZoom: 22,
attribution: "Map data © OpenStreetMap contributors"
});
const map = new L.Map('map', {
layers: [osm],
center: new L.LatLng(43.7232, 12.6368),
zoom: 19
});
map.on('zoomend', () => {
const newSize = getIconSizeForZoom(map.getZoom());
portMarkers.forEach(({ marker, iconUrl }) => {
const newIcon = L.icon({
iconUrl,
iconSize: [newSize, newSize],
iconAnchor: [newSize / 2, newSize / 2]
});
marker.setIcon(newIcon);
});
if (deviceMarker) {
const newIcon = L.icon({
iconUrl: deviceMarker.iconUrl,
iconSize: [newSize, newSize],
iconAnchor: [newSize / 2, newSize / 2]
});
deviceMarker.marker.setIcon(newIcon);
}
});
$.getJSON("plugins/Map/data.json", function (geoJSON) {
const indoorLayer = new L.Indoor(geoJSON, {
getLevel: function (feature) {
if (feature.properties.relations.length === 0) return null;
return feature.properties.relations[0].reltags.level;
},
onEachFeature: function (feature, layer) {
layer.bindPopup(JSON.stringify(feature.properties, null, 4));
},
style: function (feature) {
const tag = feature.properties.tags.buildingpart;
const colors = {
room: '#169EC6',
verticalpassage: '#89eb35',
wall: '#808080',
toilets: '#ffff00',
corridor: 'white'
};
return {
fillColor: colors[tag] || 'white',
weight: 1,
color: '#666',
fillOpacity: 1
};
}
});
indoorLayer.setLevel("0");
indoorLayer.addTo(map);
const levelControl = new L.Control.Level({
level: "0",
levels: indoorLayer.getLevels()
});
let iconUrl = "";
if (deviceType === "switch") {
iconUrl = devstatus ? "switch-on.ico" : "switch-off.ico";
} else if (deviceType === "router") {
iconUrl = devstatus ? "router-green.ico" : "router-red.ico";
}
const icon = L.icon({
iconUrl: `plugins/Map/icone/${iconUrl}`,
iconSize: [32, 32],
iconAnchor: [16, 16],
popupAnchor: [0, -16]
});
const marker = L.marker([lat, lng], { icon }).addTo(map);
deviceMarker = { marker, iconUrl: `plugins/Map/icone/${iconUrl}` };
levelControl.addEventListener("levelchange", indoorLayer.setLevel, indoorLayer);
levelControl.addEventListener("levelchange", function (e) {
if (e.newLevel == deviceLevel) {
map.addLayer(marker);
} else {
map.removeLayer(marker);
}
}, indoorLayer);
levelControl.addTo(map);
loadPortsForDevice(hostname, lat, lng);
});
async function loadPortsForDevice(hostname, lat, lng) {
const url = `/api/v0/devices/${hostname}/ports`;
try {
const response = await fetch(url, {
headers: {
'Authorization': `Token ${apiToken}`
}
});
if (!response.ok) {
console.warn(`Impossibile ottenere le porte per ${hostname}`);
return;
}
const data = await response.json();
const ports = data.ports || [];
ports.forEach((port, i) => {
if (port.ifAdminStatus === 'up') {
const isDown = port.ifOperStatus !== 'up';
const offset = 0.00005 * (i + 1);
const portLat = parseFloat(lat) + offset;
const portLng = parseFloat(lng) + offset;
const iconSize = getIconSizeForZoom(map.getZoom());
const marker = L.marker([portLat, portLng], {
icon: L.icon({
iconUrl: isDown
? "plugins/Map/icone/port-down.ico"
: "plugins/Map/icone/port-up.ico",
iconSize: [iconSize, iconSize],
iconAnchor: [iconSize / 2, iconSize / 2]
})
}).addTo(map);
marker.bindPopup(`
<b>${hostname} - ${port.ifName}</b><br>
Descrizione: ${port.ifDescr}<br>
Stato: ${port.ifOperStatus}<br>
Velocità: ${Math.round((port.ifSpeed || 0) / 1_000_000)} Mbps
`);
portMarkers.push({ marker, iconUrl: marker.options.icon.options.iconUrl });
}
});
} catch (err) {
console.error(`Errore nella chiamata API per ${hostname}`, err);
}
}
const legend = L.control({ position: 'topright' });
legend.onAdd = function (map) {
const div = L.DomUtil.create('div', 'info legend');
div.innerHTML = `
<b>Leggenda colori:</b><br>
<div class="legend-item"><img src="plugins/Map/icone/switch-on.ico" width="16"> Switch ON</div>
<div class="legend-item"><img src="plugins/Map/icone/switch-off.ico" width="16"> Switch OFF</div>
<div class="legend-item"><img src="plugins/Map/icone/router-green.ico" width="16"> Router ON</div>
<div class="legend-item"><img src="plugins/Map/icone/router-red.ico" width="16"> Router OFF</div>
<div class="legend-item"><img src="plugins/Map/icone/port-up.ico" width="16"> Porta UP</div>
<div class="legend-item"><img src="plugins/Map/icone/port-down.ico" width="16"> Porta DOWN</div>
<hr>
<div class="legend-item"><span style="color:#169EC6;">■</span> Stanza = blu</div>
<div class="legend-item"><span style="color:#89eb35;">■</span> Scala = verde</div>
<div class="legend-item"><span style="color:#ffff00;">■</span> Toilets = giallo</div>
<div class="legend-item"><span style="color:#808080;">■</span> Muro = grigio</div>
<div class="legend-item"><span style="color:#000;">■</span> Corridoio = bianco</div>
`;
return div;
};
legend.addTo(map);
</script>
</body>
</html>