Debian Codegraph @

online/blockly_routes.py

raw symbols SCIP
1 from flask import Blueprint, render_template, request, session, send_from_directory, current_app, jsonify
2 import os, tempfile, subprocess, json, glob
3 from PyQt5 import QtCore
4
5 bly = Blueprint('blockly', __name__)
6
7 showStatusSignal = None
8 kpyPath = 'kpy'
9 blockly_ip = ''
10 blocklyPath = ''
11
12 p = None
13 def setBlocklyPath(pth, ip):
14 global blocklyPath, local_ip
15 blocklyPath = pth
16 print('blockly at',pth)
17 blockly_ip = ip
18
19 def setP(dev):
20 global p
21 p = dev
22
23 @bly.route('/visual')
24 def index():
25 return render_template('visual.html')
26
27 @bly.route('/editor')
28 def javaeditor():
29 return render_template('editor.html')
30
31
32 @bly.route('/favicon.ico')
33 def favicon():
34 return send_from_directory(os.path.join(current_app.root_path, 'static'), 'logo.png', mimetype='image/png')
35
36 @bly.route('/loadxml', methods=['GET'])
37 def load_xml():
38 file = request.args.get('file')
39 # Do something with the file (e.g., load it, process it, etc.)
40 # For now, return a dummy response
41 print(file, blocklyPath)
42 f = open(os.path.join(blocklyPath,'samples',file+'.xml')).read()
43 return jsonify({'status': 'success', 'message': f'Loaded {file}','xml':f})
44
45
46 @bly.route('/get_voltage/<int:chan>', methods=['GET'])
47 def get_voltage(chan):
48 return jsonify({'voltage': p.readADC(chan)})
49
50 @bly.route('/set_reg', methods=['POST'])
51 def set_reg():
52 reg = request.json['reg'] # Get the 'reg' parameter from the JSON body
53 data = request.json['data'] # Get the 'data' parameter from the JSON body
54 p.setReg(reg, int(data)&0xFF)
55 return jsonify({'status': 'success'})
56
57 @bly.route('/get_reg/<string:reg>', methods=['GET'])
58 def get_reg(reg):
59 result = p.getReg(reg)
60 return jsonify({'data': result})
61
62
63 @bly.route('/get_device_status', methods=['GET'])
64 def get_device_status():
65 if p is not None:
66 return jsonify({'connected': p.connected})
67 else:
68 return jsonify({'connected': False})
69
70 @bly.route('/get_sensor/<string:sensor>/<string:param>', methods=['GET'])
71 def get_sensor(sensor, param):
72 result = p.get_sensor(sensor, int(param))
73 return jsonify({'value': result})
74
75 @bly.route('/stepper_move', methods=['POST'])
76 def stepper_move():
77 data = request.json
78 steps = data['steps']
79 dir = data['dir']
80 delay = data['delay']
81 p.stepper_move(steps, dir, delay)
82 return jsonify({'status': 'success'})
83
84 @bly.route('/get_all_sensors', methods=['GET'])
85 def get_all_sensors():
86 sensors = []
87 if p is not None:
88 for addr in p.addressmap:
89 sensors.append(f'[{addr}]{p.addressmap[addr]}')
90 print('getAllSensors', p.addressmap, sensors)
91 return jsonify({'sensors': sensors})
92
93 @bly.route('/scan_i2c', methods=['GET'])
94 def scan_i2c():
95 global p
96 sensors = []
97 p.active_sensors = {} # Empty sensors list
98 x = p.I2CScan()
99 print('Responses from:', x)
100 for a in x:
101 possiblesensors = p.sensormap.get(a, [])
102 for sens in possiblesensors:
103 s = p.namedsensors.get(sens)
104 sensors.append(f'[{a}]{s["name"].split(" ")[0]}')
105 print('found', sensors)
106 return jsonify({'sensors': sensors})
107
108
109 @bly.route('/get_sensor_parameters/<string:name>', methods=['GET'])
110 def get_sensor_parameters(name):
111 print('found sensor params for', name, p.namedsensors)
112 if name in p.namedsensors:
113 return jsonify({'fields': p.namedsensors[name]["fields"]})
114 else:
115 return jsonify({'fields': ['0']})
116
117
118 @bly.route('/get_generic_sensor/<string:name>/<int:addr>', methods=['GET'])
119 def get_generic_sensor(name, addr):
120 if name not in p.active_sensors:
121 p.active_sensors[name] = p.namedsensors[name]
122 p.namedsensors[name]['init'](address=addr)
123 vals = p.active_sensors[name]['read']()
124 #print(vals, type(vals))
125 if vals is not None:
126 return jsonify({'data': [float(a) for a in vals]})
127 else:
128 return jsonify({'data': None})