-
Notifications
You must be signed in to change notification settings - Fork 34
/
root_app.py
151 lines (120 loc) · 4.69 KB
/
root_app.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
#
# Start frontend and pass in ALP for it to manage
#
from flask import Flask, Response
from flask_cors import CORS, cross_origin
import threading
import time
import sys
import os
import waitress
import sdnotify
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from front.app import FrontMain, get_live_status
# sys.path.append(os.path.join(os.path.dirname(__file__), "device"))
from device.app import DeviceMain # type: ignore
from device.config import Config # type: ignore
from device import log # type: ignore
from device import telescope # type: ignore
import os
class AppRunner:
def __init__(self, log, name, app_main):
self.name = name
self.logger = log
self.thread = None
self.app_main = app_main()
def start(self):
self.logger.info(f"Starting {self.name}")
self.thread = threading.Thread(target=self.runner, args=(1,), daemon=True)
self.thread.name = f"{self.name}MainThread"
self.thread.start()
# def on_get_start(self, req, resp):
# self.start()
# resp.status = falcon.HTTP_200
# resp.content_type = 'application/text'
# resp.text = 'Started, yo!'
#
# def on_get_stop(self, req, resp):
# self.logger.info(f"Stopping {self.name}")
# self.app_main.stop()
# self.thread.join()
# resp.status = falcon.HTTP_200
# resp.content_type = 'application/text'
# resp.text = 'Stopped'
def get_imager(self, device_num: int):
return self.app_main.get_imager(device_num)
def runner(self, name):
self.logger.info(f"Seestar{self.name} %s: starting", name)
self.app_main.start()
self.logger.info(f"Seestar{self.name} %s: finishing", name)
def join(self):
self.thread.join()
def reload(self):
self.logger.setLevel(Config.log_level)
for handler in self.logger.handlers:
handler.setLevel(Config.log_level)
self.app_main.reload()
class ConfigChangeHandler(FileSystemEventHandler):
def __init__(self, path, alp, front):
self.path = path
self.alp = alp
self.front = front
self.last_restart = time.time()
def on_modified(self, event):
if event.src_path == self.path:
#print(f'ConfigChangeHandler event type: {event.event_type} path : {event.src_path}')
Config.load_toml()
self.alp.reload()
self.front.reload()
#else:
# print(f"ConfigChangeHandler Ignoring event type: {event.event_type} path : {event.src_path}")
if __name__ == "__main__":
n = sdnotify.SystemdNotifier()
if Config.rtsp_udp:
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
# We want to initialize ALP logger
logger = log.init_logging()
logger.info("Starting ALP web server")
main = AppRunner(logger, "ALP", DeviceMain)
main.start()
time.sleep(1)
logger.info("Starting Front web server")
front = AppRunner(logger, "Front", FrontMain)
front.start()
event_handler = ConfigChangeHandler(Config.path_to_dat, main, front)
observer = Observer()
#print(f"XXX observing {Config.path_to_dat}")
observer.schedule(event_handler, path=os.path.dirname(Config.path_to_dat), recursive=True)
observer.start()
time.sleep(1)
if Config.experimental:
logger.info("Setting up imaging web server")
app = Flask(__name__)
CORS(app, supports_credentials=True)
@cross_origin()
@app.route("/<dev_num>/vid/status")
def vid_status(dev_num):
return Response(telescope.get_seestar_imager(int(dev_num)).get_video_status(),
mimetype='text/event-stream')
@cross_origin()
@app.route("/<dev_num>/live/status")
def live_status(dev_num):
return Response(get_live_status(int(dev_num)), mimetype='text/event-stream')
@cross_origin()
@app.route("/<dev_num>/events")
def live_events(dev_num):
return Response(telescope.get_seestar_device(int(dev_num)).get_events(),
mimetype='text/event-stream')
@app.route('/<dev_num>/vid')
def vid(dev_num):
return Response(telescope.get_seestar_imager(int(dev_num)).get_frame(),
mimetype='multipart/x-mixed-replace; boundary=frame')
n.notify("READY=1")
print("Startup Complete")
# telescope.telescopes()
waitress.serve(app, host=Config.ip_address, port=Config.imgport, threads=10, channel_timeout=30)
else:
n.notify("READY=1")
print("Startup Complete")
front.join()