class TestCamera(unittest.TestCase): def setUp(self): #Instantiate camera test fixtures self.cam = Camera(1, 1, 1) self.ip_cam = IPCamera(1, 1, 1, "127.1.1.1", "/test_image_path", "/test_mjpeg_path", "3000") def test_get_frame_no_parser(self): #Assert camera raises error when no parser is present self.assertRaises(ClosedStreamError, self.cam.get_frame) def test_open_stream_invalid_enum(self): #Assert exception raised with invalid enum self.assertRaises(ValueError, self.ip_cam.open_stream, "INVALID_ENUM_VAL") def test_get_url_invalid_enum(self): #Assert exception raised with invalid enum self.assertRaises(ValueError, self.ip_cam.get_url, "INVALID_ENUM_VAL") def test_get_url_mjpeg(self): #Assert url correctly created for mjpeg case result = self.ip_cam.get_url(StreamFormat.MJPEG) self.assertEquals(result, "http://127.1.1.1:3000/test_mjpeg_path") def test_get_url_image(self): #Assert url correctly created for image case result = self.ip_cam.get_url(StreamFormat.IMAGE) self.assertEquals(result, "http://127.1.1.1:3000/test_image_path")
def __get_camera_from_db(self, camera_id, duration, interval): """ Reads camera IDs from file, and queries database for those cameras. Archives the images from those cameras in the indicated result path. """ connection = MySQLdb.connect(self.db_server, self.db_username, self.db_password, self.db_name) cursor = connection.cursor() camera = None # cursor.execute( 'SELECT camera.id, ip_camera.ip, ip_camera.port, ' 'ip_camera_model.image_path, ip_camera_model.video_path ' 'FROM camera, ip_camera, ip_camera_model ' 'WHERE camera.id = ip_camera.camera_id ' 'and ip_camera.ip_camera_model_id = ip_camera_model.id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create an IPCamera object. if camera_row: camera = IPCamera(camera_row[0], duration, interval, camera_row[1], camera_row[3], camera_row[4], camera_row[2]) # 1 is ip and 3 is image path else: # Get the non-IP camera with the given ID. cursor.execute('select camera.id, non_ip_camera.snapshot_url ' 'FROM camera, non_ip_camera ' 'WHERE camera.id = non_ip_camera.camera_id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create a NonIPCamera object. if camera_row: camera = NonIPCamera(camera_row[0], duration, interval, camera_row[1]) else: # Get the stream camera with the given ID. cursor.execute('select camera.id, camera.m3u8_key ' 'FROM camera, stream_camera ' 'WHERE camera.id = stream_camera.camera_id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create a stream camera object. if camera_row: camera = StreamCamera(camera_row[0], duration, interval, camera_row[1]) cursor.close() connection.close() if not camera: print 'There is no camera with the ID {}.'.format(camera_id) return None return camera
def get_camera_db(camera_id, duration, interval): """ Get a camera from the database. """ try: import MySQLdb except Exception as e: raise(e) # The server database credentials. DB_SERVER = 'localhost' DB_USER_NAME = 'root' DB_PASSWORD = '******' DB_NAME = 'cam2' camera = None # Connect to the database, and get the connection cursor connection = MySQLdb.connect(DB_SERVER, DB_USER_NAME, DB_PASSWORD, DB_NAME) cursor = connection.cursor() # Get the IP camera with the given ID. cursor.execute('SELECT camera.id, ip_camera.ip, ip_camera.port, ' 'ip_camera_model.image_path, ip_camera_model.video_path ' 'FROM camera, ip_camera, ip_camera_model ' 'WHERE camera.id = ip_camera.camera_id ' 'and ip_camera.ip_camera_model_id = ip_camera_model.id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create an IPCamera object. if camera_row: camera = IPCamera(camera_row[0], duration, interval, camera_row[1], camera_row[3], camera_row[4], camera_row[2]) print ("This is IPCamera:\n") print (camera_row) else: # Get the non-IP camera with the given ID. cursor.execute('select camera.id, non_ip_camera.snapshot_url ' 'FROM camera, non_ip_camera ' 'WHERE camera.id = non_ip_camera.camera_id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create a NonIPCamera object. if camera_row: camera = NonIPCamera(camera_row[0], duration, interval, camera_row[1]) print("This is non-IP camera:\n") print(camera_row) cursor.close() connection.close() if not camera: print 'There is no camera with the ID {}.'.format(camera_id) return None return camera
def get_camera(camera_id): """ Get a camera from the database. """ camera = None # Connect to the database, and get the connection cursor connection = MySQLdb.connect(DB_SERVER, DB_USER_NAME, DB_PASSWORD, DB_NAME) cursor = connection.cursor() # Get the IP camera with the given ID. cursor.execute('SELECT camera.id, ip_camera.ip, ip_camera.port, ' 'ip_camera_model.image_path, ip_camera_model.video_path ' 'FROM camera, ip_camera, ip_camera_model ' 'WHERE camera.id = ip_camera.camera_id ' 'and ip_camera.ip_camera_model_id = ip_camera_model.id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create an IPCamera object. if camera_row: camera = IPCamera(camera_row[0], camera_row[1], camera_row[3], camera_row[4], camera_row[2]) else: # Get the non-IP camera with the given ID. cursor.execute('select camera.id, non_ip_camera.snapshot_url ' 'FROM camera, non_ip_camera ' 'WHERE camera.id = non_ip_camera.camera_id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create a NonIPCamera object. if camera_row: camera = NonIPCamera(camera_row[0], camera_row[1]) cursor.close() connection.close() if not camera: print 'There is no camera with the ID {}.'.format(camera_id) return None return camera
def isCamera(address, port, cameras): for path in sorted(cameras.keys()): #print("These are the paths that looped through:\n") #print(path) #print("These are the cameras: \n") #print(cameras.keys()) #\video2.mjpeg #print("These are the sorted cameras: \n") #print(sorted(cameras.keys())) #\MJPEG.CGI #Only goes once through the loop, unless the first path.resp.status != 200 -> Once 200, returns #it gets a 200 response with the first key no matter what #bc it redirects to html.login try: conn = httplib.HTTPConnection(address, port, timeout=5) conn.request("GET", path) resp = conn.getresponse() except Exception: # print "exception HTTPConnect" return "Not Found" if (resp.status == 200): try: if (loginRequired(address) == 0): #no login is found camera = IPCamera(1, 1, address, path) archiver(camera) return path #This needs more improvement. #What if more than 1 path? #continue with finally, then return path? else: file = open("Ip_Requires_Login.txt", 'a') file.write("<camera>\n") printXML.out(file, "ip", address) printXML.out(file, "path", path) printXML.out(file, "brand", cameras[path]) file.write("</camera>\n") file.close() return path except Exception: return "Not Found" return "Not Found"
def video_feed(): url = session.get('url') return Response(gen(IPCamera(url)), mimetype='multipart/x-mixed-replace; boundary=frame')
def setUp(self): #Instantiate camera test fixtures self.cam = Camera(1, 1, 1) self.ip_cam = IPCamera(1, 1, 1, "127.1.1.1", "/test_image_path", "/test_mjpeg_path", "3000")
def get_camera_db(camera_id, duration, interval): """ Get a camera from the database. **Parameters:** camera_id : str The camera from the database's ID. duration : int The duration of downloading the integer in seconds. interval : int The interval between two successive snapshots. **Returns:** camera : camera object The camera from which snapshots will be taken. **Raises:** Exceptions : If there is an error downloading MYSQLdb. """ try: import MySQLdb except Exception as e: raise(e) # The server database credentials. DB_SERVER = 'localhost' DB_USER_NAME = 'root' DB_PASSWORD = '' DB_NAME = '' camera = None # Connect to the database, and get the connection cursor connection = MySQLdb.connect(DB_SERVER, DB_USER_NAME, DB_PASSWORD, DB_NAME) cursor = connection.cursor() # Get the IP camera with the given ID. cursor.execute('SELECT camera.id, ip_camera.ip, ip_camera.port, ' 'ip_camera_model.image_path, ip_camera_model.video_path ' 'FROM camera, ip_camera, ip_camera_model ' 'WHERE camera.id = ip_camera.camera_id ' 'and ip_camera.ip_camera_model_id = ip_camera_model.id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create an IPCamera object. if camera_row: camera = IPCamera(camera_row[0], duration, interval, camera_row[1], camera_row[3], camera_row[4], camera_row[2]) #1 is ip and 3 is image path else: # Get the non-IP camera with the given ID. cursor.execute('select camera.id, non_ip_camera.snapshot_url ' 'FROM camera, non_ip_camera ' 'WHERE camera.id = non_ip_camera.camera_id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create a NonIPCamera object. if camera_row: camera = NonIPCamera(camera_row[0], duration, interval, camera_row[1]) else: # Get the stream camera with the given ID. cursor.execute('select camera.id, camera.m3u8_key ' 'FROM camera, stream_camera ' 'WHERE camera.id = stream_camera.camera_id ' 'and camera.id = {};'.format(camera_id)) camera_row = cursor.fetchone() # Create a stream camera object. if camera_row: camera = StreamCamera(camera_row[0],duration, interval, camera_row[1]) cursor.close() connection.close() if not camera: print 'There is no camera with the ID {}.'.format(camera_id) return None return camera
def video_feed(): return Response(gen(IPCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')