#include "subscriber.h"


void Subscriber::onSettingsChanged(QHostAddress _host)
{
    qDebug() << "Setting host:" << _host;
    if (_host != host()) {
        disconnect();
        setHost(_host);
    }
}

void Subscriber::onConnected()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope,"BAD Soft", "Bell");

    settings.beginGroup("Subscriber");

    qDebug() << "connected";
    subscribe(settings.value("topic",EXAMPLE_TOPIC).toString(), 0);

    settings.endGroup();
}

void Subscriber::onSubscribed(const QString& topic)
{
    qDebug() << "subscribed " << topic;
}

void Subscriber::onReceived(const QMQTT::Message& message)
{
    int bellNumber;
    QString header;
    QString msg;

    qDebug() << "publish received: " << QString::fromUtf8(message.payload());

    msg = QString::fromLatin1(message.payload());
    QStringList list1 = msg.split(QLatin1Char(':'));
    qDebug() << msg;
    qDebug() << "List Size: " << list1.size();
    if (list1.size() >= 2) {
        header = list1.at(0);
        qDebug() << "H: " << header;
        if (header == "BELL") {
            bellNumber = list1.at(1).toInt();
            emit packetArrived(bellNumber);
        }
    }
}
