/*
 * SmartHome server: Base of the system
 *
 * 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 shobjecthelper.cpp
 * @brief Implements basic plug-in methods
 * @author Daniel Pisi
 */

#include "shobjecthelper.h"

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

/**
 * \brief Constructor of ShObjectHelper, the plug-in template class
 * \param parent Pointer to the parent object
 */
ShObjectHelper::ShObjectHelper(QObject *parent) : QObject(parent)
{
    mId    = 0;
    mIdApi = false;
}
// -----------------------------------------------------------------------------

/**
 * \brief Fills in the sender and dispatches the message to ShBase for routing
 * \param message Shared pointer to the message
 */
void ShObjectHelper::dispatchMessage(ShMessagePtr message)
{
    message->setSenderId(mId);
    emit newMessage(message);
}
// -----------------------------------------------------------------------------

/**
 * \brief Convenient method for messages acknowledging
 * \param message Shared pointer to the message to acknowledge
 */
void ShObjectHelper::ackMessage(ShMessagePtr message)
{
    ShMessagePtr shMsg = ShMessagePtr(new ShMessage);

    shMsg->setMsgType(ShMessage::mtAck);
    shMsg->setSignal(message->signal());
    shMsg->setValue(message->value());
    shMsg->setPin(message->pin());

    dispatchMessage(shMsg);
}
// -----------------------------------------------------------------------------

/**
 * \brief Convenient method for dispatching error messages
 * \param message Shared pointer to the message
 * \param reason Error string
 */
void ShObjectHelper::errorMessage(ShMessagePtr message, QString reason)
{
    ShMessagePtr shMsg = ShMessagePtr(new ShMessage);

    shMsg->setMsgType(ShMessage::mtError);
    shMsg->setSignal(message->signal());
    shMsg->setValue(reason);
    shMsg->setPin(message->pin());

    dispatchMessage(shMsg);
}
// -----------------------------------------------------------------------------

/**
 * \brief Convenient method for dispatching messages informing about output update
 * \param signal Name of a signal that has been updated
 * \param value New value of the signal
 */
void ShObjectHelper::outputUpdated(QString signal, QVariant value)
{
    ShMessagePtr message = ShMessagePtr(new ShMessage);

    message->setMsgType(ShMessage::mtValue);
    message->setSignal(signal);
    message->setValue(value);

    dispatchMessage(message);
}
// -----------------------------------------------------------------------------
