#ifndef SUBSCRIBER_H
#define SUBSCRIBER_H


#include <qmqtt.h>
#include <QTimer>
#include <QSettings>

const QHostAddress EXAMPLE_HOST = QHostAddress("172.16.1.254");
const quint16 EXAMPLE_PORT = 1883;
const QString EXAMPLE_TOPIC = "home/bell/ring";

class Subscriber : public QMQTT::Client
{
    Q_OBJECT
public:
    explicit Subscriber(const QHostAddress& host = EXAMPLE_HOST,
                        const quint16 port = EXAMPLE_PORT,
                        QObject* parent = NULL)
        : QMQTT::Client(host, port, parent)
    {
        QSettings settings(QSettings::IniFormat, QSettings::UserScope,"BAD Soft", "Bell");

        settings.beginGroup("Subscriber");
        setHost(QHostAddress(settings.value("address", EXAMPLE_HOST.toString()).toString()));
        setPort(settings.value("port", EXAMPLE_PORT).toString().toInt());
        settings.endGroup();

        connect(this, &Subscriber::connected, this, &Subscriber::onConnected);
        connect(this, &Subscriber::subscribed, this, &Subscriber::onSubscribed);
        connect(this, &Subscriber::received, this, &Subscriber::onReceived);
    }
    virtual ~Subscriber() {}

public slots:
    void onConnected();
    void onSubscribed(const QString& topic);
    void onReceived(const QMQTT::Message& message);
    void onSettingsChanged(QHostAddress address);

signals:
    void addressChanged(QHostAddress address);
    void packetArrived(int bellNumber);

};

#endif
