<commit_before>/* Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

#include "configurer.h"

#include <QtXml/QDomDocument>
#include <QtCore/QFile>
#include <QtCore/QDebug>

using namespace trikControl;

Configurer::Configurer()
	: mI2cDeviceId(0)
	, mLedOn(0)
	, mLedOff(0)
{
	QDomDocument config("config");

	QFile file("config.xml");
	if (!file.open(QIODevice::ReadOnly)) {
		qDebug() << "Failed to open config.xml for reading";
		throw "Failed to open config.xml for reading";
	} if (!config.setContent(&file)) {
		file.close();
		qDebug() << "config.xml parsing failed";
		throw "config.xml parsing failed";
	}

	file.close();

	QDomElement const root = config.documentElement();

	loadInit(root);
	loadServoMotors(root);
	loadPwmCaptures(root);
	loadPowerMotors(root);
	loadAnalogSensors(root);
	loadEncoders(root);
	loadSensors(root);
	loadMotorTypes(root);
	loadSensorTypes(root);
	loadSound(root);

	mAccelerometer = loadSensor3d(root, "accelerometer");
	mGyroscope = loadSensor3d(root, "gyroscope");

	loadI2c(root);
	loadLed(root);
}

QString Configurer::initScript() const
{
	return mInitScript;
}

QStringList Configurer::motorTypes() const
{
	return mMotorTypes.keys();
}

QStringList Configurer::sensorTypes() const
{
	return mSensorTypes.keys();
}

int Configurer::motorTypeMin(QString const &motorType) const
{
	return mMotorTypes[motorType].min;
}

int Configurer::motorTypeMax(QString const &motorType) const
{
	return mMotorTypes[motorType].max;
}

int Configurer::motorTypeZero(QString const &motorType) const
{
	return mMotorTypes[motorType].zero;
}

int Configurer::motorTypeStop(QString const &motorType) const
{
	return mMotorTypes[motorType].stop;
}

int Configurer::sensorTypeMin(QString const &sensorType) const
{
	return mSensorTypes[sensorType].min;
}

int Configurer::sensorTypeMax(QString const &sensorType) const
{
	return mSensorTypes[sensorType].max;
}

QStringList Configurer::servoMotorPorts() const
{
	return mServoMotorMappings.keys();
}

QString Configurer::servoMotorDeviceFile(QString const &port) const
{
	return mServoMotorMappings[port].deviceFile;
}

QString Configurer::servoMotorPeriodFile(const QString &port) const
{
	return mServoMotorMappings[port].periodFile;
}

int Configurer::servoMotorPeriod(const QString &port) const
{
	return mServoMotorMappings[port].period;
}

QString Configurer::servoMotorDefaultType(QString const &port) const
{
	return mServoMotorMappings[port].defaultType;
}

bool Configurer::servoMotorInvert(QString const &port) const
{
	return mServoMotorMappings[port].invert;
}

QStringList Configurer::pwmCapturePorts() const
{
	return mPwmCaptureMappings.keys();
}

QString Configurer::pwmCapturePeriodFile(const QString &port) const
{
	return mPwmCaptureMappings[port].periodFile;
}

QString Configurer::pwmCaptureDutyFile(const QString &port) const
{
	return mPwmCaptureMappings[port].dutyFile;
}

QStringList Configurer::powerMotorPorts() const
{
	return mPowerMotorMappings.keys();
}

int Configurer::powerMotorI2cCommandNumber(QString const &port) const
{
	return mPowerMotorMappings[port].i2cCommandNumber;
}

bool Configurer::powerMotorInvert(QString const &port) const
{
	return mPowerMotorMappings[port].invert;
}

QStringList Configurer::analogSensorPorts() const
{
	return mAnalogSensorMappings.keys();
}

int Configurer::analogSensorI2cCommandNumber(QString const &port) const
{
	return mAnalogSensorMappings[port].i2cCommandNumber;
}

QStringList Configurer::encoderPorts() const
{
	return mEncoderMappings.keys();
}

int Configurer::encoderI2cCommandNumber(QString const &port) const
{
	return mEncoderMappings[port].i2cCommandNumber;
}

QStringList Configurer::sensorPorts() const
{
	return mSensorMappings.keys();
}

QString Configurer::sensorDeviceFile(QString const &port) const
{
	return mSensorMappings[port].deviceFile;
}

QString Configurer::sensorDefaultType(QString const &port) const
{
	return mSensorMappings[port].defaultType;
}

QString Configurer::playSoundCommand() const
{
	return mPlaySoundCommand;
}

int Configurer::accelerometerMin() const
{
	return mAccelerometer.min;
}

int Configurer::accelerometerMax() const
{
	return mAccelerometer.max;
}

QString Configurer::accelerometerDeviceFile() const
{
	return mAccelerometer.deviceFile;
}

int Configurer::gyroscopeMin() const
{
	return mGyroscope.min;
}

int Configurer::gyroscopeMax() const
{
	return mGyroscope.max;
}

QString Configurer::gyroscopeDeviceFile() const
{
	return mGyroscope.deviceFile;
}

QString Configurer::i2cPath() const
{
	return mI2cPath;
}

int Configurer::i2cDeviceId() const
{
	return mI2cDeviceId;
}

QString Configurer::ledRedDeviceFile() const
{
	return mLedRedDeviceFile;
}

QString Configurer::ledGreenDeviceFile() const
{
	return mLedGreenDeviceFile;
}

int Configurer::ledOn() const
{
	return mLedOn;
}

int Configurer::ledOff() const
{
	return mLedOff;
}

void Configurer::loadInit(QDomElement const &root)
{
	if (root.elementsByTagName("initScript").isEmpty()) {
		qDebug() << "config.xml does not have <initScript> tag";
		throw "config.xml parsing failed";
	}

	QDomElement const init = root.elementsByTagName("initScript").at(0).toElement();
	mInitScript = init.text();
}

void Configurer::loadServoMotors(QDomElement const &root)
{
	if (root.elementsByTagName("servoMotors").isEmpty()) {
		qDebug() << "config.xml does not have <servoMotors> tag";
		throw "config.xml parsing failed";
	}

	QDomElement const servoMotors = root.elementsByTagName("servoMotors").at(0).toElement();
	for (QDomNode child = servoMotors.firstChild()
			; !child.isNull()
			; child = child.nextSibling())
	{
		if (!child.isElement()) {
			continue;
		}

		QDomElement const childElement = child.toElement();
		if (childElement.nodeName() != "motor") {
			qDebug() << "Malformed <servoMotors> tag";
			throw "config.xml parsing failed";
		}

		ServoMotorMapping mapping;
		mapping.port = childElement.attribute("port");
		mapping.deviceFile = childElement.attribute("deviceFile");
		mapping.periodFile = childElement.attribute("periodFile");
		mapping.period = childElement.attribute("period").toInt();
		mapping.defaultType = childElement.attribute("defaultType");
		mapping.invert = childElement.attribute("invert") == "true";

		mServoMotorMappings.insert(mapping.port, mapping);
	}
}

void Configurer::loadPwmCaptures(QDomElement const &root)
{
	if (root.elementsByTagName("pwmCaptures").isEmpty()) {
		qDebug() << "config.xml does not have <pwmCaptures> tag";
		throw "config.xml parsing failed";
	}

	QDomElement const pwmCaptures = root.elementsByTagName("pwmCaotures").at(0).toElement();
	for (QDomNode child = pwmCaptures.firstChild()
			; !child.isNull()
			; child = child.nextSibling())
	{
		if (!child.isElement()) {
			continue;
		}

		QDomElement const childElement = child.toElement();
		if (childElement.nodeName() != "capture") {
			qDebug() << "Malformed <pwmCaptures> tag";
			throw "config.xml parsing failed";
		}

		PwmCaptureMapping mapping;
		mapping.port = childElement.attribute("port");
		mapping.periodFile = childElement.attribute("periodFile");
		mapping.dutyFile = childElement.attribute("dutyFile");

		mPwmCaptureMappings.insert(mapping.port, mapping);
	}
}

void Configurer::loadPowerMotors(QDomElement const &root)
{
	if (root.elementsByTagName("powerMotors").isEmpty()) {<commit_msg>corrected a typo<commit_after>/* Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

#include "configurer.h"

#include <QtXml/QDomDocument>
#include <QtCore/QFile>
#include <QtCore/QDebug>

using namespace trikControl;

Configurer::Configurer()
	: mI2cDeviceId(0)
	, mLedOn(0)
	, mLedOff(0)
{
	QDomDocument config("config");

	QFile file("config.xml");
	if (!file.open(QIODevice::ReadOnly)) {
		qDebug() << "Failed to open config.xml for reading";
		throw "Failed to open config.xml for reading";
	} if (!config.setContent(&file)) {
		file.close();
		qDebug() << "config.xml parsing failed";
		throw "config.xml parsing failed";
	}

	file.close();

	QDomElement const root = config.documentElement();

	loadInit(root);
	loadServoMotors(root);
	loadPwmCaptures(root);
	loadPowerMotors(root);
	loadAnalogSensors(root);
	loadEncoders(root);
	loadSensors(root);
	loadMotorTypes(root);
	loadSensorTypes(root);
	loadSound(root);

	mAccelerometer = loadSensor3d(root, "accelerometer");
	mGyroscope = loadSensor3d(root, "gyroscope");

	loadI2c(root);
	loadLed(root);
}

QString Configurer::initScript() const
{
	return mInitScript;
}

QStringList Configurer::motorTypes() const
{
	return mMotorTypes.keys();
}

QStringList Configurer::sensorTypes() const
{
	return mSensorTypes.keys();
}

int Configurer::motorTypeMin(QString const &motorType) const
{
	return mMotorTypes[motorType].min;
}

int Configurer::motorTypeMax(QString const &motorType) const
{
	return mMotorTypes[motorType].max;
}

int Configurer::motorTypeZero(QString const &motorType) const
{
	return mMotorTypes[motorType].zero;
}

int Configurer::motorTypeStop(QString const &motorType) const
{
	return mMotorTypes[motorType].stop;
}

int Configurer::sensorTypeMin(QString const &sensorType) const
{
	return mSensorTypes[sensorType].min;
}

int Configurer::sensorTypeMax(QString const &sensorType) const
{
	return mSensorTypes[sensorType].max;
}

QStringList Configurer::servoMotorPorts() const
{
	return mServoMotorMappings.keys();
}

QString Configurer::servoMotorDeviceFile(QString const &port) const
{
	return mServoMotorMappings[port].deviceFile;
}

QString Configurer::servoMotorPeriodFile(const QString &port) const
{
	return mServoMotorMappings[port].periodFile;
}

int Configurer::servoMotorPeriod(const QString &port) const
{
	return mServoMotorMappings[port].period;
}

QString Configurer::servoMotorDefaultType(QString const &port) const
{
	return mServoMotorMappings[port].defaultType;
}

bool Configurer::servoMotorInvert(QString const &port) const
{
	return mServoMotorMappings[port].invert;
}

QStringList Configurer::pwmCapturePorts() const
{
	return mPwmCaptureMappings.keys();
}

QString Configurer::pwmCapturePeriodFile(const QString &port) const
{
	return mPwmCaptureMappings[port].periodFile;
}

QString Configurer::pwmCaptureDutyFile(const QString &port) const
{
	return mPwmCaptureMappings[port].dutyFile;
}

QStringList Configurer::powerMotorPorts() const
{
	return mPowerMotorMappings.keys();
}

int Configurer::powerMotorI2cCommandNumber(QString const &port) const
{
	return mPowerMotorMappings[port].i2cCommandNumber;
}

bool Configurer::powerMotorInvert(QString const &port) const
{
	return mPowerMotorMappings[port].invert;
}

QStringList Configurer::analogSensorPorts() const
{
	return mAnalogSensorMappings.keys();
}

int Configurer::analogSensorI2cCommandNumber(QString const &port) const
{
	return mAnalogSensorMappings[port].i2cCommandNumber;
}

QStringList Configurer::encoderPorts() const
{
	return mEncoderMappings.keys();
}

int Configurer::encoderI2cCommandNumber(QString const &port) const
{
	return mEncoderMappings[port].i2cCommandNumber;
}

QStringList Configurer::sensorPorts() const
{
	return mSensorMappings.keys();
}

QString Configurer::sensorDeviceFile(QString const &port) const
{
	return mSensorMappings[port].deviceFile;
}

QString Configurer::sensorDefaultType(QString const &port) const
{
	return mSensorMappings[port].defaultType;
}

QString Configurer::playSoundCommand() const
{
	return mPlaySoundCommand;
}

int Configurer::accelerometerMin() const
{
	return mAccelerometer.min;
}

int Configurer::accelerometerMax() const
{
	return mAccelerometer.max;
}

QString Configurer::accelerometerDeviceFile() const
{
	return mAccelerometer.deviceFile;
}

int Configurer::gyroscopeMin() const
{
	return mGyroscope.min;
}

int Configurer::gyroscopeMax() const
{
	return mGyroscope.max;
}

QString Configurer::gyroscopeDeviceFile() const
{
	return mGyroscope.deviceFile;
}

QString Configurer::i2cPath() const
{
	return mI2cPath;
}

int Configurer::i2cDeviceId() const
{
	return mI2cDeviceId;
}

QString Configurer::ledRedDeviceFile() const
{
	return mLedRedDeviceFile;
}

QString Configurer::ledGreenDeviceFile() const
{
	return mLedGreenDeviceFile;
}

int Configurer::ledOn() const
{
	return mLedOn;
}

int Configurer::ledOff() const
{
	return mLedOff;
}

void Configurer::loadInit(QDomElement const &root)
{
	if (root.elementsByTagName("initScript").isEmpty()) {
		qDebug() << "config.xml does not have <initScript> tag";
		throw "config.xml parsing failed";
	}

	QDomElement const init = root.elementsByTagName("initScript").at(0).toElement();
	mInitScript = init.text();
}

void Configurer::loadServoMotors(QDomElement const &root)
{
	if (root.elementsByTagName("servoMotors").isEmpty()) {
		qDebug() << "config.xml does not have <servoMotors> tag";
		throw "config.xml parsing failed";
	}

	QDomElement const servoMotors = root.elementsByTagName("servoMotors").at(0).toElement();
	for (QDomNode child = servoMotors.firstChild()
			; !child.isNull()
			; child = child.nextSibling())
	{
		if (!child.isElement()) {
			continue;
		}

		QDomElement const childElement = child.toElement();
		if (childElement.nodeName() != "motor") {
			qDebug() << "Malformed <servoMotors> tag";
			throw "config.xml parsing failed";
		}

		ServoMotorMapping mapping;
		mapping.port = childElement.attribute("port");
		mapping.deviceFile = childElement.attribute("deviceFile");
		mapping.periodFile = childElement.attribute("periodFile");
		mapping.period = childElement.attribute("period").toInt();
		mapping.defaultType = childElement.attribute("defaultType");
		mapping.invert = childElement.attribute("invert") == "true";

		mServoMotorMappings.insert(mapping.port, mapping);
	}
}

void Configurer::loadPwmCaptures(QDomElement const &root)
{
	if (root.elementsByTagName("pwmCaptures").isEmpty()) {
		qDebug() << "config.xml does not have <pwmCaptures> tag";
		throw "config.xml parsing failed";
	}

	QDomElement const pwmCaptures = root.elementsByTagName("pwmCaptures").at(0).toElement();
	for (QDomNode child = pwmCaptures.firstChild()
			; !child.isNull()
			; child = child.nextSibling())
	{
		if (!child.isElement()) {
			continue;
		}

		QDomElement const childElement = child.toElement();
		if (childElement.nodeName() != "capture") {
			qDebug() << "Malformed <pwmCaptures> tag";
			throw "config.xml parsing failed";
		}

		PwmCaptureMapping mapping;
		mapping.port = childElement.attribute("port");
		mapping.periodFile = childElement.attribute("periodFile");
		mapping.dutyFile = childElement.attribute("dutyFile");

		mPwmCaptureMappings.insert(mapping.port, mapping);
	}
}

void Configurer::loadPowerMotors(QDomElement const &root)
{
	if (root.elementsByTagName("powerMotors").isEmpty()) {
{'fraction_non_alphanumeric': 0.12160694896851248, 'fraction_numerical': 0.003576674969662132, 'mean_word_length': 6.179275561668959, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4914701', 'n_tokens_mistral': 5496, 'n_tokens_neox': 5300, 'n_words': 1086}