-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_fleet.js
87 lines (77 loc) · 3.02 KB
/
app_fleet.js
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
const express = require('express');
const { Pool } = require('pg');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
const pool = new Pool({
user: "",
host: "",
database: "",
password: "",
port: 26257,
ssl: {
rejectUnauthorized: false,
}
});
// Render files from the 'views' directory
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index');
});
// Route to fetch vehicle data
app.get('/vehicles', async (req, res) => {
try {
const result = await pool.query("SELECT v.VehicleID, v.Latitude, v.Longitude, f.FleetName, to_char(ms.NextDue, 'MM/DD/YYYY') AS NextDueDate FROM movr_demo.public.Vehicle v JOIN Fleet f ON v.FleetID = f.FleetID LEFT JOIN MaintenanceSchedule ms ON v.VehicleID = ms.VehicleID;");
const vehicleData = result.rows;
res.json(vehicleData);
} catch (error) {
console.error('Error fetching vehicle data:', error);
res.status(500).json({ error: 'Error fetching vehicle data' });
}
});
// Detailed dashboard route
app.get('/vehicle/:vehicleId/dashboard', async (req, res) => {
const vehicleId = req.params.vehicleId;
try {
// Query database to get detailed dashboard data based on vehicleId
const result = await pool.query("SELECT * FROM movr_demo.public.Vehicle WHERE VehicleID = '" + vehicleId + "'");
const vehicleData = result.rows[0];
// Render the detailed dashboard page with vehicle data
res.render('dashboard', { vehicleId });
} catch (error) {
console.error('Error fetching vehicle data:', error);
res.status(500).send('Error fetching vehicle data');
}
});
// Diagnostic data route
app.get('/vehicle/:vehicleId/diagnostic', async (req, res) => {
const vehicleId = req.params.vehicleId;
try {
// Query database to get diagnostic data based on vehicleId
const result = await pool.query("SELECT * FROM movr_demo.public.Diagnostic WHERE VehicleID = '" + vehicleId + "' ORDER BY timestamp ASC");
const diagnosticData = result.rows;
// Return the diagnostic data as JSON
res.json(diagnosticData);
} catch (error) {
console.error('Error fetching diagnostic data:', error);
res.status(500).json({ error: 'Error fetching diagnostic data' });
}
});
// Location history route
app.get('/vehicle/:vehicleId/locationhistory', async (req, res) => {
const vehicleId = req.params.vehicleId;
try {
// Query database to get location history data based on vehicleId
const result = await pool.query("SELECT * FROM movr_demo.public.LocationHistory WHERE VehicleID = '" + vehicleId + "'");
const locationHistoryData = result.rows;
// Return the location history data as JSON
res.json(locationHistoryData);
} catch (error) {
console.error('Error fetching location history data:', error);
res.status(500).json({ error: 'Error fetching location history data' });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});