-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·191 lines (129 loc) · 5.11 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
import cgi
from flask import Flask, jsonify
from bus import Serial, SerialCommand
HOST = '0.0.0.0'
PORT = 8080
DEBUG = False
app = Flask(__name__, static_folder = 'web')
serial = Serial()
@app.route('/')
def root():
return links(['/web/', '/api/'], True)
@app.route('/web/')
def web():
return app.send_static_file('main.html')
@app.route('/api/')
def api():
return links(['/api/command/', '/api/communicate/', '/api/move/', '/api/stop/', '/api/status/'], True)
@app.route('/api/command/')
def api_command():
return links(['/api/command/<string:name>'], False)
@app.route('/api/command/<string:name>')
def api_command_value(name):
return jsonify({'command': {name: serial.write(SerialCommand(name))}})
@app.route('/api/communicate/')
def api_communicate():
return links(['/api/communicate/u/', '/api/communicate/b/'], True)
@app.route('/api/communicate/u/')
def api_communicate_u():
return links(['/api/communicate/u/<int:repeat>/<int:tempo>'], False)
@app.route('/api/communicate/u/<int:repeat>/')
def api_communicate_u_repeat(repeat):
return links(['/api/communicate/u/%i/<int:tempo>' % repeat], False)
@app.route('/api/communicate/u/<int:repeat>/<int:tempo>')
def api_communicate_u_repeat_tempo(repeat, tempo):
return jsonify({'communicate': {'u': serial.write(SerialCommand('u', repeat, tempo))}})
@app.route('/api/communicate/b/')
def api_communicate_b():
return links(['/api/communicate/b/<int:note>/<int:duration>'], False)
@app.route('/api/communicate/b/<int:note>/')
def api_communicate_b_note(note):
return links(['/api/communicate/u/%i/<int:duration>' % note], False)
@app.route('/api/communicate/b/<int:note>/<int:duration>')
def api_communicate_b_note_duration(note, duration):
return jsonify({'communicate': {'b': serial.write(SerialCommand('b', note, duration))}})
@app.route('/api/move/')
def api_move():
return links(['/api/move/m/', '/api/move/i/', '/api/move/l/'], True)
@app.route('/api/move/m/')
def api_move_m():
return links(['/api/move/m/<int:index>/<int:value>'], False)
@app.route('/api/move/m/<int:index>/')
def api_move_m_index(index):
return links(['/api/move/m/%i/<int:value>' % index], False)
@app.route('/api/move/m/<int:index>/<string:value>')
def api_move_m_index_value(index, value):
return jsonify({'move': {'m': serial.write(SerialCommand('m', index, int(value)))}})
@app.route('/api/move/i/')
def api_move_i():
return links(['/api/move/i/<int:index>/<int:value>/<int:index>/<int:value>/...'], False)
@app.route('/api/move/i/<path:indexvalue>/')
def api_move_i_indexvalue(indexvalue):
args = map(int, indexvalue.split('/'))
if len(args) % 2 == 0:
return jsonify({'move': {'i': serial.write(SerialCommand('i', *args))}})
else:
return links(['/api/move/i/%s/<int:value>' % indexvalue], False)
@app.route('/api/move/l/')
def api_move_l():
return links(['/api/move/l/<int:valueofindex0>/.../<int:valueofindex15>'], False)
@app.route('/api/move/l/<path:valueofindex>/')
def api_move_l_valueofindex(valueofindex):
args = map(int, valueofindex.split('/'))
if len(args) == 16:
return jsonify({'move': {'l': serial.write(SerialCommand('l', *args))}})
else:
return links(['/api/move/l/%s%s<int:valueofindex15>' % (valueofindex, '/' if len(args) == 15 else '.../')],
False)
@app.route('/api/stop/')
def api_stop():
return links(['/api/stop/d', '/api/stop/p', '/api/stop/r'], True)
@app.route('/api/stop/d/')
def api_stop_d():
return jsonify({'stop': {'d': serial.write(SerialCommand('d'))}})
@app.route('/api/stop/p/')
def api_stop_p():
return jsonify({'stop': {'p': serial.write(SerialCommand('p'))}})
@app.route('/api/stop/r/')
def api_stop_r():
return jsonify({'stop': {'r': serial.write(SerialCommand('r'))}})
@app.route('/api/status/')
def api_status():
return links(['/api/status/j'], True)
@app.route('/api/status/j/')
def api_status_j():
return jsonify({'status': {'j': serial.write(SerialCommand('j'))}})
def links(items, linked):
ul = ['<ul>']
for item in items:
ul.append('<li>')
if linked:
ul.append('<a href="%s">%s</a>' % (item, cgi.escape(item)))
else:
ul.append(cgi.escape(item))
ul.append('</li>')
ul.append('</ul>')
return html(''.join(ul))
def html(body):
return ''.join(['<!DOCTYPE html>',
'<html>',
'<head>',
'<meta charset="utf-8">',
'<meta name="viewport" content="width=device-width,initial-scale=1.0">',
'<title>OpenCatWeb</title>',
'<link rel="icon" type="image/png" href="/web/img/favicon.png" />',
'<link rel="stylesheet" href="/web/css/reset.css" />',
'<link rel="stylesheet" href="/web/css/ocw.main.css" />',
'</head>',
'<body>',
body,
'</body>',
'</html>'
])
if __name__ == '__main__':
app.run(
host = HOST,
port = PORT,
debug = DEBUG
)