const axios = require('axios'); const polylineLib = require('@mapbox/polyline'); require('dotenv').config({ path: '.env' }); const { _distKm } = require('./src/travel.js'); // Not exported, I'll copy the logic function distKm(lat1, lng1, lat2, lng2) { const dLat = (lat2 - lat1) * Math.PI / 180; const dLng = (lng2 - lng1) * Math.PI / 180; const a = Math.sin(dLat/2)**2 + Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) * Math.sin(dLng/2)**2; return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); } async function bulkRateTest() { const apiKey = process.env.API_MAPS; const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France"; const destination = "Toulouse, France"; const routesUrl = 'https://routes.googleapis.com/directions/v2:computeRoutes'; const resToll = await axios.post(routesUrl, { travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE', origin: { address: origin }, destination: { address: destination }, }, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.polyline.encodedPolyline' } }); const poly = resToll.data.routes[0].polyline.encodedPolyline; const polylineCoords = polylineLib.decode(poly, 5); const fs = require('fs'); const path = require('path'); const csvPath = path.join(__dirname, 'travel', 'gares_peage_export.csv'); const rawCsv = fs.readFileSync(csvPath, 'utf8'); const stations = []; const lines = rawCsv.split('\n'); for (let i = 1; i < lines.length; i++) { const l = lines[i].trim(); if (!l) continue; const parts = l.split(','); if (parts.length >= 4) { const idStr = String(parts[0]).padStart(5, '0'); stations.push({ id: idStr, operatorId: idStr.substring(0, 2), tollId: idStr.substring(2, 5), name: parts[1], lat: parseFloat(parts[2]), lon: parseFloat(parts[3]), }); } } const candidates = []; stations.forEach(s => { let minDist = Infinity; let minIndex = -1; for (let i = 0; i < polylineCoords.length; i++) { const d = distKm(s.lat, s.lon, polylineCoords[i][0], polylineCoords[i][1]); if (d < minDist) { minDist = d; minIndex = i; } } if (minDist < 2) { candidates.push({ ...s, polyIndex: minIndex }); } }); candidates.sort((a, b) => a.polyIndex - b.polyIndex); const passages = candidates.map(c => ({ toll: { operatorId: c.operatorId, tollId: c.tollId }, passageDate: new Date().toISOString() })); try { console.log(`Sending ${passages.length} passages to Ulys...`); const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', { vehicleCategory: "2", paymentOption: 2, tollPassages: passages }); console.log(JSON.stringify(res.data, null, 2)); } catch(e) { console.log(e.response ? e.response.data : e.message); } } bulkRateTest();