mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-16 07:13:17 +08:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c90e54728 | ||
|
|
e70b05a529 | ||
|
|
d8c12f7ad3 | ||
|
|
8eb21afb19 | ||
|
|
7bc892d06b | ||
|
|
ca67499854 | ||
|
|
0b5623d5f6 | ||
|
|
7a71514224 | ||
|
|
fac4c591fb | ||
|
|
95e6bb12be | ||
|
|
57ea23a6fa | ||
|
|
5262d729fb | ||
|
|
428d056e58 | ||
|
|
066d9676f3 | ||
|
|
54bb2ccf21 |
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>
|
||||
@@ -48,7 +48,7 @@ def clear_screen():
|
||||
logging.info(e)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logging.info("按下了 Ctrl+C,正在退出")
|
||||
logging.info("检测到键盘中断,正在退出")
|
||||
epd2in13_V4.epdconfig.module_exit(cleanup=True)
|
||||
exit()
|
||||
|
||||
|
||||
@@ -215,7 +215,7 @@ install_packages() {
|
||||
# 安装pip包函数
|
||||
install_pip_packages() {
|
||||
echo "正在安装pip软件包"
|
||||
if ! sudo pip3 install -i "$PIPY_MIRROR" spidev borax pillow requests; then
|
||||
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
|
||||
|
||||
@@ -86,7 +86,7 @@ def Bottom_edge(): #在图片中添加底边内容
|
||||
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)
|
||||
@@ -157,7 +157,7 @@ def Partial_refresh():#局刷函数
|
||||
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)
|
||||
@@ -229,10 +229,10 @@ try:
|
||||
epd.init()
|
||||
epd.Clear(0xFF)
|
||||
epd.sleep()
|
||||
except IOError as e:
|
||||
except OSError as e:
|
||||
logging.info(e)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("检测到键盘中断,正在优雅地退出")
|
||||
logging.info("检测到键盘中断,正在清理并退出")
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#!/bin/bash
|
||||
f_name=main.py
|
||||
f1_name=weather.py
|
||||
f2_name=app.py
|
||||
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
||||
logdir="${dir%/*}/log"
|
||||
appdir="${dir%/*}/app"
|
||||
# 确保日志目录存在
|
||||
mkdir -p "${logdir}"
|
||||
pid=`ps -ef |grep $dir/$f_name | grep -v grep |awk '{print $2}'`
|
||||
@@ -12,3 +14,4 @@ do
|
||||
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 $appdir/$f2_name > $logdir/info-app.log 2>&1 &
|
||||
|
||||
@@ -17,49 +17,48 @@ if os.path.exists(libdir):
|
||||
sys.path.append(libdir) # 将引入文件添加到环境变量
|
||||
|
||||
def get_area_id(city_name):
|
||||
"""从city.js中检索AREAID"""
|
||||
"""从city.js中检索AREAID,无限重试直到成功"""
|
||||
url = "https://j.i8tq.com/weather2020/search/city.js"
|
||||
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 as e:
|
||||
logging.error("检索城市数据时发生网络错误: %s", e)
|
||||
except json.JSONDecodeError as e:
|
||||
logging.error("解析城市数据时发生JSON解码错误: %s", e)
|
||||
return None
|
||||
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"
|
||||
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 as e:
|
||||
logging.error("检索当前城市时出现网络错误: %s", e)
|
||||
except json.JSONDecodeError as e:
|
||||
logging.error("解析当前城市数据时出现JSON解码错误: %s", e)
|
||||
return None
|
||||
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 = {
|
||||
@@ -76,7 +75,7 @@ def getWeath(city='101060101'):
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
response.encoding = 'utf-8'
|
||||
Weath = response.text[11:]
|
||||
fileHandle = open('weather.json', 'w')
|
||||
fileHandle = open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w')
|
||||
fileHandle.write(str(Weath))
|
||||
fileHandle.close()
|
||||
Timer(180, getWeath).start() # 定时器函数,间隔三分钟下载文件至本地
|
||||
@@ -91,7 +90,7 @@ try:
|
||||
except IOError as e:
|
||||
logging.info(e)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("检测到键盘中断,正在优雅地退出")
|
||||
logging.info("检测到键盘中断,正在退出")
|
||||
except Exception as e:
|
||||
logging.error("发生了意外错误: %s", e)
|
||||
exit()
|
||||
|
||||
Reference in New Issue
Block a user