forked from yeyeto2788/MicroPythonScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
94 lines (68 loc) · 2.16 KB
/
Copy path__init__.py
File metadata and controls
94 lines (68 loc) · 2.16 KB
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
"""
Simple Flask server to show the usage of an API from a microcontroller
with MicroPython and also server main presentation HTML file.
"""
import json
import random
import flask
import flask_restful
from .camera import Camera
# noinspection PyTypeChecker
app = flask.Flask(__name__, static_url_path="/static")
@app.route('/')
@app.route('/home')
def home():
"""
Simple rendering of the `index.html` page.
Returns:
Flask template return.
"""
template_return = flask.render_template('index.html')
return template_return
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return flask.Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
class APIGenerator(flask_restful.Resource):
"""
Simple class from `flask_restful.Resource` to handle API endpoint
"""
@staticmethod
def get(bus: str):
"""
HTTP GET request for random bus time of arrival.
Args:
bus: Bus to retrieve information of.
Returns:
json data filled if bus found.
"""
buses = ['L2', 'X4', 'S1', 'A6', 'M3', 'R5']
api_data = {"data": 0}
if bus.capitalize() in buses:
api_data['data'] = 'Found 1 bus'
api_data['bus_name'] = bus
arrival_time = random.choice([minutes for minutes in range(1, 6)])
api_data['arrival'] = f'{arrival_time} mins'
code = 200
api_data['code'] = code
else:
api_data['data'] = 'Bus not found'
code = 201
api_data['code'] = code
return flask.Response(json.dumps(api_data), status=code, mimetype='application/json')
# API Endpoint for random bus data
API = flask_restful.Api(app)
API.add_resource(APIGenerator, '/api/<string:bus>')
VERSION_INFO = {
'MAJOR': 0,
'MINOR': 1,
'PATCH': 0,
}
__version__ = '{MAJOR:d}.{MINOR:d}.{PATCH:d}'.format(**VERSION_INFO)
__author__ = 'Juan Biondi'