-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebapp.py
70 lines (49 loc) · 1.82 KB
/
webapp.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
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
from exlink import TVRemote
app = Flask(__name__)
api = Api(app)
tvremote = TVRemote(device='/dev/ttyUSB0')
action_parser = reqparse.RequestParser()
action_parser.add_argument('action', str)
volume_parser = reqparse.RequestParser()
volume_parser.add_argument('level', int)
class TVVolume(Resource):
VOLUME_ACTIONS = ['up', 'down', 'mute', 'normal']
def _isvalid_volume_action(self, action):
return action in TVVolume.VOLUME_ACTIONS
def put(self):
args = volume_parser.parse_args()
level = int(args['level'])
if level < 0 or level > 255:
raise ValueError('"level" has to be between 0 and 255')
tvremote.cmd_volume_set(int(args['level']))
return {'level': level}, 200
def post(self):
args = action_parser.parse_args()
action = args['action']
if not self._isvalid_volume_action(action):
raise ValueError('"action" param should be up/down/mute, not "{}"'.format(action))
if action == 'up':
tvremote.cmd_volume_up()
elif action == 'down':
tvremote.cmd_volume_down()
elif action == 'normal':
tvremote.cmd_volume_set(12)
else:
tvremote.cmd_volume_set(0)
return {}, 204
class TVScreen(Resource):
def _isvalid_action(self, action):
return action == 'off'
def post(self):
args = action_parser.parse_args()
action = args['action']
if not self._isvalid_action(action):
raise ValueError('"action" should be "off"')
tvremote.cmd_power_off()
return {}, 204
api.add_resource(TVVolume, '/tv/volume')
api.add_resource(TVScreen, '/tv/screen')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')