-/* Home Automation Script
-   based on Webduino library
-   Vasily Klenov, 2014 */
+/* Arduino Relays Unit Web Server
+   This script is a part of my home automation setup 
+   Based on Webduino library https://github.com/sirleech/Webduino
+   Vasily Klenov, 2014
+
+   I use this script with Arduino Mega 2560 connected with Ethernet Shield
+
+  https://github.com/sirleech/Webduino/blob/master/examples/Web_Parms/Web_Parms.ino
+    */
 
 #include "SPI.h"
 #include "Ethernet.h"

  webserver.processConnection();
}

int find_index(int array[], int num_elements, int value)
{
   for (int i=0; i<num_elements; i++)
   {
     if (array[i] == value)
     {
        return(i); 
     }


#define DEBUG_RELAYS_UNIT 0

#define VERSION_STRING "0.0.2"

#define CRLF "\r\n"

#define SIZE(array) (sizeof(array) / sizeof(*array))

// CHANGE THIS TO YOUR OWN UNIQUE VALUE
static uint8_t mac[6] = { 0xA9, 0x10, 0x14, 0xE7, 0xAC, 0xCC };  

// CHANGE THIS TO MATCH YOUR HOST NETWORK
static uint8_t ip[4] = { 192, 168, 2, 15 };

#define PREFIX ""
WebServer webserver(PREFIX, 80);

#define RELAY_ON  0
#define RELAY_OFF 1

//const int relays_count = 7;
const int relays[]        = { 40, 42, 43, 44, 45, 46, 47 };
int relays_states[] = { RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF };

char* pin_number_as_char = "XX";

/* store the HTML in program memory using the P macro */
P(On)      = "<status>ON</status>";
P(Off)     = "<status>OFF</status>";
P(Hello) = 
    "<html>"
    "<head>"
    "<title>Arduino Web Server</title>"
    "</head>"
    "<body>"
    "<h1>Home Automation Web Server</h1>"
    "based on <a href=\"https://github.com/sirleech/Webduino\">Webduino library</a><br/>"
    "Vasily Klenov, 2014<br/>"
    "<b>version</b>"
    VERSION_STRING
    "</body></html>";

void httpNotFound(WebServer &server){
  P(failMsg) =
    "HTTP/1.0 404 Bad Request" CRLF
    "Content-Type: text/html" CRLF
    CRLF
    "<h2>File Not Found</h2>";
  server.printP(failMsg);
}  

void helloCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
  server.httpSuccess();

  /* if we're handling a GET or POST, we can output our data here.
     For a HEAD request, we just stop after outputting headers. */
  if (type != WebServer::HEAD)
  {
    server.printP(Hello);
  }
}

/* It handles both GET and PUT requests.  For a GET, it returns a pin
 * value.  For a PUT it set the pin value. */
void pinsCmd(WebServer &server, WebServer::ConnectionType type, char **url_path, char *url_tail, bool tail_complete)
{

  if(strncmp(url_path[0],"pins",5)!=0){
    httpNotFound(server);
    return;
  }

  char* pin_param = url_path[1];
  int relay_index = find_index(pin_param);

  if (type == WebServer::PUT)
  {
    #ifdef DEBUG_RELAYS_UNIT
      Serial.println("Processing PUT query...");
    #endif

    int new_state = RELAY_OFF;

    if ( relay_index >= 0 )
    {
      server.httpSuccess();
      /* for url /pins/40/ON url_path[2] will contain 'ON' 
         and  url_path[1] will contain '40' */
      if(strcmp(url_path[2], "ON") == 0){ // поменять на strncmp ?????
        new_state = RELAY_ON;
      }

      if ( relays_states[relay_index] != new_state )
      {
        digitalWrite(relays[relay_index], new_state);
        relays_states[relay_index] = new_state;
      }

      return;
    } else
    {
      httpNotFound(server);
      return;
    }
  }
