#include <"gprs/GPRS_Shield_Arduino.h">
#include <EEPROM.h>
#include <"mq/TroykaMQ.h">

const int ELECTROCITY = 3;
const int VALVE = 4;
MQ5 mq5(A0);
GPRS gprs(Serial1);
unsigned long oldMethane = 0;
int delta = 0;
boolean isIncreasing = false;
boolean isCritical = false;
String command = "";
char message[200];
// номер, с которого пришло сообщение
char phone[16];
// дата отправки сообщения
char datetime[24];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("SERIAL INIT OK");
  mq5.calibrate();
  gprs.powerOn();
  Serial.print("MQ Resistance:");
  Serial.println(mq5.getRo());
  oldMethane = mq5.readMethane();
  pinMode(ELECTROCITY, OUTPUT);
  pinMode(VALVE, OUTPUT);
  if(gprs.init())
    Serial.println("GPRS INIT OK");
  else
    Serial.println("GPRS ERR");
  }
void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()){
    char ch = Serial.read();
    if(ch == '\n'){
      check_input(command);
      command = "";
    }else{
      command += ch;
    }
  }
  check_gas_level();
   if (gprs.ifSMSNow()) {
    // читаем его
    gprs.readSMS(message, phone, datetime);
     String m(message);
     String p(phone);
     Serial.println("Upcoming message from "+p+":"+m);
    check_input(m);
  }
}
void criticalLevel(){
  Serial.println("Critical gas level!"); 
  notifyAll("Critical gas level!");
  digitalWrite(ELECTROCITY, LOW);
  digitalWrite(VALVE, LOW);
}
void normalLevel(){
  Serial.println("Normal gas level");
  notifyAll("Normal gas level!");
  digitalWrite(ELECTROCITY, HIGH);
  digitalWrite(VALVE, HIGH);
}
void increase(){
  Serial.println("Gas level is increasing");
   notifyAll("Gas level is increasing");
}
void decrease(){
  Serial.println("Gas level is decreasing");
   notifyAll("Gas level is decreasing");
}
boolean sign(int x){
  return x == 0 ? 0 : x / abs(x);
}
void check_input(String cmd){
   Serial.println(cmd);
  if(cmd.startsWith("on")){
     digitalWrite(ELECTROCITY, HIGH);
  }else if(cmd.startsWith("off")){
     digitalWrite(ELECTROCITY, LOW); 
  }else if(cmd.startsWith("reset")){
    EEPROM[0] = 0;
    Serial.println("Reset...");
  }else if(cmd.startsWith("add")){
    String phone  = cmd.substring(4);
    
    int index = EEPROM[0] * 11 + 1;
    for(int i = 0;i < phone.length(); i++){
     
     EEPROM[i + index] = phone[i];
    
    }
     EEPROM[0] = EEPROM[0] + 1; 
  }
}
void check_gas_level(){
  unsigned long methane = mq5.readMethane();
  delta = methane - oldMethane;
  if(methane > 100 && !isCritical){
    isCritical = true;
    criticalLevel();
  }
  if(methane < 100 && isCritical){
    isCritical = false;
    normalLevel();
  }
  if(abs(delta) > 100){
    oldMethane = methane;
    if(isIncreasing == false && delta >= 0){
      isIncreasing = true;
      increase();
    }
    else if(isIncreasing && delta <= 0){
      isIncreasing = false;
      decrease();
    }
  }   
}

void notifyAll(String msg){
 for(int i = 0 ;i < EEPROM[0]; i++){
    String phone = "";
    for(int j = 0; j < 11; j++){
      phone +=  (char) EEPROM[i + 1 + j];
    } 
    Serial.print("Notifing ");
     Serial.print(phone[i]);
     Serial.print(" that " + msg);
    char phoneA[12];
    char sms[200] ;
    msg.toCharArray(sms,200);
    phone.toCharArray(phoneA, 12);
    gprs.sendSMS(phoneA, sms); 
 }
}
