Skip to content
CLProxyManager.h 3.95 KiB
Newer Older
Nicolas Metts's avatar
Nicolas Metts committed
#ifndef _CLPROXYMANAGER_H_
#define _CLPROXYMANAGER_H_
/**
 * \ingroup ControllerService
 */
/**
 * @file
 * This file provides definitions for CL Proxy
 */

#include <LSFTypes.h>
#include <LampManager.h>
#include "CLControlPanel.h"
#include <Thread.h>
#include <Mutex.h>
#include <qcc/Socket.h>
#include <qcc/Debug.h>
#include <signal.h>

#include <string>
#include <map>
#include <list>
#include <algorithm>

#define DFLT_BUFFER_SIZE     1024
#define DFLT_LOCAL_PORT      27000
#define DFLT_REMOTE_PORT     27000
#define DFLT_LOCAL_IP        "0.0.0.0"
#define DFLT_REMOTE_IP       "192.168.20.115"

using namespace std;

namespace lsf {

typedef std::pair<LSFString, LampState> LampObject;

class CLProxyManager : public lsf::Thread {
public:
/**
 * CLProxyManager constructor
 */
    CLProxyManager();

/**
 * CLProxyManager destructor
 */
    ~CLProxyManager();

/**
 * The methods below are for setting up the CLProxyManager as a controller to AJ
 * After setting the LampManager, the controller will run in a separate thread
 * listening for commands and invoking those commands using the LampManager
 */

    QStatus setLampManager(LampManager* lampManager);

    void Run();

    void Stop();

    QStatus Start();

    void Join();

// Accessors
    bool getShowMenu();

// The methods below are for setting up the CLProxyManager as a notifier of AJ messages

    QStatus Notify(const void* buf, size_t len);

    QStatus SaveAndNotify(const LSFString& lampId, const LampState& lampState);
    QStatus DetailsNotify(const LSFString& lampId, const LampDetails& lampDetails);

    QStatus RetrieveLampState(const LSFString& lampId);
    QStatus RetrieveLampDetails(const LSFString& lampId);

    QStatus ConfigureNotifier();

    LampObject DecodeLamp(const LSFString& buffer);

    QStatus executeLampState(LampObject& lampObject);

// Notifications from the Control Panel

    void cpAnnounceHandler(qcc::String const& busName, unsigned short version,
                           unsigned short port, ajn::AboutObjectDescription& objectDescription,
                           ajn::AboutData& aboutData, ControlPanelDevice* device, qcc::String szArgsXMLarray);

    void cpPropertyValueChanged(ajn::services::ControlPanelDevice* device, ajn::services::Property* property);
    QStatus sendPlugStatusMsg(string szDevID, ajn::services::Property* property, bool &bOn);
    LampObject DecodePlug(const LSFString& buffer);
    QStatus executePlugState(LampObject& lampObject);

// Added for the functionality related to the Smart Plug (using the ControlPanel sample code)
	bool TransitionPlugStateOnOffField(const string& plugID, const bool& onOff);

// A function to read the configuration file

    bool readConfig(const char *szConfigFN);

private:

    void Init();

    bool bSendState;
    bool bSendDetail;
    bool bShowMenu;

    LampManager* lampManager; // = NULL;

    volatile sig_atomic_t isRunning; // = false;
    bool notifierConfigured; // = false;
    Mutex dbLock;
    PresetMap lampDb;

    SocketFd writeFd; // = INVALID_SOCKET_FD;
    SocketFd myFd; // = INVALID_SOCKET_FD;

    uint16_t localServerPort; // = DFLT_LOCAL_PORT;
    IPAddress localServerIP; // = IPAddress(DFLT_LOCAL_IP);

    // TODO: These should be configured at runtime using the ConfigureNotifier
    IPAddress remoteIP; // = IPAddress(DFLT_REMOTE_IP);
    uint16_t remotePort; // = DFLT_LOCAL_PORT;

// Added for the functionality related to the Smart Plug (using the ControlPanel sample code)
    CLControlPanel *opControlPanel;
    qcc::String makeOnOffXML(bool bState);
    void SendPlugsData();

// ID translators and mapping
    Mutex plugLock;
    map<string, qcc::String> translIDback; // Translates the Device ID to the internal Object ID
    map<qcc::String, string> translIDfwd;  // Translates the internal Object ID to Device ID
    map<string, qcc::String> mapID2Detail; // Maps the Device ID to the details XML
    map<string, bool> mapID2State;         // Maps the Device ID to the On/Off state

};

}

#endif // _CLPROXYMANAGER_H_