Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { DeviceService } from '../../api/services/deviceService'; import { MongooseDeviceRepository } from '../../api/repositories/mongoose/deviceRepository'; import { trilaterateNLLS } from './multilateration'; // Import the multilaterate function const deviceService = new DeviceService(new MongooseDeviceRepository()); /** * Processes multilateration data to calculate the position of a tag based on beacons. * @param beaconArrayData - The array of beacons and their RSSI values. * @returns The calculated position { x, y, floor_id } or null if insufficient data. */ export async function processMultilaterationData(beaconArrayData: { tag: number; beacons: { id: number; rssi: number; floor_id: string }[]; }): Promise<{ x: number; y: number; floor_id: string } | null> { const { beacons } = beaconArrayData; // Validate that at least three beacons are available Iif (!validateBeaconCount(beacons)) { return null; } // Fetch positions for all beacons from the database const beaconPositions = await fetchBeaconPositions(beacons); Iif (!validateBeaconPositions(beaconPositions)) { return null; } // Prepare RSSI and position maps const { rssiMap, positionMap } = prepareMaps(beacons, beaconPositions); // Perform multilateration return calculatePosition(rssiMap, positionMap, beaconPositions); } /** * Validates that at least three beacons are available. */ function validateBeaconCount(beacons: { id: number; rssi: number; floor_id: string }[]): boolean { Iif (!beacons || beacons.length < 3) { console.error('[ERROR] Insufficient beacons for multilateration. At least 3 beacons are required.'); return false; } return true; } /** * Fetches positions for all beacons from the database. */ async function fetchBeaconPositions(beacons: { id: number; rssi: number; floor_id: string }[]) { const beaconIds = beacons.map((beacon) => beacon.id.toString()); // Use the filterDevices function to fetch only the required beacons const beaconDevices = await deviceService.filterDevices({ type: 'beacon', name: undefined, // No name filtering required location: undefined, // No specific location filtering required }); return beaconDevices .filter((device) => beaconIds.includes(device.macAddress.toString())) .map((device) => ({ id: parseInt(device.macAddress.toString()), location: { x: Number(device.location?.x), y: Number(device.location?.y), }, floor_id: String(device.location?.floor_id) || '', // Ensure floor_id is always a string })) .filter((beacon) => beacon.location && !isNaN(beacon.location.x) && !isNaN(beacon.location.y)); } /** * Validates that at least three beacon positions are available. */ function validateBeaconPositions(beaconPositions: any[]): boolean { Iif (beaconPositions.length < 3) { console.error('[ERROR] Could not find positions for at least 3 beacons.'); return false; } return true; } /** * Prepares RSSI and position maps for multilateration. */ function prepareMaps( beacons: { id: number; rssi: number; floor_id: string }[], beaconPositions: { id: number; location: { x: number; y: number }; floor_id: string }[] ) { const rssiMap: Record<number, number> = {}; const positionMap: Record<number, { x: number; y: number }> = {}; for (const beacon of beaconPositions) { const beaconData = beacons.find((b) => b.id === beacon.id); Iif (beaconData) { rssiMap[beacon.id] = beaconData.rssi; positionMap[beacon.id] = { x: beacon.location.x, y: beacon.location.y }; } } return { rssiMap, positionMap }; } /** * Calculates the position using the multilateration function. */ function calculatePosition( rssiMap: Record<number, number>, positionMap: Record<number, { x: number; y: number }>, beaconPositions: { id: number; location: { x: number; y: number }; floor_id: string }[] ): { x: number; y: number; floor_id: string } | null { try { // Construct the array of Beacon objects expected by trilaterateNLLS const beacons = beaconPositions.map((beacon) => ({ position: { x: beacon.location.x, y: beacon.location.y }, rssi: rssiMap[beacon.id], // Pass RSSI as required by trilaterateNLLS })); const result = trilaterateNLLS(beacons); Iif (!result) { console.error('[ERROR] Multilateration returned null.'); return null; } const { x, y } = result; const floor_id = String(beaconPositions[0].floor_id); // Access floor_id directly from beaconPositions return { x, y, floor_id }; } catch (error) { console.error('[ERROR] Multilateration failed with error:', error); return null; } } |