-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (58 loc) · 1.52 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, render_template, request, make_response
import json
import struct
app = Flask(__name__)
scheme = None
interface = None
@app.route('/')
def index():
return render_template('index.html')
@app.route('/send', methods=['POST'])
def send_params():
try:
name = request.form['name']
value = int(request.form['value'])
except:
return make_response('Bad data', 400)
if scheme is None:
return make_response('Scheme is not set', 400)
elif interface is None:
return make_response('Interface is not open', 400)
code = scheme[name]
checksum = code
for i in range(4):
checksum += (value >> (i * 8)) & 0xff
try:
interface.write(struct.pack('<BIB', code, value, checksum & 0xff))
interface.flush()
except OSError:
return make_response('File was closed', 500)
return ''
@app.route('/scheme', methods=['POST'])
def set_scheme():
schemeFile = request.files.get('scheme')
if schemeFile is None:
return make_response('file is not loaded', 400)
try:
sch = json.load(schemeFile)
except json.JSONDecodeError:
return make_response('can\'t parse JSON', 400)
global scheme
scheme = {}
try:
for field in sch:
scheme[field['name']] = field['code']
except KeyError:
return make_response('bad scheme', 400)
return {'fields': list(scheme.keys())}
@app.route('/open', methods=['POST'])
def open_interface():
global interface
path = request.form['path']
try:
interface = open(path, 'wb')
except:
return make_response('', 400)
return ''
if __name__ == '__main__':
app.run('127.0.0.1', 8084, True)