feat: E1002 顶部加设备电量/温湿度列+server 四路上游并行拉取与响应预算
- 新增 SenseCraft 设备遥测客户端(api-key 走 .env),读电量/充电/温度/湿度,DEVICE_TTL 与设备 300s 上报周期同步 - 大时钟 F84 右侧一列三行左缘对齐:电池(充电绿/≤20% 红)、温度计+蓝色水滴、星期日期(与时钟基线对齐) - server 过期上游由串行改并行,整体最多等 FETCH_BUDGET 秒(默认 5);超预算的一路本次用旧值、后台拉完回填缓存,同一路不重复实拉 - 此前串行且上游 socket 超时 30s,任一路卡住即整图拖到平台抓取超时;「多路 TTL 同时到期」猜想经日志核查不成立,排查顺序写入 CLAUDE.md Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
45
data.py
45
data.py
@@ -21,6 +21,7 @@ import weather_api
|
||||
import claude_usage
|
||||
import codex_usage
|
||||
import btc_api
|
||||
import sensecraft_api
|
||||
|
||||
WEEK_CN = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
|
||||
TZ = ZoneInfo(config.TZ_NAME)
|
||||
@@ -395,6 +396,49 @@ def _usage_gpt(now, raw=None, allow_local=True):
|
||||
return out
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 设备遥测(E1002 电量/温度/湿度)
|
||||
def device_empty():
|
||||
return {"battery": None, "charging": False, "temp": None, "humidity": None}
|
||||
|
||||
|
||||
def device_fetch():
|
||||
"""纯拉取(服务端用):返回 (device dict, True);失败抛出。结构 {battery, charging, temp, humidity}。"""
|
||||
return sensecraft_api.get_iot_data(), True
|
||||
|
||||
|
||||
def _device(now):
|
||||
"""本机形态:带 DEVICE_TTL 文件缓存;失败沿用旧缓存,仍无则占位(渲染为 "--")。"""
|
||||
ts = now.timestamp()
|
||||
cached = _read_json_cache(config.DEVICE_CACHE_PATH)
|
||||
if cached and ts - cached.get("ts", 0) < config.DEVICE_TTL:
|
||||
return cached["data"]
|
||||
try:
|
||||
out, _ = device_fetch()
|
||||
_write_json_cache(config.DEVICE_CACHE_PATH, {"ts": ts, "data": out})
|
||||
return out
|
||||
except Exception as e:
|
||||
print(f"[警告] 设备遥测拉取失败:{e}")
|
||||
if cached:
|
||||
return cached["data"]
|
||||
return device_empty()
|
||||
|
||||
|
||||
def _read_json_cache(path):
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _write_json_cache(path, obj):
|
||||
try:
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(obj, f, ensure_ascii=False)
|
||||
except Exception as e:
|
||||
print(f"[警告] 缓存写入失败 {path}:{e}")
|
||||
|
||||
|
||||
def get_dashboard_data(now=None):
|
||||
"""返回渲染所需的完整数据字典(本机脚本形态:带文件缓存,用量读本地快照)。"""
|
||||
now = now or now_tz()
|
||||
@@ -409,4 +453,5 @@ def get_dashboard_data(now=None):
|
||||
"usage": _usage(now),
|
||||
"usage_gpt": _usage_gpt(now),
|
||||
"btc": _btc(now),
|
||||
"device": _device(now),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user