forked from kg-kartik/Ambulance-on-demand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
135 lines (111 loc) · 3.33 KB
/
server.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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const requestModel = require("././model/request");
const myMethods = require("./routes/ambulance");
const method = myMethods.method;
const otherMethod = myMethods.otherMethod;
const dotenv = require("dotenv");
const signupRoutes = require('./user/routes/singup_route');
const loginRoutes = require('./user/routes/login_route')
dotenv.config({
path : ".env"
})
app.use(bodyParser.json());
app.use(signupRoutes);
app.use(loginRoutes)
app.use((error, _, res, next) => {
if(error){
console.log(error)
const message = error.message;
const status = error.status_code;
res.status(status).json({
message : message
})
}
next()
})
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser : true,
useUnifiedTopology: true,
useCreateIndex : true})
.then(() => console.log('Database connected'))
.catch((err) => {
console.log(err);
})
app.use(cors());
const PORT = process.env.PORT || 4000;
var server = app.listen(PORT, () => {
console.log(`Server started on ${PORT}`);
});
//Initializing the server instance
var io =require("socket.io")(server);
//Connecting Users
io.on('connection', (socket) => {
console.log("A user just connected");
//Ambulance users joinning seperate rooms
socket.on('join' ,(data) => {
socket.join(data.displayName);
console.log(`User joined room ${data.displayName}`)
})
//Listening for help event from patient
//@User Component
socket.on("request-for-help",(data) => {
const requestTime = new Date();
const requestId = mongoose.Types.ObjectId();
const location = {
addressPatient : data.addressPatient,
coordinates : [
data.location.userLocation.longitude,
data.location.userLocation.latitude
]
}
const patientId = data.patientId;
const status = "waiting";
const request = new requestModel({
requestId,
requestTime,
location,
patientId,
status
})
//Saving request to the database
request.save().then((request) => {
console.log("ok");
}).catch((err) => {
console.log(err);
})
//Fetching nearest ambulance
console.log(location.coordinates[0]);
console.log(location.coordinates[1]);
const nearestAmbulance = otherMethod(location.coordinates[0],location.coordinates[1],5000);
console.log(nearestAmbulance,"nearest");
nearestAmbulance.then((result) => {
for(let i=0;i<result.length;i++)
{
//Emitting the event to the nearby ambulances
//@App component
console.log(result,"hello");
io.to(result[i].displayName).emit("request",data);
}
}).catch((err) => {
console.log(err);
})
});
//Listening for the event from ambulance
//@App Component
socket.on("request-accepted", (data) => {
ambulanceDetails = data;
//Emitting the event to the patient
//@User Component
io.emit("request-sent",ambulanceDetails);
})
})
//for url encoding
app.use(express.urlencoded({
extended : false
}));
app.use(bodyParser.json());
//Initializing Routes
app.use('/api/ambulance',method);