mirror of
				https://github.com/hyperion-project/hyperion.ng.git
				synced 2025-03-01 10:33:28 +00:00 
			
		
		
		
	Remove duplicated Code (#377)
* Reduce Duplicate Code * Reduce Duplicate Code * No Error Logging when "document_root" in Json config is empty * Update JsonClientConnection.cpp * Remove obsolete functions * create readConfig and readSchema function * set forgotten _error variable
This commit is contained in:
		
				
					committed by
					
						 brindosch
						brindosch
					
				
			
			
				
	
			
			
			
						parent
						
							8a4d1c5088
						
					
				
				
					commit
					ed47852518
				
			| @@ -21,8 +21,8 @@ public: | |||||||
| 	static int load(const QString& schema, const QString& config, QJsonObject& json) | 	static int load(const QString& schema, const QString& config, QJsonObject& json) | ||||||
| 	{ | 	{ | ||||||
| 		// Load the schema and the config trees | 		// Load the schema and the config trees | ||||||
| 		QJsonObject schemaTree = readJson(schema); | 		QJsonObject schemaTree = readSchema(schema); | ||||||
| 		QJsonObject configTree = readJson(config); | 		QJsonObject configTree = readConfig(config); | ||||||
|  |  | ||||||
| 		// create the validator | 		// create the validator | ||||||
| 		QJsonSchemaChecker schemaChecker; | 		QJsonSchemaChecker schemaChecker; | ||||||
| @@ -45,7 +45,7 @@ public: | |||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	static QJsonObject readJson(const QString& path) | 	static QJsonObject readConfig(const QString& path) | ||||||
| 	{ | 	{ | ||||||
| 		QFile file(path); | 		QFile file(path); | ||||||
| 		QJsonParseError error; | 		QJsonParseError error; | ||||||
| @@ -53,7 +53,7 @@ public: | |||||||
| 		if (!file.open(QIODevice::ReadOnly)) | 		if (!file.open(QIODevice::ReadOnly)) | ||||||
| 		{ | 		{ | ||||||
| 			std::stringstream sstream; | 			std::stringstream sstream; | ||||||
| 			sstream << "Configuration file not found: " << file.errorString().toStdString(); | 			sstream << "Configuration file not found: '" << path.toStdString() << "' (" <<  file.errorString().toStdString() << ")"; | ||||||
| 			throw std::runtime_error(sstream.str()); | 			throw std::runtime_error(sstream.str()); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| @@ -61,6 +61,7 @@ public: | |||||||
| 		config.remove(QRegularExpression("([^:]?\\/\\/.*)")); | 		config.remove(QRegularExpression("([^:]?\\/\\/.*)")); | ||||||
| 		 | 		 | ||||||
| 		QJsonDocument doc = QJsonDocument::fromJson(config.toUtf8(), &error); | 		QJsonDocument doc = QJsonDocument::fromJson(config.toUtf8(), &error); | ||||||
|  | 		file.close(); | ||||||
|  |  | ||||||
| 		if (error.error != QJsonParseError::NoError) | 		if (error.error != QJsonParseError::NoError) | ||||||
| 		{ | 		{ | ||||||
| @@ -82,7 +83,45 @@ public: | |||||||
| 			throw std::runtime_error(sstream.str()); | 			throw std::runtime_error(sstream.str()); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		file.close(); | 		return doc.object(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	static QJsonObject readSchema(const QString& path) | ||||||
|  | 	{ | ||||||
|  | 		QFile schemaData(path); | ||||||
|  | 		QJsonParseError error; | ||||||
|  |  | ||||||
|  | 		if (!schemaData.open(QIODevice::ReadOnly)) | ||||||
|  | 		{ | ||||||
|  | 			std::stringstream sstream; | ||||||
|  | 			sstream << "Schema not found: '" << path.toStdString() << "' (" <<  schemaData.errorString().toStdString() << ")"; | ||||||
|  | 			throw std::runtime_error(sstream.str()); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		QByteArray schema = schemaData.readAll(); | ||||||
|  | 		QJsonDocument doc = QJsonDocument::fromJson(schema, &error); | ||||||
|  | 		schemaData.close(); | ||||||
|  | 		 | ||||||
|  | 		if (error.error != QJsonParseError::NoError) | ||||||
|  | 		{ | ||||||
|  | 			// report to the user the failure and their locations in the document. | ||||||
|  | 			int errorLine(0), errorColumn(0); | ||||||
|  |  | ||||||
|  | 			for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i ) | ||||||
|  | 			{ | ||||||
|  | 				++errorColumn; | ||||||
|  | 				if(schema.at(i) == '\n' ) | ||||||
|  | 				{ | ||||||
|  | 					errorColumn = 0; | ||||||
|  | 					++errorLine; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			std::stringstream sstream; | ||||||
|  | 			sstream << "ERROR: Json schema wrong: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn; | ||||||
|  | 			throw std::runtime_error(sstream.str()); | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		return doc.object(); | 		return doc.object(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -67,14 +67,6 @@ private: | |||||||
| 	/// | 	/// | ||||||
| 	void setMessage(const std::string & message); | 	void setMessage(const std::string & message); | ||||||
|  |  | ||||||
| 	/// |  | ||||||
| 	/// Retrieves all references from the json-value as specified by the schema |  | ||||||
| 	/// |  | ||||||
| 	/// @param[in] value The json-value |  | ||||||
| 	/// @param[in] schema The schema |  | ||||||
| 	/// |  | ||||||
| 	void collectDependencies(const QJsonValue & value, const QJsonObject &schema); |  | ||||||
|  |  | ||||||
| private: | private: | ||||||
| 	// attribute check functions | 	// attribute check functions | ||||||
| 	/// | 	/// | ||||||
| @@ -106,15 +98,6 @@ private: | |||||||
| 	/// | 	/// | ||||||
| 	void checkAdditionalProperties(const QJsonObject & value, const QJsonValue & schema, const QStringList & ignoredProperties); | 	void checkAdditionalProperties(const QJsonObject & value, const QJsonValue & schema, const QStringList & ignoredProperties); | ||||||
|  |  | ||||||
| 	/// |  | ||||||
| 	/// Checks if references are configued and used correctly. If this is not the case _error is set |  | ||||||
| 	/// to true and an error-message is added to the message-queue. |  | ||||||
| 	/// |  | ||||||
| 	/// @param value The given json-object |  | ||||||
| 	/// @param schemaLink The schema of the json-object |  | ||||||
| 	/// |  | ||||||
| 	void checkDependencies(const QJsonValue & value, const QJsonValue & schemaLink); |  | ||||||
|  |  | ||||||
| 	/// | 	/// | ||||||
| 	/// Checks if the given value is larger or equal to the specified value. If this is not the case | 	/// Checks if the given value is larger or equal to the specified value. If this is not the case | ||||||
| 	/// _error is set to true and an error-message is added to the message-queue. | 	/// _error is set to true and an error-message is added to the message-queue. | ||||||
|   | |||||||
| @@ -950,7 +950,7 @@ void JsonClientConnection::handleConfigGetCommand(const QJsonObject& message, co | |||||||
| 	 | 	 | ||||||
| 	try | 	try | ||||||
| 	{ | 	{ | ||||||
| 		result["result"] = QJsonFactory::readJson(QString::fromStdString(_hyperion->getConfigFileName())); | 		result["result"] = QJsonFactory::readConfig(QString::fromStdString(_hyperion->getConfigFileName())); | ||||||
| 	} | 	} | ||||||
| 	catch(...) | 	catch(...) | ||||||
| 	{ | 	{ | ||||||
|   | |||||||
| @@ -115,6 +115,7 @@ void QJsonSchemaChecker::validate(const QJsonValue & value, const QJsonObject &s | |||||||
| 		else | 		else | ||||||
| 		{ | 		{ | ||||||
| 			// no check function defined for this attribute | 			// no check function defined for this attribute | ||||||
|  | 			_error = true; | ||||||
| 			setMessage(std::string("No check function defined for attribute ") + attribute.toStdString()); | 			setMessage(std::string("No check function defined for attribute ") + attribute.toStdString()); | ||||||
| 			continue; | 			continue; | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -23,7 +23,7 @@ WebConfig::WebConfig(QObject * parent) | |||||||
| 		_baseUrl = webconfigConfig["document_root"].toString(_baseUrl); | 		_baseUrl = webconfigConfig["document_root"].toString(_baseUrl); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (_baseUrl != ":/webconfig") | 	if ( (_baseUrl != ":/webconfig") && !_baseUrl.trimmed().isEmpty()) | ||||||
| 	{ | 	{ | ||||||
| 		QFileInfo info(_baseUrl); | 		QFileInfo info(_baseUrl); | ||||||
| 		if (!info.exists() || !info.isDir()) | 		if (!info.exists() || !info.isDir()) | ||||||
| @@ -32,6 +32,8 @@ WebConfig::WebConfig(QObject * parent) | |||||||
| 			_baseUrl = WEBCONFIG_DEFAULT_PATH; | 			_baseUrl = WEBCONFIG_DEFAULT_PATH; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	else | ||||||
|  | 		_baseUrl = WEBCONFIG_DEFAULT_PATH; | ||||||
|  |  | ||||||
| 	Debug(log, "WebUI initialized, document root: %s", _baseUrl.toUtf8().constData()); | 	Debug(log, "WebUI initialized, document root: %s", _baseUrl.toUtf8().constData()); | ||||||
| 	if ( webconfigEnable ) | 	if ( webconfigEnable ) | ||||||
|   | |||||||
| @@ -136,49 +136,26 @@ int HyperionDaemon::tryLoadConfig(const QString & configFile, const int schemaVe | |||||||
| { | { | ||||||
| 	// make sure the resources are loaded (they may be left out after static linking) | 	// make sure the resources are loaded (they may be left out after static linking) | ||||||
| 	Q_INIT_RESOURCE(resource); | 	Q_INIT_RESOURCE(resource); | ||||||
| 	QJsonParseError error; |  | ||||||
|  |  | ||||||
| 	// read the json schema from the resource | 	// read the json schema from the resource | ||||||
| 	QString schemaFile = ":/hyperion-schema"; | 	QString schemaFile = ":/hyperion-schema"; | ||||||
| 	if (schemaVersion > 0) | 	if (schemaVersion > 0) | ||||||
| 		schemaFile += "-" + QString::number(schemaVersion); | 		schemaFile += "-" + QString::number(schemaVersion); | ||||||
| 	QFile schemaData(schemaFile); |  | ||||||
| 	if (!schemaData.open(QIODevice::ReadOnly)) | 	QJsonObject schemaJson; | ||||||
|  | 	try | ||||||
| 	{ | 	{ | ||||||
| 		std::stringstream error; | 		schemaJson = QJsonFactory::readSchema(schemaFile); | ||||||
| 		error << "Schema not found or not supported: " << schemaData.errorString().toStdString(); |  | ||||||
| 		throw std::runtime_error(error.str()); |  | ||||||
| 	} | 	} | ||||||
| 	 | 	catch(const std::runtime_error& error) | ||||||
| 	QByteArray schema = schemaData.readAll(); |  | ||||||
| 	QJsonDocument schemaJson = QJsonDocument::fromJson(schema, &error); |  | ||||||
| 	schemaData.close(); |  | ||||||
| 	 |  | ||||||
| 	if (error.error != QJsonParseError::NoError) |  | ||||||
| 	{ | 	{ | ||||||
| 		// report to the user the failure and their locations in the document. | 		throw std::runtime_error(error.what()); | ||||||
| 		int errorLine(0), errorColumn(0); |  | ||||||
| 		 |  | ||||||
| 		for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i ) |  | ||||||
| 		{ |  | ||||||
| 			++errorColumn; |  | ||||||
| 			if(schema.at(i) == '\n' ) |  | ||||||
| 			{ |  | ||||||
| 				errorColumn = 0; |  | ||||||
| 				++errorLine; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		 |  | ||||||
| 		std::stringstream sstream; |  | ||||||
| 		sstream << "ERROR: Json schema wrong: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn; |  | ||||||
|  |  | ||||||
| 		throw std::runtime_error(sstream.str()); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	QJsonSchemaChecker schemaChecker; | 	QJsonSchemaChecker schemaChecker; | ||||||
| 	schemaChecker.setSchema(schemaJson.object()); | 	schemaChecker.setSchema(schemaJson); | ||||||
|  |  | ||||||
| 	_qconfig = QJsonFactory::readJson(configFile); | 	_qconfig = QJsonFactory::readConfig(configFile); | ||||||
| 	if (!schemaChecker.validate(_qconfig)) | 	if (!schemaChecker.validate(_qconfig)) | ||||||
| 	{ | 	{ | ||||||
| 		for (std::list<std::string>::const_iterator i = schemaChecker.getMessages().begin(); i != schemaChecker.getMessages().end(); ++i) | 		for (std::list<std::string>::const_iterator i = schemaChecker.getMessages().begin(); i != schemaChecker.getMessages().end(); ++i) | ||||||
| @@ -193,7 +170,6 @@ int HyperionDaemon::tryLoadConfig(const QString & configFile, const int schemaVe | |||||||
| 	return generalConfig["configVersion"].toInt(-1); | 	return generalConfig["configVersion"].toInt(-1); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| void HyperionDaemon::loadConfig(const QString & configFile, const int neededConfigVersion) | void HyperionDaemon::loadConfig(const QString & configFile, const int neededConfigVersion) | ||||||
| { | { | ||||||
| 	Info(_log, "Selected configuration file: %s", configFile.toUtf8().constData()); | 	Info(_log, "Selected configuration file: %s", configFile.toUtf8().constData()); | ||||||
| @@ -488,7 +464,7 @@ void HyperionDaemon::createSystemFrameGrabber() | |||||||
| 			else if (type == "x11")      createGrabberX11(grabberConfig); | 			else if (type == "x11")      createGrabberX11(grabberConfig); | ||||||
| 			else { Warning( _log, "unknown framegrabber type '%s'", type.toUtf8().constData()); grabberCompState = false; } | 			else { Warning( _log, "unknown framegrabber type '%s'", type.toUtf8().constData()); grabberCompState = false; } | ||||||
| 			 | 			 | ||||||
| 			_hyperion->getComponentRegister().componentStateChanged(hyperion::COMP_GRABBER, grabberCompState); | //			_hyperion->getComponentRegister().componentStateChanged(hyperion::COMP_GRABBER, grabberCompState); | ||||||
| 			_hyperion->setComponentState(hyperion::COMP_GRABBER, grabberCompState ); | 			_hyperion->setComponentState(hyperion::COMP_GRABBER, grabberCompState ); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -16,53 +16,30 @@ bool loadConfig(const QString & configFile) | |||||||
| { | { | ||||||
| 	// make sure the resources are loaded (they may be left out after static linking) | 	// make sure the resources are loaded (they may be left out after static linking) | ||||||
| 	Q_INIT_RESOURCE(resource); | 	Q_INIT_RESOURCE(resource); | ||||||
| 	QJsonParseError error; |  | ||||||
|  |  | ||||||
| 	//////////////////////////////////////////////////////////// | 	//////////////////////////////////////////////////////////// | ||||||
| 	// read and set the json schema from the resource | 	// read and set the json schema from the resource | ||||||
| 	//////////////////////////////////////////////////////////// | 	//////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
| 	QFile schemaData(":/hyperion-schema-"+QString::number(CURRENT_CONFIG_VERSION)); | 	QJsonObject schemaJson; | ||||||
| 	 | 	 | ||||||
| 	if (!schemaData.open(QIODevice::ReadOnly)) | 	try | ||||||
| 	{ | 	{ | ||||||
| 		std::stringstream error; | 		schemaJson = QJsonFactory::readSchema(":/hyperion-schema"); | ||||||
| 		error << "Schema not found: " << schemaData.errorString().toStdString(); |  | ||||||
| 		throw std::runtime_error(error.str()); |  | ||||||
| 	} | 	} | ||||||
|  | 	catch(const std::runtime_error& error) | ||||||
| 	QByteArray schema = schemaData.readAll(); |  | ||||||
| 	QJsonDocument schemaJson = QJsonDocument::fromJson(schema, &error); |  | ||||||
|  |  | ||||||
| 	if (error.error != QJsonParseError::NoError) |  | ||||||
| 	{ | 	{ | ||||||
| 		// report to the user the failure and their locations in the document. | 		throw std::runtime_error(error.what()); | ||||||
| 		int errorLine(0), errorColumn(0); |  | ||||||
| 		 |  | ||||||
| 		for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i ) |  | ||||||
| 		{ |  | ||||||
| 			++errorColumn; |  | ||||||
| 			if(schema.at(i) == '\n' ) |  | ||||||
| 			{ |  | ||||||
| 				errorColumn = 0; |  | ||||||
| 				++errorLine; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		 |  | ||||||
| 		std::stringstream sstream; |  | ||||||
| 		sstream << "Schema error: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn; |  | ||||||
|  |  | ||||||
| 		throw std::runtime_error(sstream.str()); |  | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	QJsonSchemaChecker schemaChecker; | 	QJsonSchemaChecker schemaChecker; | ||||||
| 	schemaChecker.setSchema(schemaJson.object()); | 	schemaChecker.setSchema(schemaJson); | ||||||
| 	 | 	 | ||||||
| 	//////////////////////////////////////////////////////////// | 	//////////////////////////////////////////////////////////// | ||||||
| 	// read and validate the configuration file from the command line | 	// read and validate the configuration file from the command line | ||||||
| 	//////////////////////////////////////////////////////////// | 	//////////////////////////////////////////////////////////// | ||||||
| 	 | 	 | ||||||
| 	const QJsonObject jsonConfig = QJsonFactory::readJson(configFile); | 	const QJsonObject jsonConfig = QJsonFactory::readConfig(configFile); | ||||||
| 	 | 	 | ||||||
| 	if (!schemaChecker.validate(jsonConfig)) | 	if (!schemaChecker.validate(jsonConfig)) | ||||||
| 	{ | 	{ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user