File size: 2,977 Bytes
75e2b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from app.services.chathistory import ChatSession
from app.services.environmental_condition import EnvironmentalData


def map_air_quality_index(aqi):
    if aqi <= 50:
        return {"displayValue": "Good", "value": aqi, "color": "#00C853"}
    elif aqi <= 100:
        return {"displayValue": "Moderate", "value": aqi, "color": "#FFB74D"}
    elif aqi <= 150:
        return {"displayValue": "Unhealthy Tolerate", "value": aqi, "color": "#FF7043"}
    elif aqi <= 200:
        return {"displayValue": "Unhealthy", "value": aqi, "color": "#E53935"}
    else:
        return {"displayValue": "Very Unhealthy", "value": aqi, "color": "#8E24AA"}


def map_pollution_level(aqi):
    if aqi <= 50:
        return 20
    elif aqi <= 100:
        return 40
    elif aqi <= 150:
        return 60
    elif aqi <= 200:
        return 80
    else:
        return 100

class CityNotProvidedError(Exception):
    pass


class EnvironmentalConditions:
    def __init__(self, session_id):
        self.session_id = session_id
        self.chat_session = ChatSession(session_id, "session_id")
        self.user_city = self.chat_session.get_city()

        if not self.user_city:
            raise CityNotProvidedError("City information is required but not provided")

        self.city = self.user_city
        self.environment_data = EnvironmentalData(self.city)

    def get_conditon(self):
        data = self.environment_data.get_environmental_data()

        formatted_data = [
            {
                "label": "Humidity",
                # Handle decimal values by converting to float first
                "value": int(float(data['Humidity'].strip(' %'))),
                "color": "#4FC3F7",
                "icon": "FaTint",
                "type": "numeric"
            },
            {
                "label": "UV Rays",
                "value": data['UV_Index'] * 10,
                "color": "#FFB74D",
                "icon": "FaSun",
                "type": "numeric"
            },
            {
                "label": "Pollution",
                "value": map_pollution_level(data['Air Quality Index']),
                "color": "#F06292",
                "icon": "FaLeaf",
                "type": "numeric"
            },
            {
                "label": "Air Quality",
                **map_air_quality_index(data['Air Quality Index']),
                "icon": "FaCloud",
                "type": "categorical"
            },
            {
                "label": "Wind",
                "value": float(data['Wind Speed'].strip(' m/s')) * 10,
                "color": "#9575CD",
                "icon": "FaWind",
                "type": "numeric"
            },
            {
                "label": "Temperature",
                "value": int(float(data['Temperature'].strip(' °C'))),
                "color": "#FF7043",
                "icon": "FaThermometerHalf",
                "type": "numeric"
            }
        ]

        return formatted_data