forked from mi8882255/3dprinteros-client
/
camera_controller.py
104 lines (89 loc) · 4.41 KB
/
camera_controller.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
#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 os
import sys
import logging
import subprocess
import config
def remove_non_existed_modules(camera_modules_dict):
working_dir = os.path.dirname(os.path.abspath(__file__))
verified_camera_modules = {}
for camera_name,module in camera_modules_dict.iteritems():
if not module or os.path.exists(os.path.join(working_dir, module)):
verified_camera_modules[camera_name] = module
return verified_camera_modules
class CameraController:
HD_CAMERA_NAME = 'HD camera'
CAMERA_MODULES = {HD_CAMERA_NAME: "hdcamera",
"Dual camera": "dual_cam.py",
"Multi camera": "multi_cam.py",
"Disable camera": None }
if sys.platform.startswith('win'):
CAMERA_MODULES[HD_CAMERA_NAME] = "hdcamera.exe"
CAMERA_MODULES = remove_non_existed_modules(CAMERA_MODULES)
HD_CAMERA_URL = 'hdcam-' + config.get_settings()['URL']
HD_CAMERA_PORT = '443'
def __init__(self, token):
self.logger = logging.getLogger("app." + __name__)
self.token = token
self.camera_process = None
self.current_camera_name = "Disable camera"
self.start_camera_process()
def start_camera_process(self, camera_name=None, token=None):
if not token:
token = self.token
if not config.get_settings()["camera"]["enabled"]:
self.logger.info("Can't launch camera - disabled in config")
return False
self.logger.info('Launching camera subprocess')
if not camera_name:
camera_name = config.get_settings()['camera']['default']
module_name = self.CAMERA_MODULES[camera_name]
if module_name:
cam_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), module_name)
if cam_path and cam_path.endswith('.py'):
start_command = [sys.executable, cam_path, token]
elif camera_name == self.HD_CAMERA_NAME:
start_command = [os.path.basename(cam_path)]
if sys.platform.startswith('linux'):
start_command[0] = './' + start_command[0]
start_command += [self.HD_CAMERA_URL, self.HD_CAMERA_PORT, token]
else:
self.logger.warning('Unable to launch camera: unknown module - ' + os.path.basename(cam_path))
return False
try:
self.camera_process = subprocess.Popen(start_command)
except Exception as e:
self.logger.warning('Could not launch camera due to error:\n' + str(e))
else:
self.current_camera_name = camera_name
self.token = token
self.logger.info('Camera started: ' + camera_name)
return True
def restart_camera(self):
self.logger.info('Restarting camera module: ' + self.current_camera_name)
current_camera_name = self.current_camera_name
self.stop_camera_process()
return self.start_camera_process(current_camera_name)
def get_current_camera_name(self):
return self.current_camera_name
def switch_camera(self, new_camera_name, token):
if new_camera_name != self.HD_CAMERA_NAME: #for starting camera by command from base sender
token = config.get_app().user_login.user_token
self.logger.info('Switching camera module from %s to %s' % (self.current_camera_name, new_camera_name))
self.stop_camera_process()
return self.start_camera_process(camera_name=new_camera_name, token=token)
def stop_camera_process(self):
if self.camera_process:
self.camera_process.terminate()
self.current_camera_name = "Disable camera"