#!./env/bin/python
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from pylab import mpl

# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]

# 初始化一个起始日期（这里假设是今天凌晨0点）
start_date = datetime.now().replace(hour=20, minute=21, second=0, microsecond=0)

with open('minicom.cap', 'r') as file:
    lines = file.readlines()

times = []
temperatures = []

for line in lines:
    time_str, temp_str = line.strip().split('temp = ')
    time = start_date + timedelta(seconds=int(time_str.split(':')[2])) + \
                        timedelta(minutes=int(time_str.split(':')[1])) + \
                        timedelta(hours=int(time_str.split(':')[0]))
    temperature = float(temp_str)
    if len(temperatures) > 0:
        pre_temp = temperatures[-1]
    else:
        pre_temp = temperature
    if pre_temp-0.5 < temperature < pre_temp+0.5:
        times.append(time)
        temperatures.append(temperature)

# 确保时间序列是排序的
times.sort()

# 绘制折线图
plt.plot(times, temperatures)
plt.xlabel('时间')
plt.ylabel('温度 (摄氏度)')
plt.title('温度随时间变化图')
plt.xticks(rotation=45)  # 旋转x轴标签以更好地展示时间

# 自动调整x轴刻度以显示日期时间格式
plt.gcf().autofmt_xdate()

# 显示图形
plt.savefig("img.png")
plt.show()
