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 nv_midbox_update_live_url(self, cam_obj): try: db_mgr_obj.db_start_transaction() self.__nv_midbox_update_live_url(cam_obj) except Exception as e: self.nv_log_handler.info("Failed to update live url :%s", e) finally: db_mgr_obj.db_end_transaction()
def nv_midbox_cam_status_update(self, cam_obj): try: db_mgr_obj.db_start_transaction() self.__nv_midbox_cam_status_update(cam_obj) except Exception as e: self.nv_log_handler.info("Failed to update the camera status %s", e) finally: db_mgr_obj.db_end_transaction()
def nv_midbox_stop_livestream(self, cam_obj): try: db_mgr_obj.db_start_transaction() self.__nv_midbox_stop_livestream(cam_obj) except Exception as e: self.nv_log_handler.info("Failed to stop the livestream : %s", e) finally: db_mgr_obj.db_end_transaction()
def nv_midbox_kill_stream(self, cam_obj): try: db_mgr_obj.db_start_transaction() self.__nv_midbox_kill_stream(cam_obj) except Exception as e: self.nv_log_handler.info("Killing the camera failed : %s", e) finally: db_mgr_obj.db_end_transaction()
def nv_midbox_add_camera(self, cam_obj): try: db_mgr_obj.db_start_transaction() self.__nv_midbox_add_camera(cam_obj) except Exception as e: self.nv_log_handler.info("Failed to add camera : %s", e) finally: db_mgr_obj.db_end_transaction()
def nv_midbox_del_camera(self, cam_obj): try: db_mgr_obj.db_start_transaction() self.__nv_midbox_del_camera(cam_obj) except Exception as e: self.nv_log_handler.info("Exception occured while" " deleting the camera : %s", e) finally: db_mgr_obj.db_end_transaction()
def nv_midbox_allCam_status_update(self, status): try: db_mgr_obj.db_start_transaction() self.__nv_midbox_allCam_status_update(status) except Exception as e: self.nv_log_handler.info("%s exception occured while updating " "camera status", e) finally: db_mgr_obj.db_end_transaction()
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()
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)
def stream_state_changed(self): ''' Check if the state of camera streaming changed. Only valid states are considered, which are ON, OFF. Doesnt consider DISCONNECTED and DISABLED states are valid. @return: True : If state of streaming changed and new state is valid. False : Either the state is not changed/currently not in valid state. ''' if self.state_chk_timeout: ''' No need to do the camera status check in the loop. Validate only on the timeout. The purpose of this is to avoid unnecessary DB read for the validation. ''' self.state_chk_timeout -= 1 return False self.state_chk_timeout = self.const_stream_len_sec filter_arg = {'name' : self.cam_name} cam_record = None try: db_mgr_obj.db_start_transaction() cam_record = db_mgr_obj.get_tbl_records_filterby_first(nv_camera, filter_arg) db_mgr_obj.db_end_transaction() except: # Release the db lock in error in case its not yet released db_mgr_obj.db_end_transaction() return False if cam_record is None: self.nv_log_handler.error("Empty camera record for %s, Cannot read", self.cam_name) return False if cam_record.status is not enum_camStatus.CONST_CAMERA_READY and \ cam_record.status is not enum_camStatus.CONST_CAMERA_RECORDING: return False if cam_record.status is self.stream_state: return False self.stream_state = cam_record.status return True
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 nv_midbox_start_stream(self): cam_name = (input("Enter Camera Name: ")) filter_arg = {'name': cam_name} db_mgr_obj.db_start_transaction() cam_cnt = db_mgr_obj.get_tbl_records_filterby_cnt( nv_camera, filter_arg) db_mgr_obj.db_end_transaction() if cam_cnt == 0: self.nv_log_handler.error("No record found with given name %s" % cam_name) return if cam_cnt > 1: self.nv_log_handler.error("Exiting, More than one record found" " with same name %s" % cam_name) return db_mgr_obj.db_start_transaction() cam_record = db_mgr_obj.get_tbl_records_filterby( nv_camera, filter_arg)[0] db_mgr_obj.db_end_transaction() # XXX :: No need to send out all the camera details to start stream,only # name will be enough. But nothing harm to send everything. so sending # out for integrity. 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 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)