-
Notifications
You must be signed in to change notification settings - Fork 8
/
start.py
124 lines (99 loc) · 4.14 KB
/
start.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
#!/bin/sh
''''which python >/dev/null 2>&1 && exec python "$0" "$@" # '''
''''which python3 >/dev/null 2>&1 && exec python3 "$0" "$@" # '''
''''exec echo "Error: Python not found!" # '''
# -*- coding: utf-8 -*-
import os
import sys
import locale
import tzlocal
import pytz
import datetime
import argparse
import signal
import time
# Ensure lib added to path, before any other imports
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))
import PTZController
from PTZController import PTZController, logger
def main():
"""
PTZController application entry point.
Parses arguments, setups encoding and initializes the application.
"""
if hasattr(sys, 'frozen'):
PTZController.FULL_PATH = os.path.abspath(sys.executable)
else:
PTZController.FULL_PATH = os.path.abspath(__file__)
PTZController.PROG_DIR = os.path.dirname(PTZController.FULL_PATH)
try:
locale.setlocale(locale.LC_ALL, "")
PTZController.SYS_LANGUAGE, PTZController.SYS_ENCODING = locale.getdefaultlocale()
except (locale.Error, IOError):
pass
if not PTZController.SYS_ENCODING or PTZController.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
PTZController.SYS_ENCODING = 'UTF-8'
try:
PTZController.SYS_TIMEZONE = str(tzlocal.get_localzone())
PTZController.SYS_UTC_OFFSET = datetime.datetime.now(pytz.timezone(PTZController.SYS_TIMEZONE)).strftime('%z')
except (pytz.UnknownTimeZoneError, LookupError, ValueError) as e:
print("Could not determine system timezone: %s" % e)
PTZController.SYS_TIMEZONE = 'Unknown'
PTZController.SYS_UTC_OFFSET = '+0000'
# Set up and gather command line arguments
parser = argparse.ArgumentParser(
description='A Python-based controller for PTZ cameras.')
parser.add_argument(
'-v', '--verbose', action='store_true', help='Increase console logging verbosity')
parser.add_argument(
'-q', '--quiet', action='store_true', help='Turn off console logging')
parser.add_argument(
'-d', '--daemon', action='store_true', help='Run as a daemon')
parser.add_argument(
'-p', '--port', type=int, help='Force PTZController to run on a specified port')
parser.add_argument(
'--config', help='Specify a config file to use')
parser.add_argument(
'--nolaunch', action='store_true', help='Prevent browser from launching on startup')
parser.add_argument(
'--pidfile', help='Create a pid file (only relevant when running as a daemon)')
parser.add_argument(
'--nofork', action='store_true', help='Start PTZController as a service, do not fork when restarting')
args = parser.parse_args()
if not args.verbose:
args.verbose = True
if not args.quiet:
args.quiet = False
# Do an intial setup of the logger.
logger.initLogger(console=not args.quiet, log_dir=False, verbose=args.verbose)
ptzController = PTZController(args)
# Register signals, such as CTRL + C
def sig_handler(signum=None, frame=None):
if signum is not None:
print("Signal %i caught, saving and exiting..." % signum)
if ptzController:
ptzController.SIGNAL = 'shutdown'
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
# Wait endlessy for a signal to happen
while True:
if not ptzController.SIGNAL:
try:
time.sleep(1)
except KeyboardInterrupt:
ptzController.SIGNAL = 'shutdown'
else:
print('Received signal: %s' % ptzController.SIGNAL)
if ptzController.SIGNAL == 'shutdown':
ptzController.shutdown()
os._exit(0)
elif ptzController.SIGNAL == 'restart':
ptzController.shutdown(restart=True)
elif ptzController.SIGNAL == 'checkout':
ptzController.shutdown(restart=True, checkout=True)
else:
ptzController.shutdown(restart=True, update=True)
ptzController.SIGNAL = None
# Call main()
if __name__ == "__main__":
main()