24 Commits

Author SHA1 Message Date
星光-k
118cc0a2ee Update main.py 2025-01-19 06:35:24 +08:00
星光-k
3155088921 Update weather.py 2025-01-19 06:11:12 +08:00
星光-k
28f0a9d37d Update main.py 2025-01-19 06:10:35 +08:00
星光-k
fc1678aa95 Update install.sh 2025-01-19 05:48:39 +08:00
星光-k
f0eb8dc53e Update start.sh 2025-01-19 05:47:35 +08:00
星光-k
9e5114b7bd Update start.sh 2025-01-19 05:47:17 +08:00
星光-k
bb44237caa Delete webui directory 2025-01-19 05:46:48 +08:00
星光-k
e82fd225f5 Update main.py 2025-01-19 05:44:46 +08:00
星光-k
c67ae6bd78 Update main.py 2025-01-19 04:11:49 +08:00
星光-k
72c2b23566 Update main.py 2025-01-19 04:02:43 +08:00
星光-k
590494d734 Update start.sh 2025-01-19 01:20:53 +08:00
星光-k
8a29bf9110 Update main.py 2025-01-19 01:20:15 +08:00
星光-k
ab6abaf2e3 Update weather.py 2025-01-19 01:19:21 +08:00
星光-k
5436cba3d0 Update install.sh 2025-01-18 09:38:47 +08:00
星光-k
70d1b3d710 Delete webui/1 2025-01-18 09:11:52 +08:00
星光-k
6fa3fe1407 Add files via upload 2025-01-18 01:11:29 +00:00
星光-k
cd6fc91cda Create 1 2025-01-18 09:10:49 +08:00
星光-k
60272f75ea Delete app directory 2025-01-18 09:09:51 +08:00
星光-k
faf0e2f505 Add files via upload 2025-01-18 01:09:34 +00:00
星光-k
863e9b2f5d Add files via upload 2025-01-17 02:40:52 +00:00
星光-k
d523a31006 Add files via upload 2025-01-17 02:40:26 +00:00
星光-k
9d4895fca4 Add files via upload 2025-01-17 02:02:59 +00:00
星光-k
e7f71c699e Add files via upload 2025-01-17 02:02:07 +00:00
星光-k
419f44be7e Add files via upload 2025-01-17 02:01:40 +00:00
7 changed files with 110 additions and 219 deletions

View File

@@ -1,95 +0,0 @@
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)

View File

@@ -1,23 +0,0 @@
<!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>

View File

@@ -1,26 +0,0 @@
<!-- 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>

View File

@@ -121,25 +121,14 @@ is_raspberry_pi() {
fi
}
# 定义链接变量
DEBIAN_MIRROR="http://deb.debian.org/debian/"
DEBIAN_SECURITY_MIRROR="http://security.debian.org/"
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/"
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_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian"
DEBIAN_SECURITY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian-security"
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
RASPBERRY_PI_SOURCE_DEBIAN11="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi"
RASPBERRY_PI_SOURCE_DEBIAN12="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi"
# 定义仓库链接变量
INK_SCREEN_CLOCK_REPO_URL="https://github.com/kxgx/2.13-Ink-screen-clock"
@@ -155,6 +144,7 @@ update_sources_list() {
local raspberry_pi_source_in_use=$(grep -oP 'deb\s+\K.+' /etc/apt/sources.list.d/raspi.list | head -1)
# 检查并替换 Debian 源
if [ "$USE_CN_MIRROR" = true ]; then
if [ "$debian_mirror_in_use" != "$DEBIAN_MIRROR" ]; then
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
{
@@ -196,12 +186,13 @@ update_sources_list() {
else
echo "Raspberry Pi 源链接已更新,跳过替换" >&2
fi
fi
}
# 安装包函数
install_packages() {
echo "正在更新源列表"
if ! sudo apt-get -q -y update; then
if ! sudo apt-get -q update; then
echo "更新源列表失败" >&2
exit 1
fi
@@ -317,6 +308,7 @@ if [ -f /etc/debian_version ]; then
install_packages
install_pip_packages
setup_service
#install_webui
install_pisugar-wifi-conf
install_pisugar-power-manager
;;
@@ -326,6 +318,7 @@ if [ -f /etc/debian_version ]; then
install_packages
install_pip_packages
setup_service
#install_webui
install_pisugar-wifi-conf
install_pisugar-power-manager
;;

View File

@@ -5,7 +5,6 @@ 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
import socket
@@ -20,7 +19,7 @@ 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) #字体文件
font01 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 19) #字体文件
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) #字体文件
@@ -37,7 +36,7 @@ def get_date():#返回当前年月日及星期几
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')
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地址
@@ -55,7 +54,7 @@ def Get_ipv4_address(): # 获取当前的IP地址
return f"获取失败"
def CPU_temperature():#CPU温度获取
temperatura = os.popen('vcgencmd measure_temp').readline()
temperatura = temperatura.replace('temp=','').strip()
temperatura = temperatura.replace('temp=','').replace('\'C', '').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'))
@@ -104,14 +103,14 @@ def Weather(): #在图片中添加天气内容
weather_update = Weather_text['time'] #天气更新时间
weather_date = Weather_text['date'] #日期
humidity = Weather_text['SD'] #湿度
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((175,25),"天气:",font = font06,fill =0)#显示当前天气前缀
draw.text((175,45),"温度:",font = font06,fill =0)#显示当前温度前缀
draw.text((175,65),"湿度:",font = font06,fill =0)#显示当前湿度前缀
draw.text((175,85),"城市:",font = font06,fill =0)#显示当前城市前缀
draw.text((205,25),weather,font = font06,fill =0)
draw.text((205,45),temperature,font = font06,fill =0)
draw.text((205,65),humidity,font = font06,fill =0)
draw.text((205,85),Weather_position,font = font06,fill =0)
draw.text((211,107),weather_update,font = font05,fill =255) #显示天气更新时间
def Basic_refresh(): #全刷函数
@@ -122,6 +121,18 @@ def Basic_refresh(): #全刷函数
global local_time
local_time=get_time()
draw.text((5,40),local_time,font = font03,fill =0)#显示当前时间
global cpu_temp
cpu_temp = str(CPU_temperature()) # 调用函数并转换为字符串
text_to_display = "CPU温度:" + cpu_temp + "°C"
draw.text((4, 19), text_to_display, font=font06, fill=0) # 绘制文本
global cpu_use
cpu_use = str(CPU_usage()) # 调用函数并转换为字符串
text_to_display = "CPU占用:" + cpu_use + "%"
draw.text((4, 84), text_to_display, font=font06, fill=0) # 绘制文本
global mem_use
mem_use = str(Memory_footprint()) # 调用函数并转换为字符串
text_to_display = "内存占用:" + mem_use + "%" # 连接字符串
draw.text((86, 84), text_to_display, font=font06, fill=0) # 绘制文本
Bottom_edge() #添加底边内容
Weather() #天气内容
epd.display(epd.getbuffer(info_image.rotate(180)))
@@ -176,26 +187,26 @@ def Partial_refresh():#局刷函数
weather_date1 = Weather_text['date'] #日期
humidity1 = Weather_text['SD'] #湿度
if (weather11==weather) ==False:
draw.rectangle((191, 25, 249, 38), fill = 255) #天气局刷区域
draw.text((191,25),weather11,font = font06,fill =0)
draw.rectangle((205, 25, 250, 38), fill = 255) #天气局刷区域
draw.text((205,25),weather11,font = font06,fill =0)
weather=weather11
logging.info("天气局部刷新")
Local_strong_brush() #局部强刷
if (temperature1==temperature) ==False:
draw.rectangle((191, 45, 249, 57), fill = 255) #局刷区域
draw.text((191,45),temperature1,font = font06,fill =0)
draw.rectangle((205, 45, 250, 57), fill = 255) #局刷区域
draw.text((205,45),temperature1,font = font06,fill =0)
temperature=temperature1
logging.info("温度局部刷新")
Local_strong_brush() #局部强刷
if (humidity1==humidity) ==False:
draw.rectangle((191, 65, 249, 77), fill = 255) #局刷区域
draw.text((191,65),humidity1,font = font06,fill =0)
draw.rectangle((205, 65, 250, 77), fill = 255) #局刷区域
draw.text((205,65),humidity1,font = font06,fill =0)
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)
draw.rectangle((205, 85, 250, 98), fill = 255) #局刷区域
draw.text((205,85),Weather_position1,font = font06,fill =0)
Weather_position = Weather_position1
logging.info("城市局部刷新")
Local_strong_brush() #局部强刷
@@ -213,39 +224,73 @@ def Partial_refresh():#局刷函数
draw.text((129,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
power_str=power_str1
Local_strong_brush() #局部强刷
logging.info("电源电量局部刷新")
try:
##################屏幕初始化#########################
epd = epd2in13_V4.EPD() #初始化
epd.init()#设定屏幕刷新模式
#epd.Clear(0xFF) #清除屏幕内容
##################屏幕初始化#########################
logging.info("Width = %s, Height = %s", format(epd.width), format(epd.height)) #打印屏幕高度及宽度
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.sleep()
except OSError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("检测到键盘中断,正在清理并退出")
epd.init()
epd.Clear(0xFF) # 清除屏幕内容
epd.sleep() # 使屏幕进入休眠状态
epd2in13_V4.epdconfig.module_exit() # 清理资源
exit()
#logging.info("电源电量局部刷新")
'''CPU温度显示'''
global cpu_temp
cpu_temp1 =CPU_temperature()
if (cpu_temp1==cpu_temp) ==False:
draw.rectangle((1, 19, 130, 38), fill = 255)
cpu_temp = str(CPU_temperature()) # 调用函数并转换为字符串
text_to_display = "CPU温度:" + cpu_temp + "°C"
draw.text((4, 19), text_to_display, font=font06, fill=0) # 绘制文本
cpu_temp1=cpu_temp
Local_strong_brush() #局部强刷
#logging.info("CPU温度局部刷新")
'''CPU温度显示'''
'''CPU占用显示'''
global cpu_use
cpu_use1 =CPU_usage()
if (cpu_use1==cpu_use) ==False:
draw.rectangle((1, 84, 85, 98), fill = 255)
cpu_use = str(CPU_usage()) # 调用函数并转换为字符串
text_to_display = "CPU占用:" + cpu_use + "%"
draw.text((4, 84), text_to_display, font=font06, fill=0) # 绘制文本
cpu_use1=cpu_use
Local_strong_brush() #局部强刷
#logging.info("CPU温度局部刷新")
'''CPU占用显示'''
'''内存百分比显示'''
global mem_use
mem_use1 =Memory_footprint()
if (mem_use1==mem_use) ==False:
draw.rectangle((86, 84, 170, 98), fill = 255)
mem_use = str(Memory_footprint()) # 调用函数并转换为字符串
text_to_display = "内存占用:" + mem_use + "%" # 连接字符串
draw.text((86, 84), text_to_display, font=font06, fill=0) # 绘制文本
mem_use1=mem_use
Local_strong_brush() #局部强刷
#logging.info("内存百分比局部刷新")
'''内存百分比显示'''
except Exception as e:
logging.error("发生了意外的错误: %s", e)
epd.init()
epd.Clear(0xFF) # 清除屏幕内容
epd.sleep() # 使屏幕进入休眠状态
epd2in13_V4.epdconfig.module_exit() # 清理资源
exit()
retry_interval = 180 # 设置重试间隔时间(秒)
while True:
try:
##################屏幕初始化#########################
epd = epd2in13_V4.EPD() #初始化
epd.init()#设定屏幕刷新模式
#epd.Clear(0xFF) #清除屏幕内容
##################屏幕初始化#########################
logging.info("Width = %s, Height = %s", format(epd.width), format(epd.height)) #打印屏幕高度及宽度
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.sleep()
break # 如果脚本执行成功,则退出循环
except (OSError, Exception) as e: # 捕获你提到的异常
logging.error("发生了错误: %s", e)
time.sleep(retry_interval) # 等待一段时间后重试
except KeyboardInterrupt:
logging.info("检测到键盘中断,正在清理并退出")
epd.init()
epd.Clear(0xFF) # 清除屏幕内容
epd.sleep() # 使屏幕进入休眠状态
epd2in13_V4.epdconfig.module_exit() # 清理资源
exit()
# 脚本正常结束后的清理操作
epd.init()

View File

@@ -1,10 +1,8 @@
#!/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}'`
@@ -14,4 +12,3 @@ 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 &

View File

@@ -41,7 +41,7 @@ def get_area_id(city_name):
logging.error("从city.js接收到的数据为空")
except (requests.RequestException, json.JSONDecodeError) as e:
logging.error("检索或解析城市数据时发生错误: %s", e)
time.sleep(5) # 重试前等待5秒
time.sleep(180) # 重试前等待
def get_current_city():
"""获取当前城市名称,无限重试直到成功"""
@@ -57,7 +57,7 @@ def get_current_city():
logging.error("获取当前城市失败: %s", data['message'])
except (requests.RequestException, json.JSONDecodeError) as e:
logging.error("检索或解析当前城市时出现错误: %s", e)
time.sleep(5) # 重试前等待5秒
time.sleep(180) # 重试前等待
# 注意:无限重试可能会在特定情况下导致程序无法终止,请确保在实际使用中考虑适当的退出条件或限制重试次数。
def getWeath(city='101060101'):
@@ -87,7 +87,7 @@ def getWeath(city='101060101'):
try:
getWeath() # 天气获取函数开始运行
except IOError as e:
except OSError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("检测到键盘中断,正在退出")