
#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] = '\0'; // string nullterminieren
    Serial.print("serial cmd: ");
    Serial.println(serBuffer);
    strlcpy(cmdPayload, serBuffer, 101);
    cmdInQueue = true;
    evalCmd();
    serBufferCount = 0;
  }
}



void evalCmd() {
  if (cmdInQueue) {

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

    if (strncmp(cmdPayload, "PC_POWER_H", 10) == 0 && strlen(cmdPayload) == 10) {
      PC_pwrSwitchHold();
    }
    else if (strncmp(cmdPayload, "PC_POWER", 8) == 0 && strlen(cmdPayload) == 8) {
      PC_pwrSwitchShort();
    }
    else if (strncmp(cmdPayload, "PC_RESET", 8) == 0 && strlen(cmdPayload) == 8) {
      PC_resSwitch();
    }
    else if (strncmp(cmdPayload, "PC_ON", 5) == 0 && strlen(cmdPayload) == 5) {
      PC_toOn();
    }
    else if (strncmp(cmdPayload, "PC_SLEEP", 10) == 0 && strlen(cmdPayload) == 10) {
      PC_toSleep();
    }

    else if (strncmp(cmdPayload, "loadconf", 8) == 0) {
      loadConfigWeb();
      loadConfigMqtt();
      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';

      //      Serial.print("Buf: ");
      //      Serial.println(buf);

      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
        setconfCmd[i] = buf[i];
        setconfCmdLen++;
      }
      setconfCmd[setconfCmdLen] = '\0';
      yield();

      int setconfPayloadLen = 0;
      for (int i = 0; i < len; i++) {
        if (buf[i + setconfCmdLen + 1] == 32 || buf[i + setconfCmdLen + 1] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
        setconfPayload[i] = buf[i + setconfCmdLen + 1];
        setconfPayloadLen++;
      }
      setconfPayload[setconfPayloadLen] = '\0';

      setConfig(setconfCmd, setconfPayload);

      //      else if (strncmp(cmdPayload, "set wifissid ", 13) == 0) {
      //        char buf[50];
      //        int len = strlen(cmdPayload) - 13;
      //        for (int i = 0; i < len; i++) {
      //          buf[i] = cmdPayload[i + 13];
      //        }
      //        buf[len - 1] = '\0';
      //        Serial.print("WiFi SSID: '");
      //        Serial.print(buf);
      //        Serial.println("' set");
      //      }
      //      else if (strncmp(cmdPayload, "set wifipass ", 13) == 0) {
      //        char buf[50];
      //        int len = strlen(cmdPayload) - 13;
      //        for (int i = 0; i < len; i++) {
      //          buf[i] = cmdPayload[i + 13];
      //        }
      //        buf[len - 1] = '\0';
      //        Serial.print("WiFi Password: '");
      //        Serial.print(buf);
      //        Serial.println("' set");
      //      }
    }
    else if (strncmp(cmdPayload, "restart", 7) == 0) {
      Serial.print("restarting...");
      delay(100);
      ESP.restart();
    }
    else if (strncmp(cmdPayload, "save", 4) == 0) {
      saveConfigWeb();
      yield();
      saveConfigMqtt();
      yield();
      Serial.println("saved config to SPIFFS");
      Serial.println("reloading config to check...");
      loadConfigWeb();
      yield();
      loadConfigMqtt();
      yield();
      //      loadConfigHw();
      //      yield();
    }
    else if (strncmp(cmdPayload, "getconf", 7) == 0) {
      printConfigWeb();
      printConfigMqtt();
    }
    else if (strncmp(cmdPayload, "delconf", 7) == 0) {
      deleteConfig();
    }


    cmdInQueue = false;
  }
}
