Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bc12a67ea | |||
| 978919de2a |
@@ -246,14 +246,15 @@ async def fetch_weather_data(city=None):
|
||||
if not city:
|
||||
city = config.get("cityid") or "北京"
|
||||
|
||||
print(f"正在获取{city}天气数据...")
|
||||
# 从配置获取API基础URL,默认使用官方API
|
||||
# 读取API URL,可附加其他参数
|
||||
# http://esp.foresh.com/api/ws2/?appid=xxx&appsecert=yyy
|
||||
url = config.get("weather_api_url", "http://esp.foresh.com/api/ws2/")
|
||||
params = {"uuid": uuid(), "city": city}
|
||||
full_url = f"{url}{'&' if '?' in url else '?'}uuid={uuid()}&city={city}"
|
||||
print(f"GET> {full_url}")
|
||||
|
||||
# 发送GET请求
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, params=params) as response:
|
||||
async with session.get(full_url) as response:
|
||||
# 检查响应状态
|
||||
if response.status == 200:
|
||||
# 解析JSON数据
|
||||
@@ -271,7 +272,9 @@ async def fetch_weather_data(city=None):
|
||||
advice = wdata.get("advice", [])
|
||||
lunar = wdata.get("lunar", None)
|
||||
|
||||
ip = wifi_manager.get_ip()
|
||||
if city == "N/A":
|
||||
advice.append("城市配置错误")
|
||||
advice.append(wifi_manager.get_ip())
|
||||
|
||||
display.update_ui(
|
||||
city,
|
||||
@@ -279,7 +282,6 @@ async def fetch_weather_data(city=None):
|
||||
advice,
|
||||
aqi,
|
||||
lunar,
|
||||
ip,
|
||||
envdat={"t": t, "rh": rh, "co2": co2, "pm": pm, "ap": ap},
|
||||
)
|
||||
else:
|
||||
@@ -300,7 +302,6 @@ async def sysinfo_update_task():
|
||||
last_weather = last_ntptime = start_ticks
|
||||
|
||||
while True:
|
||||
task_id = None # task执行失败后,要快速重试
|
||||
try:
|
||||
current_ticks = time.ticks_ms()
|
||||
# 计算时间差,处理溢出
|
||||
@@ -310,23 +311,17 @@ async def sysinfo_update_task():
|
||||
if weather_diff >= weather_ts * 1000:
|
||||
# 更新天气数据
|
||||
gc.collect()
|
||||
task_id = "weather"
|
||||
await fetch_weather_data()
|
||||
last_weather = current_ticks
|
||||
weather_ts = int(config.get("weather_ts", 600)) # 10min
|
||||
elif ntp_diff >= ntptime_ts * 1000:
|
||||
# 更新NTP时间
|
||||
gc.collect()
|
||||
task_id = "ntp"
|
||||
sync_ntp_time()
|
||||
last_ntptime = current_ticks
|
||||
ntptime_ts = int(config.get("ntptime_ts", 3600)) + 13 # 1hour
|
||||
except Exception as e:
|
||||
print(f"定时任务更新错误: {e}")
|
||||
if task_id == "ntp":
|
||||
ntptime_ts = 10
|
||||
elif task_id == "weather":
|
||||
weather_ts = 60
|
||||
|
||||
# 等待x秒再检查(1~30)
|
||||
_x = min(30, 1 + min(weather_ts, ntptime_ts) // 10)
|
||||
|
||||
@@ -49,7 +49,7 @@ class Display:
|
||||
|
||||
# 初始化显示屏
|
||||
self.tft = ST7789(
|
||||
SPI(1, 40_000_000, polarity=1),
|
||||
SPI(1, 40_000_000, polarity=1, phase=1),
|
||||
240,
|
||||
240,
|
||||
dc=Pin(0, Pin.OUT),
|
||||
@@ -68,7 +68,7 @@ class Display:
|
||||
self.tft.init()
|
||||
self.tft.fill(0)
|
||||
self.show_jpg(self.bootimg, 80, 80)
|
||||
self.message("WS2 v1.3.6 (20260203)")
|
||||
self.message("WS2 v1.3.8 (20260208)")
|
||||
|
||||
_print_mem()
|
||||
return True
|
||||
@@ -146,7 +146,7 @@ class Display:
|
||||
self.window("配置设备网络连接", tips, "portal ip: 192.168.4.1")
|
||||
|
||||
# 更新ui数据
|
||||
def update_ui(self, city=None, weather=None, advice=None, aqi=None, lunar=None, ip=None, envdat=None):
|
||||
def update_ui(self, city=None, weather=None, advice=None, aqi=None, lunar=None, envdat=None):
|
||||
self.ticks += 1
|
||||
if self.ui_type == 'default':
|
||||
# 中文的城市名称
|
||||
@@ -161,7 +161,6 @@ class Display:
|
||||
self.ui_data['weather'] = weather
|
||||
# 建议信息可能有很多条,需要轮换展示
|
||||
if advice is not None and advice != self.ui_data.get('advice'):
|
||||
if ip is not None: advice.append(ip)
|
||||
self.ui_data['advice'] = advice
|
||||
# AQI等级分成0-5级,分别对应优、良、中、差、污、恶
|
||||
if aqi is not None and aqi != self.ui_data.get('aqi'):
|
||||
@@ -182,7 +181,7 @@ class Display:
|
||||
if t is not None and t != self.ui_data.get('t'):
|
||||
self.ui_data['t'] = t
|
||||
self.tft.fill_rect(35,179,40,16,0)
|
||||
self.tft.draw(self.vector_font, f"{str(t)}'C", 35,187,0xFFFF,0.5)
|
||||
self.tft.draw(self.vector_font, f"{str(t)}", 35,187,0xFFFF,0.5)
|
||||
if rh is not None and rh != self.ui_data.get('rh'):
|
||||
self.ui_data['rh'] = rh
|
||||
self.tft.fill_rect(110,179,40,16,0)
|
||||
|
||||
@@ -129,7 +129,7 @@ WS2是一款基于ESP8266的桌面气象站,能够实时显示天气信息、
|
||||
href="https://iot.foresh.com/git/kicer/ws2/releases"
|
||||
target="_blank"
|
||||
>
|
||||
ws2-firmware-v1.3.6-4M.bin </a
|
||||
ws2-firmware-xxx-4M.bin </a
|
||||
><span class="badge badge-success">开源</span></div><div class="list-item"><strong>协议:</strong> HTTP REST API
|
||||
</div><div class="list-item"><strong>更新频率:</strong> 每小时
|
||||
</div></div></div></div><h3 class="mt-3">开放源码</h3><a
|
||||
|
||||
Reference in New Issue
Block a user