Files
eink-push/pusher.py
YANG JIANKUAN 2121b1eff4 refactor: 推送改为局域网直连设备,推送前清空画廊旧图
原走 Zectrix 公网 API + 设备 MAC,现改为直连局域网墨水屏设备(喵喵固件)
的 HTTP 接口:POST /upload 推送 PNG 触发刷屏。设备画廊按追加保存,故推送
前先 GET /images 清空旧图,避免图片越推越多、且避免 slideshow 模式轮播
到过期图。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-07-07 10:19:10 +08:00

47 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
推送层 —— 直连局域网墨水屏设备(喵喵固件,仅用标准库 urllib无第三方依赖
固件开源https://gitee.com/gxp666111/miaomiaoweb/ 为设备自带管理页,可在浏览器打开
http://<EPD_HOST>/ 调试同一套接口)。
"""
import json
import urllib.parse
import urllib.request
import urllib.error
import config
def _request(method, path, headers=None, body=None):
url = f"http://{config.EPD_HOST}{path}"
req = urllib.request.Request(url, data=body, headers=headers or {}, method=method)
try:
with urllib.request.urlopen(req, timeout=30) as resp:
return resp.read().decode("utf-8", "ignore")
except urllib.error.HTTPError as e:
raise RuntimeError(f"HTTP {e.code}: {e.read().decode('utf-8', 'ignore')}") from e
def _list_gallery_names():
text = _request("GET", "/images")
return [item["name"] for item in json.loads(text).get("items", [])]
def _clear_gallery():
"""设备画廊按追加保存,用完即删,使其始终只留最新一张。单张删除失败不影响后续上传。"""
for name in _list_gallery_names():
try:
_request("POST", f"/delete_image?name={urllib.parse.quote(name)}")
except Exception as e:
print(f"[推送] 清理画廊旧图 {name} 失败:{e}")
def push_image(image_path):
"""推送图片到局域网墨水屏设备:先清空画廊旧图,再上传新图触发显示刷新。"""
_clear_gallery()
with open(image_path, "rb") as fp:
content = fp.read()
resp = _request("POST", "/upload", headers={"Content-Type": "image/png"}, body=content)
return config.EPD_HOST, resp