-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
77 lines (62 loc) · 1.79 KB
/
core.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
# -*- coding: utf-8 -*-
import os
import sys
import time
import multiprocessing
from hil import routes
from hil.log import logger
from hil.api import HILApi
from hil.config import HILConfig
from hil.control import HILControl
from hil.decorator import HILNonOverridable, non_overridable
class HILCore:
__metaclass__ = HILNonOverridable
VERSION = '0.0.1'
def __init__(self, apps, context):
"""Contructor the object"""
self.api_conn, self.control_api_conn = multiprocessing.Pipe()
self.config_conn, self.control_config_conn = multiprocessing.Pipe()
try:
self.config = HILConfig(self.config_conn, apps, context)
except Exception as e:
logger.critical(str(e))
raise sys.exit(-1)
self.api = HILApi(routes, self.control_api_conn)
self.control = HILControl(self.config, self.api_conn, self.control_config_conn)
def __del__(self):
"""Destroys the object"""
self.stop()
def start(self):
"""Starts the core server"""
try:
self._welcomeMessage()
self.running = True
while self.running:
if not self.api.running:
self.api.start()
if not self.control.running:
self.control.start()
time.sleep(.1)
except (KeyboardInterrupt, SystemExit):
self.stop()
self._exitMessage()
def restart(self):
"""Restarts the core server"""
self.stop()
self.start()
def stop(self):
"""Stops the core server"""
try:
self.api.stop()
self.control.stop()
except:
pass
self.running = False
@non_overridable
def _welcomeMessage(self):
"""Prints shutdown message."""
print("=> Booting HIL version {0}".format(self.VERSION))
@non_overridable
def _exitMessage(self):
"""Prints shutdown message."""
print("\n=> Shutdown the server\n=> Bye")