File size: 6,921 Bytes
945d0f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Database Models & Schemas for Climate-Resilient Agricultural Platform

======================================================================

Handles user profiles, alert configurations, and historical data

"""

from pydantic import BaseModel, Field
from typing import Optional, List, Dict
from datetime import datetime
from enum import Enum

# ════════════════════════════════════════════════════════════════════════════
# ENUMS
# ════════════════════════════════════════════════════════════════════════════

class AlertSeverity(str, Enum):
    CRITICAL = "CRITICAL"
    HIGH = "HIGH"
    MEDIUM = "MEDIUM"
    LOW = "LOW"
    INFO = "INFO"

class CropType(str, Enum):
    RICE = "RICE"
    WHEAT = "WHEAT"
    CORN = "CORN"
    COTTON = "COTTON"
    SUGARCANE = "SUGARCANE"
    POTATO = "POTATO"
    TOMATO = "TOMATO"
    ONION = "ONION"
    SOYBEAN = "SOYBEAN"
    BANANA = "BANANA"
    CITRUS = "CITRUS"
    OTHER = "OTHER"

class Season(str, Enum):
    RABI = "RABI"
    KHARIF = "KHARIF"
    ANNUAL = "ANNUAL"

class IrrigationType(str, Enum):
    DRIP = "DRIP"
    FLOOD = "FLOOD"
    SPRINKLER = "SPRINKLER"
    FURROW = "FURROW"

# ════════════════════════════════════════════════════════════════════════════
# FARMER PROFILE
# ════════════════════════════════════════════════════════════════════════════

class FarmerProfile(BaseModel):
    farmer_id: str
    name: str
    phone: str
    telegram_chat_id: Optional[str] = None
    email: Optional[str] = None
    village: str
    latitude: float
    longitude: float
    crop_type: CropType
    soil_type: str
    motor_capacity: float = 10.0
    irrigation_type: IrrigationType = IrrigationType.DRIP
    season: Season = Season.ANNUAL
    crop_age: int = 0
    farm_size_hectares: float = 1.0
    alert_preferences: Dict[str, bool] = Field(default_factory=lambda: {
        "weather": True,
        "pest": True,
        "irrigation": True,
        "sustainability": True,
        "climate_warning": True
    })
    contact_preference: str = "telegram"
    threshold_settings: Dict[str, float] = Field(default_factory=lambda: {
        "temp_high": 40,
        "temp_low": 5,
        "wind_speed": 30,
        "wind_alert": 15,
        "rain_threshold": 10
    })
    registered_at: datetime = Field(default_factory=datetime.now)

# ════════════════════════════════════════════════════════════════════════════
# ALERT MODELS
# ════════════════════════════════════════════════════════════════════════════

class WeatherAlert(BaseModel):
    alert_type: str
    temperature: float
    humidity: float
    wind_speed: float
    condition: str
    action: str
    severity: AlertSeverity
    lead_time: str

class PestAlert(BaseModel):
    pest_name: str
    risk_level: AlertSeverity
    confidence: float
    recommended_action: str
    spray_timing: str
    product_suggestion: Optional[str] = None

class IrrigationAlert(BaseModel):
    water_needed_m3_per_sqm: float
    irrigation_duration_minutes: int
    best_time_window: str
    urgency: AlertSeverity
    reason: str

class SustainabilityAlert(BaseModel):
    tip_category: str
    action: str
    impact: str
    implementation_time: str
    expected_benefit: str

class ClimateResilienceScore(BaseModel):
    total_score: float  # 0-100
    weather_risk: float
    pest_risk: float
    water_risk: float
    resilience_level: str  # GREEN, YELLOW, RED
    critical_factors: List[str]
    improvement_suggestions: List[str]

class FarmAlert(BaseModel):
    alert_id: str
    farmer_id: str
    timestamp: datetime
    severity: AlertSeverity
    category: str  # weather, pest, irrigation, sustainability, climate
    title: str
    message: str
    detailed_data: Dict
    action_required: bool
    suggested_action: str
    sent: bool = False
    sent_via: Optional[List[str]] = None  # ["telegram", "sms", "email"]

class AlertHistory(BaseModel):
    farmer_id: str
    total_alerts_today: int
    critical_alerts: int
    high_alerts: int
    medium_alerts: int
    actions_taken: int
    last_alert_time: Optional[datetime] = None

# ════════════════════════════════════════════════════════════════════════════
# DECISION ENGINE
# ════════════════════════════════════════════════════════════════════════════

class DashboardResponse(BaseModel):
    farmer_id: str
    farmer_profile: FarmerProfile
    current_conditions: Dict
    climate_resilience_score: ClimateResilienceScore
    active_alerts: List[FarmAlert]
    weather_forecast: List[Dict]
    pest_predictions: List[PestAlert]
    irrigation_recommendations: IrrigationAlert
    sustainability_tips: List[SustainabilityAlert]
    generated_at: datetime = Field(default_factory=datetime.now)
    next_critical_window: Optional[str] = None

# ════════════════════════════════════════════════════════════════════════════
# API SCHEMAS
# ════════════════════════════════════════════════════════════════════════════

class HealthResponse(BaseModel):
    status: str
    timestamp: datetime
    services: Dict[str, str]
    message: str

class WebSocketAlert(BaseModel):
    event_type: str  # "alert", "update", "subscribe", "unsubscribe"
    farmer_id: str
    data: Dict
    timestamp: datetime = Field(default_factory=datetime.now)