import config
import motionctl
+ ioloop = tornado.ioloop.IOLoop.instance()
+
# add a motion running checker
def checker():
- ioloop = tornado.ioloop.IOLoop.instance()
if ioloop._stopped:
return
- if not motionctl.running() and config.has_enabled_cameras():
+ if not motionctl.running() and motionctl.started() and config.has_enabled_cameras():
try:
+ logging.error('motion not running, starting it')
motionctl.start()
- logging.info('motion started')
except Exception as e:
logging.error('failed to start motion: %(msg)s' % {
ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
- checker()
+ if config.has_enabled_cameras():
+ motionctl.start()
+
+ ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
def _start_cleanup():
_configure_signals()
_configure_logging()
-
if settings.SMB_SHARES:
- smbctl.update_mounts()
+ stop, start = smbctl.update_mounts()
+ if start:
+ _start_motion()
- _start_motion()
+ else:
+ _start_motion()
+
_start_cleanup()
if settings.THUMBNAILER_INTERVAL:
def finish():
if restart[0]:
+ logging.debug('motion needs to be restarted')
+
+ motionctl.stop()
+
if settings.SMB_SHARES:
logging.debug('updating SMB mounts')
- smbctl.update_mounts()
+ stop, start = smbctl.update_mounts() # @UnusedVariable
- logging.debug('motion needs to be restarted')
- motionctl.restart()
+ if start:
+ motionctl.start()
+
+ else:
+ motionctl.start()
self.finish({'reload': reload, 'error': error[0]})
camera_config['@id'] = camera_id
if proto == 'v4l2':
+ motionctl.stop()
+
if settings.SMB_SHARES:
- smbctl.update_mounts()
+ stop, start = smbctl.update_mounts() # @UnusedVariable
- motionctl.restart()
+ if start:
+ motionctl.start()
+
+ else:
+ motionctl.start()
ui_config = config.camera_dict_to_ui(camera_config)
config.rem_camera(camera_id)
if local:
- motionctl.restart()
+ motionctl.stop()
+ if config.has_enabled_cameras():
+ motionctl.start()
self.finish_json()
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import errno
+import logging
import os.path
import re
import signal
import settings
+_started = False
+
+
def find_motion():
try:
binary = subprocess.check_output('which motion', shell=True).strip()
def start():
+ global _started
+
+ _started = True
+
if running():
- raise Exception('motion is already running')
+ return
+
+ logging.debug('starting motion')
program = find_motion()
if not program:
def stop():
import mjpgclient
+ global _started
+
+ _started = False
+
if not running():
- raise Exception('motion is not running')
+ return
+
+ logging.debug('stopping motion')
mjpgclient.close_all()
if pid is None:
return False
-
try:
os.waitpid(pid, os.WNOHANG)
os.kill(pid, 0)
return False
-def restart():
- if running():
- stop()
-
- if config.has_enabled_cameras():
- start()
+def started():
+ return _started
def _get_pid():
import subprocess
import time
+from tornado import ioloop
+
import config
+import motionctl
import settings
-from tornado import ioloop
-
def find_mount_cifs():
try:
except subprocess.CalledProcessError:
logging.error('failed to mount smb share "//%s/%s" at "%s"' % (server, share, mount_point))
- return False
+ return None
# test to see if mount point is writable
try:
except:
logging.error('directory at "%s" is not writable' % mount_point)
- return False
+ return None
return mount_point
return False
try:
- os.remove(mount_point)
+ os.rmdir(mount_point)
except Exception as e:
logging.error('failed to remove smb mount point "%s": %s' % (mount_point, e))
mounts = list_mounts()
mounts = dict(((m['server'], m['share'], m['username'] or ''), False) for m in mounts)
+ should_stop = False # indicates that motion should be stopped immediately
+ should_start = True # indicates that motion can be started afterwards
for network_share in network_shares:
key = (network_share['server'], network_share['share'], network_share['username'] or '')
if key in mounts: # found
mounts[key] = True
else: # needs to be mounted
- mount(network_share['server'], network_share['share'], network_share['username'], network_share['password'])
+ should_stop = True
+ if not mount(network_share['server'], network_share['share'], network_share['username'], network_share['password']):
+ should_start = False
# unmount the no longer necessary mounts
for (network_share['server'], network_share['share'], network_share['username']), required in mounts.items():
if not required:
umount(network_share['server'], network_share['share'], network_share['username'])
+ should_stop = True
+
+ return (should_stop, should_start)
def umount_all():
def _check_mounts():
logging.debug('checking SMB mounts...')
- update_mounts()
+ stop, start = update_mounts()
+ if stop:
+ motionctl.stop()
+ if start:
+ motionctl.start()
+
io_loop = ioloop.IOLoop.instance()
io_loop.add_timeout(datetime.timedelta(seconds=settings.MOUNT_CHECK_INTERVAL), _check_mounts)