
#include "FC_InterfaceBoard.h"

#define CR "\r"
#define FIN "\n"
#define CRLF "\r\n"
#define RcvOK "OK\r"
#define RcvErr "ER\r"
#define MODE_DEBUG  false

String IdeSerial; // for 485_0

void setup() {
  // Serial setup (Debug)
  Serial.begin(9600);
  Serial.println("Start!");
  
  // modules setup (init.)
  Ethernet_setup();
  GPIO_setup();
  RS485_setup();


  // Timer set
  MsTimer2::set(1, timer_1ms);
  MsTimer2::start();
}

void loop() {
  
  // wait for a new client:
  //webReponse();
  client = server.available();

  if (client) {
    String Buf_eth;
    String message;
    String cmd;
    String cmdData;
    unsigned int data[32];        
    int dataSize;

    // check if client is connected
    Serial.println("Client Connected!!!");

    // Do What Message Command
    while(client.connected()){

      // read data check
      if(client.available() > 0) {
        char c ;//= client.read();

        while((c = client.read())) {

          if((c == '\n') && (Buff_Eth_Rd[strlen(Buff_Eth_Rd)-1] == '\r')){
            write_buff_c(Buff_Eth_Rd, c);
            break;
          }else{
            write_buff_c(Buff_Eth_Rd, c);
          }
          
        }
        // 데이터를 수신했으므로 타임아웃 타이머 초기화
        lastDataReceivedTime = millis();
      }

      message = read_buff(Buff_Eth_Rd);
      // If data read
      if(message != ""){
        cmd = demuxCMD(message, &cmdData);
        //Serial.println(">> " + message);
        message = "";
        
        //Serial.println(cmd);
        if(cmd=="ALL?"){
          client.print(Prcss_ALL_Read());
        }
        // GPIO
        else if(cmd=="DO!"){
          dataSize = demuxNum(cmdData, data);
          client.print(Prcss_DO_Write(data, dataSize));

        }else if(cmd=="AO!"){
          dataSize = demuxNum(cmdData, data);
          client.print(Prcss_AO_Write(data, dataSize));
        }
        // MC9
        else if(cmd=="SV_1!"){
          dataSize = demuxNum(cmdData, data);
          client.print(Prcss_SV_Write(data, dataSize));

        }else if(cmd=="AT_1!"){
          dataSize = demuxNum(cmdData, data);
          client.print(Prcss_AT_Write(data, dataSize));

        }
        // Init
        else if(cmd=="State?"){
          client.print(cmd + " " + RcvOK + FIN);

        }else if(cmd=="RngAO!"){
          dataSize = demuxNum(cmdData, data);
          client.print(Prcss_RngAO(data, dataSize));

        }else if(cmd=="ChMC9_1!"){
          dataSize = demuxNum(cmdData, data);
          client.print(Prcss_ChMC9(data, dataSize));

        }

        // Extra
        else{
          client.print(cmd + " " + RcvErr + FIN);
        }
        message = "";
      }
      
      // Debug
      if(MODE_DEBUG){
        //Serial.print("Received command: ");
        //Serial.println(command);
      }

      // check Timeout
      if (millis() - lastDataReceivedTime > timeoutPeriod) {
        Serial.println("Client Disconnected... (Timeout)");
        client.stop();
      }
      Periodic_run();
    }
    Serial.println("Client Disconnected...");
  }
  Periodic_run();
}





void AnalogIn_Print(){
  for (int i = 0; i < Size_AI; i++) {
    Serial.print(Values_AI[i]);
    Serial.print("\t");
  }
  Serial.println();
}



