mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-15 07:03:16 +08:00
Compare commits
119 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c90e54728 | ||
|
|
e70b05a529 | ||
|
|
d8c12f7ad3 | ||
|
|
8eb21afb19 | ||
|
|
7bc892d06b | ||
|
|
ca67499854 | ||
|
|
0b5623d5f6 | ||
|
|
7a71514224 | ||
|
|
fac4c591fb | ||
|
|
95e6bb12be | ||
|
|
57ea23a6fa | ||
|
|
5262d729fb | ||
|
|
428d056e58 | ||
|
|
066d9676f3 | ||
|
|
54bb2ccf21 | ||
|
|
47e6ae681a | ||
|
|
7e15bf4172 | ||
|
|
7eeb7ceb12 | ||
|
|
6a498cc04f | ||
|
|
7e302d0843 | ||
|
|
04930156b6 | ||
|
|
5543bc6710 | ||
|
|
0fe27553f4 | ||
|
|
9fe9e9f71a | ||
|
|
3752c55b17 | ||
|
|
620f363916 | ||
|
|
b184f08914 | ||
|
|
8efc57e655 | ||
|
|
a4e39c00fe | ||
|
|
62dc79de18 | ||
|
|
d58d806da5 | ||
|
|
693595ef0f | ||
|
|
08745ea6d4 | ||
|
|
742c46f4f2 | ||
|
|
256d2fd2cc | ||
|
|
eca6a07273 | ||
|
|
e37bdcc9bf | ||
|
|
4f3d04f357 | ||
|
|
d6715fa5d3 | ||
|
|
ff6995de24 | ||
|
|
4d675b55af | ||
|
|
f002a2fc9b | ||
|
|
7597b2a35f | ||
|
|
533374daa3 | ||
|
|
531f5a443b | ||
|
|
e1a0fdaa34 | ||
|
|
d5224e81cc | ||
|
|
a9021dc262 | ||
|
|
2485a83f2c | ||
|
|
0b7f6c4dfc | ||
|
|
bca24803a5 | ||
|
|
75528eba1c | ||
|
|
b159a7a402 | ||
|
|
c5c7b055e3 | ||
|
|
69d32309a7 | ||
|
|
cb89e6cd5c | ||
|
|
902c237010 | ||
|
|
20f8a5a181 | ||
|
|
fa4c0e5a9e | ||
|
|
c35ec31440 | ||
|
|
9d76efe6ee | ||
|
|
f4752268e8 | ||
|
|
90ae8aab3d | ||
|
|
3a3407c735 | ||
|
|
a4f140966a | ||
|
|
d0adc8b213 | ||
|
|
21f637f1cf | ||
|
|
bebbbc554c | ||
|
|
c8419c2fe8 | ||
|
|
9ec0b39b16 | ||
|
|
6ec682cb1a | ||
|
|
a9809e68f1 | ||
|
|
67fd8f3c2d | ||
|
|
e4802e0ee4 | ||
|
|
b3feb79540 | ||
|
|
5aed29edac | ||
|
|
ac7c8b2378 | ||
|
|
ee5d2bb1ba | ||
|
|
0d849f7a7e | ||
|
|
78f7f9b597 | ||
|
|
4baaf25d44 | ||
|
|
a2df4e6162 | ||
|
|
f0541b1ee0 | ||
|
|
b30d12742e | ||
|
|
aae214de87 | ||
|
|
34d2ea6840 | ||
|
|
df98d198d0 | ||
|
|
d6d86a0424 | ||
|
|
d32459ce9d | ||
|
|
6b5b8a935a | ||
|
|
0db4d98671 | ||
|
|
6bf064e941 | ||
|
|
1ae2f31142 | ||
|
|
380bea6607 | ||
|
|
964811eac8 | ||
|
|
e2fb9777b1 | ||
|
|
50fba1dde9 | ||
|
|
1be53da061 | ||
|
|
8d40fcd1b4 | ||
|
|
b7fbbcfe8f | ||
|
|
ba8b4894a5 | ||
|
|
e34c22c1be | ||
|
|
67799d0038 | ||
|
|
0fbbaa5bef | ||
|
|
0f91590755 | ||
|
|
2a93811d2f | ||
|
|
8a4cce18f1 | ||
|
|
9f31dd8677 | ||
|
|
20743401ae | ||
|
|
050d3ca8a6 | ||
|
|
3be8e7e63a | ||
|
|
b688062ca4 | ||
|
|
d63d12de38 | ||
|
|
9ef97ab7a2 | ||
|
|
6651491941 | ||
|
|
d498dad18f | ||
|
|
2bd8dc0bc6 | ||
|
|
7873004d74 | ||
|
|
a5eec2e9a3 |
61
README.md
61
README.md
@@ -8,10 +8,10 @@
|
||||
|
||||
## API说明:
|
||||
|
||||
#### 一言API
|
||||
此网站可以获取指定长度指定类型的一个句子,可以限定返回长度及返回类型
|
||||
~~#### 一言API~~
|
||||
~~此网站可以获取指定长度指定类型的一个句子,可以限定返回长度及返回类型~~
|
||||
|
||||
https://international.v1.hitokoto.cn/?c=a&encode=text&max_length=20
|
||||
~~https://international.v1.hitokoto.cn/?c=a&encode=text&max_length=20~~
|
||||
|
||||
#### 天气API
|
||||
获取的天气是通过网页的接口,获取的内容保存在当前目录下的json文件接口的地址是在这里抓取的
|
||||
@@ -26,9 +26,7 @@
|
||||
|
||||
https://github.com/PiSugar/PiSugar/wiki/PiSugar-3-Series#rtc-on-board
|
||||
https://www.pisugar.com/
|
||||
|
||||
ps:如果没有使用此模块则需要更改代码内容,以避免运行出错。
|
||||
以上可能python脚本已解决
|
||||
|
||||
ps:代码里需要nc命令需要安装netcat,安装内容已集成到“[需要安装的软件和依赖](https://github.com/kxgx/2.13-Ink-screen-clock#%E9%9C%80%E8%A6%81%E5%AE%89%E8%A3%85%E7%9A%84%E8%BD%AF%E4%BB%B6%E5%92%8C%E4%BE%9D%E8%B5%96)"
|
||||
|
||||
@@ -59,28 +57,36 @@ sudo reboot
|
||||
|
||||

|
||||
|
||||
#### wifi连接管理
|
||||
可以使用[PiSugar/sugar-wifi-conf:让树莓派提供蓝牙BLE服务,使用小程序可以随时更改树莓派的wifi连接
|
||||
](https://github.com/PiSugar/PiSugar/wiki/PiSugar-WiFi-config#sugar-wifi-conf)
|
||||
## 使用脚本直接安装(推荐)
|
||||
### 参数定义
|
||||
```Bash
|
||||
--zh 设置系统语言为zh_CN,UTF-8
|
||||
--cn 替换apt镜像源为中国镜像源
|
||||
--debug 输出详细信息
|
||||
--zh 设置系统语言为zh_CN,UTF-8
|
||||
--cn 替换apt镜像源为中国镜像源
|
||||
--gitcn 克隆中国仓库
|
||||
--pisugar-wifi-conf 安装pisugar-wifi-conf
|
||||
--pisugar-power-manager 安装pisugar-power-manager
|
||||
--version <tag> 版本号(使用方法 --version + 仓库标签,格式例如 v1.x.x ,可以是主仓库main)
|
||||
--debug 输出详细信息
|
||||
```
|
||||
###
|
||||
```Bash
|
||||
#国内源默认设置
|
||||
#中国源默认设置
|
||||
curl -sSL https://gitee.com/xingguangk/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash
|
||||
```
|
||||
```Bash
|
||||
#国内源--cn设置
|
||||
curl -sSL https://gitee.com/xingguangk/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --cn
|
||||
#中国源全参数设置(不使用--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>
|
||||
```
|
||||
```Bash
|
||||
#默认源默认设置
|
||||
curl -sSL https://github.com/kxgx/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash
|
||||
```
|
||||
```Bash
|
||||
#默认源--cn设置
|
||||
curl -sSL https://github.com/kxgx/2.13-Ink-screen-clock/raw/main/bin/install.sh | sudo bash -s -- --cn
|
||||
#默认源全参数设置(不使用--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>
|
||||
```
|
||||
|
||||
## 需要安装的软件和依赖:
|
||||
@@ -89,28 +95,25 @@ curl -sSL https://github.com/kxgx/2.13-Ink-screen-clock/raw/main/bin/install.sh
|
||||
PiSugar 3官方文档 https://github.com/PiSugar/PiSugar/wiki/PiSugar-3-Series#software-installation
|
||||
```Bash
|
||||
sudo apt-get update
|
||||
sudo apt install -y git
|
||||
sudo apt-get install -y netcat*
|
||||
sudo apt-get install -y python3-pip
|
||||
sudo apt-get install -y python3-pil
|
||||
sudo apt-get install -y python3-numpy
|
||||
sudo apt install -y python3-gpiozero
|
||||
sudo pip3 install spidev borax pillow requests --break-system-packages
|
||||
sudo apt-get install -y git pigpio i2c-tools netcat* gawk python3-dev python3-pip python3-pil python3-numpy python3-gpiozero python3-pigpio build-essential
|
||||
sudo pip3 install spidev borax pillow requests
|
||||
wget https://cdn.pisugar.com/release/pisugar-power-manager.sh
|
||||
bash pisugar-power-manager.sh -c release
|
||||
```
|
||||
#### 傻瓜式安装命令和执行启动文件:
|
||||
#### 安装命令和执行启动文件组合命令:
|
||||
```Bash
|
||||
sudo apt-get update && sudo apt install -y git && sudo apt-get install -y python3-pip && sudo apt-get install -y python3-pil && sudo apt-get install -y python3-numpy && sudo apt install -y python3-gpiozero && sudo apt-get install -y netcat* && sudo pip3 install spidev borax pillow requests --break-system-packages && wget https://cdn.pisugar.com/release/pisugar-power-manager.sh && bash pisugar-power-manager.sh -c release && cd ~/ && git clone https://github.com/kxgx/2.13-Ink-screen-clock.git && cd ~/2.13-Ink-screen-clock/bin/ && sudo chmod +x start.sh && sudo ./start.sh
|
||||
sudo apt-get update && sudo apt-get install -y git pigpio i2c-tools netcat* gawk python3-dev python3-pip python3-pil python3-numpy python3-gpiozero python3-pigpio build-essential && sudo pip3 install spidev borax pillow requests && wget https://cdn.pisugar.com/release/pisugar-power-manager.sh && bash pisugar-power-manager.sh -c release && cd ~/ && git clone https://github.com/kxgx/2.13-Ink-screen-clock.git && cd ~/2.13-Ink-screen-clock/bin/ && sudo chmod +x start.sh && sudo ./start.sh
|
||||
```
|
||||
##### 使用国内仓库:
|
||||
```Bash
|
||||
sudo apt-get update && sudo apt install -y git && sudo apt-get install -y python3-pip && sudo apt-get install -y python3-pil && sudo apt-get install -y python3-numpy && sudo apt install -y python3-gpiozero && sudo apt-get install -y netcat* && sudo pip3 install spidev borax pillow requests --break-system-packages && wget https://cdn.pisugar.com/release/pisugar-power-manager.sh && bash pisugar-power-manager.sh -c release && cd ~/ && git clone https://gitee.com/xingguangk/2.13-Ink-screen-clock.git && cd ~/2.13-Ink-screen-clock/bin/ && sudo chmod +x start.sh && sudo ./start.sh
|
||||
sudo apt-get update && sudo apt-get install -y git pigpio i2c-tools netcat* gawk python3-dev python3-pip python3-pil python3-numpy python3-gpiozero python3-pigpio build-essential && sudo pip3 install spidev borax pillow requests && sudo pip3 install spidev borax pillow requests --break-system-packages && wget https://cdn.pisugar.com/release/pisugar-power-manager.sh && bash pisugar-power-manager.sh -c release && cd ~/ && git clone https://gitee.com/xingguangk/2.13-Ink-screen-clock.git && cd ~/2.13-Ink-screen-clock/bin/ && sudo chmod +x start.sh && sudo ./start.sh
|
||||
```
|
||||
|
||||
总体采用局刷方案,程序运行后一直处于程序的获取新数据的过程中,当发现数据变化后即开始自动局刷。
|
||||
|
||||
特别注意:
|
||||
在代码文件第三十三行,此次代码需要更改,否则将展示默认城市天气数据
|
||||
###### 注意
|
||||
```Bash
|
||||
# 如果使用的是非lite系统请在pip3安装部分添加
|
||||
--break-system-packages
|
||||
```
|
||||
在weanther.py文件第64行可以修改默认城市数据
|
||||
# 效果展示
|
||||

|
||||
总体采用局刷方案,程序运行后一直处于程序的获取新数据的过程中,当发现数据变化后即开始自动局刷。
|
||||

|
||||
|
||||
95
app/app.py
Normal file
95
app/app.py
Normal file
@@ -0,0 +1,95 @@
|
||||
from flask import Flask, render_template, request, send_from_directory
|
||||
import os
|
||||
import re
|
||||
|
||||
app = Flask(__name__, template_folder='webui', static_url_path='', static_folder='webui')
|
||||
FONT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'pic') # 字体文件夹路径
|
||||
|
||||
def list_font_files(font_dir):
|
||||
try:
|
||||
return os.listdir(font_dir)
|
||||
except Exception as e:
|
||||
print(f"Error listing font files: {e}")
|
||||
return [] # 返回空列表以避免迭代错误
|
||||
|
||||
def update_main_py_font_names(font_names):
|
||||
main_py_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'main.py')
|
||||
try:
|
||||
with open(main_py_path, 'r') as file:
|
||||
content = file.read()
|
||||
|
||||
# 为每个字体变量定义正则表达式模式
|
||||
patterns = {
|
||||
'font01': re.compile(r"(?<=font01\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*20\s*\))"),
|
||||
'font02': re.compile(r"(?<=font02\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*15\s*\))"),
|
||||
'font03': re.compile(r"(?<=font03\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*38\s*\))"),
|
||||
'font04': re.compile(r"(?<=font04\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*10\s*\))"),
|
||||
'font05': re.compile(r"(?<=font05\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*12\s*\))"),
|
||||
'font06': re.compile(r"(?<=font06\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*13\s*\))"),
|
||||
}
|
||||
|
||||
# 使用正则表达式替换字体文件名
|
||||
for font_var, font_name in font_names.items():
|
||||
pattern = patterns.get(font_var)
|
||||
if pattern:
|
||||
# 确保字体文件名被正确地转义
|
||||
safe_font_name = re.escape(font_name)
|
||||
content = pattern.sub(f"'{safe_font_name}'", content)
|
||||
|
||||
with open(main_py_path, 'w') as file:
|
||||
file.write(content)
|
||||
except Exception as e:
|
||||
print(f"Error updating main.py: {e}")
|
||||
return False
|
||||
return True
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
font_files = list_font_files(FONT_DIR)
|
||||
return render_template('index.html', font_files=font_files)
|
||||
|
||||
@app.route('/upload', methods=['POST'])
|
||||
def upload():
|
||||
if 'font_file' not in request.files:
|
||||
return '没有文件部分'
|
||||
file = request.files['font_file']
|
||||
if file.filename == '':
|
||||
return '没有选择文件'
|
||||
if file:
|
||||
filename = os.path.join(FONT_DIR, file.filename)
|
||||
file.save(filename)
|
||||
return '文件已上传成功'
|
||||
|
||||
@app.route('/update_font_names')
|
||||
def update_font_names():
|
||||
font_files = list_font_files(FONT_DIR)
|
||||
return render_template('update_font_names.html', font_files=font_files)
|
||||
|
||||
@app.route('/save_font_names', methods=['POST'])
|
||||
def save_font_names():
|
||||
# 获取表单数据
|
||||
font_names = {
|
||||
'font01': request.form.get('font01'),
|
||||
'font02': request.form.get('font02'),
|
||||
'font03': request.form.get('font03'),
|
||||
'font04': request.form.get('font04'),
|
||||
'font05': request.form.get('font05'),
|
||||
'font06': request.form.get('font06'),
|
||||
}
|
||||
|
||||
# 更新 main.py 中的字体文件名
|
||||
if update_main_py_font_names(font_names):
|
||||
return '字体文件名已保存'
|
||||
else:
|
||||
return '保存字体文件名时发生错误', 500
|
||||
|
||||
@app.route('/fonts/<filename>')
|
||||
def fonts(filename):
|
||||
return send_from_directory(FONT_DIR, filename)
|
||||
|
||||
if not os.path.exists(FONT_DIR):
|
||||
os.makedirs(FONT_DIR)
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 绑定到0.0.0.0,允许远程访问
|
||||
app.run(host='0.0.0.0', port=80, debug=False)
|
||||
23
app/webui/index.html
Normal file
23
app/webui/index.html
Normal file
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>字体上传和管理</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>上传字体文件</h1>
|
||||
<form method="post" action="/upload" enctype="multipart/form-data">
|
||||
<input type="file" name="font_file">
|
||||
<input type="submit" value="上传">
|
||||
</form>
|
||||
|
||||
<h2>已上传的字体文件:</h2>
|
||||
<ul>
|
||||
{% for font_file in font_files %}
|
||||
<li>{{ font_file }}</li>
|
||||
{% else %}
|
||||
<li>没有找到字体文件。</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
26
app/webui/update_font_names.html
Normal file
26
app/webui/update_font_names.html
Normal file
@@ -0,0 +1,26 @@
|
||||
<!-- update_font_names.html -->
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>更新字体文件名</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>更新字体文件名</h1>
|
||||
<form method="post" action="/save_font_names">
|
||||
<label for="font01">字体文件名 01:</label>
|
||||
<input type="text" id="font01" name="font01"><br>
|
||||
<label for="font02">字体文件名 02:</label>
|
||||
<input type="text" id="font02" name="font02"><br>
|
||||
<label for="font03">字体文件名 03:</label>
|
||||
<input type="text" id="font03" name="font03"><br>
|
||||
<label for="font04">字体文件名 04:</label>
|
||||
<input type="text" id="font04" name="font04"><br>
|
||||
<label for="font05">字体文件名 05:</label>
|
||||
<input type="text" id="font05" name="font05"><br>
|
||||
<label for="font06">字体文件名 06:</label>
|
||||
<input type="text" id="font06" name="font06"><br>
|
||||
<input type="submit" value="保存">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
18
bin/clean.py
18
bin/clean.py
@@ -16,37 +16,39 @@ if os.path.exists(libdir):
|
||||
from waveshare_epd import epd2in13_V4 #引入墨水屏驱动文件
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
|
||||
|
||||
def clear_screen():
|
||||
try:
|
||||
logging.info("Starting e-Paper clear screen script")
|
||||
logging.info("启动电子纸清屏脚本")
|
||||
|
||||
# 初始化并创建一个 epd2in13_V4 对象
|
||||
epd = epd2in13_V4.EPD()
|
||||
|
||||
# 初始化屏幕
|
||||
logging.info("Initializing e-Paper display")
|
||||
logging.info("初始化电子纸显示屏")
|
||||
epd.init()
|
||||
|
||||
# 清除屏幕
|
||||
logging.info("Clearing e-Paper display")
|
||||
logging.info("清除电子纸显示屏内容")
|
||||
epd.Clear(0xFF)
|
||||
|
||||
# 等待一段时间,确保清除操作完成
|
||||
time.sleep(1)
|
||||
#time.sleep(1)
|
||||
|
||||
# 将屏幕置于睡眠模式
|
||||
logging.info("Putting e-Paper display to sleep")
|
||||
logging.info("将电子纸显示屏置入睡眠状态")
|
||||
epd.sleep()
|
||||
epd2in13_V4.epdconfig.module_exit(cleanup=True)
|
||||
exit()
|
||||
|
||||
logging.info("e-Paper display cleared successfully")
|
||||
logging.info("电子纸显示屏清屏成功")
|
||||
|
||||
except IOError as e:
|
||||
logging.info(e)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Ctrl+C pressed, exiting")
|
||||
logging.info("检测到键盘中断,正在退出")
|
||||
epd2in13_V4.epdconfig.module_exit(cleanup=True)
|
||||
exit()
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
f_name="clean.py"
|
||||
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
||||
logdir="${dir}/log"
|
||||
logdir="${dir%/*}/log"
|
||||
service_name="raspi_e-Paper.service"
|
||||
hold_time_seconds=3 # 设置维持时间,例如300秒(5分钟)
|
||||
hold_time_seconds=5 # 设置维持时间,例如300秒(5分钟)
|
||||
|
||||
# 停止raspi_e-Paper服务
|
||||
echo "正在停止$service_name服务..."
|
||||
|
||||
159
bin/clock.py
Normal file
159
bin/clock.py
Normal file
@@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding:utf-8 -*-
|
||||
from PIL import Image,ImageDraw,ImageFont #引入图片处理库
|
||||
import os,sys,re,json,time,datetime #引入系统相关库
|
||||
from borax.calendars.lunardate import LunarDate #农历日期以及天干地支纪年法的 Python 库
|
||||
import logging #日志库
|
||||
import subprocess
|
||||
import os
|
||||
from threading import Timer
|
||||
import requests
|
||||
white = 255 #颜色
|
||||
black = 0
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
################################引入配置文件开始################################################
|
||||
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
|
||||
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
|
||||
if os.path.exists(libdir):
|
||||
sys.path.append(libdir)#将引入文件添加到环境变量
|
||||
from waveshare_epd import epd2in13_V4 #引入墨水屏驱动文件
|
||||
logging.debug("Loading Fonts")
|
||||
font01 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20) #字体文件
|
||||
font02 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15) #字体文件
|
||||
font03 = ImageFont.truetype(os.path.join(picdir, 'DSEG7Modern-Bold.ttf'), 38) #字体文件
|
||||
font04 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 10) #字体文件
|
||||
font05 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12) #字体文件
|
||||
font06 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 13) #字体文件
|
||||
################################引入配置文件结束################################################
|
||||
def Local_strong_brush(): #局部强制刷新显示
|
||||
i = 0
|
||||
while i < 5:
|
||||
epd.displayPartial(epd.getbuffer(info_image.rotate(180)))#局刷开始
|
||||
i = i + 1
|
||||
def get_date():#返回当前年月日及星期几
|
||||
date = datetime.datetime.now()
|
||||
today=LunarDate.today()
|
||||
week_day_dict = {0: '星期一',1: '星期二',2: '星期三',3: '星期四',4: '星期五',5: '星期六',6: '星期日',}
|
||||
day = date.weekday()
|
||||
return time.strftime('%Y年%m月%d日')+' '+week_day_dict[day]+' '+today.strftime('农 %M月%D')
|
||||
def get_time():#返回当前时间,不到秒,大写
|
||||
return time.strftime('%H:%M')
|
||||
def Get_address():#获取当前的IP地址
|
||||
return (subprocess.check_output(u"hostname -I | cut -d\' \' -f1 | head --bytes -1", shell = True ).decode('gbk'))
|
||||
def CPU_temperature():#CPU温度获取
|
||||
temperatura = os.popen('vcgencmd measure_temp').readline()
|
||||
temperatura = temperatura.replace('temp=','').strip()
|
||||
return str(temperatura)
|
||||
def Memory_footprint():#显示内存占用百分比
|
||||
return(subprocess.check_output(u"free -m | awk -F '[ :]+' 'NR==2{printf \"%d\", ($3)/$2*100}'", shell = True ).decode('gbk'))
|
||||
def CPU_usage(): #显示CPU占用百分比
|
||||
return(str(int(float(os.popen("top -b -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))))
|
||||
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'%')
|
||||
# 打印电量信息
|
||||
print(power_battery())
|
||||
def Bottom_edge(): #在图片中添加底边内容
|
||||
draw.rectangle((0, 105, 250, 122), 'black', 'black')
|
||||
'''电池图标画图'''
|
||||
draw.line((126,109,154,109),fill=255, width=1) #电池顶边
|
||||
draw.line((126,110,126,119),fill=255, width=1) #电池左边
|
||||
draw.line((127,119,154,119),fill=255, width=1) #电池下边
|
||||
draw.line((154,110,154,118),fill=255, width=1)
|
||||
draw.line((155,112,157,112),fill=255, width=1)
|
||||
draw.line((155,116,157,116),fill=255, width=1)
|
||||
draw.line((157,113,157,115),fill=255, width=1)
|
||||
global power_str
|
||||
power_str=power_battery()
|
||||
draw.text((128,108),power_str,font = font04,fill =255) #显示当前电量百分比
|
||||
'''电池图标画图'''
|
||||
draw.ellipse((192, 107, 207, 120), 0, 255)# 时钟图标
|
||||
draw.line((199,109,199,114),fill=255, width=1)
|
||||
draw.line((200,114,204,114),fill=255, width=1)
|
||||
global local_addr #获取当前IP地址
|
||||
local_addr= Get_address() #获取当前IP地址
|
||||
draw.text((10,107),"IP:"+local_addr,font = font05,fill =255)#显示当前IP地址
|
||||
def Basic_refresh(): #全刷函数
|
||||
logging.info("Refresh and prepare the basic content before starting the canvas")#开始画布前刷新准备基础内容
|
||||
global get_date_var
|
||||
get_date_var=get_date() #记录开始数据
|
||||
draw.text((2,2),get_date_var,font = font02,fill =0)#将日期及星期几显示到屏幕
|
||||
global local_time
|
||||
local_time=get_time()
|
||||
draw.text((5,40),local_time,font = font03,fill =0)#显示当前时间
|
||||
Bottom_edge() #添加底边内容
|
||||
epd.display(epd.getbuffer(info_image.rotate(180)))
|
||||
def Partial_full_brush(): #局部定时全刷函数
|
||||
Basic_refresh() #全局刷新
|
||||
logging.debug("局部定时全局刷新")
|
||||
epd.init()
|
||||
def Partial_refresh():#局刷函数
|
||||
logging.info("Partial content update, this update is recommended to be synchronized with the minute to save the life of the ink screen")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
|
||||
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180)))
|
||||
epd.init()
|
||||
while (True):
|
||||
global local_time
|
||||
local_time1=get_time()
|
||||
if (local_time1==local_time) ==False:
|
||||
draw.rectangle((5, 40, 133, 82), fill = 255) #时间局刷区域
|
||||
draw.text((5,40),local_time1,font = font03,fill =0)#刷新当前时间
|
||||
local_time=local_time1
|
||||
Local_strong_brush() #局部强刷
|
||||
get_date_var1=get_date() #局刷判断,如果时间与前一次不一致说明内容变化,需要刷新显示
|
||||
global get_date_var #再次声明这个是全局变量
|
||||
if(get_date_var1==get_date_var) ==False:
|
||||
draw.rectangle((2, 2, 250, 16), fill = 255) #设置头部刷新区域
|
||||
draw.text((2,2),get_date_var1,font = font02,fill =0)#将日期及星期几刷新显示到屏幕
|
||||
get_date_var=get_date_var1 #将更新的值保存到初始变量,直到下一次变化时执行该刷新操作
|
||||
logging.debug("头部日期部位发生刷新变化.")
|
||||
Local_strong_brush() #局部强刷
|
||||
global local_addr #当前IP地址
|
||||
local_addr1 = Get_address()
|
||||
if (local_addr1==local_addr) ==False:
|
||||
draw.rectangle((1, 107, 94, 120), fill = 0) #设置头部刷新区域
|
||||
draw.text((10,107),"IP:"+local_addr1,font = font05,fill =255)#显示当前IP地址
|
||||
local_addr=local_addr1
|
||||
Local_strong_brush() #局部强刷
|
||||
#显示当前电量百分比
|
||||
power_str=power_str1
|
||||
Local_strong_brush() #局部强刷
|
||||
logging.info("电源电量局部刷新")
|
||||
try:
|
||||
##################屏幕初始化#########################
|
||||
getWeath()#天气获取函数开始运行
|
||||
epd = epd2in13_V4.EPD() #初始化
|
||||
epd.init()#设定屏幕刷新模式
|
||||
#epd.Clear(0xFF) #清除屏幕内容
|
||||
##################屏幕初始化#########################
|
||||
logging.info("Width = %s, Height = %s", format(epd.width), format(epd.height)) #打印屏幕高度及宽度
|
||||
logging.info("Initialize and clear the display")#屏幕开始准备相关展示
|
||||
info_image = Image.new('1', (epd.height, epd.width), 255) #画布创建准备
|
||||
draw = ImageDraw.Draw(info_image)
|
||||
Basic_refresh() #全局刷新
|
||||
Partial_refresh() #局部刷新
|
||||
epd.init()
|
||||
epd.Clear(0xFF)
|
||||
epd.sleep()
|
||||
except IOError as e:
|
||||
logging.info(e)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Keyboard interrupt detected, exiting gracefully.")
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
epd2in13_V4.epdconfig.module_exit() # 清理资源
|
||||
exit()
|
||||
|
||||
except Exception as e:
|
||||
logging.error("An unexpected error occurred: %s", e)
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
epd2in13_V4.epdconfig.module_exit() # 清理资源
|
||||
exit()
|
||||
|
||||
# 脚本正常结束后的清理操作
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
epd2in13_V4.epdconfig.module_exit() # 清理资源
|
||||
exit()
|
||||
@@ -7,11 +7,17 @@ if [ "$EUID" -ne 0 ]; then
|
||||
fi
|
||||
|
||||
# 默认语言环境
|
||||
DEFAULT_LANG="en_US.UTF-8"
|
||||
DEFAULT_LANG="en_GB.UTF-8"
|
||||
# 检查是否使用中国镜像源
|
||||
USE_CN_MIRROR=false
|
||||
# 控制调试输出
|
||||
DEBUG=false
|
||||
# 检查是否使用中国git仓库
|
||||
USE_CN_GIT=false
|
||||
# 检查是否安装pisugar-wifi-conf
|
||||
USE_PISUGAR_WIFI_CONF=false
|
||||
# 检查是否安装pisugar-power-manager
|
||||
USE_PISUGAR_POWER_MANAGER=false
|
||||
|
||||
# 解析命令行参数
|
||||
while [ "$#" -gt 0 ]; do
|
||||
@@ -22,6 +28,23 @@ while [ "$#" -gt 0 ]; do
|
||||
--cn)
|
||||
USE_CN_MIRROR=true
|
||||
;;
|
||||
--gitcn)
|
||||
USE_CN_GIT=true
|
||||
;;
|
||||
--pisugar-wifi-conf)
|
||||
USE_PISUGAR_WIFI_CONF=true
|
||||
;;
|
||||
--pisugar-power-manager)
|
||||
USE_PISUGAR_POWER_MANAGER=true
|
||||
;;
|
||||
--version)
|
||||
if [ -z "$2" ]; then
|
||||
echo "错误: --version 参数后需要跟版本号"
|
||||
exit 1
|
||||
fi
|
||||
VERSION="$2" # 将版本号赋值给变量 VERSION
|
||||
shift 2 # 移动参数,跳过版本号参数及其值
|
||||
;;
|
||||
--debug)
|
||||
DEBUG=true
|
||||
;;
|
||||
@@ -101,8 +124,8 @@ is_raspberry_pi() {
|
||||
# 定义链接变量
|
||||
DEBIAN_MIRROR="http://deb.debian.org/debian/"
|
||||
DEBIAN_SECURITY_MIRROR="http://security.debian.org/"
|
||||
PI_SUGAR_POWER_MANAGER_URL="https://cdn.pisugar.com/release/pisugar-power-manager.sh"
|
||||
INK_SCREEN_CLOCK_REPO_URL="https://github.com/kxgx/2.13-Ink-screen-clock.git"
|
||||
PISUGAR_WIFI_CONF_URL="https://cdn.pisugar.com/PiSugar-wificonfig/script/install.sh"
|
||||
PISUGAR_POWER_MANAGER_URL="https://cdn.pisugar.com/release/pisugar-power-manager.sh"
|
||||
PIPY_MIRROR="https://pypi.org/simple"
|
||||
# 修改 Raspberry Pi 特定源链接
|
||||
RASPBERRY_PI_SOURCE_DEBIAN11="https://archive.raspberrypi.org/debian/"
|
||||
@@ -112,13 +135,19 @@ RASPBERRY_PI_SOURCE_DEBIAN12="https://archive.raspberrypi.com/debian/"
|
||||
if [ "$USE_CN_MIRROR" = true ]; then
|
||||
DEBIAN_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian/"
|
||||
DEBIAN_SECURITY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian-security"
|
||||
INK_SCREEN_CLOCK_REPO_URL="https://gitee.com/xingguangk/2.13-Ink-screen-clock.git"
|
||||
PIPY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
|
||||
# 使用中国镜像源时,Raspberry Pi 特定源链接保持不变
|
||||
RASPBERRY_PI_SOURCE_DEBIAN11="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/"
|
||||
RASPBERRY_PI_SOURCE_DEBIAN12="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/"
|
||||
fi
|
||||
|
||||
# 定义仓库链接变量
|
||||
INK_SCREEN_CLOCK_REPO_URL="https://github.com/kxgx/2.13-Ink-screen-clock"
|
||||
# 如果使用中国仓库,则更新链接变量
|
||||
if [ "$USE_CN_GIT" = true ]; then
|
||||
INK_SCREEN_CLOCK_REPO_URL="https://gitee.com/xingguangk/2.13-Ink-screen-clock"
|
||||
fi
|
||||
|
||||
# 更新源列表函数
|
||||
update_sources_list() {
|
||||
local version=$1
|
||||
@@ -171,15 +200,13 @@ update_sources_list() {
|
||||
|
||||
# 安装包函数
|
||||
install_packages() {
|
||||
if ! sudo apt-get update; then
|
||||
echo "正在更新源列表"
|
||||
if ! sudo apt-get -q -y update; then
|
||||
echo "更新源列表失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! sudo apt-get upgrade -y; then
|
||||
echo "系统更新失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! sudo apt-get install -y git pigpio raspi-config netcat* gawk python3-dev python3-pip python3-pil python3-numpy python3-gpiozero python3-pigpio build-essential; then
|
||||
echo "正在安装软件包"
|
||||
if ! sudo apt-get -q -y install git pigpio i2c-tools netcat* gawk python3-dev python3-pip python3-pil python3-numpy python3-gpiozero python3-pigpio build-essential screen; then
|
||||
echo "软件包安装失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
@@ -187,8 +214,10 @@ install_packages() {
|
||||
|
||||
# 安装pip包函数
|
||||
install_pip_packages() {
|
||||
if ! sudo pip3 install -i "$PIPY_MIRROR" spidev borax pillow requests --break-system-packages; then
|
||||
echo "pip软件包安装包失败" >&2
|
||||
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
|
||||
}
|
||||
@@ -202,22 +231,24 @@ setup_service() {
|
||||
|
||||
# 检查墨水屏时钟仓库是否存在
|
||||
if [ ! -d "$HOME/2.13-Ink-screen-clock" ]; then
|
||||
echo "正在克隆仓库"
|
||||
cd ~
|
||||
if ! git clone $INK_SCREEN_CLOCK_REPO_URL; then
|
||||
echo "克隆墨水屏时钟仓库失败" >&2
|
||||
if ! git clone -b $VERSION $INK_SCREEN_CLOCK_REPO_URL; then
|
||||
echo "克隆仓库失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
# 设置start.sh和clean.sh脚本的执行权限
|
||||
chmod +x "$HOME/2.13-Ink-screen-clock/bin/start.sh"
|
||||
chmod +x "$HOME/2.13-Ink-screen-clock/bin/clean.sh"
|
||||
else
|
||||
echo "墨水屏时钟仓库文件夹已存在,跳过克隆"
|
||||
echo "仓库文件夹已存在,跳过克隆"
|
||||
fi
|
||||
|
||||
# 检查服务文件是否存在
|
||||
if [ -f "$service_file_path" ] && [ -f "$service1_file_path" ]; then
|
||||
# 检查服务是否已经启用
|
||||
if ! systemctl is-enabled $service_path &>/dev/null && ! systemctl is-enabled $service1_path &>/dev/null; then
|
||||
echo "正在复制服务文件并启用服务"
|
||||
# 复制服务文件到 systemd 目录
|
||||
if sudo cp "$service_file_path" /etc/systemd/system/ && sudo cp "$service1_file_path" /etc/systemd/system/; then
|
||||
# 重载 systemd 管理器配置
|
||||
@@ -239,10 +270,34 @@ setup_service() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 安装pisugar-power-manager函数
|
||||
install_pisugar-power-manager() {
|
||||
if [ "$USE_PISUGAR_POWER_MANAGER" = true ]; then
|
||||
echo "正在安装pisugar-power-manager"
|
||||
if ! curl -sSL "$PISUGAR_POWER_MANAGER_URL" | sudo bash -s - -c release; then
|
||||
echo "pisugar-power-manager安装失败" >&2
|
||||
echo "如需要请手动运行curl -sSL $PISUGAR_POWER_MANAGER_URL | sudo bash -s - -c release" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# 安装pisugar-wifi-conf函数
|
||||
install_pisugar-wifi-conf() {
|
||||
if [ "$USE_PISUGAR_WIFI_CONF" = true ]; then
|
||||
echo "正在安装pisugar-wifi-conf"
|
||||
if ! curl -sSL "$PISUGAR_WIFI_CONF_URL" | sudo bash; then
|
||||
echo "pisugar-wifi-conf安装失败" >&2
|
||||
echo "如需要请手动运行curl $PISUGAR_WIFI_CONF_URL | sudo bash" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# 主逻辑
|
||||
# 检测是否是Debian系统
|
||||
if [ -f /etc/debian_version ]; then
|
||||
echo "检测到Debian$DEBIAN_VERSION系统"
|
||||
echo "检测到Debian$DEBIAN_VERSION系统"
|
||||
|
||||
# 提取版本号的小数点前的部分
|
||||
if ! MAJOR_VERSION=$(echo $DEBIAN_VERSION | cut -d '.' -f 1); then
|
||||
@@ -262,6 +317,8 @@ if [ -f /etc/debian_version ]; then
|
||||
install_packages
|
||||
install_pip_packages
|
||||
setup_service
|
||||
install_pisugar-wifi-conf
|
||||
install_pisugar-power-manager
|
||||
;;
|
||||
12)
|
||||
echo "执行Debian 12 (Bookworm) 相关操作"
|
||||
@@ -269,6 +326,8 @@ if [ -f /etc/debian_version ]; then
|
||||
install_packages
|
||||
install_pip_packages
|
||||
setup_service
|
||||
install_pisugar-wifi-conf
|
||||
install_pisugar-power-manager
|
||||
;;
|
||||
*)
|
||||
echo "未知的Debian版本: $MAJOR_VERSION" >&2
|
||||
|
||||
112
bin/main.py
112
bin/main.py
@@ -8,9 +8,11 @@ import subprocess
|
||||
import os
|
||||
from threading import Timer
|
||||
import requests
|
||||
import socket
|
||||
|
||||
white = 255 #颜色
|
||||
black = 0
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
|
||||
################################引入配置文件开始################################################
|
||||
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
|
||||
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
|
||||
@@ -30,24 +32,6 @@ def Local_strong_brush(): #局部强制刷新显示
|
||||
while i < 5:
|
||||
epd.displayPartial(epd.getbuffer(info_image.rotate(180)))#局刷开始
|
||||
i = i + 1
|
||||
def getWeath(city='101060111'): #天气函数,下载json天气至本地
|
||||
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',
|
||||
'Referer':'http://www.weather.com.cn/'
|
||||
}
|
||||
response = requests.get('http://d1.weather.com.cn/sk_2d/'+city+'.html',headers=headers)
|
||||
response.encoding = 'utf-8'
|
||||
Weath=response.text[11:]
|
||||
fileHandle=open('weather.json','w')
|
||||
fileHandle.write(str(Weath))
|
||||
fileHandle.close()
|
||||
Timer(1800, getWeath).start() #定时器函数,间隔三分钟下载文件至本地
|
||||
print("天气文件更新")
|
||||
def gey_yiyan(): #一言API
|
||||
response = requests.get("https://international.v1.hitokoto.cn/?c=k&c=f&c=d&c=a&encode=text&min_length=7&max_length=19")
|
||||
response.encoding = 'utf-8'
|
||||
yiyan=response.text
|
||||
return yiyan
|
||||
def get_date():#返回当前年月日及星期几
|
||||
date = datetime.datetime.now()
|
||||
today=LunarDate.today()
|
||||
@@ -58,6 +42,17 @@ def get_time():#返回当前时间,不到秒,大写
|
||||
return time.strftime('%H:%M')
|
||||
def Get_address():#获取当前的IP地址
|
||||
return (subprocess.check_output(u"hostname -I | cut -d\' \' -f1 | head --bytes -1", shell = True ).decode('gbk'))
|
||||
def Get_ipv4_address(): # 获取当前的IP地址
|
||||
try:
|
||||
# 执行命令获取IP地址,并处理输出以仅返回IPv4地址
|
||||
ip_output = subprocess.check_output("hostname -I | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}'", shell=True).decode('utf-8').strip()
|
||||
# 检查是否为IPv4地址
|
||||
if ip_output and not ip_output.startswith("127."):
|
||||
return ip_output
|
||||
else:
|
||||
return "IPv4获取失败"
|
||||
except subprocess.CalledProcessError as e:
|
||||
return f"获取失败"
|
||||
def CPU_temperature():#CPU温度获取
|
||||
temperatura = os.popen('vcgencmd measure_temp').readline()
|
||||
temperatura = temperatura.replace('temp=','').strip()
|
||||
@@ -66,25 +61,8 @@ def Memory_footprint():#显示内存占用百分比
|
||||
return(subprocess.check_output(u"free -m | awk -F '[ :]+' 'NR==2{printf \"%d\", ($3)/$2*100}'", shell = True ).decode('gbk'))
|
||||
def CPU_usage(): #显示CPU占用百分比
|
||||
return(str(int(float(os.popen("top -b -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))))
|
||||
def power_battery():
|
||||
try:
|
||||
# 获取电池电量
|
||||
battery_percentage = subprocess.check_output(
|
||||
"echo \"get battery\" | nc -q 0 127.0.0.1 8423 | awk -F':' '{print $2}'",
|
||||
shell=True,
|
||||
text=True
|
||||
).strip()
|
||||
|
||||
# 检查电量信息
|
||||
if not battery_percentage:
|
||||
return "无效"
|
||||
|
||||
# 返回电量百分比
|
||||
return f"{int(battery_percentage)}%"
|
||||
except Exception as e:
|
||||
# 返回错误信息
|
||||
return "失败"
|
||||
|
||||
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'%')
|
||||
# 打印电量信息
|
||||
print(power_battery())
|
||||
def Bottom_edge(): #在图片中添加底边内容
|
||||
@@ -99,21 +77,16 @@ def Bottom_edge(): #在图片中添加底边内容
|
||||
draw.line((157,113,157,115),fill=255, width=1)
|
||||
global power_str
|
||||
power_str=power_battery()
|
||||
draw.text((128,108),power_str,font = font04,fill =255) #显示当前电量百分比
|
||||
draw.text((129,108),power_str,font = font04,fill =255) #显示当前电量百分比
|
||||
'''电池图标画图'''
|
||||
'''添加一言API'''
|
||||
global Time_keeping
|
||||
Time_keeping =0
|
||||
draw.text((4,84),gey_yiyan(),font = font06,fill =0)
|
||||
'''添加一言API end'''
|
||||
draw.ellipse((192, 107, 207, 120), 0, 255)# 时钟图标
|
||||
draw.line((199,109,199,114),fill=255, width=1)
|
||||
draw.line((200,114,204,114),fill=255, width=1)
|
||||
global local_addr #获取当前IP地址
|
||||
local_addr= Get_address() #获取当前IP地址
|
||||
local_addr= Get_ipv4_address() #获取当前IP地址
|
||||
draw.text((10,107),"IP:"+local_addr,font = font05,fill =255)#显示当前IP地址
|
||||
def Weather(): #在图片中添加天气内容
|
||||
Weather_json = open('weather.json','r')
|
||||
Weather_json = open('/root/2.13-Ink-screen-clock/bin/weather.json','r')
|
||||
Weather_data = Weather_json.read()
|
||||
Weather_json.close()
|
||||
Weather_text=json.loads(Weather_data)
|
||||
@@ -134,13 +107,15 @@ def Weather(): #在图片中添加天气内容
|
||||
draw.text((150,25),"天气:",font = font06,fill =0)#显示当前天气前缀
|
||||
draw.text((150,45),"温度:",font = font06,fill =0)#显示当前温度前缀
|
||||
draw.text((150,65),"湿度:",font = font06,fill =0)#显示当前湿度前缀
|
||||
draw.text((150,85),"城市:",font = font06,fill =0)#显示当前城市前缀
|
||||
draw.text((191,25),weather,font = font06,fill =0)
|
||||
draw.text((191,45),temperature,font = font06,fill =0)
|
||||
draw.text((191,65),humidity,font = font06,fill =0)
|
||||
draw.text((191,85),Weather_position,font = font06,fill =0)
|
||||
draw.text((211,107),weather_update,font = font05,fill =255) #显示天气更新时间
|
||||
|
||||
def Basic_refresh(): #全刷函数
|
||||
logging.info("Refresh and prepare the basic content before starting the canvas")#开始画布前刷新准备基础内容
|
||||
logging.info("在启动画布之前,刷新并准备基本内容")#开始画布前刷新准备基础内容
|
||||
global get_date_var
|
||||
get_date_var=get_date() #记录开始数据
|
||||
draw.text((2,2),get_date_var,font = font02,fill =0)#将日期及星期几显示到屏幕
|
||||
@@ -155,7 +130,7 @@ def Partial_full_brush(): #局部定时全刷函数
|
||||
logging.debug("局部定时全局刷新")
|
||||
epd.init()
|
||||
def Partial_refresh():#局刷函数
|
||||
logging.info("Partial content update, this update is recommended to be synchronized with the minute to save the life of the ink screen")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
|
||||
logging.info("部分内容更新,此更新建议与分钟同步,以节省墨水屏的使用寿命")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
|
||||
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180)))
|
||||
epd.init()
|
||||
while (True):
|
||||
@@ -175,14 +150,14 @@ def Partial_refresh():#局刷函数
|
||||
logging.debug("头部日期部位发生刷新变化.")
|
||||
Local_strong_brush() #局部强刷
|
||||
global local_addr #当前IP地址
|
||||
local_addr1 = Get_address()
|
||||
local_addr1 = Get_ipv4_address()
|
||||
if (local_addr1==local_addr) ==False:
|
||||
draw.rectangle((1, 107, 94, 120), fill = 0) #设置头部刷新区域
|
||||
draw.rectangle((1, 107, 123, 120), fill = 0) #设置头部刷新区域
|
||||
draw.text((10,107),"IP:"+local_addr1,font = font05,fill =255)#显示当前IP地址
|
||||
local_addr=local_addr1
|
||||
Local_strong_brush() #局部强刷
|
||||
'''天气局部更新函数'''
|
||||
Weather_json = open('weather.json','r')
|
||||
Weather_json = open('/root/2.13-Ink-screen-clock/bin/weather.json','r')
|
||||
Weather_data = Weather_json.read()
|
||||
Weather_json.close()
|
||||
Weather_text=json.loads(Weather_data)
|
||||
@@ -218,6 +193,12 @@ def Partial_refresh():#局刷函数
|
||||
humidity = humidity1
|
||||
logging.info("湿度局部刷新")
|
||||
Local_strong_brush() #局部强刷
|
||||
if (Weather_position1==Weather_position) ==False:
|
||||
draw.rectangle((191, 85, 249, 98), fill = 255) #局刷区域
|
||||
draw.text((191,85),Weather_position1,font = font06,fill =0)
|
||||
Weather_position = Weather_position1
|
||||
logging.info("城市局部刷新")
|
||||
Local_strong_brush() #局部强刷
|
||||
if (weather_update1==weather_update) ==False:
|
||||
draw.rectangle((211, 107, 248, 118), fill = 0) #设置更新时间刷新区域
|
||||
draw.text((211,107),weather_update1,font = font05,fill =255) #显示天气更新时间
|
||||
@@ -229,44 +210,29 @@ def Partial_refresh():#局刷函数
|
||||
power_str1 =power_battery()
|
||||
if (power_str1==power_str) ==False:
|
||||
draw.rectangle((128, 110, 153, 117), fill = 0) #设置更新时间刷新区域
|
||||
draw.text((127,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
|
||||
draw.text((129,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
|
||||
power_str=power_str1
|
||||
Local_strong_brush() #局部强刷
|
||||
logging.info("电源电量局部刷新")
|
||||
'''一言API显示'''
|
||||
global Time_keeping
|
||||
Time_keeping+=1
|
||||
if Time_keeping == 300 : #一言API局刷,当变量Time_keeping数据为10即开始执行
|
||||
draw.rectangle((1, 82, 248, 101), fill = 255)
|
||||
draw.text((4,84),gey_yiyan(),font = font06,fill =0)
|
||||
Time_keeping=0
|
||||
Local_strong_brush() #局部强刷
|
||||
logging.info("一言API局部刷新")
|
||||
'''一言API显示'''
|
||||
#epd.displayPartial(epd.getbuffer(info_image.rotate(180)))#局刷开始
|
||||
time.sleep(2)
|
||||
#if time.strftime('%H%M%S')=='000000': #如果时间是晚上00点,执行全局刷新
|
||||
#Partial_full_brush() #每天全局刷新一次
|
||||
try:
|
||||
##################屏幕初始化#########################
|
||||
getWeath()#天气获取函数开始运行
|
||||
epd = epd2in13_V4.EPD() #初始化
|
||||
epd.init()#设定屏幕刷新模式
|
||||
epd.Clear(0xFF) #清除屏幕内容
|
||||
#epd.Clear(0xFF) #清除屏幕内容
|
||||
##################屏幕初始化#########################
|
||||
logging.info("Width = %s, Height = %s", format(epd.width), format(epd.height)) #打印屏幕高度及宽度
|
||||
logging.info("Initialize and clear the display")#屏幕开始准备相关展示
|
||||
logging.info("初始化并清空显示屏")#屏幕开始准备相关展示
|
||||
info_image = Image.new('1', (epd.height, epd.width), 255) #画布创建准备
|
||||
draw = ImageDraw.Draw(info_image)
|
||||
Basic_refresh() #全局刷新
|
||||
Partial_refresh() #局部刷新
|
||||
epd.init()
|
||||
#epd.Clear(0xFF)
|
||||
epd.Clear(0xFF)
|
||||
epd.sleep()
|
||||
except IOError as e:
|
||||
except OSError as e:
|
||||
logging.info(e)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Keyboard interrupt detected, exiting gracefully.")
|
||||
logging.info("检测到键盘中断,正在清理并退出")
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
@@ -274,7 +240,7 @@ except KeyboardInterrupt:
|
||||
exit()
|
||||
|
||||
except Exception as e:
|
||||
logging.error("An unexpected error occurred: %s", e)
|
||||
logging.error("发生了意外的错误: %s", e)
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
|
||||
11
bin/start.sh
11
bin/start.sh
@@ -1,10 +1,17 @@
|
||||
#!/bin/bash
|
||||
f_name=main.py
|
||||
f1_name=weather.py
|
||||
f2_name=app.py
|
||||
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
||||
logdir=${dir%/*}
|
||||
logdir="${dir%/*}/log"
|
||||
appdir="${dir%/*}/app"
|
||||
# 确保日志目录存在
|
||||
mkdir -p "${logdir}"
|
||||
pid=`ps -ef |grep $dir/$f_name | grep -v grep |awk '{print $2}'`
|
||||
for id in $pid
|
||||
do
|
||||
kill -9 $id
|
||||
done
|
||||
nohup /usr/bin/python3 -u $dir/$f_name > $logdir/log/info.log 2>&1 &
|
||||
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 $appdir/$f2_name > $logdir/info-app.log 2>&1 &
|
||||
|
||||
99
bin/weather.py
Normal file
99
bin/weather.py
Normal file
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding:utf-8 -*-
|
||||
import os, sys, re, json, time, datetime
|
||||
import logging
|
||||
import subprocess
|
||||
import os
|
||||
from threading import Timer
|
||||
import requests
|
||||
|
||||
white = 255 # 颜色
|
||||
black = 0
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s') # 设置日志级别
|
||||
|
||||
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
|
||||
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
|
||||
if os.path.exists(libdir):
|
||||
sys.path.append(libdir) # 将引入文件添加到环境变量
|
||||
|
||||
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() # 检查请求是否成功
|
||||
# 直接打印原始数据
|
||||
logging.debug("Raw data from city.js: %s", response.text)
|
||||
# 预处理返回的数据,去除非JSON部分
|
||||
city_data_text = response.text.strip()
|
||||
city_data_text = city_data_text.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(5) # 重试前等待5秒
|
||||
|
||||
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()
|
||||
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(5) # 重试前等待5秒
|
||||
# 注意:无限重试可能会在特定情况下导致程序无法终止,请确保在实际使用中考虑适当的退出条件或限制重试次数。
|
||||
|
||||
def getWeath(city='101060101'):
|
||||
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',
|
||||
'Referer': 'http://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('http://d1.weather.com.cn/sk_2d/'+city+'.html',headers=headers)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
response.encoding = 'utf-8'
|
||||
Weath = response.text[11:]
|
||||
fileHandle = open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w')
|
||||
fileHandle.write(str(Weath))
|
||||
fileHandle.close()
|
||||
Timer(180, getWeath).start() # 定时器函数,间隔三分钟下载文件至本地
|
||||
print("天气文件更新")
|
||||
except requests.RequestException as e:
|
||||
logging.error("获取天气数据时出现网络错误: %s", e)
|
||||
except Exception as e:
|
||||
logging.error("发生了意外错误: %s", e)
|
||||
|
||||
try:
|
||||
getWeath() # 天气获取函数开始运行
|
||||
except IOError as e:
|
||||
logging.info(e)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("检测到键盘中断,正在退出")
|
||||
except Exception as e:
|
||||
logging.error("发生了意外错误: %s", e)
|
||||
exit()
|
||||
|
||||
# 脚本正常结束后的清理操作
|
||||
exit()
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 7.0 MiB |
BIN
pic/1736749257603.jpg
Normal file
BIN
pic/1736749257603.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.6 MiB |
Reference in New Issue
Block a user