def nv_midbox_start_all_stream(self):
     '''
     Start all the available cameras in the system
     '''
     db_mgr_obj.db_start_transaction()
     cam_records = db_mgr_obj.get_tbl_records(nv_camera)
     db_mgr_obj.db_end_transaction()
     for cam_record in cam_records:
         cam_ipcData = camera_data(
             op=enum_ipcOpCode.CONST_START_CAMERA_STREAM_OP,
             name=cam_record.name,
             status=cam_record.status,
             ip=cam_record.ip_addr,
             macAddr=cam_record.mac_addr,
             port=cam_record.listen_port,
             time_len=cam_record.stream_file_time_sec,
             uname=cam_record.username,
             pwd=cam_record.password,
             desc=cam_record.desc)
         try:
             GBL_CONF_QUEUE.enqueue_data(obj_len=1, obj_value=[cam_ipcData])
         except Exception as e:
             self.nv_log_handler.error(
                 "Failed to start the camera at "
                 "cli, %s", e)
    def validate_cam_details(self, name, ip_addr, mac_addr, port, uname, pwd):
        '''
        Validate the camera details are right before configuring it.
        Return true if all the details are right. False otherwise.
        All the function parameters are string type.
        '''
        db_mgr_obj.db_start_transaction()
        cam_records = db_mgr_obj.get_tbl_records(nv_camera)
        db_mgr_obj.db_end_transaction()
        for cam_record in cam_records:
            if cam_record.name == name:
                self.nv_log_handler.error(
                    "Cannot add camera, Duplicate name :%s", name)
                return False
            db_ip_addr_str = socket.inet_ntoa(
                struct.pack('!L', cam_record.ip_addr))
            if db_ip_addr_str == ip_addr:
                self.nv_log_handler.error(
                    "Cannot add camera, Duplicate IP :%s", ip_addr)
                return False
            if cam_record.mac_addr == mac_addr:
                self.nv_log_handler.error(
                    "Cannot add camera '%s' "
                    "duplicate mac-addr %s", cam_record.name, mac_addr)
                return False

        if not self.is_camera_rtsp_stream_valid(ip_addr, port, uname, pwd):
            return False
        # Check the camera is serving the rtsp stream or not.
        return True
 def open(self):
     # Read the DB for all the camera and system details
     # Format it into json
     # send out it as a message
     cam_json = []
     GBL_WEBSOCK_POOL.add_connection(self)
     try:
         db_mgr_obj.db_start_transaction()
         cameras = db_mgr_obj.get_tbl_records(nv_camera)
     except Exception as e:
         default_nv_log_handler.info("Failed to get camera details from DB"
                                     " when opening websocket in ws")
     finally:
         db_mgr_obj.db_end_transaction()
     try:
         if not cameras:
             return
         for camera in cameras:
             cam_json.append(self.get_camera_json(camera))
         if not cam_json:
             return
         GBL_WEBSOCK_POOL.write_to_connection(self, json.dumps(cam_json))
     except Exception as e:
         default_nv_log_handler.error("Exception while opening user websocket"
                                      " %s", e)
 def list_midbox_system(self):
     try:
         db_mgr_obj.db_start_transaction()
         self.nv_log_handler.debug("Listing system & webserver details "
                                   "from DB")
         if not db_mgr_obj.get_tbl_record_cnt(nv_midbox_system):
             print_color_string("No record found in system table",
                                color='red')
             self.nv_log_handler.debug("Empty system table in the DB")
             return
         sys_record = db_mgr_obj.get_tbl_records(nv_midbox_system)
         print_color_string(sys_record, color="green")
         if not db_mgr_obj.get_tbl_record_cnt(nv_webserver_system):
             self.nv_log_handler.debug(
                 "Empty webserver table in the system")
             return
         web_records = db_mgr_obj.get_tbl_records(nv_webserver_system)
         print_color_string(web_records, color="blue")
     except Exception as e:
         self.nv_log_handler.info("Failed to list the midbox system :%s", e)
     finally:
         db_mgr_obj.db_end_transaction()
def setup_db_ut():
    from src.nvdb.nvdb_manager import db_mgr_obj, nv_camera, nv_midbox_system
    db_mgr_obj.setup_session()
    print("Neoview Midbox system table record count %d"
          % db_mgr_obj.get_tbl_record_cnt(nv_midbox_system))
    print ("Neoview nv_camera record count %d"
           % db_mgr_obj.get_tbl_record_cnt(nv_camera))

    sys_id = (uuid.uuid4().int>>64) & 0xFFFFFFFF
    nv_sys_record = nv_midbox_system(sys_id = sys_id, name = "nv-system1")
    nv_cam_record1 = nv_camera(cam_id = (uuid.uuid4().int>>64) & 0xFFFFFFFF,
                               name = 'camera 1',
                               ip_addr = int(ipaddress.IPv4Address('10.10.10.1')),
                               mac_addr = "00:00:00:00:00:01",
                               listen_port = 1234,
                               username = '******',
                               password = '******',
                               nv_midbox = nv_sys_record
                               )
    nv_cam_record2 = nv_camera(cam_id = (uuid.uuid4().int>>64) & 0xFFFFFFFF,
                               name = 'camera 2',
                               ip_addr = int(ipaddress.IPv4Address('10.10.10.2')),
                               mac_addr = "00:00:00:00:00:02",
                               listen_port = 1234,
                               username = '******',
                               password = '******',
                               nv_midbox = nv_sys_record
                               )
    db_mgr_obj.add_record(nv_sys_record)
    db_mgr_obj.add_record(nv_cam_record1)
    db_mgr_obj.add_record(nv_cam_record2)
    db_mgr_obj.db_commit()
    print("Neoview Midbox system table record count %d"
          % db_mgr_obj.get_tbl_record_cnt(nv_midbox_system))
    print ("Neoview nv_camera record count %d"
           % db_mgr_obj.get_tbl_record_cnt(nv_camera))
    print(db_mgr_obj.get_tbl_records(nv_midbox_system))
    print(db_mgr_obj.get_tbl_records(nv_camera))
 def nv_midbox_list_cameras(self):
     try:
         db_mgr_obj.db_start_transaction()
         if not db_mgr_obj.get_tbl_record_cnt(nv_camera):
             print_color_string("No camera record found in the system",
                                color='red')
             self.nv_log_handler.debug("Camera table empty in the system")
             return
         cam_records = db_mgr_obj.get_tbl_records(nv_camera)
         print_color_string(cam_records, color="green")
         self.nv_log_handler.debug("Listing all the cameras in the DB")
     except Exception as e:
         self.nv_log_handler.info("Failed to list cameras : %s", e)
     finally:
         db_mgr_obj.db_end_transaction()
Exemple #7
0
    def midbox_camera_init(self):
        '''
        When starting middlebox at first time, validate all camera states,
        set them to OFF state unconditionally. start the live stream for each
         ** DO NOT INVOKE THE FUNCTION OTHER THAN AT THE TIME OF INIT **
        '''
        try:
            db_mgr_obj.db_start_transaction()
            cam_records = db_mgr_obj.get_tbl_records(nv_camera)
            for camera in cam_records:
                camera.status = enum_camStatus.CONST_CAMERA_READY
                camera.live_url = None
            db_mgr_obj.db_commit()

            # Populate the webserver if configured.
            db_mgr_obj.get_webserver_record()
        except Exception as e:
            self.nv_log_handler.info("Failed in midbox init : %s", e)
        finally:
            db_mgr_obj.db_end_transaction()

        # start the live stream for all the configured cameras
        for camera in cam_records:
            if not camera.name:
                self.nv_log_handler.error("Cannot start live stream for"
                                          " non-existent camera")
                continue
            try:
                live_obj = camera_data(
                            op = enum_ipcOpCode.CONST_START_CAMERA_STREAM_OP,
                            name = camera.name,
                            # Everything else can be None
                            status = None,
                            ip = None,
                            macAddr = None,
                            port = None,
                            time_len = None,
                            uname = None,
                            pwd = None,
                            desc = None
                            )
                self.nv_midbox_start_livestream(live_obj)
            except:
                self.nv_log_handler.error("Failed to start live stream of %s",
                                      camera.name)
Exemple #8
0
 def __nv_midbox_allCam_status_update(self, status):
     '''
     Update all the camera status to 'status'
     '''
     # Update all the camera status to deferred before exiting.
     if not db_mgr_obj.get_tbl_record_cnt(nv_camera):
         self.nv_log_handler.debug("Camera table empty in the system "\
                                   "not modifying the camera status")
         return
     cam_records = db_mgr_obj.get_tbl_records(nv_camera)
     for camera in cam_records:
         valid_camera_status = self._is_nv_midbox_cam_status_update_valid(
                                     camera.status, status)
         if not valid_camera_status:
             self.nv_log_handler.error("Cannot change state %s to %s for "
                                       "camera: %s",
                                       enum_camStatus.CAM_STATUS_STR[camera.status],
                                       enum_camStatus.CAM_STATUS_STR[status],
                                       camera.name)
             continue
         camera.status = status
     db_mgr_obj.db_commit()
     GBL_WSCLIENT.send_notify()
 def nv_midbox_stop_all_stream(self):
     db_mgr_obj.db_start_transaction()
     cam_records = db_mgr_obj.get_tbl_records(nv_camera)
     db_mgr_obj.db_end_transaction()
     for cam_record in cam_records:
         cam_name = cam_record.name
         cam_ipcData = camera_data(
             op=enum_ipcOpCode.CONST_STOP_CAMERA_STREAM_OP,
             name=cam_name,
             # Everything else is None
             status=None,
             ip=None,
             macAddr=None,
             port=None,
             time_len=None,
             uname=None,
             pwd=None,
             desc=None)
         try:
             GBL_CONF_QUEUE.enqueue_data(obj_len=1, obj_value=[cam_ipcData])
         except Exception as e:
             self.nv_log_handler.error(
                 "Failed to stop the camera at cli,"
                 " %s", e)
 def send_all_camera_to_all_ws(self):
     cam_json = []
     try:
         db_mgr_obj.db_start_transaction()
         cameras = db_mgr_obj.get_tbl_records(nv_camera)
     except Exception as e:
         default_nv_log_handler.info("Failed to collecte camera details"
                                      " from the db in ws process.")
     finally:
         db_mgr_obj.db_end_transaction()
     try:
         if not cameras:
             # No cameras configured, return empty json.
             cam_json.append({})
             GBL_WEBSOCK_POOL.write_to_all_connections(json.dumps(cam_json))
             return
         for camera in cameras:
             cam_json.append(self.get_camera_json(camera))
         if not cam_json:
             return
         GBL_WEBSOCK_POOL.write_to_all_connections(json.dumps(cam_json))
     except Exception as e:
         default_nv_log_handler.error("Exception while opening user websocket"
                                      " %s", e)