In my company we have many HIL modules that provide OpenAPI to perform some basic operation, such as start and stop measurements and download log files or user files.
I am providing a common frontend as a simple web server running flask on apache, hence the devlopers can just connect to the web page, select the HIL and then perform some operations.
As a side note, during the developement I am running Flask on localhost in pycharm, once I have a stable RC I upload on git and sync the server side with GIT.
Up to this moment, in the interface I have two simple buttons: one to retrieve the list of the files that can be downloaded and one to select and download the actual file(s).
The connect button performs a POST request to the flask server via javascript:
function myget(id, cb){
const xhttp = new XMLHttpRequest
var data = new URLSearchParams();
var url = "MiniHILSFunctions";
var ip = document.getElementById(id).innerHTML;
var params = 'ip='+ip
// console.log(ip)
// console.log(params)
xhttp.open("POST", url, true)
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send(params)
xhttp.onload = () => document.getElementById(cb).innerHTML = xhttp.responseText
}
Then the flask server handles the actual request to the selected HIL server:
@app.route('/MiniHILSFunctions', methods=['POST', 'GET'])
def MiniHilsfunctions(par=None):
if request.method == 'POST':
print("request here")
ip = request.form.get('ip')
print(ip)
headers={
"accept": "text/json"
}
url="http://" + ip + "/api/RemoteFiles/Info"
print(url)
r = requests.get(url, headers=headers)
response = r.json()
print(type(response))
print(response[0])
response = jsonify(response)
else:
response = "getting data"
return response, 200, {'Content-Type': 'text/plain'}
returningt the json response to the web interface updating some paragraph to show the json received.
Now, when the user selcts the file I would like to trigger a download of the selected file. The problem here is that the mini hil server issues a CORS error when I try to download a file using javascript, hence I need to ask the flask server to trigger the request instead of the browser.
@app.route('/MiniHILSDownload', methods=['POST', 'GET'])
def MiniHILSDownload(par=None):
if request.method == 'POST':
print("request here")
ip = request.form.get('ip')
ftype = request.form.get('ftype')
fname = request.form.get('fname')
print(ip, " ", ftype, " ", fname)
if ftype == '1': # UserFile
ftype = 'UserFile'
if ftype == '2': # LoggingFile
ftype = 'LoggingFile'
if ftype == '3': # Configuration
ftype = 'Configuration'
if ftype == '4': # ReportFile
ftype = 'ReportFile'
print(ftype)
headers={
"accept": "text/plain",
"FileType": ftype
}
url = r"http://" + ip + r"/api/RemoteFiles/" + fname
print(url)
response = requests.get(url, headers=headers)
# response = r.json()
print(type(response))
print(response)
#response = jsonify(response)
response = "getting data"
return ?
My issues here are:
- I don’t want to store any file on the flask server, one approach would be to store the chunks being downloaded to a ram buffer and then:
- I don’t know how to pass each chunk of the file from the server to the web interface
Any hint would be gladly appreciated.