#include "qpiconnection.h"


QPIConnection::QPIConnection(const QString & name): QObject(), PIConnection(Q2PIString(name)) {
	setObjectName(name);
	CONNECT2(void, const PIString &, const PIByteArray &, this, dataReceivedEvent, this, piDataRec);
	CONNECT2(void, const PIString &, const PIByteArray &, this, packetReceivedEvent, this, piPacketRec);
}


bool QPIConnection::loadFromCMFile(const QString & file) {
	QPIConfig f(file, QIODevice::ReadOnly);
	if (!f.isOpen()) return false;
	PIString cs = Q2PIString(QString::fromLatin1(f.getValue("config").toByteArray()));
	configureFromString(&cs, Q2PIString(f.getValue("name").toString()));
	return true;
}


void QPIConnection::piDataRec(const PIString & from, const PIByteArray & data) {
	// piCout << from << PICoutManipulators::Hex << data;
	QMetaObject::invokeMethod(this,
	                          "qDataReceivedEvent",
	                          Qt::QueuedConnection,
	                          Q_ARG(QString, PI2QString(from)),
	                          Q_ARG(QByteArray, PI2QByteArray(data)));
}


void QPIConnection::piPacketRec(const PIString & from, const PIByteArray & data) {
	QMetaObject::invokeMethod(this,
	                          "qPacketReceivedEvent",
	                          Qt::QueuedConnection,
	                          Q_ARG(QString, PI2QString(from)),
	                          Q_ARG(QByteArray, PI2QByteArray(data)));
}
