def update_light_status(self, light_status): if light_status != self.current_light_status: self.__light_controller.update_relay_status(light_status) self.current_light_status = light_status LogsApiDataSource.log_info( "HandleLights - LightStatusRepository: changing status to: " + light_status.name) else: LogsApiDataSource.log_info( "HandleLights - LightStatusRepository: keeping the current status: " + light_status.name)
def get_heating_temperature(): try: temperature = ApiDataSource.get_water_preferences() return WaterTemperaturePreferences( temperature, WaterTemperaturePreferencesSource.API) except (NoApiPreferenceException, NoSerialException): LogsApiDataSource.log_warning( "HeaterControl - HeatingTemperatureRepository: " "error reading desired temperature from api") temperature = LocalDataSource.local_heater_temperature return WaterTemperaturePreferences( temperature, WaterTemperaturePreferencesSource.LOCAL)
def get_light_preferences(): try: LogsApiDataSource.log_info("HandleLights - PreferenceRepository: getting lights preferences from api") preferences = ApiDataSource.get_light_preferences() LogsApiDataSource.log_info("HandleLights - PreferenceRepository: starting: " + preferences.starting_hour + ", finishing: " + preferences.finishing_hour + ", mode: " + preferences.light_mode.name) return LightPreferences(preferences.starting_hour, preferences.finishing_hour, preferences.light_mode, LightPreferencesSource.API) except (NoApiPreferenceException, NoSerialException): LogsApiDataSource.log_warning("HandleLights - PreferenceRepository: api failure") preferences = LocalDataSource.get_light_preferences() LogsApiDataSource.log_info("HandleLights - PreferenceRepository: starting: " + preferences.starting_hour + ", finishing: " + preferences.finishing_hour + ", mode: " + preferences.light_mode.name) return LightPreferences(preferences.starting_hour, preferences.finishing_hour, preferences.light_mode, LightPreferencesSource.LOCAL)
def handle_lights(self): current_time = datetime.now( timezone('Europe/Madrid')).strftime("%H:%M") LogsApiDataSource.log_info( "HandleLights - UseCase checking if lights should be on/off at " + current_time) preferences = Preferences.get_light_preferences() if should_turn_on_lights(current_time, preferences): LogsApiDataSource.log_info( "HandleLights - UseCase: lights must be on") self.__light_repository.update_light_status(RelayStatus.ON) else: LogsApiDataSource.log_info( "HandleLights - UseCase: lights must be off") self.__light_repository.update_light_status(RelayStatus.OFF) self.__handle_possible_api_errors(preferences)
def control_heating(self): LogsApiDataSource.log_info( "HeaterControl - UseCase: comparing if water temperature fits requirements" ) desired_water_temperature: WaterTemperaturePreferences = WaterTemperatureRepository.get_heating_temperature( ) LogsApiDataSource.log_info("HeaterControl - UseCase: " "the desired temperature is " + str(desired_water_temperature.temperature)) current_water_temperature = LocalDataSource.water_temperature LogsApiDataSource.log_info("HeaterControl - UseCase: " "the current temperature is " + str(current_water_temperature)) if current_water_temperature < (desired_water_temperature.temperature + 0.1): self.__heater_status_repository.update_heating_status( RelayStatus.ON, current_water_temperature) else: self.__heater_status_repository.update_heating_status( RelayStatus.OFF, current_water_temperature) self.__handle_possible_api_errors(desired_water_temperature)
def __handle_possible_api_errors(self, preferences: LightPreferences): if preferences.source != LightPreferencesSource.API and self.__api_errors_count < self.max_errors: self.__api_errors_count += 1 LogsApiDataSource.log_warning( "HandleLights - UseCase: api error number: " + str(self.__api_errors_count)) else: if self.__api_errors_count > 0: self.__api_errors_count -= 1 LogsApiDataSource.log_info( "HandleLights - UseCase: api error number: " + str(self.__api_errors_count)) if self.__api_errors_count == self.max_errors: NotificationsApiDataSource.create_notification( "Something seems to be wrong with the remote preferences." "Impossible to get the lights range of hours") LogsApiDataSource.log_error( "HandleLights - UseCase: api error number: " + str(self.__api_errors_count) + ". Creating remote notification") self.__api_errors_count = 0
def __handle_possible_api_errors(self, preferences: WaterTemperaturePreferences): if preferences.source != WaterTemperaturePreferencesSource.API and self.__api_errors_count < self.max_errors: LogsApiDataSource.log_warning( "HeaterControl - UseCase: api error number: " + str(self.__api_errors_count)) self.__api_errors_count += 1 else: if self.__api_errors_count > 0: self.__api_errors_count -= 1 LogsApiDataSource.log_info( "HeaterControl - UseCase: api error number: " + str(self.__api_errors_count)) if self.__api_errors_count == self.max_errors: LogsApiDataSource.log_error( "HeaterControl - UseCase: api error number: " + str(self.__api_errors_count) + ". Creating remote notification") NotificationsApiDataSource.create_notification( "Something seems to be wrong with the remote preferences." "Impossible to get the desired water temperature") self.__api_errors_count = 0
def __log_heater_status(self): if self.__heater_active == ActiveHeater.NONE: LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 1 set to OFF") LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 2 set to OFF") elif self.__heater_active == ActiveHeater.FIRST_HEATER: LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 1 set to ON") LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 2 set to OFF") elif self.__activate_heaters == ActiveHeater.SECOND_HEATER: LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 1 set to OFF") LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 2 set to ON") elif self.__heater_active == ActiveHeater.BOTH: LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 1 set to ON") LogsApiDataSource.log_info( "HeaterControl - HeaterStatusRepository: " "Heater 2 set to ON")