2024-12-04 12:27:46 +00:00
|
|
|
from flask import Flask, request, jsonify, send_from_directory
|
|
|
|
from parse_inp import parse_inp_file, update_inp_file, save_comments
|
|
|
|
import os
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
# 确保在启动时解析inp文件
|
|
|
|
INP_FILE_PATH = 'visa-scpi-example/File.inp'
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
|
|
|
commands, comments = parse_inp_file(INP_FILE_PATH)
|
|
|
|
return send_from_directory(os.path.abspath('visa-scpi-example'), 'index.html')
|
|
|
|
|
|
|
|
@app.route('/commands.json')
|
|
|
|
def get_commands():
|
|
|
|
return send_from_directory(os.path.abspath('visa-scpi-example'), 'commands.json')
|
|
|
|
|
|
|
|
@app.route('/update_command', methods=['POST'])
|
|
|
|
def update_command():
|
|
|
|
data = request.json
|
|
|
|
try:
|
2024-12-05 12:25:35 +00:00
|
|
|
# 读取当前的指令和参数
|
|
|
|
with open(INP_FILE_PATH, 'r') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
# 创建一个列表来存储更新后的指令
|
|
|
|
updated_lines = []
|
|
|
|
|
|
|
|
# 遍历现有的指令行
|
|
|
|
for line in lines:
|
|
|
|
parts = line.strip().split(' ')
|
|
|
|
if len(parts) == 2: # 只处理有一个参数的指令
|
|
|
|
command, value = parts
|
|
|
|
# 检查是否需要更新该指令
|
|
|
|
for item in data:
|
|
|
|
if item['command'] == command:
|
|
|
|
# 如果指令匹配,使用新的值
|
|
|
|
updated_lines.append(f"{command} {item['value']}\n")
|
|
|
|
# 匹配后应删除data中对应的指令及参数
|
|
|
|
data.remove(item)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# 如果没有匹配,保留原行
|
|
|
|
updated_lines.append(line)
|
|
|
|
|
|
|
|
# 将更新后的指令写回文件
|
|
|
|
with open(INP_FILE_PATH, 'w') as f:
|
|
|
|
f.writelines(updated_lines)
|
|
|
|
|
2024-12-04 12:27:46 +00:00
|
|
|
# 更新后重新解析文件
|
|
|
|
parse_inp_file(INP_FILE_PATH)
|
|
|
|
return jsonify({'success': True})
|
|
|
|
except Exception as e:
|
|
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
|
|
@app.route('/save_comments', methods=['POST'])
|
|
|
|
def save_comments_route():
|
|
|
|
comments = request.json
|
|
|
|
save_comments(comments)
|
|
|
|
return jsonify({'success': True})
|
|
|
|
|
|
|
|
@app.route('/comments.json')
|
|
|
|
def get_comments():
|
|
|
|
return send_from_directory(os.path.abspath('visa-scpi-example'), 'comments.json')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# 启动时先解析一次文件
|
|
|
|
parse_inp_file(INP_FILE_PATH)
|
|
|
|
app.run(debug=True)
|