-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
298 lines (244 loc) · 7.94 KB
/
app.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import bodyParser from 'body-parser';
import cookies from "cookie-parser";
import cors from 'cors';
import dotEnv from 'dotenv';
import express from 'express';
import i18next from 'i18next';
import backend from 'i18next-fs-backend';
import middleware from 'i18next-http-middleware';
import swaggerJsDoc from 'swagger-jsdoc';
import swaggerUI from 'swagger-ui-express';
import options from './config/options.js';
import { success } from "./function/respond.js";
import assignRouter from './routes/assign-bus-routes/assign.js';
import dashboardRoutes from './routes/dashboard/dashboard.js';
import languageRoutes from './routes/language';
import loginRoute from './routes/logins';
import profileRoutes from './routes/profile/profilePic.js'
import permission from './routes/permissions/permissions.js';
import rolesRoute from './routes/roles/roles.js';
import accountRouter from './routes/users/accounts.js';
import busesRoute from './routes/buses/busesRoute.js';
import usersRoutes from './routes/users/users.js';
import routesRoute from './routes/routes/routesRoute'
import activeBusSimulationRoute from './routes/busSimulation/busSimulationRoute'
import http from "http";
import socketIo from "socket.io"
import client from './config/client.js';
import initBusStatus from './controllers/schema/busStatusSchema.js';
/* ========== setting up dotenv ============= */
dotEnv.config()
const app = express();
/* c8 ignore next 1 */
const PORT = process.env.PORT || 5000;
const specs = swaggerJsDoc(options);
const socketServer = http.Server(app);
const io = socketIo(socketServer,{
cors: {
origin: '*',
methods: ['GET', 'POST'],
transports: ['websocket', 'polling'],
credentials: true,
},
allowEIO3: true,
});
app.use(cookies());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(middleware.handle(i18next))
app.use(express.json())
/* ========== setting up multi language configuration ============= */
i18next
.use(backend)
.use(middleware.LanguageDetector)
.init({
fallbackLng: 'en',
backend: {
loadPath: './locales/{{lng}}/translation.json'
}
})
const newClient = new Set();
/* c8 ignore next 162*/
io.on('connection', function (socket) {
/* redis client */
newClient.add(socket.id);
console.log(socket.id);
socket.on('startBus', async (data) => {
const busStatus = await initBusStatus();
socket.emit('busStarted', {
bus: data
});
});
socket.on('stopBus', async (data) => {
const busStatus = await initBusStatus();
let busInfo = await busStatus.fetch(data.id);
if(busInfo != null){
const currentLocation = {
latitude: data.location.latitude,
longitude: data.location.longitude,
};
busInfo.currentLocation = JSON.stringify(currentLocation) ;
busInfo.passengers = 0;
busInfo.status = "stopped";
await busStatus.save(busInfo);
}
socket.emit('busStoped', {
bus: busInfo
});
const allBusInfo = await busStatus.search().return.all();
io.emit('location_update', {
bus: allBusInfo
});
});
socket.on('alight', async (data) => {
const busStatus = await initBusStatus();
let busInfo = await busStatus.fetch(data.id);
if (busInfo) {
const currentLocation = {
latitude: data.location.latitude,
longitude: data.location.longitude,
};
busInfo.currentLocation = JSON.stringify(currentLocation) ;
busInfo.passengers = data.passengers;
busInfo.status = "on board";
await busStatus.save(busInfo);
}
socket.emit('alighted',{
bus: busInfo
});
const allBusInfo = await busStatus.search().return.all();
io.emit('location_update', {
bus: allBusInfo
});
});
socket.on('location_update', async (data)=>{
const busStatus = await initBusStatus();
let busInfo = await busStatus.fetch(data.id);
if (busInfo) {
const currentLocation = {
latitude: data.location.latitude,
longitude: data.location.longitude,
};
busInfo.currentLocation = JSON.stringify(currentLocation) ;
await busStatus.save(busInfo);
}
const allBusInfo = await busStatus.search().return.all();
io.emit('location_update', {
bus: allBusInfo
});
})
socket.on('passengers_update', async (data)=>{
const busStatus = await initBusStatus();
let busInfo = await busStatus.fetch(data.id);
if (busInfo) {
const currentLocation = {
latitude: data.location.latitude,
longitude: data.location.longitude,
};
busInfo.passengers = Number(data.passengers) ;
await busStatus.save(busInfo);
}
const allBusInfo = await busStatus.search().return.all();
socket.emit('passengers_update', {
bus: allBusInfo
});
io.emit('location_update', {
bus: allBusInfo
});
})
socket.on('get_passengers', async (data) => {
const busStatus = await initBusStatus();
let busInfo = await busStatus.fetch(data.id);
socket.emit('receive_passengers', {
bus: busInfo
});
});
socket.on('get_current', async (data) => {
const busStatus = await initBusStatus();
let busInfo = await busStatus.fetch(data.id);
socket.emit('receive_current_passengers', {
bus: busInfo
});
});
socket.on('locate', async (data) => {
const busStatus = await initBusStatus();
const allBusInfo = await busStatus.search().return.all();
socket.emit('located', {
buses: allBusInfo
});
});
socket.on('finish', async (data) => {
const busStatus = await initBusStatus();
socket.emit('location_update', {
id: "all"
});
});
socket.on("alighting", async (data) =>{
const busStatus = await initBusStatus();
const allBusInfo = await busStatus.search().return.all();
io.emit('location_update', {
bus: allBusInfo
});
let busInfo = await busStatus.fetch(data.id);
busInfo.status = "Alighting";
busStatus.save(busInfo)
socket.emit('alighting', {
bus: busInfo
});
})
socket.on("killAlighting", async (data) =>{
const busStatus = await initBusStatus();
const allBusInfo = await busStatus.search().return.all();
let busInfo = await busStatus.fetch(data.id);
busInfo.status = "on board";
busStatus.save(busInfo)
socket.emit('killAlighting', {
bus: busInfo
});
io.emit('location_update', {
bus: allBusInfo
});
})
});
/* ========== Start:: Root directory ========= */
app.get('/', (req, res) => {
return success(res,200,null,"welcome", req);
});
/* ============ End:: Root directory ========= */
/* ========== Start:: Route for active buses ========= */
app.use('/api/v1/simulation', activeBusSimulationRoute);
/* ============== End:: Route for active buses ========= */
/* ========== Start:: Route api url ========= */
app.use('/api/v1/routes', routesRoute);
/* ============== End:: Route api ========= */
/* ========== Start:: Admin api url ========= */
app.use('/api/v1/dashboard', dashboardRoutes);
/* ============== End:: Admin api ========= */
/* ========== Start:: User api url ========= */
app.use('/api/v1/users', usersRoutes);
app.use('/api/v1/users/login', loginRoute);
/* ============== Start:: User api ========= */
/*======= START:: Update profile api ======= */
app.use('/api/v1/profile', profileRoutes);
/*======= START:: Update profile api ======= */
/* ========== Start:: role api url ========= */
app.use('/api/v1/roles', rolesRoute);
/* ============== Start:: role api ========= */
/* ========== Start:: permissions api url ========= */
app.use('/api/v1/permissions', permission);
/* ============== Start:: permissions api ========= */
/* ========== Start:: Api documantation version one ============ */
app.use('/api/v1/doc', swaggerUI.serve, swaggerUI.setup(specs));
/* ========== Start:: buses api url ========= */
app.use('/api/v1/buses', busesRoute);
/* ============== End:: buses api ========= */
/* ========= Start:: forget password ======== */
app.use('/api/v1/accounts', accountRouter);
/* ========= End:: forget password ======== */
app.use('/api/v1/lng', languageRoutes);
app.use("/api/v1/assign", assignRouter)
socketServer.listen(PORT, () => {
app.emit("Started")
console.log(`app is listening on port ${PORT}`);
})
export { app };