|
|
@ -7,17 +7,19 @@ import time
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
|
|
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EnvironmentMonitorApp:
|
|
|
|
class EnvironmentMonitorApp:
|
|
|
|
def __init__(self, root):
|
|
|
|
def __init__(self):
|
|
|
|
self.root = root
|
|
|
|
super().__init__()
|
|
|
|
self.root.title("环境监测数据展示")
|
|
|
|
self.title("空调自动调节系统")
|
|
|
|
self.setup_ui()
|
|
|
|
self.geometry("800x600")
|
|
|
|
self.data_records = [] # 保存所有监测记录
|
|
|
|
|
|
|
|
self.plot_window = None # 初始化图表窗口变量
|
|
|
|
# 环境监测实例
|
|
|
|
self.figure = None # 初始化图表figure变量
|
|
|
|
self.env_monitor = EnvironmentMonitor()
|
|
|
|
self.canvas = None # 初始化图表canvas变量
|
|
|
|
|
|
|
|
self.axs = None # 初始化子图变量
|
|
|
|
# 获取初始环境条件
|
|
|
|
|
|
|
|
initial_temp, initial_humid = self.env_monitor.get_current_conditions()
|
|
|
|
|
|
|
|
# 空调调节实例
|
|
|
|
|
|
|
|
self.aircon = AirConditioner(initial_temp, initial_humid)
|
|
|
|
self.root.after(5000, self.update_data_and_display) # 每5秒更新一次数据
|
|
|
|
self.root.after(5000, self.update_data_and_display) # 每5秒更新一次数据
|
|
|
|
|
|
|
|
|
|
|
|
# 设置matplotlib的字体以支持中文显示
|
|
|
|
# 设置matplotlib的字体以支持中文显示
|
|
|
@ -42,8 +44,8 @@ class EnvironmentMonitorApp:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def simulate_data(self):
|
|
|
|
def simulate_data(self):
|
|
|
|
self.temperature = random.uniform(-15, 40)#20-24
|
|
|
|
self.temperature = random.uniform(-15, 45)#18-25
|
|
|
|
self.humidity = random.uniform(0, 100)#40-60
|
|
|
|
self.humidity = random.uniform(0, 100)#30-60
|
|
|
|
self.light_intensity = random.randint(100, 1000)#300-500
|
|
|
|
self.light_intensity = random.randint(100, 1000)#300-500
|
|
|
|
self.data_records.append({
|
|
|
|
self.data_records.append({
|
|
|
|
"序号": len(self.data_records) + 1,
|
|
|
|
"序号": len(self.data_records) + 1,
|
|
|
@ -101,15 +103,36 @@ class EnvironmentMonitorApp:
|
|
|
|
ax.grid(True)
|
|
|
|
ax.grid(True)
|
|
|
|
self.canvas.draw_idle() # 更新图表而不重新绘制整个画布
|
|
|
|
self.canvas.draw_idle() # 更新图表而不重新绘制整个画布
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 环境监测模拟
|
|
|
|
class EnvironmentMonitor:
|
|
|
|
class EnvironmentMonitor:
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
# 这里仅作为示例,实际应用中可能从传感器读取
|
|
|
|
self.temperature = random.uniform(18, 25) # 初始温度范围调整为更合理的室温范围
|
|
|
|
self.temperature = random.uniform(16, 30)
|
|
|
|
self.humidity = random.uniform(30, 60) # 初始湿度范围调整为常见室内湿度范围
|
|
|
|
self.humidity = random.uniform(0, 100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_conditions(self):
|
|
|
|
def get_current_conditions(self):
|
|
|
|
return self.temperature, self.humidity
|
|
|
|
return self.temperature, self.humidity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 空调调节模拟
|
|
|
|
|
|
|
|
class AirConditioner:
|
|
|
|
|
|
|
|
def __init__(self, initial_temperature, initial_humidity):
|
|
|
|
|
|
|
|
self.current_temperature = initial_temperature
|
|
|
|
|
|
|
|
self.current_humidity = initial_humidity
|
|
|
|
|
|
|
|
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(self):
|
|
|
|
|
|
|
|
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]:
|
|
|
|
|
|
|
|
self.current_temperature += 1 if self.current_temperature < self.comfortable_temperature_range[0] else -1
|
|
|
|
|
|
|
|
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
|
|
|
|
|
|
|
|
self.current_humidity += 5 if self.current_humidity < self.comfortable_humidity_range[0] else -5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def display_status(self):
|
|
|
|
|
|
|
|
print(f"当前温度: {self.current_temperature:.1f}°C, 当前湿度: {self.current_humidity:.1f}%")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
root = tk.Tk()
|
|
|
|
root = tk.Tk()
|
|
|
|
app = EnvironmentMonitorApp(root)
|
|
|
|
app = EnvironmentMonitorApp(root)
|
|
|
|