/*
 * TODO:
 * bestehende steuerung aus espcontroller übernehmen und testen
 * topics und handler implementieren
 */

#include "Ethernet.h"
#include "PubSubClient.h"

String ip = "";
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x06};

#define CLIENT_ID       "Hal"
EthernetClient ethClient;
PubSubClient mqttClient;

#define PUBLISH_DELAY 10000
long last_send=0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN,HIGH);

  // setup serial communication
  Serial.begin(9600);
  while (!Serial) {};
  Serial.println(F("MQTT Arduino Demo"));

  // setup ethernet communication using DHCP
  if (Ethernet.begin(mac) == 0) {
    //Serial.println(F("Unable to configure Ethernet using DHCP"));
    for (;;);
  }

  Serial.println(F("Ethernet configured via DHCP"));
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
  Serial.println();

  ip = String (Ethernet.localIP()[0]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[1]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[2]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[3]);
  //Serial.println(ip);

  // setup mqtt client
  mqttClient.setClient(ethClient);
  mqttClient.setServer("10.0.0.1", 1883);
  Serial.println(F("MQTT client configured"));
  mqttClient.setCallback(callback);
  

  Serial.println();
  Serial.println(F("Ready to send data"));
  last_send = millis();
  


}

void reconnect() {
  // Loop until we're reconnected
  while (!mqttClient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttClient.connect(CLIENT_ID)) {
      Serial.println("connected");
      mqttClient.publish("audiomixer/ip", ip.c_str()); //Publish own ip
      mqttClient.subscribe("audiomixer/main/volume/set"); //subscribe to /set, republish without /set
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
  if (!mqttClient.connected()) {
    reconnect();
  }
  
  if (millis() - last_send > PUBLISH_DELAY) {
    //sendData();
    last_send = millis();
  }

  mqttClient.loop();
}

void sendData() {
  char msgBuffer[20];
  float h = 50;
  float testvalue = 42;
  /*
  Serial.print("testvalue: ");
  Serial.print(testvalue);
  Serial.println();
  
  if (mqttClient.connect(CLIENT_ID)) {
    mqttClient.publish("audiomixer/messwert/parameter", dtostrf(testvalue, 6, 2, msgBuffer));
    //mqttClient.publish(DEVICENAME+"/br/nb/deur", (statusBD == HIGH) ? "OPEN" : "CLOSED");
  }*/
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");//MQTT_BROKER
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  

 
  //if (strncmp((const char*)payload, "ON", 2) == 0) {
  //}
  
  if (strncmp((const char*)topic, "audiomixer/main/volume/set",sizeof(topic)) == 0) {
    //Serial.println("republish");
    mqttClient.publish("audiomixer/main/volume", payload, length );
  }
  
  
}
