NetworkAgent::NetworkAgent( const std::string& serverIPAddress, const std::string& serverPortNumber, bool connectOnConstruction /* = true */ ) :
	m_serverIPAddress( serverIPAddress ),
	m_serverPortNumber( serverPortNumber )
{
	setNetworkAgentDefaults();

	if ( connectOnConstruction ) {

		establishConnectionToServer(); 
	}
}
/* This will process library data. It will return true, if nothing was processing -> the data are for the sketch, otherwise, it returns false, so the data are processed here
 * and nothing should be processed by the sketch.
 */
boolean RemoteHomeWifi::processCommonData() {
    if (stringComplete) {
        if (inputString.startsWith(" ")) {
            //wifi config
            createTableWithForm(WIFI_CONFIG, WIFI_CONFIG_ACTION);
            char* netName = getNetworkName();
            createTextBoxTableRow(CAPTION_WIFI_SSID, ACTION_S, netName, MAXSIZE_32);
            free(netName);
            createTextBoxTableRow(CAPTION_WIFI_PWD, ACTION_P, "", MAXSIZE_64);
            createTextBoxTableRow(CAPTION_WIFI_IP, ACTION_I, "", MAXSIZE_15);
            createSubmitButton();
            endTableWithForm();
        } else if (inputString.startsWith("ca")) {
            //device config
            createTableWithForm(DEVICE_CONFIG, DEVICE_CONFIG_ACTION);
            char* ip = readIpAddrFromEEPROM(EEPROM_POSITION_SERVER_IP);
            createTextBoxTableRow(CAPTION_SERVER_IP, ACTION_S, ip, MAXSIZE_15);
            free(ip);
            createTextBoxTableRow(CAPTION_SERVER_PORT, ACTION_P, readIntFromEEPROM(EEPROM_POSITION_SERVER_PORT), MAXSIZE_5);
            createTextBoxTableRow(CAPTION_PGM_PORT, ACTION_R, readIntFromEEPROM(EEPROM_POSITION_SERVER_PROGPORT), MAXSIZE_5);            
            createTextBoxTableRow(CAPTION_DEVICE_ID, ACTION_D, readByteFromEEPROM(EEPROM_POSITION_NODE_ID), MAXSIZE_4);            
            if (0 != fpAppendConfigTable) (*fpAppendConfigTable)();
            createSubmitButton();
            endTableWithForm();
        } else if (inputString.startsWith("cb")) {
            //sketch upload
            pageHeadString = F("<meta http-equiv='refresh' content=\"60;URL='/'\"/>");
            outputString = F("<p>Programming, the page is going to reload after 1 min.</p>");
            sendPageWithMenuAndHeaderResponse();
            delay(30);
            cleanVariablesAfterProcessing();
            if (establishConnectionToServer(true, EEPROM_POSITION_SERVER_IP, EEPROM_POSITION_SERVER_PROGPORT)) {
                delay(10);
                _ser.print((byte)1);
                if (!_ser.find("a")) {
                    setup();
                }               
            }
        } else if (inputString.startsWith("cc")) {
                //it is join network request cc?s=SSID&p=Password&i=192.168.1.30 HTTP/1.1
                skipInputToChar('=');
                String ssid = inputString.substring(0,inputString.indexOf('&'));
                skipInputToChar('=');
                String password = inputString.substring(0,inputString.indexOf('&'));
                skipInputToChar('=');
                String ip = inputString.substring(0,inputString.indexOf(' '));
                pageHeadString = F("<meta http-equiv='refresh' content=\"25;URL='");
                if (ip.length() != 0) {
                    pageHeadString += F("http://");
                    pageHeadString += ip;
                    pageHeadString += F("/");
                }
                pageHeadString += F("cd'\"/>");
                outputString = F("<p>Connecting, please wait, the result is going to be displayed within 25 seconds...</p>");
                sendPageWithMenuAndHeaderResponse();
                if (!joinNetwork(ssid, password, ip)) {
                    becomeAdHocNetwork();
                    listenOnPort();
                    connectedToWifi = false;
                }
                cleanVariablesAfterProcessing();
        } else if (inputString.startsWith("cd")) {
                if (waitToConnectToNetwork(1)) {
                    outputString = F("<p>Connected:<b>");
                    outputString += getIPAddress();
                    outputString += F("</b><BR>Please reserve the IP in your router.</p>");
                    sendPageWithMenuAndHeaderResponse();
                    delay(1000);
                    setSingleJoinNetwork();
                    outputString = "";
                } else {
                    outputString = F("<p>Not connected, please try again.</p>");
                }
        } else if (inputString.startsWith("ce")) {
            //it is configure device: ce?s=192.168.1.2&p=8080&r=8081&d=1&e=0 HTTP/1.1
            saveIpAddrToEEPROM(EEPROM_POSITION_SERVER_IP);
            saveIntToEEPROM(EEPROM_POSITION_SERVER_PORT);
            saveIntToEEPROM(EEPROM_POSITION_SERVER_PROGPORT);
            saveByteToEEPROM(EEPROM_POSITION_NODE_ID);
            nodeId = EEPROM.read(EEPROM_POSITION_NODE_ID);
            if (0 != fpSaveConfigValues) (*fpSaveConfigValues)();
            outputString = F("<p>Configured.</p>");
        } else {
            return true;
        }
        if (outputString.length()!=0) {
            sendPageWithMenuAndHeaderResponse();
        }
        cleanVariablesAfterProcessing();
        return false;                                
    }
    return true;
}
/* This will send the data, stored in the output string to the server. */
boolean RemoteHomeWifi::sendDataToServer() {
    return establishConnectionToServer(false, EEPROM_POSITION_SERVER_IP, EEPROM_POSITION_SERVER_PORT);
}