From 905b38c36a569e364ee51ffeaab6e98f886abcd2 Mon Sep 17 00:00:00 2001 From: 123 <2070873540@qq.com> Date: Mon, 3 Jun 2024 23:49:31 +0800 Subject: [PATCH] 8 commit --- 智能家居系统/空调调节.py | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/智能家居系统/空调调节.py b/智能家居系统/空调调节.py index ff3ff6a..a915e02 100644 --- a/智能家居系统/空调调节.py +++ b/智能家居系统/空调调节.py @@ -13,7 +13,17 @@ class AirConditioner: self.max_humidity = 100 self.comfortable_temperature_range = (20, 24) # 舒适温度范围 self.comfortable_humidity_range = (40, 60) # 舒适湿度范围 + self.environment_variation = True # 控制环境波动开关 + def simulate_environment_change(self): + if self.environment_variation: + # 模拟温度和湿度的随机微小波动 + self.current_temperature += random.uniform(-1, 1) + self.current_humidity += random.uniform(-5, 5) + + # 确保波动后的值仍在合理范围内 + self.current_temperature = max(min(self.current_temperature, self.max_temperature), self.min_temperature) + self.current_humidity = max(min(self.current_humidity, self.max_humidity), self.min_humidity) def adjust_temperature(self): if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[ 1]: @@ -107,26 +117,28 @@ class ACApp(tk.Tk): print(f"温度: {self.current_temperature:.1f}°C, 湿度: {self.current_humidity:.1f}%") self.is_adjusting = False # 设置标志位为False,停止调节后的数据更新 - def main_loop(self, root): + def main_loop(self): if self.is_adjusting: - if not self.comfortable_temperature_range[0] <= self.current_temperature <= \ - self.comfortable_temperature_range[1]: + # 新增模拟环境变化 + self.aircon.simulate_environment_change() + + if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]: self.adjust_temperature() if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]: self.adjust_humidity() if not ( - self.comfortable_temperature_range[0] <= self.current_temperature <= - self.comfortable_temperature_range[1] - and - self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1] + self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1] + and + self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1] ): self.display_status() - root.after(1000, self.main_loop, root) + self.after(1000, self.main_loop) # 继续循环 else: - self.is_adjusting = False # 温湿度达到舒适范围后,设置标志位为False + self.is_adjusting = False + self.update_gui_final() # 假设这里调用最后的更新GUI方法 else: - self.update_gui() # 最后调用一次update_gui以显示最终状态 + pass # 已经调整完毕,无需操作 if __name__ == "__main__":