Vehicle API
Work with vehicle data from simulations
Interactive Examples
Fetch Vehicles
No vehicles loaded yet. Click the button to fetch vehicles.
Collection Access
import { initializeSGERP } from 'sgerp-frontend-lib';
const api = initializeSGERP({
activeConnection: 'admin@localhost',
connections: {
'admin@localhost': {
id: 'admin@localhost',
environment: 'localhost',
user: 'admin',
password: 'your-password',
baseUrl: 'https://sgerp-stage.d.gcdev.swatrider.com',
},
},
});
const vehicles = api.collections.vehicle;
Fetching Vehicles
// Fetch first 20 vehicles
await vehicles.fetch({ limit: 20 });
// Fetch with offset
await vehicles.fetch({ limit: 20, offset: 20 });
// Fetch all vehicles (be careful with large datasets)
await vehicles.fetchAll();
Accessing Vehicle Data
// Get all vehicles
const allVehicles = vehicles.models;
// Get a specific vehicle by ID
const vehicle = vehicles.get(36);
// Check if a vehicle exists
const exists = vehicles.has(36);
// Get first/last vehicle
const first = vehicles.first();
const last = vehicles.last();
// Check collection state
const isEmpty = vehicles.isEmpty();
const count = vehicles.length;
const hasMore = vehicles.hasMore;
Vehicle Model
interface Vehicle {
id: number;
lat: number;
lon: number;
bearing: number;
simulation_id: number;
status: string;
speed: number;
capacity: Record<string, number>; // Common keys: passenger, wheelchair, g, cbcm
routing_engine_settings: {
routing_engine_name: string;
road_network: string;
speed: number;
};
current_route: {
points: Array<[number, number]>;
} | null;
registration_number: string | null;
project_id: number;
is_invalidated: boolean;
in_use: string;
// ... many more fields
}
Collection Utility Methods
Grouping
// Group vehicles by simulation
const bySimulation = vehicles.groupBy('simulation_id');
// Group by status
const byStatus = vehicles.groupBy('status');
// Group by project
const byProject = vehicles.groupBy('project_id');
Sorting
// Sort by speed
const bySpeed = vehicles.sortBy('speed');
// Sort by ID
const byId = vehicles.sortBy('id');
// Sort by custom function
const sorted = vehicles.sortBy(v => v.lat);
Filtering
// Filter with custom logic
const fastVehicles = vehicles.filter(v => v.speed > 10);
// Find specific vehicle
const found = vehicles.find(v => v.registration_number === 'ABC123');
// Where clause
const filtered = vehicles.where({
project_id: 1,
status: 'idle'
});
Plucking Values
// Get all simulation IDs
const simIds = vehicles.pluck('simulation_id');
// Get all speeds
const speeds = vehicles.pluck('speed');
// Get all statuses
const statuses = vehicles.pluck('status');
Pagination
// Fetch first page
await vehicles.fetch({ limit: 10 });
console.log(`Loaded ${vehicles.length} vehicles`);
console.log(`Has more: ${vehicles.hasMore}`);
// Load next page
if (vehicles.hasMore) {
await vehicles.fetchNext();
console.log(`Now have ${vehicles.length} vehicles`);
}
Error Handling
try {
await vehicles.fetch({ limit: 20 });
console.log('Vehicles loaded:', vehicles.length);
} catch (error) {
if (error instanceof APIError) {
console.error('API Error:', error.message);
console.error('Status:', error.status);
} else {
console.error('Error:', error);
}
}
Common Patterns
Get All Vehicles for a Simulation
// Fetch all vehicles filtered by simulation on the server
await vehicles.fetchAll({ simulation_id: 7 });
console.log(`Simulation has ${vehicles.length} vehicles`);
Find Available Vehicles
await vehicles.fetch({ limit: 100 });
const available = vehicles.filter(v => v.status === 'idle' && v.in_use === 'enabled' && !v.is_invalidated);
console.log(`${available.length} vehicles available`);
Calculate Average Speed
await vehicles.fetch({ limit: 100 });
const speeds = vehicles.pluck('speed');
const avgSpeed = speeds.reduce((a, b) => a + b, 0) / speeds.length;
console.log(`Average speed: ${avgSpeed.toFixed(2)}`);
Type Definitions
The full Vehicle type is exported from the library:
import type { Vehicle } from 'sgerp-frontend-lib';
const vehicle: Vehicle = {
id: 36,
lat: 1.279,
lon: 103.869,
bearing: 0,
simulation_id: 7,
status: 'idle',
speed: 6.18,
capacity: { units: 5, passenger: 40 },
// ... other fields
};