-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
792 lines (677 loc) · 26.4 KB
/
app.py
File metadata and controls
792 lines (677 loc) · 26.4 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
import os
import jwt
import json
import time
import datetime
import uuid
import psutil
import sqlite3
from flask import Flask, request, jsonify, render_template, send_from_directory
from flask_cors import CORS
from flask_socketio import SocketIO
from werkzeug.security import generate_password_hash, check_password_hash
from functools import wraps
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import secrets
from database import db, User, SimCard, Message, Log, init_db, SmsStatus
# Load config
with open('config.json') as f:
config = json.load(f)
MQTT_HOST = config['MQTT']['host']
MQTT_PORT = config['MQTT']['port']
# Import eventlet and monkey patch
import eventlet
eventlet.monkey_patch()
app = Flask(__name__, static_folder='static', template_folder='templates')
CORS(app, resources={r"/*": {"origins": "*"}})
# Configure Socket.io with CORS
socketio = SocketIO(app,
cors_allowed_origins="*",
async_mode='eventlet',
ping_timeout=60,
ping_interval=25,
max_http_buffer_size=1e8
)
# Configure SQLite database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sms_gateway.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
'pool_pre_ping': True,
'pool_recycle': 300,
'pool_use_lifo': True
}
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', secrets.token_hex(32))
# Initialize database
db.init_app(app)
init_db(app)
import json
import paho.mqtt.client as mqtt
# MQTT Client setup with a message callback parameter
def create_mqtt_client(topic, on_message_callback):
mqtt_client = mqtt.Client()
def on_connect(client, userdata, flags, rc):
print(f"Connected to MQTT broker with result code: {rc}")
print(f"Subscribing to topic: {topic}")
client.subscribe(topic)
print(f"Successfully subscribed to topic: {topic}")
def on_message(client, userdata, msg):
try:
print(f"Received message on topic {msg.topic}")
payload = msg.payload.decode()
print(f"Message payload: {payload}")
on_message_callback(payload)
except Exception as e:
print(f"Error processing MQTT message: {str(e)}")
def on_disconnect(client, userdata, rc):
print(f"Disconnected from MQTT broker with result code: {rc}")
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.on_disconnect = on_disconnect
return mqtt_client
def handle_sms_status_message(payload):
print("Received status message:", payload)
try:
data = json.loads(payload)
print("Parsed status data:", data)
with app.app_context():
try:
# Update the message status in the database
message = Message.query.filter_by(id=data.get('message_id')).first()
if message:
print(f"Found message with ID: {message.id}, updating status to: {data['status']}")
message.status = data['status']
db.session.commit()
print("Message status updated successfully")
else:
print(f"No message found with ID: {data.get('message_id')}")
# Create SMS status record
sms_status = SmsStatus(
sender_number=data["sender_number"],
receiver_number=data["receiver_number"],
message=data["message"],
status=data["status"]
)
db.session.add(sms_status)
db.session.commit()
print("Saved status record:", sms_status.to_dict())
# Emit socket event for status update
socketio.emit('sms_status_update', {
'message_id': data.get('message_id'),
'status': data['status'],
'timestamp': datetime.datetime.now().isoformat()
})
# Also emit the full status data
socketio.emit('mqtt_status', payload)
except Exception as e:
db.session.rollback()
print(f"Error updating message status: {str(e)}")
raise
except json.JSONDecodeError as json_error:
print(f"Invalid JSON payload: {str(json_error)}")
except Exception as e:
print(f"Error processing status message: {str(e)}")
# Create client and pass callback
mqtt_client = create_mqtt_client('/sms/status',handle_sms_status_message)
def handle_sms_receive_message(payload):
try:
# Parse the incoming payload
data = json.loads(payload)
# Validate required fields
required_fields = ['sender_number', 'receiver_number', 'message']
if not all(field in data for field in required_fields):
print(f"Missing required fields in payload: {payload}")
return
sender_number = data['sender_number']
receiver_number = data['receiver_number']
message_text = data['message']
with app.app_context():
try:
# Find the SimCard by sender number
sim_card = SimCard.query.filter_by(number=sender_number).first()
if not sim_card:
print(f"No SIM card found for sender number: {sender_number}")
# Create a new SIM card entry if not found
sim_card = SimCard(
number=sender_number,
status='active'
)
db.session.add(sim_card)
db.session.flush() # Get the ID without committing
# Create the Message object
message = Message(
sender=sender_number,
recipient=receiver_number,
message=message_text,
direction='incoming',
sender_sim=sim_card.id,
status='received'
)
db.session.add(message)
db.session.flush() # Get the message ID
# Create a single log entry
log = Log(
action='receive_sms',
details=json.dumps({
'recipient': receiver_number,
'message_id': message.id,
'sim_card': sim_card.number,
'sender': sender_number,
'message': message_text[:100] # Store first 100 chars
}),
status='SUCCESS',
sender_sim=sim_card.id
)
db.session.add(log)
# Commit all changes in a single transaction
db.session.commit()
# Emit socket event with the complete message data
socketio.emit('sms_status_update', {
'message_id': message.id,
'recipient': receiver_number,
'sender': sender_number,
'message': message_text,
'status': 'received',
'timestamp': message.timestamp.isoformat(),
'sim_card': sim_card.number
})
# Publish status update for the received message
status_payload = {
'message_id': message.id,
'sender_number': sender_number,
'receiver_number': receiver_number,
'message': message_text,
'status': 'SUCCESS'
}
publish.single("sms/status", json.dumps(status_payload), hostname="localhost", port=1883)
print(f"Successfully saved incoming message from {sender_number} to {receiver_number}")
print(f"Message ID: {message.id}, SIM Card: {sim_card.number}")
except Exception as db_error:
db.session.rollback()
print(f"Database error while saving message: {str(db_error)}")
raise
except json.JSONDecodeError as json_error:
print(f"Invalid JSON payload: {str(json_error)}")
except Exception as e:
print(f"Error processing received message: {str(e)}")
# Create and connect client, subscribing to /sms/receive with the callback
mqtt_client_receive_msg = create_mqtt_client('/sms/receive', handle_sms_receive_message)
try:
print("Attempting to connect to MQTT broker...")
mqtt_client_receive_msg.connect("localhost", 1883, 60)
mqtt_client_receive_msg.loop_start()
print("MQTT client started successfully")
except Exception as e:
print(f"Failed to connect to MQTT broker: {str(e)}")
try:
print("Attempting to connect to MQTT broker...")
mqtt_client.connect("localhost", 1883, 60)
mqtt_client.loop_start()
print("MQTT client started successfully")
except Exception as e:
print(f"Failed to connect to MQTT broker: {str(e)}")
def get_next_available_sim():
return SimCard.query.filter_by(status='active').first()
def require_api_key(f):
@wraps(f)
def decorated(*args, **kwargs):
api_key = request.headers.get('X-API-Key')
if not api_key:
return jsonify({'message': 'API key is missing'}), 401
user = User.query.filter_by(api_key=api_key).first()
if not user:
return jsonify({'message': 'Invalid API key'}), 401
return f(*args, **kwargs)
return decorated
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = request.headers.get('Authorization')
if not token:
return jsonify({'message': 'Token is missing'}), 401
try:
token = token.split(' ')[1] # Remove 'Bearer ' prefix
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=["HS256"])
current_user = User.query.get(data['user_id'])
if not current_user:
return jsonify({'message': 'User not found'}), 401
except:
return jsonify({'message': 'Token is invalid'}), 401
return f(current_user, *args, **kwargs)
return decorated
@app.route('/')
def index():
return render_template('index.html')
@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')
@app.route('/api/auth', methods=['POST'])
def auth():
data = request.get_json()
if not data or not data.get('username') or not data.get('password'):
return jsonify({'message': 'Missing username or password'}), 400
user = User.query.filter_by(username=data['username']).first()
if not user or not check_password_hash(user.password_hash, data['password']):
return jsonify({'message': 'Invalid username or password'}), 401
token = jwt.encode({
'user_id': user.id,
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=24)
}, app.config['SECRET_KEY'])
return jsonify({
'token': token,
'username': user.username,
'role': user.role,
'api_key': user.api_key
})
@app.route('/api/sim-cards', methods=['GET'])
@require_api_key
def get_sim_cards():
sim_cards = SimCard.query.all()
return jsonify({
'sim_cards': [{
'id': sim.id,
'number': sim.number,
'status': sim.status
} for sim in sim_cards]
})
@app.route('/api/sim-cards', methods=['POST'])
@require_api_key
def add_sim_card():
data = request.get_json()
if not data or not data.get('number'):
return jsonify({'message': 'Phone number is required'}), 400
try:
sim_card = SimCard(
number=data['number'],
status=data.get('status', 'active')
)
db.session.add(sim_card)
db.session.commit()
log = Log(
action='add_sim_card',
details=json.dumps({'number': data['number'], 'status': data.get('status', 'active')}),
status='success'
)
db.session.add(log)
db.session.commit()
return jsonify({
'message': 'SIM card added successfully',
'sim_card': {
'id': sim_card.id,
'number': sim_card.number,
'status': sim_card.status
}
})
except Exception as e:
db.session.rollback()
return jsonify({'message': str(e)}), 500
@app.route('/api/sim-cards/<sim_id>', methods=['PUT'])
@require_api_key
def update_sim_card(sim_id):
data = request.get_json()
if not data:
return jsonify({'message': 'No data provided'}), 400
sim_card = SimCard.query.get(sim_id)
if not sim_card:
return jsonify({'message': 'SIM card not found'}), 404
try:
if 'number' in data:
sim_card.number = data['number']
if 'status' in data:
sim_card.status = data['status']
db.session.commit()
log = Log(
action='update_sim_card',
details=json.dumps({'sim_id': sim_id, 'updates': data}),
status='success'
)
db.session.add(log)
db.session.commit()
return jsonify({
'message': 'SIM card updated successfully',
'sim_card': {
'id': sim_card.id,
'number': sim_card.number,
'status': sim_card.status
}
})
except Exception as e:
db.session.rollback()
return jsonify({'message': str(e)}), 500
@app.route('/api/sim-cards/<sim_id>', methods=['DELETE'])
@require_api_key
def delete_sim_card(sim_id):
sim_card = SimCard.query.get(sim_id)
if not sim_card:
return jsonify({'message': 'SIM card not found'}), 404
try:
db.session.delete(sim_card)
db.session.commit()
log = Log(
action='delete_sim_card',
details=json.dumps({'sim_id': sim_id}),
status='success'
)
db.session.add(log)
db.session.commit()
return jsonify({'message': 'SIM card deleted successfully'})
except Exception as e:
db.session.rollback()
return jsonify({'message': str(e)}), 500
@app.route('/api/sms', methods=['POST'])
@require_api_key
def send_sms():
try:
print("Received SMS send request")
data = request.get_json()
if not data:
print("No data provided in request")
return jsonify({'error': 'No data provided'}), 400
# Validate required fields
if not data.get('recipient'):
print("Missing recipient in request")
return jsonify({'error': 'Recipient phone number is required'}), 400
if not data.get('message'):
print("Missing message in request")
return jsonify({'error': 'Message content is required'}), 400
recipient = data['recipient']
message_text = data['message']
sim_card_id = data.get('sim_card_id')
print(f"Processing SMS to {recipient} with sim_card_id: {sim_card_id}")
# Validate phone number format
if not recipient.startswith('+'):
print(f"Invalid phone number format: {recipient}")
return jsonify({'error': 'Phone number must start with + and include country code'}), 400
# Get SIM card
sim_card = None
if sim_card_id:
sim_card = SimCard.query.get(sim_card_id)
if not sim_card:
print(f"No SIM card found with ID: {sim_card_id}")
return jsonify({'error': f'No SIM card found with ID: {sim_card_id}'}), 400
if sim_card.status != 'active':
print(f"SIM card {sim_card_id} is not active")
return jsonify({'error': f'SIM card {sim_card_id} is not active'}), 400
else:
sim_card = get_next_available_sim()
if not sim_card:
print("No active SIM cards available")
return jsonify({'error': 'No active SIM cards available'}), 400
print(f"Using SIM card: {sim_card.number}")
# Create message record
message = Message(
recipient=recipient,
message=message_text,
status='pending',
direction='outgoing',
sender_sim=sim_card.id
)
db.session.add(message)
db.session.flush() # ensure defaults like id and timestamp are generated
print(f"Created message record with ID: {message.id}")
print(f"Publishing message to MQTT broker at {MQTT_HOST}:{MQTT_PORT}")
try:
message_dict = message.to_dict()
print(f"Message content: {message_dict}")
# Choose MQTT topic based on whether sim_card_id is provided
mqtt_topic = "sms/send" if not sim_card_id else f"sms/send/{sim_card.number.replace('+', '')}"
publish.single(
mqtt_topic,
json.dumps(message_dict),
hostname=MQTT_HOST,
port=MQTT_PORT,
keepalive=60,
qos=1
)
print(f"Message published successfully to topic: {mqtt_topic}")
except Exception as e:
print(f"Failed to publish message: {str(e)}")
raise
# Create log details
log_details = {
'recipient': recipient,
'message_id': message.id,
'sim_card': sim_card.number,
'message': message_text[:100] # Store first 100 chars
}
# Create log entry with properly formatted details
log = Log(
action='send_sms',
details=json.dumps(log_details, ensure_ascii=False),
status='pending',
sender_sim=sim_card.id
)
db.session.add(log)
# Commit both message and log
db.session.commit()
# Emit socket event
socketio.emit('sms_status_update', {
'message_id': message.id,
'recipient': recipient,
'status': 'pending'
})
return jsonify({
'message': 'SMS queued for sending',
'message_id': message.id,
'sender_sim': sim_card.number
})
except Exception as e:
db.session.rollback()
print(f"Error in send_sms: {str(e)}") # Add logging
return jsonify({'error': f'Failed to send SMS: {str(e)}'}), 500
@app.route('/api/sms/inbox', methods=['GET'])
@require_api_key
def get_inbox():
try:
messages = Message.query.filter_by(direction='incoming').order_by(Message.timestamp.desc()).all()
return jsonify({
'messages': [msg.to_dict() for msg in messages]
})
except Exception as e:
print(f"Error in get_inbox: {str(e)}") # Add logging
return jsonify({'error': 'Failed to fetch inbox messages'}), 500
@app.route('/api/sms/outbox', methods=['GET'])
@require_api_key
def get_outbox():
try:
messages = Message.query.filter_by(direction='outgoing').order_by(Message.timestamp.desc()).all()
return jsonify({
'messages': [msg.to_dict() for msg in messages]
})
except Exception as e:
print(f"Error in get_outbox: {str(e)}") # Add logging
return jsonify({'error': 'Failed to fetch outbox messages'}), 500
@app.route('/api/logs', methods=['GET'])
@require_api_key
def get_logs():
logs = Log.query.order_by(Log.timestamp.desc()).all()
return jsonify({
'logs': [log.to_dict() for log in logs]
})
@app.route('/api/generate-api-key', methods=['POST'])
@token_required
def generate_api_key(current_user):
try:
api_key = secrets.token_hex(32)
current_user.api_key = api_key
db.session.commit()
log = Log(
action='generate_api_key',
details=json.dumps({'user_id': current_user.id}),
status='success'
)
db.session.add(log)
db.session.commit()
return jsonify({'api_key': api_key})
except Exception as e:
db.session.rollback()
return jsonify({'message': str(e)}), 500
@app.route('/api/sms-status', methods=['GET'])
@require_api_key
def get_all_sms_statuses():
try:
# Get all messages with their statuses
messages = SmsStatus.query.order_by(SmsStatus.timestamp.desc()).all()
# Format the response
statuses = []
for msg in messages:
status = {
'id': msg.id,
'timestamp': int(msg.timestamp.timestamp()),
'sender_number': msg.sender_number,
'receiver_number': msg.receiver_number,
'message': msg.message,
'status': msg.status,
}
statuses.append(status)
return jsonify({
'statuses': statuses
})
except Exception as e:
print(f"Error in get_all_sms_statuses: {str(e)}")
return jsonify({'error': 'Failed to fetch SMS statuses'}), 500
def get_system_memory_stats():
"""Get system memory statistics"""
memory = psutil.virtual_memory()
return {
'total': memory.total,
'used': memory.used,
'free': memory.free,
'percent': memory.percent
}
def get_database_file_size(db_path='instance/sms_gateway.db'):
"""Return the SQLite DB file size"""
try:
size_bytes = os.path.getsize(db_path)
size_mb = size_bytes / (1024 * 1024)
return {
'name': os.path.basename(db_path),
'size_bytes': size_bytes,
'size_mb': round(size_mb, 2),
'last_updated': datetime.datetime.now().isoformat()
}
except Exception as e:
print(f"Error getting database size: {e}")
return {
'name': os.path.basename(db_path),
'error': str(e),
'size_bytes': 0,
'size_mb': 0.0
}
def get_component_memory_usage():
"""Get memory usage by main process and database file"""
components = []
process = psutil.Process()
process_memory = process.memory_info().rss
total_memory = psutil.virtual_memory().total
components.append({
'name': 'Main Process',
'memory_usage': process_memory,
'percentage': (process_memory / total_memory) * 100,
'last_updated': datetime.datetime.now().isoformat()
})
db_info = get_database_file_size()
db_size_bytes = db_info.get('size_bytes', 0)
components.append({
'name': 'Database File',
'memory_usage': db_size_bytes,
'percentage': (db_size_bytes / total_memory) * 100,
'last_updated': datetime.datetime.now().isoformat()
})
return components
def get_message_statistics():
"""Get statistics about messages"""
try:
total_messages = Message.query.count()
incoming_messages = Message.query.filter_by(direction='incoming').count()
outgoing_messages = Message.query.filter_by(direction='outgoing').count()
# Get messages for today
today = datetime.datetime.now().date()
messages_today = Message.query.filter(
db.func.date(Message.timestamp) == today
).count()
# Get messages per day for the last 7 days
seven_days_ago = datetime.datetime.now() - datetime.timedelta(days=7)
messages_per_day = db.session.query(
db.func.date(Message.timestamp).label('date'),
db.func.count(Message.id).label('count')
).filter(Message.timestamp >= seven_days_ago)\
.group_by(db.func.date(Message.timestamp))\
.all()
messages_per_day = [{
'date': day.date,
'count': day.count
} for day in messages_per_day]
return {
'total_messages': total_messages,
'incoming_messages': incoming_messages,
'outgoing_messages': outgoing_messages,
'messages_today': messages_today,
'messages_per_day': messages_per_day
}
except Exception as e:
print(f"Error getting message statistics: {e}")
return {
'total_messages': 0,
'incoming_messages': 0,
'outgoing_messages': 0,
'messages_today': 0,
'messages_per_day': []
}
from sqlalchemy import func, desc
def get_sim_card_statistics():
"""Get statistics about SIM cards"""
try:
total_sims = SimCard.query.count()
active_sims = SimCard.query.filter_by(status='active').count()
inactive_sims = SimCard.query.filter_by(status='inactive').count()
# Get top 5 most used SIMs
results = (
db.session.query(
SimCard.number.label("number"),
func.count(Message.id).label("message_count")
)
.join(Message, SimCard.id == Message.sender_sim)
.group_by(SimCard.number)
.order_by(desc("message_count"))
.limit(5)
.all()
)
most_used_sims = [
{'number': r.number, 'message_count': r.message_count}
for r in results
]
most_used_sim = most_used_sims[0] if most_used_sims else {'number': 'N/A', 'message_count': 0}
return {
'total_sims': total_sims,
'active_sims': active_sims,
'inactive_sims': inactive_sims,
'most_used_sim': most_used_sim,
'most_used_sims': most_used_sims
}
except Exception as e:
print(f"Error getting SIM card statistics: {e}")
return {
'total_sims': 0,
'active_sims': 0,
'inactive_sims': 0,
'most_used_sim': {'number': 'N/A', 'message_count': 0},
'most_used_sims': []
}
@app.route('/api/statistics', methods=['GET'])
@require_api_key
def get_statistics():
"""Get system statistics including memory usage, database size, messages, and SIM cards"""
try:
stats = {
'memory': get_system_memory_stats(),
'components': get_component_memory_usage(),
'database': get_database_file_size(),
'messages': get_message_statistics(),
'sim_cards': get_sim_card_statistics()
}
return jsonify(stats)
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=5001, debug=True)