
#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, "toggle ", 7) == 0) {
      char inValue[1];
      inValue[0] = (char)cmdPayload[7];
      int relnr = atoi(inValue);
      if (relnr > 0 && relnr <= RELAIS_COUNT) {
        lastSwitchSource[relnr] = 1;
        relaisToggle(relnr - 1);
      }
    }

    else if (strncmp(cmdPayload, "on ", 3) == 0) {
      char inValue[1];
      inValue[0] = (char)cmdPayload[3];
      int relnr = atoi(inValue);
      if (relnr > 0 && relnr <= RELAIS_COUNT) {
        lastSwitchSource[relnr] = 1;
        relaisOn(relnr - 1);
      }
    }

    else if (strncmp(cmdPayload, "off ", 4) == 0) {
      char inValue[1];
      inValue[0] = (char)cmdPayload[4];
      int relnr = atoi(inValue);
      if (relnr > 0 && relnr <= RELAIS_COUNT) {
        lastSwitchSource[relnr] = 1;
        relaisOff(relnr - 1);
      }
    }

    else if (strncmp(cmdPayload, "loadconf", 8) == 0) {
      loadConfig();
    }
    
    else if (strncmp(cmdPayload, "set ", 4) == 0) {
      char buf[80];
      char setconfCmd[16];
      char setconfPayload[62];

      int len = strlen(cmdPayload) - 4;
      for (int i = 0; i < len; i++) {
        buf[i] = cmdPayload[i + 4];
      }
      buf[len - 1] = '\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';
      //Serial.print("setconfCmdLen: ");
      //Serial.println(setconfCmdLen);

      //      Serial.print("setconf '");
      //      Serial.print(setconfCmd);

      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';
      //Serial.print("setconfPayloadLen: ");
      //Serial.println(setconfPayloadLen);

      //      Serial.print("' to value '");
      //      Serial.print(setconfPayload);
      //      Serial.println("'");

      setConfig(setconfCmd, setconfPayload);


      //
      //
      //
      //
      //
      //      if (strncmp(cmdPayload, "mqtthost ", 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("MQTT host: '");
      //        Serial.print(buf);
      //        Serial.println("' set");
      //      }
      //
      //
      //      else if (strncmp(cmdPayload, "set mqttport ", 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("MQTT port: '");
      //        Serial.print(buf);
      //        Serial.println("' set");
      //      }
      //      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...");
      ESP.restart();
    }
    else if (strncmp(cmdPayload, "save", 4) == 0) {
      saveConfig();
      Serial.println("saved config to SPIFFS");
      Serial.println("reloading config to check...");
      loadConfig();
    }
    else if (strncmp(cmdPayload, "getconf", 7) == 0) {
      printConfig();
    }



    cmdInQueue = false;
  }
}

