def __init__(self, files_to_monitor): self._pipes = [] for path in files_to_monitor: os_help.ignore_exist(os.unlink, path) os.mkfifo(path) pipe_fd = os.open(path, os.O_RDONLY | os.O_NONBLOCK) self._pipes.append((os.fdopen(pipe_fd), path))
def log(self, file_path, time, what): if self.log_file is None or self.log_file.name != file_path: os_help.ignore_exist(os.makedirs, os.path.dirname(file_path)) self.log_file = open(file_path, 'a') timestamp = time.strftime(config.date_time_format) self.log_file.write(timestamp + ", " + what + "\n") self.log_file.flush()
def process_segments(self, now): """ Mark video segments as having motion or not. All segments are linked to the "all" subdir, segments with motion are also linked to the "motion" subdir. Segments are removed from the "unprocessed" dir except if that segment is still the currently-being-written-to segment. It would be safe to unlink that segment, but we leave it to make it easy to find that file. """ file_list = os.listdir(self.capture_path) segments = [] for _file in file_list: pieces = _file.split('.') if len(pieces) < 2 or pieces[len(pieces) - 1] != "mp4": print "Unexpected pieces count: " + str(pieces) continue try: start_time = datetime.datetime.strptime( pieces[len(pieces) - 2], config.date_time_format) segments.append((start_time, _file)) except Exception: syslog.syslog(1, traceback.format_exc()) traceback.print_exc() # Sort by starting date segments.sort(key=lambda tup: tup[0]) for segment in segments: motion_file = os.path.join(get_motion_dir(segment[0]), segment[1]) if (self.motion_start_time is None or (self.motion_start_time - config.event_gap > segment[0] + config.segment_length) or segment[0] > now or os.path.exists(motion_file)): continue os_help.ignore_exist(os.makedirs, os.path.dirname(motion_file)) os.link(os.path.join(self.capture_path, segment[1]), motion_file) for segnum, segment in enumerate(segments): date_stamp = (segment[0].strftime(config.directory_date_format)) dest = os.path.join(config.video_store_path, date_stamp, config.video_all_dir, segment[1]) src = os.path.join(self.capture_path, segment[1]) os_help.ignore_exist(os.makedirs, os.path.dirname(dest)) os_help.ignore_exist2(os.link, src, dest) if (segnum + 1 != len(segments)): os.unlink(src)
def __init__(self, camera_index): self.name = config.cameras[camera_index].name self.capture_url = config.cameras[camera_index].record_url self.capture_path = os.path.join(config.video_unprocessed_path, self.name) self.capture_process = None self.save_prefix = "save-" self.motion_index = camera_index + 1 self.monitor_url = config.cameras[camera_index].monitor_url self.pipe = os.path.join(config.working_area, "motion_pipe_cam" + str(self.motion_index)) self.motion_start_time = None os_help.ignore_exist(os.makedirs, self.capture_path) self.write_motion_config() self.start_capture() print "Monitoring Camera: " + self.name
def __del__(self): for pipe in self._pipes: pipe[0].close() os_help.ignore_exist(os.unlink, pipe[1])
syslog.syslog( 2, "Starting motion detection: " + str(_cmd) + " pid: " + str(motion_pid.pid)) def sighandler(signum, frame): sys.exit("caught signal: " + str(signum)) try: config.init(sys.argv[1]) signal.signal(signal.SIGTERM, sighandler) shutil.rmtree(config.working_area, True) shutil.rmtree(config.motion_config_path, True) shutil.rmtree(config.video_unprocessed_path, True) os_help.ignore_exist(os.makedirs, config.motion_config_path) initialize_cameras() my_input = pipe_watcher.PipesWatcher(get_pipes()) start_motion_detection() start_time = datetime.datetime.now() space_check_time = start_time - config.space_check_rate while True: my_input.check(on_change) now = datetime.datetime.now() if (now - space_check_time) > config.space_check_rate: space_check_time = now disk_usage.cleanup() if (now - start_time) > config.periodic_process_rate: start_time = now