File size: 3,099 Bytes
c514c85 6513666 |
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 |
# controllers/health_management.py
from flask import request, jsonify
from models.schemas.health_schema import create_health_record, get_health_record_by_id, get_health_records_by_user
from services.disease_detection import detect_disease
from controllers.data_logging import log_health_record
# Detect Disease and Log Health Record
def detect_and_log_disease():
"""
Detect disease from the provided image and log the health record.
Expects a POST request with the following fields:
- user_id: ID of the user submitting the disease detection request.
- poultry_id: The ID of the poultry bird.
- image: Image of the poultry feces for disease detection (binary file).
Returns:
- JSON response with the disease detection result and health record logging status.
"""
try:
# Extract the data from the request
user_id = request.form.get('user_id')
poultry_id = request.form.get('poultry_id')
image = request.files.get('image')
if not user_id or not poultry_id or not image:
return jsonify({"error": "Missing required fields: user_id, poultry_id, or image"}), 400
# Run disease detection
disease_name, status, recommendation = detect_disease(image)
# Log the health record
record_id = log_health_record(
user_id=user_id,
poultry_id=poultry_id,
disease_name=disease_name,
status=status,
recommendation=recommendation
)
return jsonify({
"message": "Disease detected and health record logged successfully",
"disease_name": disease_name,
"status": status,
"recommendation": recommendation,
"record_id": record_id
}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
# Get Health Record by ID
def get_health_record(record_id):
"""
Retrieves a health record by its record_id (MongoDB ObjectId).
Expects:
- record_id: ID of the health record.
Returns:
- JSON response with the health record details or an error message if not found.
"""
try:
health_record = get_health_record_by_id(record_id)
if not health_record:
return jsonify({"message": "Health record not found"}), 404
return jsonify(health_record), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
# Get Health Records by User
def get_health_records_for_user(user_id):
"""
Retrieves all health records created by a specific user.
Expects:
- user_id: ID of the user whose records are being requested.
Returns:
- JSON response with the list of health records for the user or an error message if not found.
"""
try:
health_records = get_health_records_by_user(user_id)
if not health_records:
return jsonify({"message": "No health records found for this user"}), 404
return jsonify(health_records), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
|