CLProxyManager.h 3.95 KB
Newer Older
Nicolas Metts's avatar
Nicolas Metts committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#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_