This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (157 loc) · 4.86 KB
/
index.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
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import { Server } from "socket.io";
import dotenv from "dotenv";
import bodyParser from "express";
import random from "./middleware/RandomNumber.js";
import userRoutes from "./routes/users.js";
import roomRoutes from "./routes/rooms.js";
import billRoutes from "./routes/bills.js";
import medicineRoutes from "./routes/medicines.js";
import prescriptionRoutes from "./routes/prescriptions.js";
import diseaseRoutes from "./routes/diseases.js";
import doctorRoutes from "./routes/doctors.js";
import imageRoutes from "./routes/images.js";
import scheduleRoutes from "./routes/schedules.js";
import medicalCheckupRoutes from "./routes/medical_checkups.js";
import customerRoutes from "./routes/customers.js";
import fileRoutes from "./routes/file.js";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 80;
const dbUri = process.env.MONGODB_URI;
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());
app.use("/users", userRoutes);
app.use("/rooms", roomRoutes);
app.use("/bills", billRoutes);
app.use("/customers", customerRoutes);
app.use("/medicalCheckups", medicalCheckupRoutes);
app.use("/doctors", doctorRoutes);
app.use("/medicines", medicineRoutes);
app.use("/images", imageRoutes);
app.use("/prescriptions", prescriptionRoutes);
app.use("/schedules", scheduleRoutes);
app.use("/diseases", diseaseRoutes);
app.use("/files", fileRoutes);
mongoose
.connect(dbUri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(
() => {}
// app.listen(PORT, () =>
// console.log(`Server is running on port: http://localhost:${PORT}`)
// )
)
.catch((error) => console.log(`${error} cannot connect`));
mongoose.set("useFindAndModify", false);
const server = app.listen(PORT, () =>
console.log(`Server is running on port: http://localhost:${PORT}`)
);
const io = new Server(server, {
cors: {
origins: ["http://localhost:4200"],
},
});
const conditions = {};
const messages = {};
const commands = {};
let isCo2 = true;
let isThermometer = true;
let isAneroid = true;
let isStethoscope = true;
let co2Switch = random(1, 100);
let thermometerSwitch = random(10, 90);
let aneroidSwitch = random(70, 200);
let stethoscopeSwitch = random(0, 210);
io.on("connection", (socket) => {
let previousId;
// to do: set interval these values
const safeJoin = (currentId) => {
socket.leave(previousId);
socket.join(currentId, () =>
console.log(`Socket ${socket.id} joined room ${currentId}`)
);
previousId = currentId;
};
socket.on("getCondition", (customerID) => {
safeJoin(customerID);
conditions[customerID] = {
id: customerID,
condition: {
bloodPressure: aneroidSwitch,
respiratoryRate: co2Switch,
bodyTemperature: thermometerSwitch,
heartRate: stethoscopeSwitch,
},
};
io.to(customerID).emit("condition", conditions[customerID]);
});
socket.on("newCondition", (condition) => {
conditions[condition.id] = condition;
safeJoin(condition.id);
io.emit("conditions", Object.keys(conditions));
socket.emit("condition", condition);
});
socket.on("co2Switch", (power) => {
isCo2 = power;
});
const co2Listener = () => {
if (isCo2 === true) {
co2Switch = random(1, 100);
} else {
co2Switch = 0;
}
};
socket.on("co2", co2Listener);
socket.on("thermometerSwitch", (power) => {
isThermometer = power;
});
const thermometerListener = () => {
if (isThermometer === true) {
thermometerSwitch = random(10, 90);
} else {
thermometerSwitch = 0;
}
};
socket.on("thermometer", thermometerListener);
socket.on("aneroidSwitch", (power) => {
isAneroid = power;
});
const aneroidListener = () => {
if (isAneroid === true) {
aneroidSwitch = random(70, 200);
} else {
aneroidSwitch = 0;
}
};
socket.on("aneroid", aneroidListener);
socket.on("stethoscopeSwitch", (power) => {
isStethoscope = power;
});
const stethoscopeListener = () => {
if (isStethoscope === true) {
stethoscopeSwitch = random(0, 210);
} else {
stethoscopeSwitch = 0;
}
};
socket.on("stethoscope", stethoscopeListener);
socket.on("sendMessage", (message) => {
console.log(message);
io.to(previousId).emit("message", message);
});
socket.on("call", (command) => {
console.log(command);
io.to(previousId).emit("command", command);
});
io.emit("conditions", Object.keys(conditions));
// chat history of system
io.emit("messages", Object.keys(messages));
console.log(`Socket ${socket.id} has connected`);
});
app.use(express.static("./client/dist/ng-health-care-demo"));
app.get("/*", function (req, res) {
res.sendFile("index.html", { root: "./client/dist/ng-health-care-demo" });
});