diff options
author | Vasilii Chernov <vchernov@inr.ru> | 2016-02-25 17:02:35 +0100 |
---|---|---|
committer | Vasilii Chernov <vchernov@inr.ru> | 2016-02-25 17:02:35 +0100 |
commit | 3bf5383a7ea03c5aa263aa4d8acf8b4949547319 (patch) | |
tree | ef091c8d1084429ad8cb5d40220fe0546da04389 /html_server/html_server.py | |
parent | ed7a7ff66c64cc2e164070865306e7d79652063a (diff) | |
download | pcitool-3bf5383a7ea03c5aa263aa4d8acf8b4949547319.tar.gz pcitool-3bf5383a7ea03c5aa263aa4d8acf8b4949547319.tar.bz2 pcitool-3bf5383a7ea03c5aa263aa4d8acf8b4949547319.tar.xz pcitool-3bf5383a7ea03c5aa263aa4d8acf8b4949547319.zip |
Move html_server to separate folder. Update html server templates.
Diffstat (limited to 'html_server/html_server.py')
-rw-r--r-- | html_server/html_server.py | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/html_server/html_server.py b/html_server/html_server.py new file mode 100644 index 0000000..2e0af72 --- /dev/null +++ b/html_server/html_server.py @@ -0,0 +1,194 @@ +import pcipywrap +import json +from optparse import OptionParser + +#import flask elements +from flask import render_template +from flask import Flask +from flask import request +from flask import url_for +from flask import redirect +from flask import send_from_directory + +app = Flask(__name__) +pcilib = 0; +device = '/dev/fpga0' +model = 'test_pywrap' + +# property json api +@app.route("/property_info_json") +def get_property_list_json(): + branch = request.args.get('branch') + if not branch is None: + branch = str(branch) + + prop_info = 0 + try: + prop_info = pcilib.get_property_list(branch) + return json.dumps(prop_info) + except Exception as e: + return json.dumps({'error': str(e)}) + +@app.route('/get_property_json') +def get_property_json(): + prop = request.args.get('prop') + + try: + val = pcilib.get_property(str(prop)) + return json.dumps({'value': val}) + except Exception as e: + return json.dumps({'error': str(e)}) + +@app.route('/set_property_json') +def set_property_json(): + val = request.args.get('val') + prop = request.args.get('prop') + + try: + pcilib.set_property(float(val), str(prop)) + return json.dumps({'status': 'ok'}) + except Exception as e: + return json.dumps({'error': str(e)}) + +# register json api +@app.route("/registers_list_json") +def get_registers_list_json(): + reg_list = 0 + try: + reg_list = pcilib.get_registers_list() + return json.dumps(reg_list) + except Exception as e: + return json.dumps({'error': str(e)}) + +@app.route('/read_register_json') +def read_register_json(): + name = request.args.get('name') + bank = request.args.get('bank') + + try: + value = pcilib.read_register(str(name), str(bank)) + return json.dumps({'value': value}) + except Exception as e: + return json.dumps({'error': str(e)}) + +@app.route('/write_register_json') +def write_register_json(): + val = request.args.get('val') + name = request.args.get('name') + bank = request.args.get('bank') + + try: + pcilib.write_register(float(val), str(name), str(bank)) + return json.dumps({'status': 'ok'}) + except Exception as e: + return json.dumps({'error': str(e)}) + +#html api +@app.route('/set_property') +def set_property(): + val = request.args.get('val') + prop = request.args.get('prop') + + try: + pcilib.set_property(float(val), str(prop)) + return redirect(url_for('get_property_list', branch=prop)) + except Exception as e: + return str(e) + +@app.route('/write_register') +def write_register(): + val = request.args.get('val') + name = request.args.get('name') + bank = request.args.get('bank') + + try: + pcilib.write_register(float(val), str(name), str(bank)) + return redirect(url_for('get_register_info', name=name, bank=bank)) + except Exception as e: + return str(e) + +@app.route('/register_info') +def get_register_info(): + name = request.args.get('name') + bank = request.args.get('bank') + + reg_info = 0 + value = 0 + try: + reg_info = pcilib.get_register_info(str(name), str(bank)) + value = pcilib.read_register(str(name), str(bank)) + except Exception as e: + return str(e) + return render_template('register_info.html', + register=reg_info, + value=value) + +@app.route("/registers_list") +def get_registers_list(): + reg_list = 0 + try: + reg_list = pcilib.get_registers_list() + except Exception as e: + return str(e) + + return render_template('registers_list.html', + registers=reg_list, + ) + +@app.route("/property_info") +def get_property_list(): + branch = request.args.get('branch') + if not branch is None: + branch = str(branch) + + prop_info = 0 + try: + prop_info = pcilib.get_property_list(branch) + except Exception as e: + return str(e) + + value = -1 + if (len(prop_info) == 1) and not ('childs' in (prop_info[0])['flags']): + try: + branch = (prop_info[0])['path'] + value = pcilib.get_property(branch) + except Exception as e: + return str(e) + + return render_template('property_info.html', + value = value, + branch = branch, + properties = prop_info + ) + +@app.route("/") +def greet(): + return render_template('base.html', + device = device, + model = model) + +if __name__ == "__main__": + #parse command line options + parser = OptionParser() + parser.add_option("-p", "--port", action="store", + type="int", dest="port", default=5000, + help="Set server port (5000)") + parser.add_option("-d", "--device", action="store", + type="string", dest="device", default=str('/dev/fpga0'), + help="FPGA device (/dev/fpga0)") + parser.add_option("-m", "--model", action="store", + type="string", dest="model", default=None, + help="Memory model (autodetected)") + opts = parser.parse_args()[0] + + HOST_NAME = '0.0.0.0' + PORT_NUMBER = opts.port + + device = opts.device + model = opts.model + + app.debug = True + + pcilib = pcipywrap.Pcipywrap(device, model) + pcipywrap.__redirect_logs_to_exeption() + app.run(host = HOST_NAME, port = PORT_NUMBER) |