def camera_streams(app, ftp_client, image_downloader): ic = ImageCounter() with app.app_context(): while is_enabled(): time.sleep(2) app.logger.info('sending stream') ip_cameras = IPCamera.query.all() for cam in ip_cameras: if not cam.ftp: # check if remote folder exists if cam.name not in ftp_client.ls(): app.logger.warning('Folder ' + cam.name + " not found") continue files = ftp_client.ls(cam.name) image_counter = ic.extract_next(cam.id, files) image_name = ic.build_name(image_counter) remote_file_path = os.path.join(cam.name, image_name) # image_downloader = ImageDownloader(cam.host, cam.port) image_content = image_downloader.get_image( cam.host, cam.port) app.logger.info('uploading file:' + remote_file_path) ftp_client.upload_content(remote_file_path, image_content)
def gen_serve_upload(self, app): # it bounds the life of the thread to the life of the flask app with app.app_context(): ic = ImageCounter() while self.is_enabled(): file_path = self.image_generator.generate() app.logger.info('serving') self.serve(file_path) if self.is_ftp_supported(): # check if remote folder exists if self.camera_name not in self.ftp_client.ls(): app.logger.warning('Folder ' + self.camera_name + " not found") continue files = self.ftp_client.ls(self.camera_name) image_counter = ic.extract_next(1, files) image_name = ic.build_name(image_counter) remote_file_path = os.path.join(self.camera_name, image_name) self.ftp_client.upload_file(remote_file_path, file_path)
def test_extract_next_empty(self): image_counter = ImageCounter() files = [] assert image_counter.extract_next(1, files) == 1
def test_extract_next(self): image_counter = ImageCounter() files = ['image0001.jpg', 'image0002.jpg', 'image0003.jpg'] assert image_counter.extract_next(1, files) == 4