|
|
@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
|
|
|
from tkinter import ttk
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from 环境监测 import EnvironmentMonitor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_initial_conditions():
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
生成初始的温度和湿度条件,模拟从环境监测系统获取数据。
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
initial_temperature = random.uniform(16, 30) # 模拟从环境监测获得的温度
|
|
|
|
|
|
|
|
initial_humidity = random.uniform(0, 100) # 模拟从环境监测获得的湿度
|
|
|
|
|
|
|
|
return initial_temperature, initial_humidity
|
|
|
|
|
|
|
|
class AirConditioner:
|
|
|
|
|
|
|
|
def __init__(self, initial_temperature=None, initial_humidity=None):
|
|
|
|
|
|
|
|
if initial_temperature is not None:
|
|
|
|
|
|
|
|
self.current_temperature = initial_temperature
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.current_temperature = random.uniform(16, 30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if initial_humidity is not None:
|
|
|
|
|
|
|
|
self.current_humidity = initial_humidity
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.current_humidity = random.uniform(0, 100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, initial_temperature, initial_humidity):
|
|
|
|
|
|
|
|
self.current_temperature = initial_temperature
|
|
|
|
|
|
|
|
self.current_humidity = initial_humidity
|
|
|
|
|
|
|
|
self.current_temperature = random.uniform(16, 30)
|
|
|
|
|
|
|
|
self.current_humidity = random.uniform(0, 100)
|
|
|
|
|
|
|
|
self.min_temperature = 16
|
|
|
|
|
|
|
|
self.max_temperature = 30
|
|
|
|
|
|
|
|
self.min_humidity = 0
|
|
|
|
|
|
|
|
self.max_humidity = 100
|
|
|
|
|
|
|
|
self.comfortable_temperature_range = (20, 24) # 舒适温度范围
|
|
|
|
|
|
|
|
self.comfortable_humidity_range = (40, 60) # 舒适湿度范围
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def adjust_temperature(self):
|
|
|
|
|
|
|
|
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[
|
|
|
|
|
|
|
|
1]:
|
|
|
|
|
|
|
|
if self.current_temperature < self.comfortable_temperature_range[0]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.current_temperature += 1
|
|
|
|
|
|
|
|
elif self.current_temperature > self.comfortable_temperature_range[1]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.current_temperature -= 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def adjust_humidity(self):
|
|
|
|
|
|
|
|
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
|
|
|
|
|
|
|
|
if self.current_humidity < self.comfortable_humidity_range[0]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.current_humidity += 5
|
|
|
|
|
|
|
|
elif self.current_humidity > self.comfortable_humidity_range[1]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.current_humidity -= 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def display_status(self):
|
|
|
|
|
|
|
|
print(f"当前温度: {self.current_temperature:.1f}°C, 当前湿度: {self.current_humidity:.1f}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main_loop(self, root): # 接受root参数
|
|
|
|
|
|
|
|
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.display_status()
|
|
|
|
|
|
|
|
root.after(1000, self.main_loop, root) # 使用after而非sleep
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
print("温度和湿度均在舒适范围内,停止调节。")
|
|
|
|
|
|
|
|
print(f"温度: {self.current_temperature:.1f}°C, 湿度: {self.current_humidity:.1f}%")
|
|
|
|
|
|
|
|
# 可能需要在这里调用一次update_gui以最终更新GUI显示结果
|
|
|
|
|
|
|
|
# self.app.update_gui_final() 如果在ACApp类中实现了这个方法来显示最终状态
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ACApp(tk.Tk):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.title("空调自动调节系统")
|
|
|
|
|
|
|
|
self.geometry("800x600") # 调整窗口大小以适应内容
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 从环境监测模拟获取初始温度和湿度
|
|
|
|
|
|
|
|
self.initial_temperature, self.initial_humidity = generate_initial_conditions()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化空调对象,传入初始温度和湿度
|
|
|
|
|
|
|
|
self.aircon = AirConditioner(
|
|
|
|
|
|
|
|
initial_temperature=self.initial_temperature,
|
|
|
|
|
|
|
|
initial_humidity=self.initial_humidity
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 创建Treeview表格
|
|
|
|
|
|
|
|
self.treeview = ttk.Treeview(self, columns=("编号", "初始温度", "初始湿度", "调节后温度", "调节后湿度"),
|
|
|
|
|
|
|
|
show="headings")
|
|
|
|
|
|
|
|
self.treeview.heading("编号", text="编号")
|
|
|
|
|
|
|
|
self.treeview.heading("初始温度", text="初始温度")
|
|
|
|
|
|
|
|
self.treeview.heading("初始湿度", text="初始湿度")
|
|
|
|
|
|
|
|
self.treeview.heading("调节后温度", text="调节后温度")
|
|
|
|
|
|
|
|
self.treeview.heading("调节后湿度", text="调节后湿度")
|
|
|
|
|
|
|
|
self.treeview.column("编号", width=50, anchor=tk.CENTER)
|
|
|
|
|
|
|
|
self.treeview.column("初始温度", width=80, anchor=tk.CENTER)
|
|
|
|
|
|
|
|
self.treeview.column("初始湿度", width=80, anchor=tk.CENTER)
|
|
|
|
|
|
|
|
self.treeview.column("调节后温度", width=80, anchor=tk.CENTER)
|
|
|
|
|
|
|
|
self.treeview.column("调节后湿度", width=80, anchor=tk.CENTER)
|
|
|
|
|
|
|
|
self.treeview.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.aircon = AirConditioner()
|
|
|
|
|
|
|
|
self.line_number = 1 # 初始化行编号
|
|
|
|
|
|
|
|
self.is_adjusting = True # 添加标志位,判断是否仍在调节过程中
|
|
|
|
|
|
|
|
self.update_gui() # 先更新一次GUI
|
|
|
|
|
|
|
|
self.aircon.main_loop(self) # 传递self给main_loop以便使用after方法
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.treeview.pack(expand=True, fill=tk.BOTH, padx=10, pady=10) # 让Treeview自适应填充空间
|
|
|
|
|
|
|
|
self.rowconfigure(0, weight=1) # 让树状视图所在的行填充额外空间
|
|
|
|
|
|
|
|
self.columnconfigure(0, weight=1) # 让树状视图所在的列填充额外空间
|
|
|
|
|
|
|
|
def update_gui(self):
|
|
|
|
|
|
|
|
if self.is_adjusting: # 只在调节过程中更新数据
|
|
|
|
|
|
|
|
# 更新Treeview中的数据,这里假设初始数据就是当前数据,实际应用中可能需要记录调节前的实际初始值
|
|
|
|
|
|
|
|
self.treeview.insert("", tk.END, values=(self.line_number,
|
|
|
|
|
|
|
|
f"{self.aircon.current_temperature:.1f}°C",
|
|
|
|
|
|
|
|
f"{self.aircon.current_humidity:.1f}%",
|
|
|
|
|
|
|
|
f"{self.aircon.current_temperature:.1f}°C",
|
|
|
|
|
|
|
|
f"{self.aircon.current_humidity:.1f}%"))
|
|
|
|
|
|
|
|
self.line_number += 1
|
|
|
|
|
|
|
|
else: # 调节完成后,添加最终状态
|
|
|
|
|
|
|
|
if self.line_number == 1: # 如果还没有插入过数据,则插入第一行
|
|
|
|
|
|
|
|
self.treeview.insert("", tk.END, values=(self.line_number,
|
|
|
|
|
|
|
|
f"{self.aircon.current_temperature:.1f}°C",
|
|
|
|
|
|
|
|
f"{self.aircon.current_humidity:.1f}%",
|
|
|
|
|
|
|
|
f"{self.aircon.current_temperature:.1f}°C",
|
|
|
|
|
|
|
|
f"{self.aircon.current_humidity:.1f}%"))
|
|
|
|
|
|
|
|
self.line_number += 1
|
|
|
|
|
|
|
|
print("温度和湿度均在舒适范围内,停止调节。")
|
|
|
|
|
|
|
|
print(f"温度: {self.current_temperature:.1f}°C, 湿度: {self.current_humidity:.1f}%")
|
|
|
|
|
|
|
|
self.is_adjusting = False # 设置标志位为False,停止调节后的数据更新
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main_loop(self, root):
|
|
|
|
|
|
|
|
if self.is_adjusting:
|
|
|
|
|
|
|
|
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.display_status()
|
|
|
|
|
|
|
|
root.after(1000, self.main_loop, root)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.is_adjusting = False # 温湿度达到舒适范围后,设置标志位为False
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.update_gui() # 最后调用一次update_gui以显示最终状态
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
env_monitor = EnvironmentMonitor() # 创建环境监测实例
|
|
|
|
|
|
|
|
current_temp, current_humidity = env_monitor.get_current_conditions() # 获取当前环境温湿度
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = ACApp(current_temp, current_humidity) # 传入实际的温湿度值初始化ACApp
|
|
|
|
|
|
|
|
app.mainloop()
|