mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-15 07:03:16 +08:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d9959cd77 | ||
|
|
10f8b8182b | ||
|
|
469aec331a | ||
|
|
a8247c1932 | ||
|
|
f37acd5a20 | ||
|
|
6836c448be | ||
|
|
2efc2c4f5d | ||
|
|
01f62f696b | ||
|
|
e8b3668ed7 | ||
|
|
b840257988 | ||
|
|
3541ecc135 |
22
README.md
22
README.md
@@ -66,6 +66,7 @@ sudo reboot
|
||||
--zh 设置系统语言为zh_CN,UTF-8
|
||||
--cn 替换apt镜像源为中国镜像源
|
||||
--gitcn 克隆中国仓库
|
||||
--pip-offline pip依赖离线安装
|
||||
--pisugar-wifi-conf 安装pisugar-wifi-conf
|
||||
--pisugar-power-manager 安装pisugar-power-manager
|
||||
--version <tag> 版本号(使用方法 --version + 仓库标签,格式例如 v1.x.x ,可以是主仓库main)
|
||||
@@ -73,22 +74,29 @@ sudo reboot
|
||||
```
|
||||
###
|
||||
```Bash
|
||||
#中国源默认设置
|
||||
#中国源默认设置(不加参数)
|
||||
curl -sSL https://gitee.com/xingguangk/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash
|
||||
```
|
||||
```Bash
|
||||
#中国源全参数设置(不使用--debug参数)
|
||||
curl -sSL https://gitee.com/xingguangk/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --zh --cn --gitcn --pisugar-power-manager --pisugar-wifi-conf --version <tag>
|
||||
#中国源参数设置(不使用--debug参数,替换cn镜像源,pip在线安装,不安装pisugar)
|
||||
curl -sSL https://gitee.com/xingguangk/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --zh --cn --gitcn --version <tag>
|
||||
```
|
||||
```Bash
|
||||
#默认源默认设置
|
||||
#中国源参数设置(不使用--debug参数,替换cn镜像源,pip依赖离线安装,不安装pisugar)
|
||||
curl -sSL https://gitee.com/xingguangk/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --zh --cn --gitcn --pip-offline --version <tag>
|
||||
```
|
||||
```Bash
|
||||
#默认源默认设置(不加参数)
|
||||
curl -sSL https://github.com/kxgx/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash
|
||||
```
|
||||
```Bash
|
||||
#默认源全参数设置(不使用--debug参数)
|
||||
curl -sSL https://github.com/kxgx/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --zh --cn --gitcn --pisugar-power-manager --pisugar-wifi-conf --version <tag>
|
||||
#默认源默认设置(不使用--debug参数,替换cn镜像源,pip在线安装,不安装pisugar)
|
||||
curl -sSL https://github.com/kxgx/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --zh --cn --gitcn --version <tag>
|
||||
```
|
||||
```Bash
|
||||
#默认源默认设置(不使用--debug参数,替换cn镜像源,pip依赖离线安装,不安装pisugar)
|
||||
curl -sSL https://github.com/kxgx/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --zh --cn --gitcn --pip-offline --version <tag>
|
||||
```
|
||||
|
||||
## 需要安装的软件和依赖:
|
||||
参考
|
||||
微雪电子 https://www.waveshare.net/wiki/2.13inch_e-Paper_HAT+#Raspberry_Pi
|
||||
|
||||
@@ -18,6 +18,8 @@ USE_CN_GIT=false
|
||||
USE_PISUGAR_WIFI_CONF=false
|
||||
# 检查是否安装pisugar-power-manager
|
||||
USE_PISUGAR_POWER_MANAGER=false
|
||||
# 检查是否使用离线安装pip依赖
|
||||
USE_OFFLINE_PIP=false
|
||||
|
||||
# 解析命令行参数
|
||||
while [ "$#" -gt 0 ]; do
|
||||
@@ -31,6 +33,9 @@ while [ "$#" -gt 0 ]; do
|
||||
--gitcn)
|
||||
USE_CN_GIT=true
|
||||
;;
|
||||
--pip-offline)
|
||||
USE_OFFLINE_PIP=true
|
||||
;;
|
||||
--pisugar-wifi-conf)
|
||||
USE_PISUGAR_WIFI_CONF=true
|
||||
;;
|
||||
@@ -203,23 +208,7 @@ install_packages() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 安装pip包函数
|
||||
install_pip_packages() {
|
||||
echo "正在安装pip软件包"
|
||||
if ! sudo pip3 install -i "$PIPY_MIRROR" spidev borax pillow requests Flask; then
|
||||
echo "pip软件包安装失败,如果是最新版系统或是非lite系统" >&2
|
||||
echo "请手动运行sudo pip3 install -i "$PIPY_MIRROR" spidev borax pillow requests --break-system-packages" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 复制服务文件并设置为开机启动
|
||||
setup_service() {
|
||||
local service_path="raspi_e-Paper.service"
|
||||
local service1_path="e-Paper_clean.service"
|
||||
local service_file_path="$HOME/2.13-Ink-screen-clock/bin/$service_path"
|
||||
local service1_file_path="$HOME/2.13-Ink-screen-clock/bin/$service1_path"
|
||||
|
||||
install_Ink-screen-clock() {
|
||||
# 检查墨水屏时钟仓库是否存在
|
||||
if [ ! -d "$HOME/2.13-Ink-screen-clock" ]; then
|
||||
echo "正在克隆仓库"
|
||||
@@ -234,6 +223,41 @@ setup_service() {
|
||||
else
|
||||
echo "仓库文件夹已存在,跳过克隆"
|
||||
fi
|
||||
}
|
||||
|
||||
# 安装pip包函数
|
||||
install_oline_pip_packages() {
|
||||
echo "正在安装pip软件包"
|
||||
if ! sudo pip3 install -i "$PIPY_MIRROR" -r "$HOME/2.13-Ink-screen-clock/bin/requirements.txt"; then
|
||||
echo "pip软件包安装失败,如果是最新版系统或是非lite系统" >&2
|
||||
echo "请手动运行sudo pip3 install -i "$PIPY_MIRROR" -r "$HOME/2.13-Ink-screen-clock/bin/requirements.txt" --break-system-packages" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
install_offline_pip_packages() {
|
||||
echo "正在安装pip软件包"
|
||||
if ! sudo pip3 install --no-index --find-links="$HOME/2.13-Ink-screen-clock/bin/vendor" -r "$HOME/2.13-Ink-screen-clock/bin/requirements.txt"; then
|
||||
echo "pip软件包安装失败,如果是最新版系统或是非lite系统" >&2
|
||||
echo "请手动运行sudo pip3 install --no-index --find-links="$HOME/2.13-Ink-screen-clock/bin/vendor" -r "$HOME/2.13-Ink-screen-clock/bin/requirements.txt" --break-system-packages" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
install_pip_packages() {
|
||||
if [ "$USE_OFFLINE_PIP" = true ]; then
|
||||
install_offline_pip_packages
|
||||
else
|
||||
install_oline_pip_packages
|
||||
fi
|
||||
}
|
||||
|
||||
# 复制服务文件并设置为开机启动
|
||||
setup_service() {
|
||||
local service_path="raspi_e-Paper.service"
|
||||
local service1_path="e-Paper_clean.service"
|
||||
local service_file_path="$HOME/2.13-Ink-screen-clock/bin/$service_path"
|
||||
local service1_file_path="$HOME/2.13-Ink-screen-clock/bin/$service1_path"
|
||||
|
||||
# 检查服务文件是否存在
|
||||
if [ -f "$service_file_path" ] && [ -f "$service1_file_path" ]; then
|
||||
@@ -306,6 +330,7 @@ if [ -f /etc/debian_version ]; then
|
||||
echo "执行Debian 11 (Bullseye) 相关操作"
|
||||
update_sources_list "bullseye"
|
||||
install_packages
|
||||
install_Ink-screen-clock
|
||||
install_pip_packages
|
||||
setup_service
|
||||
#install_webui
|
||||
@@ -316,6 +341,7 @@ if [ -f /etc/debian_version ]; then
|
||||
echo "执行Debian 12 (Bookworm) 相关操作"
|
||||
update_sources_list "bookworm"
|
||||
install_packages
|
||||
install_Ink-screen-clock
|
||||
install_pip_packages
|
||||
setup_service
|
||||
#install_webui
|
||||
|
||||
47
bin/main.py
47
bin/main.py
@@ -55,28 +55,28 @@ def set_system_time_from_hwclock(utc=True):
|
||||
hwclock_args = ['sudo', 'hwclock', '--hctosys']
|
||||
if not utc:
|
||||
hwclock_args.append('--localtime')
|
||||
|
||||
|
||||
logging.debug(f"Executing hwclock command: {' '.join(hwclock_args)}")
|
||||
|
||||
|
||||
# 使用 subprocess.run 执行 hwclock --hctosys 并捕获输出
|
||||
result = subprocess.run(hwclock_args,
|
||||
check=True, # 如果命令失败,则抛出 CalledProcessError 异常
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True)
|
||||
|
||||
|
||||
# 等待一小段时间以确保时间更新完成
|
||||
time.sleep(0.1) # 根据需要调整
|
||||
|
||||
|
||||
# 记录调用 hwclock 后的时间
|
||||
after_time = datetime.datetime.now()
|
||||
logging.debug("System time successfully set from hardware clock.")
|
||||
logging.debug(f"System time after hwclock call: {after_time}")
|
||||
|
||||
|
||||
# 检查时间是否发生了倒退
|
||||
if after_time < before_time:
|
||||
logging.warning(f"Time went backwards after hwclock call: before={before_time}, after={after_time}")
|
||||
|
||||
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
logging.error(f"Failed to set system time from hardware clock: {e.stderr}")
|
||||
@@ -87,18 +87,17 @@ def set_system_time_from_hwclock(utc=True):
|
||||
|
||||
def get_time():
|
||||
global has_set_system_time
|
||||
|
||||
|
||||
if not has_set_system_time:
|
||||
# 尝试从硬件时钟设置系统时间,默认假设 RTC 是 UTC
|
||||
success = set_system_time_from_hwclock(utc=True)
|
||||
# 无论成功与否,都更新标志变量以避免重复尝试
|
||||
has_set_system_time = True
|
||||
|
||||
|
||||
# 获取并返回当前时间,格式为 HH:MM 大写
|
||||
current_time = time.strftime('%H:%M').upper()
|
||||
logging.debug(f"Returning current time: {current_time}")
|
||||
return current_time
|
||||
|
||||
def Get_ipv4_address(): # 获取当前的IP地址
|
||||
try:
|
||||
ip_output = subprocess.check_output(
|
||||
@@ -108,14 +107,36 @@ def Get_ipv4_address(): # 获取当前的IP地址
|
||||
return filtered_ips[0] if filtered_ips else "地址获取失败"
|
||||
except subprocess.CalledProcessError:
|
||||
return "获取失败"
|
||||
|
||||
# 全局变量声明
|
||||
last_power = None # 用于存储上一次获取的电量值
|
||||
last_power_time = 0 # 用于存储上一次获取电量的时间戳
|
||||
|
||||
def power_battery(): # 获取当前电池电量
|
||||
return str(int(subprocess.check_output(
|
||||
u"echo \"get battery\" | nc -q 0 127.0.0.1 8423|awk -F':' '{print int($2)}'",
|
||||
shell=True).decode('gbk'))) + u'%'
|
||||
global last_power, last_power_time
|
||||
current_time = time.time() # 获取当前时间戳
|
||||
|
||||
# 每3分钟或首次获取时更新
|
||||
if (current_time - last_power_time >= 180) or (last_power is None):
|
||||
try:
|
||||
# 执行命令获取电池电量
|
||||
result = subprocess.check_output(
|
||||
u"echo \"get battery\" | nc -q 0 127.0.0.1 8423 | awk -F':' '{print int($2)}'",
|
||||
shell=True, stderr=subprocess.STDOUT
|
||||
).decode('gbk').strip()
|
||||
new_power = f"{int(result)}%" # 格式化电量值
|
||||
|
||||
# 仅在电量变化或首次时更新显示
|
||||
if new_power != last_power or last_power is None:
|
||||
logging.info(f"电池电量更新: {new_power}")
|
||||
last_power = new_power # 更新缓存值
|
||||
last_power_time = current_time # 更新获取时间
|
||||
except Exception as e:
|
||||
logging.error(f"电量获取失败: {str(e)}")
|
||||
new_power = last_power if last_power else "0%" # 失败时使用缓存值或默认值
|
||||
else:
|
||||
new_power = last_power # 未到更新时间,使用缓存值
|
||||
|
||||
return new_power
|
||||
def Bottom_edge(): # 在图片中添加底边内容
|
||||
draw.rectangle((0, 105, 250, 122), 'black', 'black')
|
||||
'''电池图标画图'''
|
||||
|
||||
8
bin/requirements.txt
Normal file
8
bin/requirements.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
borax==4.1.2
|
||||
certifi==2025.1.31
|
||||
charset-normalizer==3.4.1
|
||||
idna==3.10
|
||||
pillow==11.1.0
|
||||
requests==2.32.3
|
||||
spidev==3.6
|
||||
urllib3==2.3.0
|
||||
@@ -11,4 +11,4 @@ do
|
||||
kill -9 $id
|
||||
done
|
||||
nohup /usr/bin/python3 -u $dir/$f_name > $logdir/info.log 2>&1 &
|
||||
nohup /usr/bin/python3 -u $dir/$f1_name > $logdir/info-wenter.log 2>&1 &
|
||||
nohup /usr/bin/python3 -u $dir/$f1_name > $logdir/info-weather.log 2>&1 &
|
||||
|
||||
BIN
bin/vendor/borax-4.1.2-py3-none-any.whl
vendored
Normal file
BIN
bin/vendor/borax-4.1.2-py3-none-any.whl
vendored
Normal file
Binary file not shown.
BIN
bin/vendor/certifi-2025.1.31-py3-none-any.whl
vendored
Normal file
BIN
bin/vendor/certifi-2025.1.31-py3-none-any.whl
vendored
Normal file
Binary file not shown.
BIN
bin/vendor/charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
vendored
Normal file
BIN
bin/vendor/charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
vendored
Normal file
Binary file not shown.
BIN
bin/vendor/idna-3.10-py3-none-any.whl
vendored
Normal file
BIN
bin/vendor/idna-3.10-py3-none-any.whl
vendored
Normal file
Binary file not shown.
BIN
bin/vendor/pillow-11.1.0-cp39-cp39-manylinux_2_28_aarch64.whl
vendored
Normal file
BIN
bin/vendor/pillow-11.1.0-cp39-cp39-manylinux_2_28_aarch64.whl
vendored
Normal file
Binary file not shown.
BIN
bin/vendor/requests-2.32.3-py3-none-any.whl
vendored
Normal file
BIN
bin/vendor/requests-2.32.3-py3-none-any.whl
vendored
Normal file
Binary file not shown.
BIN
bin/vendor/spidev-3.6.tar.gz
vendored
Normal file
BIN
bin/vendor/spidev-3.6.tar.gz
vendored
Normal file
Binary file not shown.
BIN
bin/vendor/urllib3-2.3.0-py3-none-any.whl
vendored
Normal file
BIN
bin/vendor/urllib3-2.3.0-py3-none-any.whl
vendored
Normal file
Binary file not shown.
127
bin/weather.py
127
bin/weather.py
@@ -7,83 +7,92 @@ import logging
|
||||
import requests
|
||||
from threading import Timer
|
||||
|
||||
white = 255 # 颜色
|
||||
black = 0
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s') # 设置日志级别
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
def get_area_id(city_name):
|
||||
"""从city.js中检索AREAID,无限重试直到成功"""
|
||||
url = "https://j.i8tq.com/weather2020/search/city.js"
|
||||
while True:
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
city_data_text = response.text.strip().split('var city_data = ')[-1].rstrip(';')
|
||||
if city_data_text:
|
||||
city_data = json.loads(city_data_text)
|
||||
for province, cities in city_data.items():
|
||||
for city, districts in cities.items():
|
||||
for district, info in districts.items():
|
||||
if info['NAMECN'] == city_name:
|
||||
return info['AREAID']
|
||||
logging.error("城市名称 '%s' 在城市数据中未找到", city_name)
|
||||
else:
|
||||
logging.error("从city.js接收到的数据为空")
|
||||
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||
logging.error("检索或解析城市数据时发生错误: %s", e)
|
||||
time.sleep(180) # 重试前等待
|
||||
resp = requests.get(url, timeout=10)
|
||||
resp.encoding = 'utf-8' # 显式设置编码
|
||||
city_data = json.loads(resp.text.split('=', 1)[1].rstrip(';'))
|
||||
for province in city_data.values():
|
||||
for city in province.values():
|
||||
for district, info in city.items():
|
||||
if info['NAMECN'] == city_name:
|
||||
return info['AREAID']
|
||||
logging.error("未找到城市: %s", city_name)
|
||||
except Exception as e:
|
||||
logging.error("获取城市ID失败: %s", str(e))
|
||||
time.sleep(180)
|
||||
|
||||
def get_current_city():
|
||||
"""获取当前城市名称,无限重试直到成功"""
|
||||
"""获取当前定位城市并去除'市'后缀"""
|
||||
url = "http://ip-api.com/json/?lang=zh-CN"
|
||||
while True:
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
data = response.json()
|
||||
resp = requests.get(url, timeout=10)
|
||||
resp.encoding = 'utf-8' # 显式设置编码
|
||||
data = resp.json()
|
||||
if data['status'] == 'success':
|
||||
return data['city']
|
||||
else:
|
||||
logging.error("获取当前城市失败: %s", data['message'])
|
||||
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||
logging.error("检索或解析当前城市时出现错误: %s", e)
|
||||
time.sleep(180) # 重试前等待
|
||||
return data['city'].rstrip('市')
|
||||
logging.error("定位失败: %s", data.get('message'))
|
||||
except Exception as e:
|
||||
logging.error("定位异常: %s", str(e))
|
||||
time.sleep(180)
|
||||
|
||||
def schedule_getWeath():
|
||||
"""调度 getWeath 函数每3分钟执行一次"""
|
||||
getWeath()
|
||||
Timer(180, schedule_getWeath).start() # 重新设置定时器
|
||||
"""定时任务调度"""
|
||||
try:
|
||||
getWeath()
|
||||
finally:
|
||||
Timer(180, schedule_getWeath).start()
|
||||
|
||||
def getWeath(city='101060101'):
|
||||
def getWeath(default_city='101060101'):
|
||||
"""获取天气数据核心函数"""
|
||||
city_name = get_current_city()
|
||||
area_id = default_city # 设置默认值
|
||||
|
||||
if city_name:
|
||||
try:
|
||||
area_id = get_area_id(city_name) or default_city
|
||||
except Exception as e:
|
||||
logging.error("获取区域ID失败: %s", str(e))
|
||||
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Referer': 'https://www.weather.com.cn/'
|
||||
}
|
||||
current_city = get_current_city()
|
||||
if current_city:
|
||||
area_id = get_area_id(current_city)
|
||||
if area_id:
|
||||
city = area_id
|
||||
|
||||
try:
|
||||
response = requests.get(f'https://d1.weather.com.cn/sk_2d/{city}.html', headers=headers)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
response.encoding = 'utf-8'
|
||||
weather_data = response.text[11:]
|
||||
with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w') as fileHandle:
|
||||
fileHandle.write(weather_data)
|
||||
print("天气文件更新")
|
||||
except requests.RequestException as e:
|
||||
logging.error("获取天气数据时出现网络错误: %s", e)
|
||||
resp = requests.get(
|
||||
f'https://d1.weather.com.cn/sk_2d/{area_id}.html',
|
||||
headers=headers,
|
||||
timeout=15
|
||||
)
|
||||
resp.encoding = 'utf-8' # 关键修改:强制设置响应编码
|
||||
resp.raise_for_status()
|
||||
|
||||
# 直接处理原始字节数据
|
||||
weather_data = resp.content[11:].decode('utf-8')
|
||||
|
||||
# 使用utf-8编码写入文件
|
||||
with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w', encoding='utf-8') as f:
|
||||
f.write(weather_data)
|
||||
|
||||
logging.info("天气数据更新成功")
|
||||
except Exception as e:
|
||||
logging.error("发生了意外错误: %s", e)
|
||||
logging.error("天气更新失败: %s", str(e))
|
||||
|
||||
try:
|
||||
schedule_getWeath() # 开始调度天气获取函数
|
||||
except KeyboardInterrupt:
|
||||
logging.info("检测到键盘中断,正在退出")
|
||||
except Exception as e:
|
||||
logging.error("发生了意外错误: %s", e)
|
||||
exit()
|
||||
|
||||
# 脚本正常结束后的清理操作
|
||||
exit()
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
schedule_getWeath()
|
||||
while True:
|
||||
time.sleep(1) # 保持主线程存活
|
||||
except KeyboardInterrupt:
|
||||
logging.info("程序已终止")
|
||||
Reference in New Issue
Block a user