File size: 1,429 Bytes
6f9f2ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException
from app.services.sensor_service import SensorService
from app.schemas.sensor import SensorData, SensorRecommendation

router = APIRouter()
sensor_service = SensorService()

@router.post("/sensor/analyze", response_model=SensorRecommendation)
async def analyze_sensor_data(sensor_data: SensorData, crop_type: str, bbch_stage: str):
    """
    Analyze sensor data against thresholds for a specific crop and BBCH stage.
    
    - **soil_moisture**: Percentage (%)
    - **soil_temperature**: Celsius
    - **air_temperature**: Celsius  
    - **humidity**: Percentage (%)
    - **crop_type**: Crop name (vite, soia, cavolo, etc.)
    - **bbch_stage**: BBCH code (e.g., "65", "30-39")
    """
    try:
        result = sensor_service.analyze_sensor_data(sensor_data, crop_type, bbch_stage)
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Sensor analysis failed: {str(e)}")

@router.get("/sensor/thresholds")
async def get_thresholds(crop_type: str, bbch_stage: str):
    """Get thresholds for a specific crop and BBCH stage"""
    thresholds = sensor_service.get_thresholds(crop_type, bbch_stage)
    if thresholds:
        return {"crop_type": crop_type, "bbch_stage": bbch_stage, "thresholds": thresholds}
    else:
        raise HTTPException(status_code=404, detail=f"No thresholds found for {crop_type} at BBCH {bbch_stage}")