-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest_service.py
80 lines (61 loc) · 1.84 KB
/
rest_service.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
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
from dynon_decoder import EfisAndEmsDecoder
RESTFUL_HOST_PORT = 8180
class ServerDecoderInterface(object):
__DECODER__ = None
@staticmethod
def set_decoder(
decoder: EfisAndEmsDecoder
):
ServerDecoderInterface.__DECODER__ = decoder
@staticmethod
def get_situation():
response = {'Service': 'OFFLINE'}
if ServerDecoderInterface.__DECODER__ is not None:
response = ServerDecoderInterface.__DECODER__.get_ahrs_package()
return json.dumps(
response,
indent=4,
sort_keys=False)
class RestfulHost(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
# Send the html message
self.wfile.write(ServerDecoderInterface.get_situation().encode())
class HudServer(object):
"""
Class to handle running a REST endpoint to handle configuration.
"""
def get_server_ip(
self
):
"""
Returns the IP address of this REST server.
Returns:
string -- The IP address of this server.
"""
return ''
def run(
self
):
"""
Starts the server.
"""
print("localhost = {}:{}".format(self.__local_ip__, self.__port__))
self.__httpd__.serve_forever()
def stop(
self
):
if self.__httpd__ is not None:
self.__httpd__.shutdown()
self.__httpd__.server_close()
def __init__(
self
):
self.__port__ = RESTFUL_HOST_PORT
self.__local_ip__ = self.get_server_ip()
server_address = (self.__local_ip__, self.__port__)
self.__httpd__ = HTTPServer(server_address, RestfulHost)