mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-15 07:03:16 +08:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99780f1bf9 | ||
|
|
118cc0a2ee | ||
|
|
3155088921 | ||
|
|
28f0a9d37d | ||
|
|
fc1678aa95 | ||
|
|
f0eb8dc53e | ||
|
|
9e5114b7bd | ||
|
|
bb44237caa | ||
|
|
e82fd225f5 | ||
|
|
c67ae6bd78 | ||
|
|
72c2b23566 | ||
|
|
590494d734 | ||
|
|
8a29bf9110 | ||
|
|
ab6abaf2e3 |
@@ -18,8 +18,6 @@ USE_CN_GIT=false
|
||||
USE_PISUGAR_WIFI_CONF=false
|
||||
# 检查是否安装pisugar-power-manager
|
||||
USE_PISUGAR_POWER_MANAGER=false
|
||||
# 检查是否安装webui
|
||||
#INSTALL_WEBUI=false
|
||||
|
||||
# 解析命令行参数
|
||||
while [ "$#" -gt 0 ]; do
|
||||
@@ -39,9 +37,6 @@ while [ "$#" -gt 0 ]; do
|
||||
--pisugar-power-manager)
|
||||
USE_PISUGAR_POWER_MANAGER=true
|
||||
;;
|
||||
# --webui)
|
||||
# INSTALL_WEBUI=true
|
||||
# ;;
|
||||
--version)
|
||||
if [ -z "$2" ]; then
|
||||
echo "错误: --version 参数后需要跟版本号"
|
||||
@@ -290,86 +285,6 @@ install_pisugar-wifi-conf() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 如果指定了--webui参数,则添加Nginx配置
|
||||
install_webui() {
|
||||
if [ "$INSTALL_WEBUI" = true ]; then
|
||||
WEBUI_FILE="/root/2.13-Ink-screen-clock/webui"
|
||||
NGINX_WEBUI_FILE="/var/www/html"
|
||||
echo "正在安装并配置webui"
|
||||
echo "正在安装软件包"
|
||||
if ! sudo apt-get update && sudo apt-get -q -y install nginx php7.4-fpm; then
|
||||
echo "软件包安装失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "正在复制webui文件"
|
||||
if ! sudo cp -r "$WEBUI_FILE" "$NGINX_WEBUI_FILE"; then
|
||||
echo "webui文件复制失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "正在修改nginx配置文件"
|
||||
# 定义Nginx主配置文件的路径
|
||||
NGINX_CONFIG_PATH="/etc/nginx/nginx.conf"
|
||||
NGINX_CONFIG_BAK="$NGINX_CONFIG_PATH.bak"
|
||||
|
||||
# 备份现有的Nginx配置文件
|
||||
cp "$NGINX_CONFIG_PATH" "$NGINX_CONFIG_BAK"
|
||||
echo "已备份现有Nginx配置文件到 $NGINX_CONFIG_BAK"
|
||||
|
||||
# 定义文件路径变量
|
||||
WEBROOT_PATH="$NGINX_WEBUI_FILE"
|
||||
CGI_BIN_PATH="$WEBROOT_PATH/cgi-bin"
|
||||
CGI_PASS="127.0.0.1:9000" # 根据您的CGI服务器配置
|
||||
|
||||
# 定义要添加的服务器配置
|
||||
SERVER_CONFIG="
|
||||
server {
|
||||
listen 80;
|
||||
server_name 0.0.0.0;
|
||||
|
||||
location / {
|
||||
root $WEBROOT_PATH;
|
||||
index index.html index.htm;
|
||||
}
|
||||
|
||||
location /save.py {
|
||||
include snippets/fastcgi-php.conf;
|
||||
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据您的PHP版本进行调整
|
||||
fastcgi_param SCRIPT_FILENAME \$document_root/save.py;
|
||||
add_header X-Robots-Tag \"noindex,noarchive,nosnippet,nofollow\";
|
||||
auth_basic \"Restricted Content\";
|
||||
auth_basic_user_file /etc/nginx/.htpasswd; # 确保您有.htpasswd文件用于基本认证
|
||||
}
|
||||
}
|
||||
"
|
||||
|
||||
# 检查是否已经存在类似的server配置
|
||||
if ! grep -q 'server_name 0.0.0.0;' "$NGINX_CONFIG_PATH"; then
|
||||
# 如果不存在,将配置添加到http块中
|
||||
awk -v config="$SERVER_CONFIG" '
|
||||
/http {/ {
|
||||
print
|
||||
print config
|
||||
next
|
||||
}
|
||||
1
|
||||
' "$NGINX_CONFIG_PATH" > temp && mv temp "$NGINX_CONFIG_PATH"
|
||||
else
|
||||
echo "服务器配置已存在,未进行修改"
|
||||
fi
|
||||
|
||||
# 测试Nginx配置
|
||||
nginx -t
|
||||
|
||||
# 如果配置测试成功,则重启Nginx
|
||||
if [ $? -eq 0 ]; then
|
||||
systemctl restart nginx
|
||||
echo "Nginx配置已成功应用并重启"
|
||||
else
|
||||
echo "Nginx配置测试失败,请检查配置文件"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# 主逻辑
|
||||
# 检测是否是Debian系统
|
||||
if [ -f /etc/debian_version ]; then
|
||||
|
||||
151
bin/main.py
151
bin/main.py
@@ -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,43 +224,77 @@ 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()
|
||||
time.sleep(300)
|
||||
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()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
epd2in13_V4.epdconfig.module_exit() # 清理资源
|
||||
exit()
|
||||
exit()
|
||||
|
||||
@@ -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 &
|
||||
|
||||
@@ -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("检测到键盘中断,正在退出")
|
||||
@@ -96,4 +96,4 @@ except Exception as e:
|
||||
exit()
|
||||
|
||||
# 脚本正常结束后的清理操作
|
||||
exit()
|
||||
exit()
|
||||
|
||||
9
webui/default.min.css
vendored
9
webui/default.min.css
vendored
@@ -1,9 +0,0 @@
|
||||
/*!
|
||||
Theme: Default
|
||||
Description: Original highlight.js style
|
||||
Author: (c) Ivan Sagalaev <maniac@softwaremaniacs.org>
|
||||
Maintainer: @highlightjs/core-team
|
||||
Website: https://highlightjs.org/
|
||||
License: see project LICENSE
|
||||
Touched: 2021
|
||||
*/pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}.hljs{background:#f3f3f3;color:#444}.hljs-comment{color:#697070}.hljs-punctuation,.hljs-tag{color:#444a}.hljs-tag .hljs-attr,.hljs-tag .hljs-name{color:#444}.hljs-attribute,.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-name,.hljs-selector-tag{font-weight:700}.hljs-deletion,.hljs-number,.hljs-quote,.hljs-selector-class,.hljs-selector-id,.hljs-string,.hljs-template-tag,.hljs-type{color:#800}.hljs-section,.hljs-title{color:#800;font-weight:700}.hljs-link,.hljs-operator,.hljs-regexp,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-symbol,.hljs-template-variable,.hljs-variable{color:#ab5656}.hljs-literal{color:#695}.hljs-addition,.hljs-built_in,.hljs-bullet,.hljs-code{color:#397300}.hljs-meta{color:#1f7199}.hljs-meta .hljs-string{color:#38a}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}
|
||||
1213
webui/highlight.min.js
vendored
1213
webui/highlight.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,40 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ace-editor@1.4.12/css/ace.min.css">
|
||||
<script src="https://cdn.jsdelivr.net/npm/ace-editor@1.4.12/js/ace.min.js"></script>
|
||||
<meta charset="UTF-8">
|
||||
<title>编辑 main.py</title>
|
||||
<style>
|
||||
#editor {
|
||||
width: 100%;
|
||||
height: 500px;
|
||||
font-family: monospace;
|
||||
font-size: 16px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>编辑 main.py</h1>
|
||||
<form action="/save.py" method="post">
|
||||
<div id="editor"><?php echo file_get_contents('/root/2.13-Ink-screen-clock/bin/main.py'); ?></div>
|
||||
<input type="hidden" name="content" id="hidden-content">
|
||||
<br>
|
||||
<input type="submit" value="保存">
|
||||
</form>
|
||||
<script>
|
||||
// 初始化 Ace Editor
|
||||
var editor = ace.edit("editor");
|
||||
editor.setTheme("ace/theme/monokai");
|
||||
editor.getSession().setMode("ace/mode/python");
|
||||
editor.setValue(<?php echo json_encode(file_get_contents($filePath)); ?>);
|
||||
|
||||
// 在表单提交前将编辑器内容赋值给隐藏的textarea
|
||||
document.querySelector('form').addEventListener('submit', function() {
|
||||
document.getElementById('hidden-content').value = editor.getValue();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
|
||||
# 设置文件路径
|
||||
file_path = '/root/2.13-Ink-screen-clock/bin/main.py'
|
||||
|
||||
# 读取POST数据
|
||||
content_length = int(os.environ.get('CONTENT_LENGTH', 0))
|
||||
content = sys.stdin.read(content_length)
|
||||
|
||||
# 保存文件
|
||||
try:
|
||||
with open(file_path, 'w') as file:
|
||||
file.write(content)
|
||||
response = "<h1>文件保存成功!</h1>"
|
||||
except Exception as e:
|
||||
response = "<h1>文件保存失败:{}</h1>".format(e)
|
||||
|
||||
# 返回响应
|
||||
print("Content-Type: text/html")
|
||||
print()
|
||||
print(response)
|
||||
Reference in New Issue
Block a user