/*
 * SmartHome server: Plug-in xbee
 *
 * Copyright (C) 2010 Daniel Pisi <dan.pisi@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 */

/**
 * @file shxbee.h
 * @brief Plug-in interfacing ZigBee network via Digi XBee2 module
 * @author Daniel Pisi
 */

#ifndef SHXBEE_H
#define SHXBEE_H

#include <QtCore/QCoreApplication>
#include <QtPlugin>
#include <QObject>
#include <QString>
#include <QMap>
#include "shobjecthelper.h"
#include "xbinterface.h"
#include "xbmessage.h"
#include "shbase.h"
#include "shpin.h"

// -----------------------------------------------------------------------------

/**
 * \class XbModuleData
 * \brief Stores XBee module's last input/output values
 */
class XbModuleData
{
public:
    XbModuleData();

    quint16 lastDigitalSample;
    quint16 inputs, outputs;
};
// -----------------------------------------------------------------------------

/**
 * \class ShXbee
 * \brief Main class of the xbee plug-in
 */
class ShXbee : public ShObjectHelper
{
    Q_OBJECT
    Q_INTERFACES(ShObject)

public:
    ShXbee(QObject *parent = 0);
    virtual ~ShXbee() {};
    virtual bool init(QString param);
    virtual void updatePins(const QList<ShPin> &pins);
    virtual void processMessage(ShMessagePtr message);

    void setInputMonitoring();

private slots:
    void processPacket(QByteArray data);
    void messageProcessed(int id);
    void messageError(int id, QString reason);
    void messageTimeout();

private:
    quint8 nextFrameId();
    bool   sendPacket(int id, ShMessagePtr message, XbPacket *packet);

    XbInterface *mInterface;
    quint8       mLastFrameId;
    QMap<int, XbMessagePtr>  mMsgPool;
    QMap<quint64, XbModuleData>  mModules;
};
// -----------------------------------------------------------------------------

#endif // SHXBEE_H
