-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (136 loc) · 4.58 KB
/
index.js
File metadata and controls
144 lines (136 loc) · 4.58 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
const yargs = require('yargs');
const ltaApi = require('./lta-api');
yargs
.command({
command: 'bus-arrival',
describe: 'Get bus arrival information',
builder: {
busStopCode: {
describe: 'Bus Stop Code',
demandOption: true,
type: 'string',
},
serviceNo: {
describe: 'Service Number',
demandOption: true,
type: 'string',
},
},
handler: async (argv) => {
try {
const busArrival = await ltaApi.getBusArrival(argv.busStopCode, argv.serviceNo);
const nextBus = busArrival.Services[0].NextBus;
const nextBus2 = busArrival.Services[0].NextBus2;
console.log(`Bus Service No: ${argv.serviceNo}`);
console.log(`Bus Stop Code: ${argv.busStopCode}`);
if (nextBus.EstimatedArrival) {
const arrivalTime = new Date(nextBus.EstimatedArrival);
console.log(`Next Bus Arrival: ${arrivalTime.toLocaleString()}`);
} else {
console.log('Next Bus Arrival: Not available');
}
if (nextBus2.EstimatedArrival) {
const arrivalTime2 = new Date(nextBus2.EstimatedArrival);
console.log(`Next Bus 2 Arrival: ${arrivalTime2.toLocaleString()}`);
} else {
console.log('Next Bus 2 Arrival: Not available');
}
} catch (error) {
console.error('Error fetching bus arrival:', error);
}
},
})
.command({
command: 'parking-availability',
describe: 'Get parking availability information',
handler: async () => {
try {
const parkingAvailability = await ltaApi.getParkingAvailability();
console.log('Parking Availability:', parkingAvailability);
} catch (error) {
console.error('Error fetching parking availability:', error);
}
},
})
.command({
command: 'bus-travel-time',
describe: 'Estimate travel time between two bus stops',
builder: {
fromBusStopCode: {
describe: 'From Bus Stop Code',
demandOption: true,
type: 'string',
},
toBusStopCode: {
describe: 'To Bus Stop Code',
demandOption: true,
type: 'string',
},
},
handler: async (argv) => {
try {
const busRoutes = await ltaApi.getBusRoutes();
const fromBusStopRoutes = busRoutes.filter(route => route.BusStopCode === argv.fromBusStopCode);
const toBusStopRoutes = busRoutes.filter(route => route.BusStopCode === argv.toBusStopCode);
const commonServiceRoutes = fromBusStopRoutes.filter(fromRoute =>
toBusStopRoutes.some(toRoute => fromRoute.ServiceNo === toRoute.ServiceNo)
);
if (commonServiceRoutes.length === 0) {
console.log('No direct bus service found between the two bus stops.');
return;
}
const travelTimes = commonServiceRoutes.map(route => {
const toRoute = toBusStopRoutes.find(r => r.ServiceNo === route.ServiceNo);
const distance = Math.abs(route.Distance - toRoute.Distance);
const estimatedTime = distance / 0.5; // Assuming average bus speed of 0.5 km/min
return {
serviceNo: route.ServiceNo,
distance: distance,
estimatedTime: estimatedTime,
};
});
console.log(`Travel time between bus stops ${argv.fromBusStopCode} and ${argv.toBusStopCode}:`);
travelTimes.forEach(time => {
console.log(`Bus Service No: ${time.serviceNo}`);
console.log(`Estimated Time: ${time.estimatedTime.toFixed(2)} minutes`);
console.log('');
});
} catch (error) {
console.error('Error estimating travel time between bus stops:', error);
}
},
})
.command({
command: 'nearest-bus-stops',
describe: 'Find the nearest bus stops based on latitude and longitude',
builder: {
latitude: {
describe: 'Latitude',
demandOption: true,
type: 'number',
},
longitude: {
describe: 'Longitude',
demandOption: true,
type: 'number',
},
limit: {
describe: 'Number of nearest bus stops to display',
type: 'number',
default: 5,
},
},
handler: async (argv) => {
try {
const nearestBusStops = await ltaApi.getNearestBusStops(argv.latitude, argv.longitude, argv.limit);
console.log('Nearest Bus Stops:', nearestBusStops);
} catch (error) {
console.error('Error fetching nearest bus stops:', error);
}
},
})
.demandCommand(1, 'You must provide a valid command.')
.help()
.alias('h', 'help')
.strict()
.parse();