forked from mi8882255/3dprinteros-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
177 lines (157 loc) · 7.13 KB
/
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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Copyright (c) 2015 3D Control Systems LTD
#3DPrinterOS client is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#3DPrinterOS client is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU Affero General Public License for more details.
#You should have received a copy of the GNU Affero General Public License
#along with 3DPrinterOS client. If not, see <http://www.gnu.org/licenses/>.
# Author: Vladimir Avdeev <another.vic@yandex.ru>
import time
import signal
import platform
import log
import paths
paths.init_path_to_libs()
import usb_detect
import http_client
import camera_controller
import cloud_sync_controller
import user_login
import updater
import version
import printer_interface
import config
import rights
import makerware_utils
class App(object):
MAIN_LOOP_SLEEP = 2
LOG_FLUSH_TIME = 30
PRINTER_INTERFACES_CLOSING_TIMEOUT = 15
@log.log_exception
def __init__(self):
config.Config.instance().set_app_pointer(self)
self.logger = log.create_logger("app", log.LOG_FILE)
self.logger.info("Starting 3DPrinterOS client. Version %s_%s" % (version.version, version.build))
self.logger.info('Operating system: ' + platform.system() + ' ' + platform.release())
self.time_stamp()
signal.signal(signal.SIGINT, self.intercept_signal)
signal.signal(signal.SIGTERM, self.intercept_signal)
self.detected_printers = []
self.printer_interfaces = []
self.virtual_printer_enabled = False
self.stop_flag = False
self.rights_checker_waiter = rights.RightsCheckerWaiter(self)
self.conveyor_kill_waiter = makerware_utils.ConveyorKillWaiter(self)
self.network_connection_checker = http_client.NetworkConnectionChecker(self)
self.init_interface()
self.rights_checker_waiter.wait()
self.conveyor_kill_waiter.wait()
self.network_connection_checker.wait()
self.updater = updater.Updater()
self.user_login = user_login.UserLogin(self)
self.user_login.wait()
if self.user_login.user_token and hasattr(self.user_login, "profiles"):
config.Config.instance().set_profiles(self.user_login.profiles)
self.virtual_printer_enabled = False
self.camera_controller = camera_controller.CameraController(self.user_login.user_token)
self.cloud_sync_controller = cloud_sync_controller.CloudSyncController()
else:
self.logger.error("Can't retrieve user login or printer profiles. Exiting...")
self.quit()
def init_interface(self):
if config.get_settings()['web_interface']['enabled']:
import webbrowser
from web_interface import WebInterface
self.web_interface = WebInterface(self)
self.web_interface.start()
self.logger.debug("Waiting for webserver to start...")
while not self.web_interface.server:
time.sleep(0.01)
if self.stop_flag:
return
self.logger.debug("...server is up and running. Connecting browser...")
time.sleep(3)
if config.get_settings()['web_interface']['browser_opening_on_start']:
webbrowser.open("http://" + self.web_interface.SERVER_IP \
+ ':' + str(self.web_interface.SERVER_PORT), 2, True)
self.logger.debug("...done")
def intercept_signal(self, signal_code, frame):
self.logger.warning("SIGINT or SIGTERM received. Closing 3DPrinterOS Client version %s_%s" % \
(version.version, version.build))
self.stop_flag = True
@log.log_exception
def start_main_loop(self):
self.last_flush_time = 0
self.detector = usb_detect.USBDetector()
self.http_client = http_client.HTTPClient()
while not self.stop_flag:
self.updater.timer_check_for_updates()
self.time_stamp()
self.detected_printers = self.detector.get_printers_list()
if self.virtual_printer_enabled:
self.detected_printers.append( {"VID" : "ZZZZ", "PID": "ZZZZ", "SNR": "0", "COM": None} )
self.connect_new_printers()
for pi in self.printer_interfaces:
if pi.usb_info not in self.detected_printers:
if not pi.error_state == "error":
config.create_error_report(99, "Printer is no longer detected as USB device.", pi.usb_info, self.logger, is_blocking=True)
if not pi.is_alive():
if not pi.stop_flag:
self.logger.warning("Printer interface of %s had crushed" % pi.usb_info)
self.logger.info('Removing %s from printer list' % pi.usb_info)
self.printer_interfaces.remove(pi)
if not self.stop_flag:
time.sleep(self.MAIN_LOOP_SLEEP)
now = time.time()
if now - self.last_flush_time > self.LOG_FLUSH_TIME:
self.last_flush_time = now
self.flush_log()
self.quit()
def connect_new_printers(self):
currently_connected_usb_info = [pi.usb_info for pi in self.printer_interfaces]
for usb_info in self.detected_printers:
if usb_info not in currently_connected_usb_info:
pi = printer_interface.PrinterInterface(usb_info, self.user_login)
pi.start()
self.printer_interfaces.append(pi)
def flush_log(self):
self.logger.info('Flushing logger handlers')
for handler in self.logger.handlers:
handler.flush()
def time_stamp(self):
self.logger.debug("Time stamp: " + time.strftime("%d %b %Y %H:%M:%S", time.localtime()))
def shutdown_web_interface(self):
self.logger.debug("Waiting web interface server to shutdown...")
self.time_stamp()
try:
self.web_interface.server.shutdown()
self.web_interface.join()
except:
pass
self.time_stamp()
self.logger.debug("...done")
def quit(self):
self.logger.info("Starting exit sequence...")
if hasattr(self, 'camera_controller'):
self.camera_controller.stop_camera_process()
if hasattr(self, 'cloud_sync_controller'):
self.cloud_sync_controller.stop_cloud_sync_process()
for pi in self.printer_interfaces:
pi.close()
for pi in self.printer_interfaces:
pi.join(self.PRINTER_INTERFACES_CLOSING_TIMEOUT)
time.sleep(0.2) #to reduce logging spam in next
self.shutdown_web_interface()
self.logger.info("...everything is correctly closed.")
self.logger.info("Goodbye ;-)")
for handler in self.logger.handlers:
handler.flush()
if __name__ == '__main__':
app = App()
app.start_main_loop()