-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·212 lines (167 loc) · 6.44 KB
/
server.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/python3
import logging
import sys
import os
import configparser
from blynclightrunner import BlyncLightRunner
from flask import Flask, jsonify, request, json
from flask_restful import abort
from functools import wraps
app = Flask(__name__)
logger = logging.getLogger(__name__)
light = BlyncLightRunner(logger)
# color_name
# curl http://localhost:8080/api/v1/color_name -d '{"color_name" : "red"}' -H 'Content-Type: application/json'
# curl http://localhost:8080/api/v1/color_name
# flash
# curl http://localhost:8080/api/v1/flash -d '{"flash" : false}' -H 'Content-Type: application/json'
# curl http://localhost:8080/api/v1/flash
# dim
# curl http://localhost:8080/api/v1/dim -d '{"dim" : false}' -H 'Content-Type: application/json'
# curl http://localhost:8080/api/v1/dim
# on
# curl http://localhost:8080/api/v1/on -d '{"on" : true}' -H 'Content-Type: application/json'
# curl http://localhost:8080/api/v1/on -d '{"on" : true}' -H 'Content-Type: application/json'
# curl http://localhost:8080/api/v1/on
# speed
# curl http://localhost:8080/api/v1/speed -d '{"speed" : 0}' -H 'Content-Type: application/json'
# curl http://localhost:8080/api/v1/speed
# curl http://localhost:8080/api/v1/color -d '{"color_name":"blue"}' -H 'Content-Type: application/json'
@app.route('/api/v1/healthz', methods=['GET'])
def healthz():
response = jsonify({'status': 'okay'})
response.status_code = 200
return response
@app.route('/api/v1/dim', methods=['GET', 'POST'])
def dim():
key = "dim"
if request.method == 'GET':
pass
if request.method == 'POST':
handle_post_request(request)
light.dim = request.json.get('dim', False)
j = {key: json.dumps(light.dim)}
response = jsonify(j)
response.status_code = 200
return response
@app.route('/api/v1/speed', methods=['GET', 'POST'])
def speed():
key = "speed"
if request.method == 'GET':
pass
if request.method == 'POST':
handle_post_request(request)
speed = request.json.get('speed', 4)
if speed > 4 or speed < 0:
abort(400, msg='Invalid parameter value')
light.flashspeed = speed
j = "{'" + key + "': " + str(light.flashspeed) + "}"
response = jsonify(j)
response.status_code = 200
return response
@app.route('/api/v1/flash', methods=['GET', 'POST'])
def flash():
key = "flash"
if request.method == 'GET':
pass
if request.method == 'POST':
handle_post_request(request)
b = request.json.get('flash', False)
logger.debug("flash: " + str(b))
light.flash = int(b)
j = "{'" + key + "': " + json.dumps(bool(light.flash)) + "}"
response = jsonify(j)
response.status_code = 200
return response
@app.route('/api/v1/color_name', methods=['GET', 'POST'])
def color():
key = "color_name"
if request.method == 'GET':
pass
if request.method == 'POST':
handle_post_request(request)
color_name = request.json.get('color_name', None)
if not color_name:
abort(400, error='Missing color_name')
light.colorname = color_name
j = {key: str(light.colorname)}
response = jsonify(j)
response.status_code = 200
return response
@app.route('/api/v1/on', methods=['GET', 'POST'])
def on():
key = "on"
if request.method == 'GET':
pass
if request.method == 'POST':
handle_post_request(request)
on = request.json.get('on', False)
force = request.json.get('force', False)
if(light.on == on and not force):
logger.debug("Light status matches, run status. Nothing will be done.")
else:
light.on = on
j = "{'" + key + "': " + json.dumps(light.on) + "}"
response = jsonify(j)
response.status_code = 200
return response
def handle_post_request(request):
if not request.is_json:
logger.error("Request is no in JSON format.")
abort(400, msg='Missing JSON in request')
def get_http_exception_handler(app):
"""Overrides the default http exception handler to return JSON."""
handle_http_exception = app.handle_http_exception
@wraps(handle_http_exception)
def ret_val(exception):
exc = handle_http_exception(exception)
return jsonify({'code': exc.code, 'msg': exc.description}), exc.code
return ret_val
# App entry point.
if __name__ == "__main__":
try:
config = configparser.ConfigParser()
config.read('config.ini')
serverconfig = config['server']
globalconfig = config['global']
# setup logging
log_datefmt = '%m-%d-%Y %H:%M:%S'
log_fmt = '%(asctime)-15s %(levelname)-8s %(message)s'
log_level = logging.getLevelName(serverconfig.get('log_level', globalconfig.get('log_level', 'INFO')))
log_file = serverconfig.get('server_log_file')
if "TTY" in os.environ:
logging.basicConfig(format=log_fmt, datefmt=log_datefmt, level=log_level)
else:
if sys.stdout.isatty():
# Connected to a real terminal - log to stdout
logging.basicConfig(format=log_fmt, datefmt=log_datefmt, level=log_level)
else:
# Background mode - log to file
logging.basicConfig(format=log_fmt, datefmt=log_datefmt, level=log_level, filename=log_file)
logger.info("==========================================================")
logger.info("Starting Blync Light Server")
# Server Properties
app.debug = serverconfig.getboolean('server_debug', False)
app.handle_http_exception = get_http_exception_handler(app)
# Finally start the web process and list on 8080 all IP addresses
app.run(host='0.0.0.0', port=8080)
except KeyboardInterrupt:
try:
logging.debug("Caught KeyboardInterrupt Exception")
light.on = False
except Exception:
logging.critical("Unable to reset light")
logging.critical("Terminating due to keyboard interrupt")
except Exception:
try:
logging.debug("Caught Exception")
light.on = False
except Exception:
logging.critical("Unable to reset light")
logging.critical("Terminating due to unexpected error: %s", sys.exc_info()[0])
finally:
try:
logging.debug("Caught finally")
light.on = False
except Exception:
logging.critical("Terminating due to unexpected error: %s", sys.exc_info()[0])