-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
246 lines (197 loc) · 7.68 KB
/
Copy pathapp.py
File metadata and controls
246 lines (197 loc) · 7.68 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
from flask import Flask, request, jsonify, render_template
from token_queue import import_tokens, get_queue, generate_token
from inventory import get_inventory, update_stock, get_low_stock
from broadcast import create_broadcast, get_broadcasts
from apscheduler.schedulers.background import BackgroundScheduler
from bson import ObjectId
from flask_socketio import SocketIO
import csv
from datetime import datetime, timedelta
import os
import random
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret!"
socketio = SocketIO(app, cors_allowed_origins="*")
def generate_random_token_csv(filename="token_queue.csv", num_entries=20):
departments = ['D001', 'D002', 'D003', 'D004']
patient_names = ['A. Kumar', 'B. Reddy', 'C. Fernandes', 'D. Shetty', 'E. Nair', 'F. Rao']
statuses = ['Waiting', 'Called', 'In Progress']
now = datetime.now()
with open(filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["token_id", "dept_id", "patient_name", "status", "timestamp"])
current_time = datetime.now()
for i in range(num_entries): # Generate 20 time slots
num_tokens = random.randint(0, 5) # 👈 Random tokens per time slot
for _ in range(num_tokens):
token_id = random.randint(100, 999)
row = [
token_id,
random.choice(departments),
random.choice(patient_names),
random.choice(statuses),
(current_time + timedelta(minutes=i)).strftime('%Y-%m-%d %H:%M:%S')
]
writer.writerow(row)
# Call it when app starts
generate_random_token_csv()
# --- TOKEN ENDPOINTS ---
@app.route("/token", methods=["POST"])
def create_token():
data = request.json
department = data.get("department")
if not department:
return jsonify({"error": "Department is required"}), 400
token = generate_token(department)
token["_id"] = str(token["_id"])
return jsonify(token), 201
@app.route('/api/token_chart')
def token_chart():
from collections import Counter
time_counts = Counter()
with open("token_queue.csv", mode="r") as file:
reader = csv.DictReader(file)
for row in reader:
try:
dt = datetime.strptime(row["timestamp"], "%Y-%m-%d %H:%M:%S")
time_key = dt.strftime("%H:%M")
time_counts[time_key] += 1
except:
continue
sorted_data = sorted(time_counts.items())
return jsonify([{"time": t, "count": c} for t, c in sorted_data])
@app.route("/token/<department>", methods=["GET"])
def queue(department):
tokens = get_queue(department)
for token in tokens:
token["_id"] = str(token["_id"])
return jsonify(tokens), 200
@app.route('/api/tokens')
def get_tokens():
tokens = []
# Load department metadata
dept_map = {}
with open("data/departments.csv", "r") as dept_file:
reader = csv.DictReader(dept_file)
for row in reader:
dept_map[row["dept_id"]] = {
"department_name": row["department_name"],
"location": row["location"]
}
# Read token data and join with department
with open("token_queue.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
dept_id = row["dept_id"]
dept_info = dept_map.get(dept_id, {"department_name": "Unknown", "location": "Unknown"})
tokens.append({
"token_number": row["token_id"],
"department": dept_info["department_name"],
"location": dept_info["location"],
"patient_name": row["patient_name"],
"status": row["status"],
"timestamp": row["timestamp"]
})
return jsonify(tokens)
# --- INVENTORY ENDPOINTS ---
@app.route("/inventory", methods=["GET"])
def fetch_inventory():
return jsonify(get_inventory()), 200
@app.route("/inventory/update", methods=["POST"])
def modify_stock():
data = request.json
name = data.get("name")
change = data.get("change")
if not name or change is None:
return jsonify({"error": "Drug name and change amount are required"}), 400
updated = update_stock(name, change)
if updated:
return jsonify({"message": "Stock updated"}), 200
else:
return jsonify({"error": "Drug not found"}), 404
@app.route("/inventory/low", methods=["GET"])
def low_stock_alerts():
threshold = int(request.args.get("threshold", 20))
return jsonify(get_low_stock(threshold)), 200
# --- BROADCAST ENDPOINTS ---
@app.route("/broadcasts", methods=["GET"])
def fetch_broadcasts():
alerts = get_broadcasts()
for a in alerts:
a["_id"] = str(a["_id"])
a["timestamp"] = a["timestamp"].strftime("%Y-%m-%d %H:%M:%S")
return jsonify(alerts), 200
@app.route("/broadcasts/send", methods=["POST"])
def manual_broadcast():
data = request.json
alert_type = data.get("alert_type")
message = data.get("message")
if not alert_type or not message:
return jsonify({"error": "Missing alert_type or message"}), 400
alert = create_broadcast(alert_type, message)
alert["_id"] = str(alert["_id"])
alert["timestamp"] = alert["timestamp"].strftime("%Y-%m-%d %H:%M:%S")
return jsonify(alert), 201
@app.route("/api/alerts", methods=["GET"])
def api_alerts():
alerts = get_broadcasts()
for a in alerts:
a["_id"] = str(a["_id"])
a["timestamp"] = a["timestamp"].strftime("%Y-%m-%d %H:%M:%S")
return jsonify(alerts), 200
# --- DISPLAY SCREEN ---
@app.route("/display")
def display_screen():
alerts = get_broadcasts()[:5]
inventory = get_inventory()
tokens = get_queue()
# Ensure timestamps are formatted as strings
for token in tokens:
issued_at = token.get("issued_at") or token.get("timestamp")
if isinstance(issued_at, datetime):
token["issued_at"] = issued_at.strftime("%Y-%m-%d %H:%M:%S")
elif isinstance(issued_at, str):
token["issued_at"] = issued_at
else:
token["issued_at"] = "N/A"
print("Tokens Data:", tokens)
for alert in alerts:
alert["_id"] = str(alert["_id"])
alert["timestamp"] = alert["timestamp"].strftime("%Y-%m-%d %H:%M:%S")
return render_template("display.html", alerts=alerts, inventory=inventory, tokens=tokens)
# --- BACKGROUND LOW STOCK CHECK ---
def check_and_broadcast_low_stock():
low_stock_drugs = get_low_stock()
for drug in low_stock_drugs:
create_broadcast(
alert_type="Code Red",
message=f"Low stock for {drug['name']}: Only {drug['stock']} left!"
)
# --- SCHEDULER SETUP ---
scheduler = BackgroundScheduler()
scheduler.add_job(check_and_broadcast_low_stock, 'interval', seconds=30)
scheduler.start()
from bson import ObjectId
from datetime import datetime
@app.route("/api/broadcast", methods=["POST"])
def broadcast_alert():
data = request.json
alert_type = data.get("alert_type")
message = data.get("message")
alert = create_broadcast(alert_type, message, socketio)
if alert:
# Convert ObjectId and timestamp to string
alert["_id"] = str(alert["_id"])
alert["timestamp"] = alert["timestamp"].strftime("%Y-%m-%d %H:%M:%S")
return jsonify({"status": "success", "alert": alert}), 200
else:
return jsonify({"status": "duplicate", "message": "Alert already exists"}), 409
@socketio.on("connect")
def handle_connect():
print("Client connected")
@socketio.on("disconnect")
def handle_disconnect():
print("Client disconnected")
# --- RUN FLASK APP ---
if __name__ == "__main__":
socketio.run(app, debug=True)