def on_close(self): logging.debug( 'connection closed for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id }) if MjpgClient.clients.pop(self._camera_id, None): logging.debug( 'mjpg client for camera %(camera_id)s on port %(port)s removed' % { 'port': self._port, 'camera_id': self._camera_id }) if getattr(self, 'error', None) and self.error.errno != errno.ECONNREFUSED: now = time.time() if now - MjpgClient._last_erroneous_close_time < settings.MJPG_CLIENT_TIMEOUT: msg = 'connection problem detected for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id } logging.error(msg) if settings.MOTION_RESTART_ON_ERRORS: motionctl.stop(invalidate=True ) # this will close all the mjpg clients motionctl.start(deferred=True) MjpgClient._last_erroneous_close_time = now
def start_motion(): import tornado.ioloop import config import motionctl ioloop = tornado.ioloop.IOLoop.instance() # add a motion running checker def checker(): if ioloop._stopped: return if not motionctl.running() and motionctl.started() and config.get_enabled_local_motion_cameras(): try: logging.error('motion not running, starting it') motionctl.start() except Exception as e: logging.error('failed to start motion: %(msg)s' % { 'msg': unicode(e)}, exc_info=True) ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker) motionctl.start() ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
def _garbage_collector(): io_loop = IOLoop.instance() io_loop.add_timeout( datetime.timedelta(seconds=settings.MJPG_CLIENT_TIMEOUT), _garbage_collector) now = time.time() for camera_id, client in MjpgClient.clients.items(): port = client.get_port() if client.closed(): continue # check for jpeg frame timeout last_jpg_time = client.get_last_jpg_time() delta = now - last_jpg_time if delta > settings.MJPG_CLIENT_TIMEOUT: logging.error( 'mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s' % { 'camera_id': camera_id, 'port': port }) if settings.MOTION_RESTART_ON_ERRORS: motionctl.stop( invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) break # check for last access timeout '''
def on_close(self): logging.debug( "connection closed for mjpg client for camera %(camera_id)s on port %(port)s" % {"port": self._port, "camera_id": self._camera_id} ) if MjpgClient.clients.pop(self._camera_id, None): MjpgClient.last_access.pop(self._camera_id, None) MjpgClient.last_jpg_moment.pop(self._camera_id, None) logging.debug( "mjpg client for camera %(camera_id)s on port %(port)s removed" % {"port": self._port, "camera_id": self._camera_id} ) if getattr(self, "error", None) and self.error.errno != errno.ECONNREFUSED: now = time.time() if now - MjpgClient.last_erroneous_close_time < settings.MJPG_CLIENT_TIMEOUT: logging.error( "connection problem detected for mjpg client for camera %(camera_id)s on port %(port)s" % {"port": self._port, "camera_id": self._camera_id} ) motionctl.stop(invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) MjpgClient.last_erroneous_close_time = now # remove the cached picture MjpgClient.last_jpgs.pop(self._camera_id, None)
def _garbage_collector(): logging.debug('running garbage collector for mjpg clients...') now = datetime.datetime.utcnow() for client in MjpgClient.clients.values(): camera_id = client._camera_id port = client._port # check for last jpg moment timeout last_jpg_moment = MjpgClient.last_jpg_moment.get(camera_id) if last_jpg_moment is None: MjpgClient.last_jpg_moment[camera_id] = now continue if client.closed(): continue delta = now - last_jpg_moment delta = delta.days * 86400 + delta.seconds if delta > settings.MJPG_CLIENT_TIMEOUT: logging.error( 'mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s' % { 'camera_id': camera_id, 'port': port }) motionctl.stop( invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) break # check for last access timeout last_access = MjpgClient.last_access.get(camera_id) if last_access is None: continue delta = now - last_access delta = delta.days * 86400 + delta.seconds if settings.MJPG_CLIENT_IDLE_TIMEOUT and delta > settings.MJPG_CLIENT_IDLE_TIMEOUT: logging.debug( 'mjpg client for camera %(camera_id)s on port %(port)s has been idle for %(timeout)s seconds, removing it' % { 'camera_id': camera_id, 'port': port, 'timeout': settings.MJPG_CLIENT_IDLE_TIMEOUT }) client.close() continue io_loop = ioloop.IOLoop.instance() io_loop.add_timeout( datetime.timedelta(seconds=settings.MJPG_CLIENT_TIMEOUT), _garbage_collector)
def start_motion(): import config import motionctl io_loop = IOLoop.instance() # add a motion running checker def checker(): if io_loop._stopped: return if not motionctl.running() and motionctl.started( ) and config.get_enabled_local_motion_cameras(): try: logging.error('motion not running, starting it') motionctl.start() except Exception as e: logging.error('failed to start motion: %(msg)s' % {'msg': unicode(e)}, exc_info=True) io_loop.add_timeout( datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker) try: motionctl.start() except Exception as e: logging.error(str(e), exc_info=True) io_loop.add_timeout( datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
def _garbage_collector(): logging.debug('running garbage collector for mjpg clients...') io_loop = IOLoop.instance() io_loop.add_timeout( datetime.timedelta(seconds=settings.MJPG_CLIENT_TIMEOUT), _garbage_collector) now = time.time() for camera_id, client in MjpgClient.clients.items(): port = client._port # check for jpeg frame timeout if not client._last_jpg_times: client._last_jpg_times.append(now) last_jpg_time = client._last_jpg_times[-1] if client.closed(): continue delta = now - last_jpg_time if delta > settings.MJPG_CLIENT_TIMEOUT: logging.error( 'mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s' % { 'camera_id': camera_id, 'port': port }) if settings.MOTION_RESTART_ON_ERRORS: motionctl.stop( invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) break # check for last access timeout if client._last_access is None: continue delta = now - client._last_access if settings.MJPG_CLIENT_IDLE_TIMEOUT and delta > settings.MJPG_CLIENT_IDLE_TIMEOUT: logging.debug( 'mjpg client for camera %(camera_id)s on port %(port)s has been idle for %(timeout)s seconds, removing it' % { 'camera_id': camera_id, 'port': port, 'timeout': settings.MJPG_CLIENT_IDLE_TIMEOUT }) client.close() continue
def _check_mounts(): logging.debug('checking SMB 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)
def _garbage_collector(): logging.debug("running garbage collector for mjpg clients...") io_loop = ioloop.IOLoop.instance() io_loop.add_timeout(datetime.timedelta(seconds=settings.MJPG_CLIENT_TIMEOUT), _garbage_collector) now = datetime.datetime.utcnow() for client in MjpgClient.clients.values(): camera_id = client._camera_id port = client._port # check for last jpg moment timeout last_jpg_moment = MjpgClient.last_jpg_moment.get(camera_id) if last_jpg_moment is None: MjpgClient.last_jpg_moment[camera_id] = now continue if client.closed(): continue delta = now - last_jpg_moment delta = delta.days * 86400 + delta.seconds if delta > settings.MJPG_CLIENT_TIMEOUT: logging.error( "mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s" % {"camera_id": camera_id, "port": port} ) motionctl.stop(invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) break # check for last access timeout last_access = MjpgClient.last_access.get(camera_id) if last_access is None: continue delta = now - last_access delta = delta.days * 86400 + delta.seconds if settings.MJPG_CLIENT_IDLE_TIMEOUT and delta > settings.MJPG_CLIENT_IDLE_TIMEOUT: logging.debug( "mjpg client for camera %(camera_id)s on port %(port)s has been idle for %(timeout)s seconds, removing it" % {"camera_id": camera_id, "port": port, "timeout": settings.MJPG_CLIENT_IDLE_TIMEOUT} ) client.close() continue
def checker(): if ioloop._stopped: return if not motionctl.running() and motionctl.started() and config.has_local_enabled_cameras(): try: logging.error("motion not running, starting it") motionctl.start() except Exception as e: logging.error("failed to start motion: %(msg)s" % {"msg": unicode(e)}, exc_info=True) ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
def _check_mounts(): import motionctl logging.debug('checking SMB mounts...') stop, start = update_mounts() if stop: motionctl.stop() if start: motionctl.start() io_loop = IOLoop.instance() io_loop.add_timeout(datetime.timedelta(seconds=settings.MOUNT_CHECK_INTERVAL), _check_mounts)
def _garbage_collector(): logging.debug('running garbage collector for mjpg clients...') now = datetime.datetime.utcnow() for client in MjpgClient.clients.values(): camera_id = client._camera_id port = client._port # check for last jpg moment timeout last_jpg_moment = MjpgClient.last_jpg_moment.get(camera_id) if last_jpg_moment is None: MjpgClient.last_jpg_moment[camera_id] = now continue if client.closed(): continue delta = now - last_jpg_moment delta = delta.days * 86400 + delta.seconds if delta > settings.MJPG_CLIENT_TIMEOUT: logging.error('mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s' % { 'camera_id': camera_id, 'port': port}) motionctl.stop() # this will close all the mjpg clients motionctl.start() break # check for last access timeout last_access = MjpgClient.last_access.get(camera_id) if last_access is None: continue delta = now - last_access delta = delta.days * 86400 + delta.seconds if delta > settings.MJPG_CLIENT_TIMEOUT: logging.debug('mjpg client for camera %(camera_id)s on port %(port)s timed out' % { 'camera_id': camera_id, 'port': port}) client.close() continue io_loop = ioloop.IOLoop.instance() io_loop.add_timeout(datetime.timedelta(seconds=settings.MJPG_CLIENT_TIMEOUT), _garbage_collector)
def on_close(self): logging.debug('connection closed for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id}) if MjpgClient.clients.pop(self._camera_id, None): logging.debug('mjpg client for camera %(camera_id)s on port %(port)s removed' % { 'port': self._port, 'camera_id': self._camera_id}) if getattr(self, 'error', None) and self.error.errno != errno.ECONNREFUSED: now = time.time() if now - MjpgClient._last_erroneous_close_time < settings.MJPG_CLIENT_TIMEOUT: logging.error('connection problem detected for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id}) motionctl.stop(invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) MjpgClient._last_erroneous_close_time = now
def _garbage_collector(): logging.debug('running garbage collector for mjpg clients...') io_loop = IOLoop.instance() io_loop.add_timeout(datetime.timedelta(seconds=settings.MJPG_CLIENT_TIMEOUT), _garbage_collector) now = time.time() for camera_id, client in MjpgClient.clients.items(): port = client._port # check for jpeg frame timeout if not client._last_jpg_times: client._last_jpg_times.append(now) last_jpg_time = client._last_jpg_times[-1] if client.closed(): continue delta = now - last_jpg_time if delta > settings.MJPG_CLIENT_TIMEOUT: logging.error('mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s' % { 'camera_id': camera_id, 'port': port}) if settings.MOTION_RESTART_ON_ERRORS: motionctl.stop(invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) break # check for last access timeout if client._last_access is None: continue delta = now - client._last_access if settings.MJPG_CLIENT_IDLE_TIMEOUT and delta > settings.MJPG_CLIENT_IDLE_TIMEOUT: logging.debug('mjpg client for camera %(camera_id)s on port %(port)s has been idle for %(timeout)s seconds, removing it' % { 'camera_id': camera_id, 'port': port, 'timeout': settings.MJPG_CLIENT_IDLE_TIMEOUT}) client.close() continue
def on_close(self): logging.debug('connection closed for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id}) if MjpgClient.clients.pop(self._camera_id, None): MjpgClient.last_access.pop(self._camera_id, None) MjpgClient.last_jpg_moment.pop(self._camera_id, None) logging.debug('mjpg client for camera %(camera_id)s on port %(port)s removed' % { 'port': self._port, 'camera_id': self._camera_id}) if getattr(self, 'error', None): now = time.time() if now - MjpgClient.last_erroneous_close_time < settings.MJPG_CLIENT_TIMEOUT: logging.error('connection problem detected for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id}) motionctl.stop() # this will close all the mjpg clients motionctl.start() MjpgClient.last_erroneous_close_time = now
def checker(): #vector fix me: print ("Type:",type(io_loop)) if not io_loop: #if io_loop._stopped: print("Return...") return if not motionctl.running() and motionctl.started( ) and config.get_enabled_local_motion_cameras(): try: logging.error('motion not running, starting it') motionctl.start() except Exception as e: logging.error('failed to start motion: %(msg)s' % {'msg': str(e)}, exc_info=True) io_loop.add_timeout( datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
def on_close(self): logging.debug( 'connection closed for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id }) if MjpgClient.clients.pop(self._camera_id, None): MjpgClient.last_access.pop(self._camera_id, None) MjpgClient.last_jpg_moment.pop(self._camera_id, None) logging.debug( 'mjpg client for camera %(camera_id)s on port %(port)s removed' % { 'port': self._port, 'camera_id': self._camera_id }) if getattr(self, 'error', None) and self.error.errno != errno.ECONNREFUSED: now = time.time() if now - MjpgClient.last_erroneous_close_time < settings.MJPG_CLIENT_TIMEOUT: logging.error( 'connection problem detected for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id }) motionctl.stop( invalidate=True) # this will close all the mjpg clients motionctl.start(deferred=True) MjpgClient.last_erroneous_close_time = now # remove the cached picture MjpgClient.last_jpgs.pop(self._camera_id, None)