/*
 * SmartHome server: Plug-in x10
 *
 * 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 x10interface.h
 * @brief Interfaces the CM12U controller via serial port
 * @author Daniel Pisi
 */

#ifndef X10INTERFACE_H
#define X10INTERFACE_H

#include <QObject>
#include <QDateTime>
#include <QQueue>
#include <QRegExp>
#include <QStringList>
#include <QTimer>
#include "qextserialport.h"
#include "x10packet.h"

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

/**
 * \class X10Interface
 * \brief Interfaces the CM12U controller via serial port
 */
class X10Interface : public QObject
{
Q_OBJECT

public:
    enum X10State
    {
        stReady = 0, stCmdSent, stChecksumOk
    };

    X10Interface(QString device, QObject *parent = 0);

    bool onOffDevices(QString devices, bool on);
    bool dimLights(QString devices, int dim);
    bool onOffAllLights(char house, bool on);

    void sendTime();
    X10State state() { return mState; };

signals:
    void transmissionDone();
    void transmissionFailed(QString reason);

private slots:
    void onReadyRead();
    void onTimeout();

private:
    void sendNextPacket();
    bool enqueueAddressing(QString devices);

    QextSerialPort *mX10Port;
    X10State mState;
    QQueue<X10PacketPtr> mPacketQueue;
    QTimer *mTimer;
};
// -----------------------------------------------------------------------------

#endif // X10INTERFACE_H
