mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-17 07:23:16 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5436cba3d0 | ||
|
|
70d1b3d710 | ||
|
|
6fa3fe1407 | ||
|
|
cd6fc91cda | ||
|
|
60272f75ea | ||
|
|
faf0e2f505 |
79
app/app.py
79
app/app.py
@@ -1,79 +0,0 @@
|
||||
from flask import Flask, render_template, request, send_from_directory, redirect, url_for, jsonify
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
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') # 字体文件夹路径
|
||||
MAIN_PY_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'bin', 'main.py')
|
||||
|
||||
def list_font_files(font_dir):
|
||||
try:
|
||||
return os.listdir(font_dir)
|
||||
except Exception as e:
|
||||
print(f"Error listing font files: {e}")
|
||||
return [] # 返回空列表以避免迭代错误
|
||||
|
||||
@app.route('/execute-shell')
|
||||
def execute_shell():
|
||||
# 替换以下命令为您想要执行的Shell命令
|
||||
command = "sudo reboot now"
|
||||
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
if result.returncode == 0:
|
||||
return jsonify({'status': 'success', 'output': result.stdout})
|
||||
else:
|
||||
return jsonify({'status': 'error', 'output': result.stderr})
|
||||
|
||||
@app.route('/edit_main_py')
|
||||
def edit_main_py():
|
||||
try:
|
||||
with open(MAIN_PY_PATH, 'r') as file:
|
||||
content = file.read()
|
||||
except Exception as e:
|
||||
return f"Error reading main.py: {e}", 500
|
||||
return render_template('edit_main_py.html', content=content)
|
||||
|
||||
@app.route('/save_main_py', methods=['POST'])
|
||||
def save_main_py():
|
||||
new_content = request.form.get('content')
|
||||
if new_content is None:
|
||||
return "No content provided", 400
|
||||
try:
|
||||
with open(MAIN_PY_PATH, 'w') as file:
|
||||
file.write(new_content)
|
||||
except Exception as e:
|
||||
return f"Error saving main.py: {e}", 500
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@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('/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)
|
||||
@@ -1,59 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>编辑 main.py</title>
|
||||
<!-- 引用本地的highlight.js CSS -->
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='default.min.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<h1>编辑 main.py</h1>
|
||||
<!-- 添加一个表单来提交编辑后的代码 -->
|
||||
<form method="post" action="{{ url_for('save_main_py') }}">
|
||||
<pre style="background-color: black !important;"><code class="python" id="code">{{ content }}</code></pre>
|
||||
<input type="hidden" name="content" id="hidden-content">
|
||||
<input type="submit" value="保存">
|
||||
</form>
|
||||
|
||||
<!-- 引用本地的highlight.js JavaScript -->
|
||||
<script src="{{ url_for('static', filename='highlight.min.js') }}"></script>
|
||||
<script>
|
||||
// 初始化highlight.js
|
||||
document.addEventListener('DOMContentLoaded', (event) => {
|
||||
hljs.highlightAll();
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var preElements = document.querySelectorAll('pre.code-editor');
|
||||
preElements.forEach(function(pre) {
|
||||
pre.style.backgroundColor = 'black';
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<h2>重启</h2>
|
||||
</head>
|
||||
<body>
|
||||
<button id="executeShellBtn">重启</button>
|
||||
|
||||
<script>
|
||||
document.getElementById('executeShellBtn').addEventListener('click', function() {
|
||||
fetch('/execute-shell')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.status === 'success') {
|
||||
alert('Command executed successfully:\n' + data.output);
|
||||
} else {
|
||||
alert('Error executing command:\n' + data.output);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
alert('Error: ' + error);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,27 +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>
|
||||
|
||||
<!-- 添加跳转到edit_main_py的按钮 -->
|
||||
<a href="/edit_main_py"><button>编辑main.py</button></a>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -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>
|
||||
112
bin/install.sh
112
bin/install.sh
@@ -18,6 +18,8 @@ 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
|
||||
@@ -37,6 +39,9 @@ while [ "$#" -gt 0 ]; do
|
||||
--pisugar-power-manager)
|
||||
USE_PISUGAR_POWER_MANAGER=true
|
||||
;;
|
||||
# --webui)
|
||||
# INSTALL_WEBUI=true
|
||||
# ;;
|
||||
--version)
|
||||
if [ -z "$2" ]; then
|
||||
echo "错误: --version 参数后需要跟版本号"
|
||||
@@ -121,25 +126,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 +149,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 +191,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
|
||||
@@ -294,6 +290,86 @@ 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
|
||||
@@ -317,6 +393,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 +403,7 @@ if [ -f /etc/debian_version ]; then
|
||||
install_packages
|
||||
install_pip_packages
|
||||
setup_service
|
||||
#install_webui
|
||||
install_pisugar-wifi-conf
|
||||
install_pisugar-power-manager
|
||||
;;
|
||||
|
||||
40
webui/index.html
Normal file
40
webui/index.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<!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>
|
||||
23
webui/save.py
Normal file
23
webui/save.py
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/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