/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2010 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt

You may alternatively use this source under the terms of a specific version of
the OpenSpace3D Unrestricted License provided you have obtained such a license from
I-maginer.
-----------------------------------------------------------------------------
*/

/*! \class X10Device
  *  \brief Manage X10 CM11 device
  *  \
  *  \version 1.0
  *  \date may 2011
  */
#ifndef __X10_DEVICE_H__
#define __X10_DEVICE_H__

#include "x10_api.h"
#include "thread.h"


/*!
*/
class X10Command
{
public:
	
protected:
private:
  char houseCode;
  int unitCode;
  int command;
  int value;

public:
  /*!
  Constructor.
  */
  X10Command(int cmd, char hcode, int ucode = 0, int val = 0)
  {
    command = cmd;
    houseCode = hcode;
    unitCode = ucode;
    value = val;
  }

  int Command(){return command;}
  char HouseCode(){return houseCode;}
  int UnitCode(){return unitCode;}
  int Value(){return value;}
protected:
private:
};

/*!
*/
class X10Device : public openutils::Thread
{
public:
	
protected:
private:
  struct x10_status_t* m_pxif;
	bool m_bConnect;
  bool m_bRunning;
  openutils::Mutex m_mutex;
  std::list<X10Command> lCommand;

public:
  /*!
  Constructor.
  */
  X10Device(int port);

  /*!
  Destructor.
  */
  ~X10Device();

  /*!
  Tell if the device is connected
  */
  bool IsConnected();

  /*!
  send AllLightsOn command
  */
  void AllLightsOn(char houseCode);

  /*!
  send AllOff command
  */
  void AllOff(char houseCode);

  /*!
  send On command
  */
  void On(char houseCode, int unitCode);

  /*!
  send Off command
  */
  void Off(char houseCode, int unitCode);

  /*!
  send Dimm command
  */
  void Dim(char houseCode, int unitCode, int value);

  /*!
  send Bright command
  */
  void Bright(char houseCode, int unitCode, int value);

protected:
private:
  /*!
  Forbiden Constructor.
  */
  X10Device();
  void run();
};

#endif
