-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
280 lines (216 loc) · 6.43 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
// The requirements imports
const cron = require('node-cron');
const client = require('twilio')('ACa89ea8891f0bd66f6a58377cc076a804','7c9501e04cb05e31092058623737ceed');
const express = require('express');
const app = express();
const fs = require('fs');
const multer = require('multer');
const { stringify } = require('querystring');
const { createWorker } = require('tesseract.js');
const mongoose = require('mongoose');
app.set('view engine', 'ejs');
app.use('/public', express.static('D:/MediRemind/public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// globale variables;
let theText="";
let str ="";
let lines=[];
// The Storage Declaration
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './uploads');
},
filename: (req, file, cb) => {
cb(null, file.originalname);
}
});
const upload = multer({ storage: storage }).single('avatar');
mongoose.connect('mongodb://127.0.0.1:27017/PatientDB',{
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(()=>
{
console.log('Connected to the DB');
})
.catch((error)=>
{
console.log("error occured",error);
});
const patientSchema = new mongoose.Schema({
Name: String,
Password: String,
Phone: Number,
});
const Patient = new mongoose.model("Patient", patientSchema);
const medicineSchema = new mongoose.Schema({
Name: String,
Medicines: String,
});
const Medicine = new mongoose.model("Medicine", medicineSchema);
app.post('/upload/:name', (req, res) => {
upload(req, res, (err) => {
console.log(req.file);
fs.readFile(`./uploads/${req.file.originalname}`, async (err, data) => {
if (err) return console.log('This is your error', err);
const worker = await createWorker(); // Wait for worker to be created
await worker.load();
await worker.initialize('eng');
try {
const { data: { text } } = await worker.recognize(data);
theText=text;
Alarams(theText);
// for (let i = 0; i < theText.length; i++) {
// const element = theText[i];
// console.log(element);
//
if(theFlag)
{
res.render("index2",{
Name: req.params.name,
Morn:morMessage,
After:afMessage,
Even:niMessage
});
}
} finally {
await worker.terminate();
}
});
});
});
let theFlag=false;
let morMessage="";
let afMessage="";
let niMessage="";
function Alarams(theText) {
var totalLines = (theText.match(/\n/g) || '').length;
console.log(totalLines);
str=theText.replace(/-/gi,"");
let s ="";
let i=1;
console.log("the lines in information extracted from the Image \n");
for(let j=0;j<str.length;j++)
{
if(str[j]!=str[j].match(/\n/g))
{
s+=str[j];
}
else
{
console.log("the line",i,":",s,"\n");
i++;
lines.push(s);
s="";
}
morMessage="";
afMessage="";
niMessage="";
console.log("the medicines that need be reminded to the user \n");
for(let i=0;i<lines.length;i++)
{
if(lines[i].match(/morning/gi) )
{
morMessage=lines[i].replace(/morning/gi,"");
console.log("the morning medicine :",morMessage,"\n");
}
if(lines[i].match(/afternoon/gi) )
{
afMessage=lines[i].replace(/afternoon/gi,"");
console.log("the afternoon medicine :",afMessage,"\n");
}
if(lines[i].match(/night/gi))
{
niMessage=lines[i].replace(/night/gi,"");
console.log("the nigth medicine :",niMessage,"\n");
}
}
}
theFlag=true;
}
function sendNotification(_Meds)
{
let mes = String(_Meds);
client.messages.create({
body:`Dont forget to take your ${mes}`,
to:'+918897231558',
from:'++447723563766'
})
.then(result => console.log("sent the message"+result))
.catch(error => console.log(error));
console.log(_Meds);
}
cron.schedule('31 10 * * *', () => {
let me = " Tafenoquine,(ArakodaTM) and Zestril";
sendNotification(me);
});
cron.schedule('59 11 * * *', () => {
sendNotification(afMessage);
});
cron.schedule('44 19 * * *', () => {
sendNotification(niMessage);
});
app.get("/",(req,res)=>{
res.render("home");
});
app.get("/register",(req,res)=>{
res.render("register");
})
app.get("/login",(req,res)=>{
res.render("login");
})
app.get("/MediRemind/:name",(req,res)=>{
res.render("index",{
Name:req.params.name,
});
});
app.post("/register",(req,res)=>{
const newUser = new Patient({
Name:req.body.name,
Password: req.body.password,
Number:req.body.number
});
newUser.save()
.then((result)=>
{
console.log("entered the new Patient details into the DB");
res.redirect(`/MediRemind/${result.Name}`)
})
.catch((error)=>
{
console.log("error occurred while entering the details",error);
});
})
app.post("/login",(req,res)=>{
const Name = req.body.username;
const Password = req.body.password;
Patient.findOne({Name: Name})
.then((result)=>
{
if(result)
{
if(result.Password === Password)
{
console.log("found",result);
res.redirect(`/MediRemind/${result.Name}`);
}
else
{
console.log("username or password",username,password,"do not exist");
res.render("login");
}
}
})
.catch((error)=>
{
console.error("error while searching for the credentials",error);
});
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// One last step - save your recovery code
// If you lose your phone, or don't have access to your verification device, this code is your failsafe to access your account.
// HQQGBTH1JL1SAL6KMZ69RVDY