This commit is contained in:
@@ -6,101 +6,147 @@ const dataDir = path.join(process.cwd(), "src", "dal", "data");
|
||||
function readJSON(name) {
|
||||
try {
|
||||
const p = path.join(dataDir, name);
|
||||
|
||||
if (!fs.existsSync(p)) return null;
|
||||
const raw = fs.readFileSync(p, "utf8");
|
||||
return JSON.parse(raw);
|
||||
} catch (err) {
|
||||
return JSON.parse(fs.readFileSync(p, "utf8"));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function getVehicles() {
|
||||
return readJSON("vehicles.json") || [];
|
||||
const raw = readJSON("vehicles.json");
|
||||
|
||||
if (!Array.isArray(raw)) return [];
|
||||
return raw.map(v => {
|
||||
const loc = v.location || {};
|
||||
|
||||
return {
|
||||
vehicleId: v.vehicleId,
|
||||
routeNum: v.routeNum,
|
||||
routeName: v.routeName,
|
||||
destination: v.destination,
|
||||
bearing: v.bearing == null ? undefined : Number(v.bearing),
|
||||
speed: v.speed == null ? undefined : Number(v.speed),
|
||||
location: {
|
||||
latitude: loc.latitude == null ? undefined : Number(loc.latitude),
|
||||
longitude: loc.longitude == null ? undefined : Number(loc.longitude)
|
||||
}
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export function getVehicleById(id) {
|
||||
if (id == null) return null;
|
||||
const vehicles = getVehicles();
|
||||
|
||||
|
||||
return vehicles.find(v => String(v.vehicleId) === String(id)) || null;
|
||||
}
|
||||
|
||||
export function getRoutes() {
|
||||
const explicit = readJSON("routes.json");
|
||||
if (Array.isArray(explicit) && explicit.length) return explicit;
|
||||
const raw = readJSON("routes.json");
|
||||
|
||||
const vehicles = getVehicles();
|
||||
const map = new Map();
|
||||
vehicles.forEach(v => {
|
||||
const key = String(v.routeNum ?? "");
|
||||
if (!map.has(key)) {
|
||||
map.set(key, {
|
||||
routeId: key,
|
||||
routeName: v.routeName ?? null,
|
||||
startTime: null,
|
||||
endTime: null,
|
||||
trains: []
|
||||
});
|
||||
}
|
||||
|
||||
map.get(key).trains.push(v.vehicleId);
|
||||
|
||||
});
|
||||
|
||||
return Array.from(map.values());
|
||||
if (!Array.isArray(raw)) return [];
|
||||
return raw;
|
||||
}
|
||||
|
||||
export function getRouteById(routeId) {
|
||||
if (routeId == null) return null;
|
||||
const routes = getRoutes();
|
||||
|
||||
return routes.find(r => String(r.routeId) === String(routeId)) || null;
|
||||
|
||||
return routes.find(r => String(r.route_id) === String(routeId)) || null;
|
||||
}
|
||||
|
||||
export function getRoutePathsMap() {
|
||||
const raw = readJSON("routepaths.json");
|
||||
|
||||
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
||||
if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw;
|
||||
return {};
|
||||
}
|
||||
|
||||
export function getRoutePath(routeId) {
|
||||
if (routeId == null) return null;
|
||||
const map = getRoutePathsMap();
|
||||
const keys = Object.keys(map || {});
|
||||
const foundKey = keys.find(k => String(k) === String(routeId));
|
||||
|
||||
return foundKey ? map[foundKey] : null;
|
||||
return Object.prototype.hasOwnProperty.call(map, routeId) ? map[routeId] : null;
|
||||
}
|
||||
|
||||
export function getStationsRaw() {
|
||||
return readJSON("stations.json") || null;
|
||||
}
|
||||
|
||||
export function getStations() {
|
||||
return readJSON("stations.json") || [];
|
||||
const raw = getStationsRaw();
|
||||
|
||||
if (raw == null) return [];
|
||||
if (Array.isArray(raw)) {
|
||||
const isArrayOfMaps = raw.every(item => typeof item === "object" && !Array.isArray(item) && Object.values(item).every(v => Array.isArray(v)));
|
||||
if (isArrayOfMaps) return raw.flatMap(item => Object.values(item).flat());
|
||||
return raw;
|
||||
}
|
||||
if (typeof raw === "object") {
|
||||
return Object.values(raw).flat();
|
||||
}
|
||||
return [];
|
||||
|
||||
}
|
||||
|
||||
export function getStopsByRoute(routeId) {
|
||||
if (routeId == null) return [];
|
||||
const stations = getStations();
|
||||
const raw = getStationsRaw();
|
||||
|
||||
if (!Array.isArray(stations)) return [];
|
||||
return stations.filter(s => {
|
||||
const lines = s.lines ?? s.lines_arr ?? s.line_ids ?? null;
|
||||
if (!Array.isArray(lines)) return false;
|
||||
if (raw == null) return [];
|
||||
if (typeof raw === "object" && !Array.isArray(raw)) {
|
||||
return Array.isArray(raw[routeId]) ? raw[routeId] : [];
|
||||
}
|
||||
|
||||
return lines.map(String).includes(String(routeId));
|
||||
});
|
||||
if (Array.isArray(raw)) {
|
||||
const matchesFromArrayMaps = [];
|
||||
for (const item of raw) {
|
||||
if (typeof item === "object" && !Array.isArray(item) && Object.prototype.hasOwnProperty.call(item, routeId)) {
|
||||
const arr = item[routeId];
|
||||
if (Array.isArray(arr)) matchesFromArrayMaps.push(...arr);
|
||||
}
|
||||
}
|
||||
if (matchesFromArrayMaps.length) return matchesFromArrayMaps;
|
||||
|
||||
return raw.filter(s => Array.isArray(s.lines) && s.lines.map(String).includes(String(routeId)));
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
export function getStationById(stationId) {
|
||||
if (stationId == null) return null;
|
||||
const stations = getStations();
|
||||
export function getStationById(id) {
|
||||
if (id == null) return null;
|
||||
const raw = getStationsRaw();
|
||||
if (raw == null) return null;
|
||||
|
||||
if (!Array.isArray(stations)) return null;
|
||||
return stations.find(s => {
|
||||
if (s.stop_id && String(s.stop_id) === String(stationId)) return true;
|
||||
if (s.stationId && String(s.stationId) === String(stationId)) return true;
|
||||
if (s.id && String(s.id) === String(stationId)) return true;
|
||||
if (typeof raw === "object" && !Array.isArray(raw)) {
|
||||
for (const key of Object.keys(raw)) {
|
||||
const arr = raw[key];
|
||||
if (!Array.isArray(arr)) continue;
|
||||
const found = arr.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id));
|
||||
if (found) return found;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}) || null;
|
||||
if (Array.isArray(raw)) {
|
||||
const isArrayOfMaps = raw.every(item => typeof item === "object" && !Array.isArray(item) && Object.values(item).every(v => Array.isArray(v)));
|
||||
if (isArrayOfMaps) {
|
||||
for (const item of raw) {
|
||||
for (const key of Object.keys(item)) {
|
||||
const arr = item[key];
|
||||
const found = arr.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id));
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)) || null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user