Pārlūkot izejas kodu

add winter logic for early start prediction

highing666 3 gadi atpakaļ
vecāks
revīzija
826848fa9f

+ 17 - 54
app/controllers/equipment/fcu/early_start.py

@@ -1,6 +1,3 @@
-from typing import Tuple
-
-from httpx import AsyncClient
 from joblib import load
 from loguru import logger
 from sqlalchemy.orm import Session
@@ -8,9 +5,8 @@ from sqlalchemy.orm import Session
 from app.core.config import settings
 from app.crud.model_path.early_start import model_path_early_start_dtr
 from app.models.domain.devices import ACATFCEarlyStartPredictionRequest
-from app.services.platform import DataPlatformService
-from app.services.transfer import SpaceInfoService
-from app.services.weather import WeatherService
+from app.models.ml_models_path.early_start import EarlyStartDTRModelPath
+from app.schemas.season import Season
 
 
 class EarlyStartTimeDTRBuilder:
@@ -18,12 +14,23 @@ class EarlyStartTimeDTRBuilder:
     Build early start time by decision tree regression.
     """
 
-    def __init__(self, model_path: str):
-        self.model_path = f"{settings.ML_MODELS_DIR}{model_path}"
+    def __init__(self, model_path: EarlyStartDTRModelPath, season: Season):
+        self.summer_model_path = (
+            f"{settings.ML_MODELS_DIR}{model_path.summer_model_path}"
+        )
+        self.winter_model_path = (
+            f"{settings.ML_MODELS_DIR}{model_path.winter_model_path}"
+        )
+        self.season = season
 
     async def get_prediction(self, indoor_temp: float, outdoor_temp: float) -> float:
         try:
-            model = load(self.model_path)
+            if self.season == Season.cooling:
+                model = load(self.summer_model_path)
+            elif self.season == Season.heating:
+                model = load(self.winter_model_path)
+            else:
+                return 0
         except (FileNotFoundError, IsADirectoryError) as e:
             logger.debug(e)
             return 0
@@ -38,57 +45,13 @@ class EarlyStartTimeDTRBuilder:
         return pre_time
 
 
-async def fetch_params(
-    project_id: str, space_id: str, db: Session
-) -> Tuple[float, float, str]:
-    async with AsyncClient() as client:
-        platform = DataPlatformService(client, project_id)
-        space_service = SpaceInfoService(client, project_id, space_id)
-        weather_service = WeatherService(client)
-
-        indoor_temp = await platform.get_realtime_temperature(space_id)
-        weather_info = await weather_service.get_realtime_weather(project_id)
-        outdoor_temp = weather_info.get("temperature")
-
-        device_list = await space_service.get_equipment()
-        device_id = ""
-        for device in device_list:
-            if device.get("category") == "ACATFC":
-                device_id = device.get("id")
-                break
-
-        if device_id:
-            model_path = model_path_early_start_dtr.get_path_by_device(db, device_id)
-            model_path = model_path.model_path
-        else:
-            model_path = ""
-
-        return indoor_temp, outdoor_temp, model_path
-
-
-@logger.catch()
-async def get_recommended_early_start_time(
-    db: Session, project_id: str, space_id: str
-) -> float:
-    indoor_temp, outdoor_temp, model_path = await fetch_params(project_id, space_id, db)
-
-    builder = EarlyStartTimeDTRBuilder(model_path)
-    hour = await builder.get_prediction(indoor_temp, outdoor_temp)
-
-    logger.debug(
-        f"{space_id}: indoor-{indoor_temp}, outdoor-{outdoor_temp}, prediction-{hour * 60}"
-    )
-
-    return hour * 60
-
-
 @logger.catch()
 async def build_acatfc_early_start_prediction(
     params: ACATFCEarlyStartPredictionRequest, db: Session
 ) -> float:
     model_path = model_path_early_start_dtr.get_path_by_device(db, params.device_id)
 
-    builder = EarlyStartTimeDTRBuilder(model_path.model_path)
+    builder = EarlyStartTimeDTRBuilder(model_path, params.season)
     hour = await builder.get_prediction(
         params.space_realtime_temperature, params.outdoor_realtime_temperature
     )

+ 3 - 1
app/models/domain/devices.py

@@ -28,7 +28,6 @@ class DevicesEarlyStartTime(BaseModel):
 
 
 class ACATVIInstructionsRequest(BaseModel):
-    season: Season
     device_id: str
     return_air_temperature: float
     running_status: bool
@@ -38,6 +37,8 @@ class ACATVIInstructionsRequest(BaseModel):
     space_temperature_target: float
     space_realtime_temperature: float
     feedback: FeedbackValue
+    on_time: str
+    off_time: str
 
 
 class ACATVIInstructionsTemporaryResponse(BaseModel):
@@ -67,6 +68,7 @@ class ACATFCInstructionsResponse(BaseModel):
 
 
 class ACATFCEarlyStartPredictionRequest(BaseModel):
+    season: Season
     space_id: Optional[str]
     device_id: str
     space_realtime_temperature: float

+ 3 - 2
app/models/ml_models_path/early_start.py

@@ -6,9 +6,10 @@ from app.db.session import Base
 
 
 class EarlyStartDTRModelPath(Base):
-    __tablename__ = "early_start_DTR_models"
+    __tablename__ = "early_start_dtr_models"
 
     id = Column(Integer, primary_key=True, index=True)
     project_id = Column(String, nullable=False)
     device_id = Column(String, unique=True, nullable=False)
-    model_path = Column(String)
+    summer_model_path = Column(String)
+    winter_model_path = Column(String)

+ 2 - 1
app/schemas/model_path/early_start.py

@@ -6,7 +6,8 @@ from pydantic import BaseModel
 class EarlyStartDTRModelPathBase(BaseModel):
     project_id: Optional[str] = None
     device_id: Optional[str] = None
-    model_path: Optional[str] = None
+    summer_model_path: Optional[str] = None
+    winter_model_path: Optional[str] = None
 
 
 class EarlyStartDTRModelPathCreate(EarlyStartDTRModelPathBase):