Files
CPY_ScannerScript/webserver.py
T
2026-06-20 15:44:09 +08:00

624 lines
22 KiB
Python

import sys
import time
import json
import gc
import machine
import network
import os
import binascii
import socket
from json_config import read_config
sys.path.append('/flash/script')
from MicroWebSrv2 import *
# ----------------------------------------------------------------------------
# ========== 文件系统API ========== #
SCRIPT_ROOT = '/'
current_web_socket = None
status_req_flag = False
wslock = None
config = read_config()
def send_ws(topic, msg):
if not current_web_socket:
return
try:
if isinstance(msg, bytes) or isinstance(msg, bytearray):
current_web_socket.SendBinaryMessage(msg)
else:
current_web_socket.SendTextMessage(json.dumps({'topic':topic, 'message':msg}))
except:
pass
def ws_send_status(topic, status):
global status_req_flag
if status_req_flag:
send_ws(topic, status)
status_req_flag = False
def ws_send_data(topic, data):
if not current_web_socket:
return False
try:
current_web_socket.SendBinaryMessage(data)
except:
return False
return True
@WebRoute(GET, '/api/script/list')
def api_script_list(httpClient, httpResponse):
params = httpResponse.QueryParams
print(params)
rel_path = params.get('id', '')
if rel_path.startswith('#'):
rel_path = rel_path[1:]
print("Listing scripts in path: ", rel_path)
abs_path = rel_path if rel_path else '/'
if abs_path == '#':
abs_path = '/'
if not abs_path.startswith('/'):
abs_path = '/' + abs_path
print("abs path: ", abs_path)
# 防止越权
if not abs_path.startswith(SCRIPT_ROOT):
httpResponse.Response.ReturnOkJSON({"success": False, "message": "Invalid path"})
return
data = []
try:
for name in os.listdir(abs_path):
if abs_path.endswith('/'):
item_path = abs_path + name
else:
item_path = abs_path + '/' + name
try:
is_dir = os.stat(item_path)[0] & 0x4000 == 0x4000
except:
is_dir = False
file_type = 'folder' if is_dir else 'file'
if not is_dir:
dotidx = name.rfind('.')
ext = name[dotidx:].lower() if dotidx >= 0 else ''
if ext == '.py': file_type = 'python'
elif ext == '.json': file_type = 'json'
elif ext == '.sh': file_type = 'shell'
elif ext == '.js': file_type = 'js'
elif ext == '.css': file_type = 'css'
elif ext == '.html': file_type = 'html'
if rel_path.endswith('/') or rel_path == '':
id_path = rel_path + name
else:
id_path = rel_path + '/' + name
id_path = id_path.replace('\\', '/')
data.append({
"id": id_path,
"text": name,
"type": file_type,
"children": is_dir,
"state": {"opened": False, "disabled": False, "selected": False}
})
httpResponse.Response.ReturnOkJSON({"success": True, "data": data})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(GET, '/api/script/info')
def api_script_info(httpClient, httpResponse):
params = httpResponse.QueryParams
rel_path = params.get('path', '')
abs_path = '/' + rel_path
if not abs_path.startswith(SCRIPT_ROOT):
httpResponse.Response.ReturnOkJSON({"success": False, "message": "Invalid path"})
return
# 判断文件是否存在
try:
st = os.stat(abs_path)
is_file = not (st[0] & 0x4000 == 0x4000)
except:
is_file = False
if not is_file:
httpResponse.Response.ReturnOkJSON({"success": False, "message": "File not found"})
return
try:
file_size = st[6] # 获取文件大小
httpResponse.Response.ReturnOkJSON({"success": True, "size": file_size})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(GET, '/api/script/read')
def api_script_read(httpClient, httpResponse):
params = httpResponse.QueryParams
rel_path = params.get('path', '')
# 获取分段参数,默认从头开始读取4K
try:
offset = int(params.get('offset', '0'))
size = int(params.get('size', '4096'))
except:
offset = 0
size = 4096
abs_path = '/' + rel_path
if not abs_path.startswith(SCRIPT_ROOT):
httpResponse.Response.ReturnOkJSON({"success": False, "message": "Invalid path"})
return
# 判断文件是否存在
try:
st = os.stat(abs_path)
is_file = not (st[0] & 0x4000 == 0x4000)
except:
is_file = False
if not is_file:
httpResponse.Response.ReturnOkJSON({"success": False, "message": "File not found"})
return
try:
file_size = st[6]
with open(abs_path, 'rb') as f:
# 移动到指定位置
f.seek(offset)
# 读取指定大小的数据
content = f.read(size)
encoded = binascii.b2a_base64(content).decode('ascii').rstrip('\n')
httpResponse.Response.ReturnOkJSON({
"success": True,
"data": encoded,
"offset": offset,
"size": len(content),
"total": file_size,
"done": offset + len(content) >= file_size
})
# 手动触发垃圾回收
gc.collect()
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
# 计算简单的校验和,用于验证文件完整性
def calculate_checksum(data):
checksum = 0
for b in data:
checksum = (checksum + b) & 0xFFFFFFFF # 使用32位校验和
return checksum
@WebRoute(POST, '/api/script/write')
def api_script_write(httpClient, httpResponse):
try:
# 手动触发垃圾回收
gc.collect()
data = httpResponse.GetPostedJSONObject()
rel_path = data.get('path', '')
content_b64 = data.get('content', '')
offset = int(data.get('offset', '0'))
is_final = data.get('final', False)
print(rel_path,offset,is_final)
abs_path = '/' + rel_path
if not abs_path.startswith(SCRIPT_ROOT):
httpResponse.Response.ReturnOkJSON({"success": False, "message": "Invalid path"})
return
print('abs_path', abs_path)
# 确保目录存在
slash_idx = abs_path.rfind('/')
if slash_idx > 0:
dir_path = abs_path[:slash_idx]
try:
os.mkdir(dir_path)
except:
pass
# base64解码
content = binascii.a2b_base64(content_b64)
# 创建临时文件路径(用于分段写入)
temp_path = abs_path + '.tmp'
print('temp_path', temp_path)
# 对于分段写入的情况,我们需要在临时文件中进行所有操作
if offset > 0:
# 分段写入 - 使用临时文件
try:
# 如果是第一个分段(offset=0),创建新文件;否则追加
# 在MicroPython中,os模块没有path子模块,使用os.stat检查文件是否存在
try:
os.stat(temp_path)
mode = 'r+b'
except OSError:
mode = 'wb'
with open(temp_path, mode) as f:
# 移动到指定位置
f.seek(offset)
# 记录写入前的位置
write_pos = f.tell()
# 写入数据
f.write(content)
# 强制刷新缓冲区
f.flush()
# 验证写入的字节数
if f.tell() - write_pos != len(content):
raise Exception("写入的字节数与预期不符")
# 手动触发垃圾回收
gc.collect()
httpResponse.Response.ReturnOkJSON({
"success": True,
"message": "File segment written to temporary file successfully",
"offset": offset + len(content),
"final": is_final
})
except Exception as e:
# 出现错误时,尝试删除临时文件
try:
# 在MicroPython中,使用os.stat检查文件是否存在
try:
os.stat(temp_path)
os.remove(temp_path)
except OSError:
# 文件不存在,无需删除
pass
except:
pass
raise e
else:
# 对于完整文件写入(offset=0)或第一个分段,使用临时文件机制
try:
# 写入临时文件
with open(temp_path, 'wb') as f:
f.write(content)
f.flush() # 确保数据写入磁盘
# 验证临时文件
# 1. 检查文件是否存在
try:
st = os.stat(temp_path)
except:
raise Exception("临时文件创建失败")
# 2. 检查文件大小
temp_size = st[6]
if temp_size != len(content):
raise Exception("临时文件大小与预期不符")
# 3. 读取并校验内容
with open(temp_path, 'rb') as f:
read_content = f.read()
if len(read_content) != len(content):
raise Exception("读取的临时文件内容长度与预期不符")
# 计算校验和并比较
original_checksum = calculate_checksum(content)
read_checksum = calculate_checksum(read_content)
if original_checksum != read_checksum:
raise Exception("临时文件内容校验失败")
# 如果是完整文件写入(非分段模式),直接重命名
if not is_final:
# 对于非分段模式或第一个分段,直接保留临时文件
httpResponse.Response.ReturnOkJSON({
"success": True,
"message": "File written to temporary file successfully",
"offset": len(content),
"final": is_final
})
else:
# 如果是单个完整分块,直接重命名
# 先删除原文件(如果存在)
try:
os.remove(abs_path)
except:
pass
# 将临时文件重命名为目标文件
os.rename(temp_path, abs_path)
# 验证重命名是否成功
os.stat(abs_path)
# 手动触发垃圾回收
gc.collect()
httpResponse.Response.ReturnOkJSON({
"success": True,
"message": "File written successfully",
"offset": len(content),
"final": is_final
})
except Exception as e:
# 出现错误时,尝试删除临时文件
try:
# 在MicroPython中,使用os.stat检查文件是否存在
try:
os.stat(temp_path)
os.remove(temp_path)
except OSError:
# 文件不存在,无需删除
pass
except:
pass
# 抛出异常
raise e
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(POST, '/api/script/write/finalize')
def api_script_write_finalize(httpClient, httpResponse):
try:
# 手动触发垃圾回收
gc.collect()
data = httpResponse.GetPostedJSONObject()
rel_path = data.get('path', '')
expected_size = int(data.get('total_size', '0'))
abs_path = '/' + rel_path
temp_path = abs_path + '.tmp'
if not abs_path.startswith(SCRIPT_ROOT):
httpResponse.Response.ReturnOkJSON({"success": False, "message": "Invalid path"})
return
# 检查临时文件是否存在(对于分段写入)
temp_exists = False
try:
os.stat(temp_path)
temp_exists = True
except OSError:
pass
if not temp_exists:
# 检查原文件是否存在(对于可能的非分段写入)
abs_exists = False
try:
os.stat(abs_path)
abs_exists = True
except OSError:
pass
if abs_exists:
# 文件已经存在,直接进行校验
pass
else:
httpResponse.Response.ReturnOkJSON({"success": False, "message": "临时文件不存在"})
return
# 对于分段写入,现在需要验证临时文件并将其重命名为目标文件
if temp_exists:
# 验证临时文件
try:
# 1. 检查文件大小
st = os.stat(temp_path)
actual_size = st[6]
# 由于前端报告的是字符数,后端存储的是字节数,可能会有编码差异
# 因此我们只做粗略检查,允许一定误差
size_diff = abs(actual_size - expected_size)
size_tolerance = 1024 # 允许1KB的误差
if size_diff > size_tolerance:
# 大小差异过大,可能存在问题
# 计算差异百分比
if expected_size > 0:
diff_percent = (size_diff / expected_size) * 100
else:
diff_percent = 0
# 如果差异超过50%,认为是严重问题
if diff_percent > 50:
# 删除临时文件
try:
# 使用os.stat检查文件是否存在
try:
os.stat(temp_path)
os.remove(temp_path)
except OSError:
# 文件不存在,无需删除
pass
except:
pass
httpResponse.Response.ReturnOkJSON({"success": False,
"message": "临时文件大小异常:期望%d,实际%d,差异%.1f%%" % (expected_size, actual_size, diff_percent)})
return
else:
# 差异在可接受范围内,记录警告但继续
print("警告:临时文件大小不匹配 - 期望%d,实际%d,差异%.1f%%" % (expected_size, actual_size, diff_percent))
# 2. 尝试刷新临时文件缓存
with open(temp_path, 'r+b') as f:
f.flush()
# 校验成功,先删除原文件(如果存在)
try:
# 使用os.stat检查文件是否存在
try:
os.stat(abs_path)
os.remove(abs_path)
except OSError:
# 文件不存在,无需删除
pass
except Exception as e:
print("删除原文件时出错,但继续执行:", e)
# 将临时文件重命名为目标文件
os.rename(temp_path, abs_path)
# 验证重命名是否成功
os.stat(abs_path)
# 手动触发垃圾回收
gc.collect()
except Exception as e:
# 出现错误时,尝试删除临时文件
try:
# 使用os.stat检查文件是否存在
try:
os.stat(temp_path)
os.remove(temp_path)
except OSError:
# 文件不存在,无需删除
pass
except:
pass
raise e
# 检查目标文件是否存在
abs_exists = False
try:
os.stat(abs_path)
abs_exists = True
except OSError:
pass
if not abs_exists:
httpResponse.Response.ReturnOkJSON({"success": False, "message": "文件校验失败:文件不存在"})
return
# 尝试刷新文件系统缓存,确保数据持久化
try:
with open(abs_path, 'r+b') as f:
f.flush()
except Exception as e:
print("刷新文件时出错:", e)
# 手动触发垃圾回收
gc.collect()
# 最后检查文件是否仍然存在
final_exists = False
try:
os.stat(abs_path)
final_exists = True
except OSError:
pass
if final_exists:
response_data = {"success": True, "message": "文件写入完成并校验成功"}
if 'actual_size' in locals():
response_data['size'] = actual_size
else:
# 尝试获取文件大小
try:
st = os.stat(abs_path)
response_data['size'] = st[6]
except:
pass
httpResponse.Response.ReturnOkJSON(response_data)
else:
httpResponse.Response.ReturnOkJSON({"success": False, "message": "文件校验失败:文件不存在"})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(POST, '/api/script/delete')
def api_script_delete(httpClient, httpResponse):
try:
data = httpResponse.GetPostedJSONObject()
rel_path = data.get('path', '')
abs_path = '/' + rel_path
if not abs_path.startswith(SCRIPT_ROOT):
httpResponse.Response.ReturnOkJSON({"success": False, "message": "Invalid path"})
return
# 判断文件是否存在
try:
st = os.stat(abs_path)
is_file = not (st[0] & 0x4000 == 0x4000)
except:
is_file = False
if is_file:
os.remove(abs_path)
httpResponse.Response.ReturnOkJSON({"success": True, "message": "File deleted successfully"})
else:
httpResponse.Response.ReturnOkJSON({"success": False, "message": "File not found"})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(GET, '/api/device/reboot')
def api_device_reboot(httpClient, httpResponse):
try:
httpResponse.Response.ReturnOkJSON({"success": True, "message": "device reboot now"})
machine.reset()
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
ws_cmd_handler = None
def _acceptWebSocketCallback(microWebSrv2, webSocket) :
print("WS ACCEPT")
webSocket.OnTextMessage = _recvTextCallback
webSocket.OnBinaryMessage = _recvBinaryCallback
webSocket.OnClosed = _closedCallback
current_web_socket = webSocket
def _recvTextCallback(webSocket, msg) :
# print("WS RECV TEXT : %s" % msg)
try:
obj = json.loads(msg)
if ws_cmd_handler and 'message' in obj:
ws_cmd_handler(webSocket, obj['message'])
except Exception as e:
sys.print_exception(e)
def _recvBinaryCallback(webSocket, data) :
print("WS RECV DATA : %s" % data)
def _closedCallback(webSocket):
print("WS CLOSED")
current_web_socket = None
from net import local_cmd_handle
def __cmd_handler(webSocket, msg):
global current_web_socket, status_req_flag
current_web_socket = webSocket
# print(f"WS CMD: {msg}")
try:
obj = msg
if obj['cmd'] in ['start', 'stop', 'reset', 'emer_stop']:
webSocket.SendTextMessage(json.dumps({"success": True, "message": f"cmd {obj['cmd']} received"}))
local_cmd_handle(json.dumps(obj).encode('utf-8'))
elif obj['cmd'] in ['status']:
status_req_flag = True
except Exception as e:
sys.print_exception(e)
gc.collect()
def py_server(cmd_handler):
global ws_cmd_handler
ws_cmd_handler = cmd_handler
print('py server start')
wsMod = MicroWebSrv2.LoadModule('WebSockets')
wsMod.OnWebSocketAccepted = _acceptWebSocketCallback
mws2 = MicroWebSrv2()
mws2.SetEmbeddedConfig()
mws2.BufferSlotsCount = 16
mws2.BufferSlotSize = 1024
mws2.ConnQueueCapacity = 1
mws2.MaxRequestContentLength = 16 * 1024
mws2.RootPath = '/flash/web_root/scanner/'
mws2.NotFoundURL = '/'
mws2.StartManaged(procStackSize = 20*1024)
def ws_run():
py_server(__cmd_handler)
if __name__ == '__main__':
ws_run()