/*
  Juni 2025
  myMQTT_Data.h
  die MQTT Daten für den MQTT-Datenaustausch
  wir vom WiFi_Manager.h eingebunden
*/


// MQTT stuff Topic
const char* mqtt_main_Topic = "My_SmartHome/Wohnraum_1/#";           // alle Topic subscribe
const char* mqtt_LWT_Topic = "My_SmartHome/Wohnraum_1/Wifi_Status";  // Last Will & Testament if connection lost

const char* mqtt_TimeStamp_Topic = "My_SmartHome/Wohnraum_1/TimeStamp";
const char* mqtt_debug_Topic = "My_SmartHome/Wohnraum_1/Debug_Info";
const char* mqtt_DHT_Temp_Topic = "My_SmartHome/Wohnraum_1/HTU_Temperatur_°C";
const char* mqtt_DHT_Luft_Topic = "My_SmartHome/Wohnraum_1/HTU_Luftfeuchte_%rel";
const char* mqtt_LED1_Topic = "My_SmartHome/Wohnraum_1/LED1";

const char* mqtt_DeviceName_Topic = "My_SmartHome/Wohnraum_1/Device_Name";
const char* mqtt_DevicePlace_Topic = "My_SmartHome/Wohnraum_1/Device_Place";
const char* mqtt_DeviceSSID_Topic = "My_SmartHome/Wohnraum_1/Device_SSD";
const char* mqtt_DeviceIP_Topic = "My_SmartHome/Wohnraum_1/Device_ID";


const char* deviceName = "D1mini_HTU_MQTT";
const char* devicePlace = "HTL-Leonding";

uint32_t chipId = ESP.getChipId();
String clientId = "ESP-" + String(chipId) + "-" + String(random(0xffff), HEX);  // eine eindeutige einzigartige ID
String str_WiFi_IP = " -- ";

int qos = 1;         // MQTT Quality of Service
bool retain = true;  // sende diese Message if new Client


//============  Program Code Message income ===================
void callbackMessage(char* topic, byte* payload, unsigned int length) {
  String incommingMessage = "";
  for (int i = 0; i < length; i++) incommingMessage += (char)payload[i];
  //Serial.println("Message arrived \t[" + String(topic) + "]\t" + incommingMessage);   // jede abonnierte Message kommt hier herein

  // MESSAGE RECEPTION HERE 
  if (strcmp(topic, mqtt_DHT_Temp_Topic) == 0) {
    Serial.println("MQTT received: HTU-Temperatur = " + incommingMessage + " °C");
  }
  // hier die nächsten Abfragen auf Topics ....
}

//================ publising as string ====================
void publishMessage(const char* topic, String payload , boolean retained) {
  if (mqttClient.publish(topic, payload.c_str(), retained)) {
    //Serial.println("Message publised [" + String(topic) + "]: " + payload);
  }
}

