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

816 lines
30 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 *
# ----------------------------------------------------------------------------
# ========== 全局变量 ========== #
SCRIPT_ROOT = '/'
web_socket_list = []
wslock = None
config = read_config()
ws_cmd_handler = None
# ========== WebSocket 工具函数 ========== #
def send_ws_list(topic, msg):
try:
for ws in web_socket_list:
ws.SendTextMessage(json.dumps({'topic':topic, 'message':msg}))
except:
pass
def send_ws(webSocket,topic, msg):
try:
webSocket.SendTextMessage(json.dumps({'topic':topic, 'message':msg}))
except:
pass
# ========== WebSocket 回调函数 ========== #
def _acceptWebSocketCallback(microWebSrv2, webSocket) :
print("WS ACCEPT")
webSocket.OnTextMessage = _recvTextCallback
webSocket.OnBinaryMessage = _recvBinaryCallback
webSocket.OnClosed = _closedCallback
def _recvTextCallback(webSocket, msg) :
global web_socket_list
print("WS RECV TEXT : %s" % msg)
try:
obj = json.loads(msg)
if obj['action'] == 'subscribe':
if webSocket not in web_socket_list:
web_socket_list.append(webSocket)
else:
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) :
global web_socket_list
print("WS CLOSED")
if webSocket in web_socket_list:
web_socket_list.remove(webSocket)
# ========== 命令处理 ========== #
from net import local_cmd_handle
def __cmd_handler(webSocket, msg):
print(f"WS CMD: {msg}")
try:
obj = msg
if obj['cmd'] in ['start', 'stop', 'reset', 'daq_set_sn']:
webSocket.SendTextMessage(json.dumps({"success": True, "message": f"cmd {obj['cmd']} received"}))
local_cmd_handle(json.dumps(obj).encode('utf-8'))
except Exception as e:
sys.print_exception(e)
# ========== 文件系统API ========== #
@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
})
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"})
time.sleep(1)
machine.reset()
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
# ========== 固件升级API ========== #
FIRMWARE_TEMP_PATH = '/flash/firmware.tmp'
@WebRoute(POST, '/api/firmware/upload')
def api_firmware_upload(httpClient, httpResponse):
try:
# 手动触发垃圾回收
gc.collect()
data = httpResponse.GetPostedJSONObject()
content_b64 = data.get('content', '')
offset = int(data.get('offset', '0'))
is_final = data.get('final', False)
# base64解码
content = binascii.a2b_base64(content_b64)
# 写入临时文件
mode = 'r+b' if offset > 0 else 'wb'
with open(FIRMWARE_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": "固件分段上传成功",
"offset": offset + len(content),
"final": is_final
})
except Exception as e:
# 出现错误时,尝试删除临时文件
try:
os.stat(FIRMWARE_TEMP_PATH)
os.remove(FIRMWARE_TEMP_PATH)
except OSError:
pass
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(POST, '/api/firmware/verify')
def api_firmware_verify(httpClient, httpResponse):
try:
# 手动触发垃圾回收
gc.collect()
data = httpResponse.GetPostedJSONObject()
expected_checksum = data.get('checksum', '')
# 检查临时文件是否存在
try:
st = os.stat(FIRMWARE_TEMP_PATH)
except:
return httpResponse.Response.ReturnOkJSON({"success": False, "message": "固件文件不存在"})
# 读取文件内容并计算校验和
with open(FIRMWARE_TEMP_PATH, 'rb') as f:
content = f.read()
# 计算校验和
actual_checksum = calculate_checksum(content)
actual_checksum_hex = "%08x" % actual_checksum
# 验证校验和
success = actual_checksum_hex == expected_checksum.lower()
httpResponse.Response.ReturnOkJSON({
"success": success,
"message": "校验成功" if success else "校验失败",
"actual_checksum": actual_checksum_hex,
"expected_checksum": expected_checksum,
"file_size": len(content)
})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(POST, '/api/firmware/upgrade')
def api_firmware_upgrade(httpClient, httpResponse):
try:
# 检查临时文件是否存在
try:
os.stat(FIRMWARE_TEMP_PATH)
except:
return httpResponse.Response.ReturnOkJSON({"success": False, "message": "固件文件不存在"})
# 这里应该添加实际的固件升级逻辑
# 例如:将临时文件写入到固件分区,然后重启设备
# 模拟升级过程
print("开始执行固件升级...")
# 1. 验证固件文件(实际项目中应该有更严格的验证)
# 2. 写入固件到目标位置
# 3. 验证写入结果
# 模拟升级成功
time.sleep(1)
# 清理临时文件
try:
os.remove(FIRMWARE_TEMP_PATH)
except:
pass
# 重启设备(实际项目中应该启用这行代码)
# machine.reset()
httpResponse.Response.ReturnOkJSON({"success": True, "message": "固件升级成功,设备将在1秒后重启"})
except Exception as e:
# 清理临时文件
try:
os.remove(FIRMWARE_TEMP_PATH)
except:
pass
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
@WebRoute(POST, '/api/firmware/cancel')
def api_firmware_cancel(httpClient, httpResponse):
try:
# 清理临时文件
try:
os.remove(FIRMWARE_TEMP_PATH)
except:
pass
httpResponse.Response.ReturnOkJSON({"success": True, "message": "固件升级已取消"})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": str(e)})
# ========== 统计数据API ========== #
# 保存统计数据API
@WebRoute(POST, '/api/stats/save')
def api_save_stats(httpClient, httpResponse):
try:
# 解析请求体
obj = httpResponse.GetPostedJSONObject()
# 确保保存目录存在
save_dir = '/flash/data'
# 格式:年4位_月2位_日2位_时2位_分2位_秒2位
filename = obj.get('filename', '')
sn = obj.get('sn', '')
if not sn:
raise ValueError("设备号不能为空")
#创建目录
try:
os.stat(f"{save_dir}/{sn}")
except:
os.mkdir(f"{save_dir}/{sn}")
save_dir = f"{save_dir}/{sn}"
if not filename:
raise ValueError("文件名不能为空")
stats_data = obj.get('stats', None)
if stats_data is None:
raise ValueError("统计数据不能为空")
# 确保文件名包含目录路径
filename = f"{save_dir}/{filename}"
with open(filename, 'w') as f:
json.dump(stats_data, f)
print(f"统计数据已保存到文件: {filename}")
httpResponse.Response.ReturnOkJSON({"success": True, "message": "统计数据已保存到设备"})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": f"保存失败: {str(e)}"})
sys.print_exception(e)
# 加载统计数据API
@WebRoute(GET, '/api/stats/load')
def api_load_stats(httpClient, httpResponse):
try:
# 获取最新的统计文件
sn = httpResponse.QueryParams.get('sn', '')
if not sn:
raise ValueError("设备号不能为空")
save_dir = f"/flash/data/{sn}"
try:
os.stat(f"{save_dir}")
except:
raise ValueError("设备号不存在")
files = []
for f in os.listdir(save_dir):
if f.startswith('stats_') and f.endswith('.json'):
files.append(f)
if not files:
httpResponse.Response.ReturnOkJSON({"success": False, "message": "没有找到统计文件"})
return
# 按时间排序,获取最新的文件
files.sort(reverse=True)
latest_file = f"{save_dir}/{files[0]}"
# 读取文件内容
with open(latest_file, 'r') as f:
stats_data = json.load(f)
httpResponse.Response.ReturnOkJSON({"success": True, "message": "统计数据已加载", "data": {'filename': files[0], 'stats': stats_data}})
except Exception as e:
httpResponse.Response.ReturnOkJSON({"success": False, "message": f"加载失败: {str(e)}"})
sys.print_exception(e)
# ========== 服务器启动 ========== #
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.BufferSlotsCount = 8
mws2.BufferSlotSize = 512
mws2.ConnQueueCapacity = 1
mws2.MaxRequestContentLength = 16 * 1024
#mws2.BindAddress = ('0.0.0.0', 8001)
mws2.RootPath = '/flash/web_root/daq_test/'
mws2.SetEmbeddedConfig()
mws2.NotFoundURL = '/'
mws2.StartManaged(procStackSize = 40*1024)
def ws_run():
py_server(__cmd_handler)
if __name__ == '__main__':
ws_run()