
#define SER_INPUT_SIZE 70
char serBuffer[SER_INPUT_SIZE + 1];    // Serial Input-Buffer
int serBufferCount;    // Anzahl der eingelesenen Zeichen

void serialEvent() {
  char ch = Serial.read();
  serBuffer[serBufferCount] = ch;
  serBufferCount++;
  if (ch == 13 || ch == 10) { // ASCII code 13 = "CR",  10 = "LF"
    serBuffer[serBufferCount - 1] = '\0'; // string nullterminieren
#ifdef DEBUG_VERBOSE
    Serial.print("serial cmd: '");
    Serial.print(serBuffer);
    Serial.println("'");
#endif
    strlcpy(cmdPayload, serBuffer, 101);
    cmdInQueue = true;
    evalCmd();
    serBufferCount = 0;
  }
}



void evalCmd() {
  if (cmdInQueue) {

    //Serial.print("cmdPayload: ");
    //Serial.println(cmdPayload);

    if (strncmp(cmdPayload, "loadconf", 8) == 0) {
      loadConfig();
      loadConfig2();
      mqttPrepareConnection();
    }

    else if (strncmp(cmdPayload, "set ", 4) == 0) {
      char buf[81];
      char setconfCmd[16];
      char setconfPayload[62];

      int len = strlen(cmdPayload) - 4;
      for (int i = 0; i < len; i++) {
        if (i < 81) buf[i] = cmdPayload[i + 4];
      }
      if (len <= 81) buf[len] = '\0';
      else buf[81] = '\0';

#ifdef DEBUG_VERBOSE
      Serial.print("Buf: ");
      Serial.println(buf);
#endif

      bool cmdNoPayload = false;
      int setconfCmdLen = 0;
      for (int i = 0; i < len; i++) {
        //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
        if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
        else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
          cmdNoPayload = true;
          break;
        }
        setconfCmd[i] = buf[i];
        setconfCmdLen++;
      }
      setconfCmd[setconfCmdLen] = '\0';
      yield();

      if (setconfCmdLen == len) cmdNoPayload = true;

#ifdef DEBUG_VERBOSE
      Serial.print("setconfCmd: '");
      Serial.print(setconfCmd);
      Serial.print("'");
#endif

      if ( cmdNoPayload ) {
#ifdef DEBUG_VERBOSE
        Serial.println(", no payload, displaying current value");
#endif
        getConfig(setconfCmd);
      }
      else {
#ifdef DEBUG_VERBOSE
        Serial.println();
#endif
        int setconfPayloadLen = 0;
        for (int i = 0; i < len; i++) {
          char c = buf[i + setconfCmdLen + 1];
          if (c == 0 || c == 10 || c == 13) break; // if \0, LF (10) or CR (13) command parameter is finished
          setconfPayload[i] = c;
          setconfPayloadLen++;
        }
        setconfPayload[setconfPayloadLen] = '\0';

#ifdef DEBUG_VERBOSE
        Serial.print("setconfPayload: '");
        Serial.print(setconfPayload);
        Serial.println("'");
#endif
        setConfig(setconfCmd, setconfPayload);
      }
    }
    else if (strncmp(cmdPayload, "get ", 4) == 0) {
      char buf[81];
      char setconfCmd[16];
      char setconfPayload[62];

      int len = strlen(cmdPayload) - 4;
      for (int i = 0; i < len; i++) {
        if (i < 81) buf[i] = cmdPayload[i + 4];
      }
      if (len <= 81) buf[len] = '\0';
      else buf[81] = '\0';

#ifdef DEBUG_VERBOSE
      Serial.print("Buf: ");
      Serial.println(buf);
#endif

      bool cmdNoPayload = false;
      int setconfCmdLen = 0;
      for (int i = 0; i < len; i++) {
        //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
        if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
        else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
          cmdNoPayload = true;
          break;
        }
        setconfCmd[i] = buf[i];
        setconfCmdLen++;
      }
      setconfCmd[setconfCmdLen] = '\0';
      yield();
#ifdef DEBUG_VERBOSE
      Serial.print("setconfCmd: '");
      Serial.print(setconfCmd);
      Serial.println("'");
#endif
      getConfig(setconfCmd);
    }
    else if (strncmp(cmdPayload, "restart", 7) == 0) {
      Serial.print("restarting...");
      delay(100);
      ESP.restart();
    }
    else if (strncmp(cmdPayload, "save", 4) == 0) {
      saveConfig();
      yield();
      saveConfig2();
      yield();

      saveSetTemp();
      saveHeatingMode();
      
      //Serial.println("saved config to SPIFFS");
      sendStatus("saved config to SPIFFS");
      Serial.println("reloading config to check...");
      loadConfig();
      yield();
      loadConfig2();
      yield();
    }
    else if (strncmp(cmdPayload, "getconf", 7) == 0) {
      printConfig();
      printConfig2();
    }
    else if (strncmp(cmdPayload, "delconf", 7) == 0) {
      deleteConfig();
    }


    cmdInQueue = false;
  }
}

