Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

266 Zeilen
9.2 KiB

  1. const MAP_WIDTH = 2982;
  2. const MAP_HEIGHT = 1592;
  3. document.addEventListener('DOMContentLoaded', () => {
  4. loadBeacons();
  5. loadGateways();
  6. loadFingerprints();
  7. setupFloorSelector();
  8. // Setup mouse tracking
  9. const wrapper0 = document.getElementById('wrapper-floor-0');
  10. if (wrapper0) wrapper0.addEventListener('click', (e) => handleClick(e, 0));
  11. const wrapper1 = document.getElementById('wrapper-floor-1');
  12. if (wrapper1) wrapper1.addEventListener('click', (e) => handleClick(e, 1));
  13. });
  14. function setupFloorSelector() {
  15. const selector = document.getElementById('floor-select');
  16. selector.addEventListener('change', (e) => {
  17. const selectedFloor = e.target.value;
  18. // Hide all floors
  19. document.getElementById('container-floor-0').classList.add('hidden');
  20. document.getElementById('container-floor-1').classList.add('hidden');
  21. // Show selected
  22. const target = document.getElementById(`container-floor-${selectedFloor}`);
  23. if (target) target.classList.remove('hidden');
  24. });
  25. }
  26. async function loadBeacons() {
  27. try {
  28. const response = await fetch('beacon.csv'); // Adjust path if needed relative to index.html
  29. if (!response.ok) throw new Error('Failed to load beacon.csv');
  30. const text = await response.text();
  31. const beacons = parseCSV(text);
  32. renderIcons(beacons, 'beacon');
  33. } catch (error) {
  34. console.error('Error loading beacons:', error);
  35. showError('Failled to load beacon.csv. If you are opening this file locally, you might need to run a local server (e.g., "python3 -m http.server") due to CORS restrictions.');
  36. }
  37. }
  38. async function loadGateways() {
  39. try {
  40. const response = await fetch('gateway.csv');
  41. if (!response.ok) throw new Error('Failed to load gateway.csv');
  42. const text = await response.text();
  43. const gateways = parseCSV(text);
  44. renderIcons(gateways, 'gateway');
  45. } catch (error) {
  46. console.error('Error loading gateways:', error);
  47. showError('Failed to load gateway.csv. Check console for details.');
  48. }
  49. }
  50. function showError(message) {
  51. const errorDiv = document.createElement('div');
  52. errorDiv.style.backgroundColor = '#fee';
  53. errorDiv.style.color = 'red';
  54. errorDiv.style.padding = '10px';
  55. errorDiv.style.margin = '10px 0';
  56. errorDiv.style.border = '1px solid red';
  57. errorDiv.textContent = message;
  58. document.body.insertBefore(errorDiv, document.querySelector('.floor-container'));
  59. }
  60. function parseCSV(text) {
  61. const lines = text.trim().split('\n');
  62. const headers = lines[0].split(';').map(h => h.trim());
  63. // We expect headers to include: Floor, X, Y, etc.
  64. // CSV format based on file view: Position;Floor;RoomName;X;Y;Z;[Name];MAC
  65. const data = [];
  66. for (let i = 1; i < lines.length; i++) {
  67. const line = lines[i].trim();
  68. if (!line) continue;
  69. const values = line.split(';');
  70. const entry = {};
  71. headers.forEach((header, index) => {
  72. entry[header] = values[index] ? values[index].trim() : '';
  73. });
  74. // Parse numbers
  75. entry.Floor = parseInt(entry.Floor, 10);
  76. entry.X = parseFloat(entry.X);
  77. entry.Y = parseFloat(entry.Y);
  78. data.push(entry);
  79. }
  80. return data;
  81. }
  82. // Fingerprint data storage
  83. let fingerprintData = {
  84. 0: [], // Floor 0
  85. 1: [] // Floor 1
  86. };
  87. async function loadFingerprints() {
  88. try {
  89. const [response0, response1] = await Promise.all([
  90. fetch('fingerprints-floor0.json'),
  91. fetch('fingerprints-floor1.json')
  92. ]);
  93. if (response0.ok) fingerprintData[0] = await response0.json();
  94. else console.error('Failed to load fingerprints-floor0.json');
  95. if (response1.ok) fingerprintData[1] = await response1.json();
  96. else console.error('Failed to load fingerprints-floor1.json');
  97. console.log('Fingerprints loaded:', fingerprintData);
  98. } catch (error) {
  99. console.error('Error loading fingerprints:', error);
  100. }
  101. }
  102. function renderIcons(items, type) {
  103. items.forEach(item => {
  104. // Validation: Ensure valid coordinates and floor
  105. if (isNaN(item.X) || isNaN(item.Y) || isNaN(item.Floor)) {
  106. console.warn('Invalid item skipped:', item);
  107. return;
  108. }
  109. const overlayId = `overlay-floor-${item.Floor}`;
  110. const overlay = document.getElementById(overlayId);
  111. if (!overlay) {
  112. console.warn(`Overlay not found for floor: ${item.Floor}`);
  113. return;
  114. }
  115. // Container: Absolute at coordinates
  116. const iconContainer = document.createElement('div');
  117. // Structure: Flex column, centered horizontally.
  118. // Positioning:
  119. // left/top put the top-left corner of div at the coordinate.
  120. // -translate-x-1/2 centers it horizontally.
  121. // -translate-y-[20px] moves it up by 20px (half of 40px icon), so the CENTER of the icon is at the coordinate.
  122. // If we used -translate-y-1/2, it would center the whole text+icon group, which varies in height.
  123. iconContainer.className = `absolute flex flex-col items-center pointer-events-auto cursor-pointer transform -translate-x-1/2 -translate-y-[20px] z-30 group`;
  124. // Add identifiers for gateways
  125. if (type === 'gateway') {
  126. iconContainer.classList.add('gateway-icon');
  127. iconContainer.setAttribute('data-gateway-name', item.GatewayName);
  128. // Set transition for smooth opacity change
  129. iconContainer.style.transition = 'opacity 0.2s ease-in-out';
  130. }
  131. const leftPercent = (item.X / MAP_WIDTH) * 100;
  132. const topPercent = (item.Y / MAP_HEIGHT) * 100;
  133. iconContainer.style.left = `${leftPercent}%`;
  134. iconContainer.style.top = `${topPercent}%`;
  135. // Icon
  136. const iconSpan = document.createElement('span');
  137. iconSpan.className = 'iconify text-[#008EED] drop-shadow-sm';
  138. iconSpan.style.width = '40px';
  139. iconSpan.style.height = '40px'; // Explicit size
  140. // Label (BeaconName or GatewayName)
  141. const labelDiv = document.createElement('div');
  142. // Label styling: small text, centered, semi-transparent bg for readability if overlapping
  143. labelDiv.className = 'text-[10px] font-bold text-gray-700 bg-white/70 px-1 rounded mt-[-4px] whitespace-nowrap shadow-sm';
  144. labelDiv.textContent = (type === 'beacon' ? item.BeaconName : item.GatewayName) || item.Position;
  145. if (type === 'beacon') {
  146. iconSpan.setAttribute('data-icon', 'heroicons:signal');
  147. } else {
  148. iconSpan.setAttribute('data-icon', 'lsicon:online-gateway-outline');
  149. iconContainer.style.zIndex = '40'; // Gateways on top
  150. }
  151. iconContainer.appendChild(iconSpan);
  152. iconContainer.appendChild(labelDiv);
  153. // Optional Tooltip for details (MAC)
  154. const tooltip = document.createElement('div');
  155. tooltip.className = `hidden group-hover:block absolute bottom-full mb-1 px-2 py-1 bg-black text-white text-xs rounded z-50 whitespace-nowrap`;
  156. tooltip.textContent = `MAC: ${item.MAC}`;
  157. iconContainer.appendChild(tooltip);
  158. overlay.appendChild(iconContainer);
  159. });
  160. }
  161. function handleClick(e, floor) {
  162. if (!fingerprintData[floor] || fingerprintData[floor].length === 0) return;
  163. // Get wrapper relative coordinates
  164. const wrapper = e.currentTarget;
  165. const rect = wrapper.getBoundingClientRect();
  166. // Mouse relative to the wrapper
  167. const mouseX = e.clientX - rect.left;
  168. const mouseY = e.clientY - rect.top;
  169. // Convert to map coordinates
  170. if (rect.width === 0 || rect.height === 0) return;
  171. const mapX = (mouseX / rect.width) * MAP_WIDTH;
  172. const mapY = (mouseY / rect.height) * MAP_HEIGHT;
  173. // Round to nearest 50
  174. let roundedX = Math.round(mapX / 50) * 50;
  175. let roundedY = Math.round(mapY / 50) * 50;
  176. // If not ending in 50, add 50
  177. if (String(roundedX).slice(-2) !== '50') roundedX += 50;
  178. if (String(roundedY).slice(-2) !== '50') roundedY += 50;
  179. console.log(`Click at: ${mapX.toFixed(0)}, ${mapY.toFixed(0)} -> Rounded: ${roundedX}, ${roundedY}`);
  180. // Find exact match
  181. // Note: JSON coords are numbers, strict equality should work if they are exactly 50.0 etc.
  182. // Use a tolerance just in case, but user said "same position".
  183. const match = fingerprintData[floor].find(p => p.X === roundedX && p.Y === roundedY);
  184. if (match) {
  185. console.log('Match found:', match);
  186. updateGatewayOpacity(floor, match.gateways);
  187. } else {
  188. console.log('No data at rounded coordinates.');
  189. }
  190. }
  191. function updateGatewayOpacity(floor, signalMap) {
  192. const container = document.getElementById(`overlay-floor-${floor}`);
  193. if (!container) return;
  194. const gateways = container.querySelectorAll('.gateway-icon');
  195. gateways.forEach(gw => {
  196. const name = gw.getAttribute('data-gateway-name');
  197. if (name && signalMap.hasOwnProperty(name)) {
  198. const signal = signalMap[name];
  199. // Logic: -60 (best) -> opacity 1, -100 (worst) -> opacity 0
  200. // Range is 40 (-60 to -100)
  201. // Normalized: (signal - (-100)) / (-60 - (-100)) = (signal + 100) / 40
  202. let opacity = (signal + 100) / 40;
  203. // Clamp
  204. if (opacity < 0) opacity = 0;
  205. if (opacity > 1) opacity = 1;
  206. gw.style.opacity = opacity;
  207. } else {
  208. // No signal data for this gateway at this point -> 0 opacity
  209. gw.style.opacity = 0;
  210. }
  211. });
  212. }