Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
449 lines (386 loc) · 18 KB
/
Copy pathapp.py
File metadata and controls
449 lines (386 loc) · 18 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import os
from datetime import datetime
import json
import time
import smtplib
from email.mime.text import MIMEText
from dotenv import load_dotenv
from flask import Flask, render_template, redirect, url_for, request, jsonify, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from flask_cors import CORS
import google.generativeai as genai
# ==================== LOAD ENV VARIABLES ====================
load_dotenv()
# ==================== CONFIGURATION ====================
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'default-secret-key')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database', 'medical_robot.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# --- EMAIL CONFIGURATION (For SOS Alerts) ---
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
SENDER_EMAIL = os.getenv('MAIL_USERNAME', 'your-project-email@gmail.com')
SENDER_PASSWORD = os.getenv('MAIL_PASSWORD', 'your-app-password')
CAREGIVER_EMAIL = "caregiver-email@example.com"
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
CORS(app)
# 🔴 GET API KEY
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
# AI Setup
AI_AVAILABLE = False
model = None
if GOOGLE_API_KEY and "PASTE_YOUR_KEY_HERE" not in GOOGLE_API_KEY:
try:
genai.configure(api_key=GOOGLE_API_KEY)
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
model = genai.GenerativeModel(m.name)
AI_AVAILABLE = True
print(f"✅ AI Connected: {m.name}")
break
except Exception as e:
print(f"❌ AI Connection Failed: {e}")
else:
print("⚠️ Google API Key not found or invalid in .env file.")
# ==================== DATABASE MODELS ====================
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), unique=True, nullable=False)
password = db.Column(db.String(150), nullable=False)
patients = db.relationship('Patient', backref='caregiver', lazy=True)
class Patient(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
name = db.Column(db.String(100), nullable=False)
medications = db.relationship('Medication', backref='patient', lazy=True)
class Medication(db.Model):
id = db.Column(db.Integer, primary_key=True)
patient_id = db.Column(db.Integer, db.ForeignKey('patient.id'), nullable=False)
name = db.Column(db.String(100), nullable=False)
dosage = db.Column(db.String(50))
stock = db.Column(db.Integer, default=30)
# === NEW COLUMN: MAX STOCK (For the "60/60" logic) ===
max_stock = db.Column(db.Integer, default=30)
schedule_time = db.Column(db.String(5))
instructions = db.Column(db.String(100))
frequency = db.Column(db.String(20), default="Daily")
days = db.Column(db.String(50), default="All")
last_taken = db.Column(db.String(20))
class ActivityLog(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
action = db.Column(db.String(100), nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.now)
details = db.Column(db.String(200))
class UserMap(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
grid_data = db.Column(db.Text, nullable=False)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# ==================== HELPER FUNCTIONS ====================
def get_user_context():
if not current_user.is_authenticated: return "No user logged in."
context = []
for patient in current_user.patients:
p_info = f"PATIENT: {patient.name}\nMEDS:"
for med in patient.medications:
p_info += f"\n - {med.name} ({med.dosage}): Stock {med.stock}, Due {med.schedule_time}, Note: {med.instructions}"
context.append(p_info)
return "\n\n".join(context)
def send_emergency_email(user_name, details):
"""Sends a real email alert using SMTP"""
try:
if SENDER_EMAIL == 'your-project-email@gmail.com':
print(">> EMAIL SIMULATION: Config not set. Skipping actual send.")
return False
msg = MIMEText(f"URGENT ALERT: {user_name} has triggered an emergency.\n\nDetails: {details}\nTime: {datetime.now()}")
msg['Subject'] = f"🚨 SOS ALERT - {user_name}"
msg['From'] = SENDER_EMAIL
msg['To'] = CAREGIVER_EMAIL
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
server.sendmail(SENDER_EMAIL, [CAREGIVER_EMAIL], msg.as_string())
print(">> EMAIL SENT SUCCESSFULLY")
return True
except Exception as e:
print(f"!! Email Failed: {e}")
return False
# ==================== ROUTES ====================
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
if not user:
flash('Account not found.', 'error')
return redirect(url_for('register'))
if not check_password_hash(user.password, password):
flash('Incorrect password.', 'error')
return redirect(url_for('login'))
login_user(user)
if not user.patients: return redirect(url_for('setup'))
return redirect(url_for('index'))
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if User.query.filter_by(username=username).first():
flash('Username exists'); return redirect(url_for('register'))
new_user = User(username=username, password=generate_password_hash(password, method='pbkdf2:sha256'))
db.session.add(new_user)
db.session.commit()
login_user(new_user)
return redirect(url_for('setup'))
return render_template('register.html')
@app.route('/logout')
@login_required
def logout(): logout_user(); return redirect(url_for('login'))
@app.route('/setup', methods=['GET', 'POST'])
@login_required
def setup():
if request.method == 'POST':
data = request.json
Patient.query.filter_by(user_id=current_user.id).delete()
for p_data in data.get('patients', []):
new_patient = Patient(name=p_data['name'], user_id=current_user.id)
db.session.add(new_patient)
db.session.commit()
for m_data in p_data['meds']:
new_med = Medication(
patient_id=new_patient.id, name=m_data['name'], dosage=m_data['dosage'],
# UPDATED: Initialize max_stock with initial stock
stock=int(m_data['stock']), max_stock=int(m_data['stock']),
schedule_time=m_data['time'], instructions=m_data['instructions'],
frequency=m_data['frequency'], days=m_data['selected_days']
)
db.session.add(new_med)
db.session.commit()
return jsonify({'success': True, 'redirect': url_for('index')})
return render_template('setup.html')
@app.route('/')
def landing():
if current_user.is_authenticated: return redirect(url_for('index'))
return render_template('landing.html')
@app.route('/dashboard')
@login_required
def index(): return render_template('index.html', user=current_user)
@app.route('/logs')
@login_required
def logs_page(): return render_template('logs.html')
@app.route('/map')
@login_required
def map_page(): return render_template('map.html')
@app.route('/history')
@login_required
def history_page():
logs = ActivityLog.query.filter_by(user_id=current_user.id).order_by(ActivityLog.timestamp.desc()).all()
return render_template('history.html', logs=logs)
# ==================== ANALYTICS API ====================
@app.route('/api/stats')
@login_required
def get_stats():
"""Returns adherence data for charts"""
today_str = datetime.now().strftime("%Y-%m-%d")
total_meds = 0
taken_today = 0
for patient in current_user.patients:
for med in patient.medications:
total_meds += 1
if med.last_taken == today_str:
taken_today += 1
missed = total_meds - taken_today
score = int((taken_today / total_meds * 100) if total_meds > 0 else 0)
return jsonify({
'total': total_meds,
'taken': taken_today,
'missed': missed,
'score': score
})
# ==================== MAP API ====================
@app.route('/api/map/save', methods=['POST'])
@login_required
def save_map():
data = request.json
user_map = UserMap.query.filter_by(user_id=current_user.id).first()
if user_map: user_map.grid_data = json.dumps(data.get('grid'))
else: db.session.add(UserMap(user_id=current_user.id, grid_data=json.dumps(data.get('grid'))))
db.session.commit()
return jsonify({'success': True, 'message': 'Map Layout Saved'})
@app.route('/api/map/load')
@login_required
def load_map():
user_map = UserMap.query.filter_by(user_id=current_user.id).first()
if user_map: return jsonify({'success': True, 'grid': json.loads(user_map.grid_data)})
else: return jsonify({'success': False, 'message': 'No saved map found'})
# ==================== SCHEDULE & INVENTORY API (FIXED) ====================
@app.route('/api/schedule')
@login_required
def get_schedule():
schedule = []
now_time = datetime.now().strftime("%H:%M")
today_str = datetime.now().strftime("%Y-%m-%d")
today_day = datetime.now().strftime("%a")
for patient in current_user.patients:
for med in patient.medications:
is_today = (med.frequency == "Daily") or (med.days and today_day in med.days)
if is_today:
is_done = (med.last_taken == today_str)
status = 'completed' if is_done else ('upcoming' if med.schedule_time > now_time else 'pending')
schedule.append({
"id": med.id, "day": "Today", "time": med.schedule_time,
"task": f"{med.name}", "patient": patient.name,
"type": "medicine", "status": status, "is_done": is_done
})
schedule.sort(key=lambda x: x['time'])
return jsonify(schedule)
@app.route('/api/inventory')
@login_required
def get_inventory():
inventory = []
for patient in current_user.patients:
for med in patient.medications:
# === NEW LOGIC: CHECK FOR TODAY AND TOMORROW ===
# We assume "Today + Tomorrow" means we need at least 2 doses available.
status = 'ok'
if med.stock < 2:
status = 'low' # Logic: Not enough for today + tomorrow
elif med.stock < 5:
status = 'low' # Standard low warning
# Ensure we don't divide by zero
total = med.max_stock if med.max_stock > 0 else 30
inventory.append({
"name": f"{med.name} ({patient.name})",
"dosage": med.dosage,
"stock": med.stock,
"total": total, # FIXED: Uses real max_stock
"unit": "tablets",
"status": status,
"instructions": med.instructions
})
return jsonify(inventory)
# ==================== VOICE AI API ====================
@app.route('/api/voice/process', methods=['POST'])
@login_required
def process_voice():
user_text = request.json.get('text', '')
if not AI_AVAILABLE: return jsonify({'success': True, 'message': "AI Missing", 'action': 'NONE'})
prompt = f"""You are MediBot. SYSTEM DATA: {get_user_context()}. USER COMMAND: "{user_text}".
Output JSON ONLY: {{"response": "text", "action": "NONE" or "DISPENSE"}}"""
try:
res = model.generate_content(prompt)
parsed = json.loads(res.text.replace('```json','').replace('```','').strip())
return jsonify({'success': True, 'message': parsed['response'], 'action': parsed.get('action')})
except: return jsonify({'success': False, 'error': "AI Error"})
# ==================== TASK OPERATIONS (FIXED) ====================
@app.route('/api/task/add', methods=['POST'])
@login_required
def add_task():
data = request.json
patient_id = data.get('patient_id') or current_user.patients[0].id
new_med = Medication(
patient_id=patient_id, name=data['name'], dosage=data.get('dosage', '1 pill'),
# Initialize max_stock to 30 for manual adds
stock=30, max_stock=30,
schedule_time=data['time'], instructions=data.get('instructions', 'Manual Add'),
frequency="Daily", days="All"
)
db.session.add(new_med)
db.session.commit()
return jsonify({'success': True})
@app.route('/api/task/delete', methods=['POST'])
@login_required
def delete_task():
Medication.query.filter_by(id=request.json.get('id')).delete()
db.session.commit()
return jsonify({'success': True})
@app.route('/api/task/toggle', methods=['POST'])
@login_required
def toggle_task():
med = Medication.query.get(request.json.get('id'))
if not med: return jsonify({'success': False})
today_str = datetime.now().strftime("%Y-%m-%d")
if med.last_taken == today_str: # UNDO
med.last_taken = None
# Only refund if we haven't exceeded max stock (sanity check)
if med.stock < med.max_stock:
med.stock += 1
db.session.add(ActivityLog(user_id=current_user.id, action=f"Undo: {med.name}", details=f"Stock restored to {med.stock}"))
else: # TAKE
if med.stock > 0:
med.last_taken = today_str
med.stock -= 1
db.session.add(ActivityLog(user_id=current_user.id, action=f"Dispensed {med.name}", details=f"Stock reduced to {med.stock}"))
else:
return jsonify({'success': False, 'message': 'Out of Stock!'})
db.session.commit()
return jsonify({'success': True})
# ==================== EMERGENCY & REQUESTS ====================
@app.route('/api/request', methods=['POST'])
@login_required
def handle_request():
data = request.json
req_type = data.get('type') # 'medicine', 'water', 'help'
details = "Manual Request via Dashboard"
action_log = f"Requested {req_type.capitalize()}"
if req_type == 'help':
action_log = "EMERGENCY ALERT"
details = "Patient pressed Panic Button. Notifying Caregiver..."
email_sent = send_emergency_email(current_user.username, "Panic Button Pressed on Dashboard")
if email_sent: details += " [Email Sent]"
else: details += " [Email Failed]"
new_log = ActivityLog(user_id=current_user.id, action=action_log, details=details)
db.session.add(new_log)
db.session.commit()
return jsonify({'success': True, 'message': f'{req_type} request processed'})
# ==================== SEEDING (UPDATED) ====================
@app.route('/seed_full_day')
@login_required
def seed_full_day():
patient = Patient.query.filter_by(user_id=current_user.id).first()
if not patient:
patient = Patient(name="Grandpa Joe", user_id=current_user.id)
db.session.add(patient); db.session.commit()
schedule_data = [
{"name": "Omeprazole", "dosage": "20mg", "time": "08:00", "instructions": "Stomach Protector", "stock": 28, "max": 30},
{"name": "Metformin", "dosage": "500mg", "time": "08:00", "instructions": "Diabetes", "stock": 60, "max": 60},
{"name": "Amoxicillin", "dosage": "500mg", "time": "08:00", "instructions": "Antibiotic", "stock": 21, "max": 21},
{"name": "Aspirin", "dosage": "75mg", "time": "08:00", "instructions": "Heart Health", "stock": 30, "max": 30},
{"name": "Amoxicillin", "dosage": "500mg", "time": "13:00", "instructions": "Antibiotic Dose 2", "stock": 21, "max": 21},
{"name": "Vitamin D3", "dosage": "1000 IU", "time": "13:00", "instructions": "Bone Supplement", "stock": 90, "max": 90},
{"name": "Metformin", "dosage": "500mg", "time": "20:00", "instructions": "Diabetes Evening", "stock": 60, "max": 60},
{"name": "Amoxicillin", "dosage": "500mg", "time": "20:00", "instructions": "Antibiotic Dose 3", "stock": 21, "max": 21},
{"name": "Atorvastatin", "dosage": "20mg", "time": "20:00", "instructions": "Cholesterol", "stock": 30, "max": 30}
]
added = 0
for item in schedule_data:
# Check if already exists to prevent duplicate seeding
if not Medication.query.filter_by(name=item['name'], schedule_time=item['time'], patient_id=patient.id).first():
db.session.add(Medication(
patient_id=patient.id, name=item['name'], dosage=item['dosage'],
stock=item['stock'], max_stock=item['max'], # SAVE MAX
schedule_time=item['time'], instructions=item['instructions'],
frequency="Daily", days="All", last_taken=None
))
added += 1
db.session.commit()
return f"""<div style="font-family:sans-serif;text-align:center;padding:50px;background:#111;color:white;">
<h1 style="color:#48bb78;">✓ System Seeded</h1><p>Added {added} complex medications.</p>
<a href='/dashboard' style="background:#0a84ff;color:white;padding:15px;text-decoration:none;border-radius:20px;">Back to Dashboard</a></div>"""
# ==================== RUN APP ====================
if __name__ == '__main__':
with app.app_context():
db.create_all()
# Host 0.0.0.0 makes it accessible to other devices (Laptop/Mobile)
app.run(host='0.0.0.0', port=5000, debug=True)