 /* Home Automation Script
    based on Webduino library
    Vasily Klenov, 2014 */
+
+   /* надо переписать пины на массивы
+      и digitalWrite делать только при изменении значения
+    */
 
 #include "SPI.h"
 #include "Ethernet.h"

#define PIN_6 46
#define PIN_7 47

int pin_1_value = RELAY_OFF;            
int pin_2_value = RELAY_OFF;           
int pin_3_value = RELAY_OFF;  
int pin_4_value = RELAY_OFF;  
int pin_5_value = RELAY_OFF; 
int pin_6_value = RELAY_OFF; 
int pin_7_value = RELAY_OFF; 

/* store the HTML in program memory using the P macro */
P(On) = "<status>ON</status>";
P(Off) = "<status>OFF</status>";

void httpNotFound(WebServer &server){
  P(failMsg) =

  { 
    boolean is_on = false;
    int pin_number;
    int pwm_value = -1;
    
    pin_number = atol(url_path[0]);
              
    switch (pin_number) {
      case PIN_1:
        if(pin_1_value == RELAY_ON)
        {is_on = true;}
        break;
      case PIN_2:
        if(pin_2_value == RELAY_ON)
        {is_on = true;}
        break;
       case PIN_3:
        if(pin_3_value == RELAY_ON)
        {is_on = true;}
        break;
       case PIN_4:
        if(pin_4_value == RELAY_ON)
        {is_on = true;}
        break;
      case PIN_5:
        if(pin_5_value == RELAY_ON)
        {is_on = true;}
        break;
      case PIN_6:
        if(pin_6_value == RELAY_ON)
        {is_on = true;}
        break;
      case PIN_7:
        if(pin_7_value == RELAY_ON)
        {is_on = true;}
        break;
      case PWM_PIN_1:
        pwm_value = pwm_pin_1_value;
        break;
    }
    
    if (pwm_value >= 0)
    {
      printPwmStatus( pwm_value );
    } else
    {
      if(is_on)
      { 
        server.printP( On );
      } else 
      {
        server.printP( Off );
      }
    }
   
  }
}
