* Rationalisation of logs and removal of duplicate codes
This commit is contained in:
1 parent
10f0d4cff4
commit
bdda5df839
5 files changed
+132
-116
No files matched your search
@@ -101,6 +101,7 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) {
|
||||
if (data.containsKey("servicedata"))
|
||||
data.remove("servicedata");
|
||||
String topic = String(Base_Topic) + "home_presence/" + String(gateway_name);
|
||||
Log.trace(F("Pub HA Presence %s" CR), topic.c_str());
|
||||
pub_custom_topic((char*)topic.c_str(), data, false);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-22
@@ -73,7 +73,6 @@ void createDiscoveryFromList(const char* mac,
|
||||
const char* device_manufacturer,
|
||||
const char* device_model) {
|
||||
for (int i = 0; i < sensorCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR), sensorList[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + sensorList[i][1];
|
||||
|
||||
@@ -151,7 +150,6 @@ void announceDeviceTrigger(bool use_gateway_info,
|
||||
if (use_gateway_info) {
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
serializeJson(modules, JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
Log.notice(F("Received json : %s" CR), JSONmessageBuffer);
|
||||
|
||||
device["name"] = gateway_name;
|
||||
device["model"] = JSONmessageBuffer;
|
||||
@@ -195,6 +193,7 @@ void announceDeviceTrigger(bool use_gateway_info,
|
||||
|
||||
/* Publish on the topic */
|
||||
String topic_to_publish = String(discovery_Topic) + "/device_automation/" + String(unique_id) + "/config";
|
||||
Log.trace(F("Announce Device Trigger %s" CR), topic_to_publish.c_str());
|
||||
pub_custom_topic((char*)topic_to_publish.c_str(), sensor, true);
|
||||
}
|
||||
|
||||
@@ -292,7 +291,6 @@ void createDiscovery(const char* sensor_type,
|
||||
JsonObject device = jsonDeviceBuffer.to<JsonObject>();
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
serializeJson(modules, JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
Log.notice(F("Received json : %s" CR), JSONmessageBuffer);
|
||||
device["name"] = gateway_name;
|
||||
device["model"] = JSONmessageBuffer;
|
||||
device["manufacturer"] = DEVICEMANUFACTURER;
|
||||
@@ -327,6 +325,7 @@ void createDiscovery(const char* sensor_type,
|
||||
sensor["device"] = device; //device representing the actual sensor/switch device
|
||||
}
|
||||
String topic = String(discovery_Topic) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config";
|
||||
Log.trace(F("Announce Device %s on %s" CR), String(sensor_type).c_str(), topic.c_str());
|
||||
pub_custom_topic((char*)topic.c_str(), sensor, true);
|
||||
}
|
||||
|
||||
@@ -501,7 +500,6 @@ void pubMqttDiscovery() {
|
||||
};
|
||||
|
||||
for (int i = 0; i < BMEparametersCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(BMEsensor[i][1]);
|
||||
createDiscovery(BMEsensor[i][0],
|
||||
BMETOPIC, BMEsensor[i][1], (char*)getUniqueId(BMEsensor[i][1], BMEsensor[i][2]).c_str(),
|
||||
@@ -524,7 +522,6 @@ void pubMqttDiscovery() {
|
||||
};
|
||||
|
||||
for (int i = 0; i < HTUparametersCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(HTUsensor[i][1]);
|
||||
createDiscovery(HTUsensor[i][0],
|
||||
HTUTOPIC, HTUsensor[i][1], (char*)getUniqueId(HTUsensor[i][1], HTUsensor[i][2]).c_str(),
|
||||
@@ -547,7 +544,6 @@ void pubMqttDiscovery() {
|
||||
};
|
||||
|
||||
for (int i = 0; i < AHTparametersCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
createDiscovery(AHTsensor[i][0],
|
||||
AHTTOPIC, AHTsensor[i][1], (char*)getUniqueId(AHTsensor[i][1], AHTsensor[i][2]).c_str(),
|
||||
will_Topic, AHTsensor[i][3], AHTsensor[i][4],
|
||||
@@ -569,7 +565,6 @@ void pubMqttDiscovery() {
|
||||
};
|
||||
|
||||
for (int i = 0; i < DHTparametersCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(DHTsensor[i][1]);
|
||||
createDiscovery(DHTsensor[i][0],
|
||||
DHTTOPIC, DHTsensor[i][1], (char*)getUniqueId(DHTsensor[i][1], DHTsensor[i][2]).c_str(),
|
||||
@@ -587,7 +582,6 @@ void pubMqttDiscovery() {
|
||||
char* ADCsensor[8] = {"sensor", "adc", "", "", jsonAdc, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(ADCsensor[1]);
|
||||
createDiscovery(ADCsensor[0],
|
||||
ADCTOPIC, ADCsensor[1], (char*)getUniqueId(ADCsensor[1], ADCsensor[2]).c_str(),
|
||||
@@ -610,7 +604,6 @@ void pubMqttDiscovery() {
|
||||
};
|
||||
|
||||
for (int i = 0; i < BH1750parametersCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(BH1750sensor[i][1]);
|
||||
createDiscovery(BH1750sensor[i][0],
|
||||
subjectBH1750toMQTT, BH1750sensor[i][1], (char*)getUniqueId(BH1750sensor[i][1], BH1750sensor[i][2]).c_str(),
|
||||
@@ -634,7 +627,6 @@ void pubMqttDiscovery() {
|
||||
};
|
||||
|
||||
for (int i = 0; i < TSL2561parametersCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(TSL2561sensor[i][1]);
|
||||
createDiscovery(TSL2561sensor[i][0],
|
||||
subjectTSL12561toMQTT, TSL2561sensor[i][1], (char*)getUniqueId(TSL2561sensor[i][1], TSL2561sensor[i][2]).c_str(),
|
||||
@@ -652,7 +644,6 @@ void pubMqttDiscovery() {
|
||||
char* HCSR501sensor[8] = {"binary_sensor", "hcsr501", "", "", jsonPresence, "true", "false", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(HCSR501sensor[1]);
|
||||
createDiscovery(HCSR501sensor[0],
|
||||
subjectHCSR501toMQTT, HCSR501sensor[1], (char*)getUniqueId(HCSR501sensor[1], HCSR501sensor[2]).c_str(),
|
||||
@@ -669,7 +660,6 @@ void pubMqttDiscovery() {
|
||||
char* GPIOInputsensor[8] = {"binary_sensor", "GPIOInput", "", "", jsonGpio, INPUT_GPIO_ON_VALUE, INPUT_GPIO_OFF_VALUE, ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(GPIOInputsensor[1]);
|
||||
createDiscovery(GPIOInputsensor[0],
|
||||
subjectGPIOInputtoMQTT, GPIOInputsensor[1], (char*)getUniqueId(GPIOInputsensor[1], GPIOInputsensor[2]).c_str(),
|
||||
@@ -692,7 +682,6 @@ void pubMqttDiscovery() {
|
||||
};
|
||||
|
||||
for (int i = 0; i < INA226parametersCount; i++) {
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(INA226sensor[i][1]);
|
||||
createDiscovery(INA226sensor[i][0],
|
||||
subjectINA226toMQTT, INA226sensor[i][1], (char*)getUniqueId(INA226sensor[i][1], INA226sensor[i][2]).c_str(),
|
||||
@@ -715,7 +704,6 @@ void pubMqttDiscovery() {
|
||||
char* actuatorONOFF[8] = {"switch", "actuatorONOFF", "", "", "", "{\"cmd\":1}", "{\"cmd\":0}", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(actuatorONOFF[1]);
|
||||
createDiscovery(actuatorONOFF[0],
|
||||
subjectGTWONOFFtoMQTT, actuatorONOFF[1], (char*)getUniqueId(actuatorONOFF[1], actuatorONOFF[2]).c_str(),
|
||||
@@ -733,7 +721,6 @@ void pubMqttDiscovery() {
|
||||
char* gatewayRF[8] = {"sensor", "gatewayRF", "", "", jsonVal, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayRF[1]);
|
||||
createDiscovery(gatewayRF[0],
|
||||
subjectRFtoMQTT, gatewayRF[1], (char*)getUniqueId(gatewayRF[1], gatewayRF[2]).c_str(),
|
||||
@@ -752,7 +739,6 @@ void pubMqttDiscovery() {
|
||||
char* gatewayRF2[8] = {"sensor", "gatewayRF2", "", "", jsonAddress, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayRF2[1]);
|
||||
createDiscovery(gatewayRF2[0],
|
||||
subjectRF2toMQTT, gatewayRF2[1], (char*)getUniqueId(gatewayRF2[1], gatewayRF2[2]).c_str(),
|
||||
@@ -770,7 +756,6 @@ void pubMqttDiscovery() {
|
||||
char* gatewayRFM69[8] = {"sensor", "gatewayRFM69", "", "", jsonVal, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayRFM69[1]);
|
||||
createDiscovery(gatewayRFM69[0],
|
||||
subjectRFM69toMQTT, gatewayRFM69[1], (char*)getUniqueId(gatewayRFM69[1], gatewayRFM69[2]).c_str(),
|
||||
@@ -788,7 +773,6 @@ void pubMqttDiscovery() {
|
||||
char* gatewayLORA[8] = {"sensor", "gatewayLORA", "", "", jsonMsg, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayLORA[1]);
|
||||
createDiscovery(gatewayLORA[0],
|
||||
subjectLORAtoMQTT, gatewayLORA[1], (char*)getUniqueId(gatewayLORA[1], gatewayLORA[2]).c_str(),
|
||||
@@ -806,7 +790,6 @@ void pubMqttDiscovery() {
|
||||
char* gatewaySRFB[8] = {"sensor", "gatewaySRFB", "", "", jsonVal, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewaySRFB[1]);
|
||||
createDiscovery(gatewaySRFB[0],
|
||||
subjectSRFBtoMQTT, gatewaySRFB[1], (char*)getUniqueId(gatewaySRFB[1], gatewaySRFB[2]).c_str(),
|
||||
@@ -824,7 +807,6 @@ void pubMqttDiscovery() {
|
||||
char* gatewayPilight[8] = {"sensor", "gatewayPilight", "", "", jsonMsg, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayPilight[1]);
|
||||
createDiscovery(gatewayPilight[0],
|
||||
subjectPilighttoMQTT, gatewayPilight[1], (char*)getUniqueId(gatewayPilight[1], gatewayPilight[2]).c_str(),
|
||||
@@ -842,7 +824,6 @@ void pubMqttDiscovery() {
|
||||
char* gatewayIR[8] = {"sensor", "gatewayIR", "", "", jsonVal, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayIR[1]);
|
||||
createDiscovery(gatewayIR[0],
|
||||
subjectIRtoMQTT, gatewayIR[1], (char*)getUniqueId(gatewayIR[1], gatewayIR[2]).c_str(),
|
||||
@@ -860,7 +841,6 @@ void pubMqttDiscovery() {
|
||||
char* gateway2G[8] = {"sensor", "gateway2G", "", "", jsonMsg, "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gateway2G[1]);
|
||||
createDiscovery(gateway2G[0],
|
||||
subject2GtoMQTT, gateway2G[1], (char*)getUniqueId(gateway2G[1], gateway2G[2]).c_str(),
|
||||
|
||||
@@ -102,7 +102,6 @@ void pubOneWire_HADiscovery() {
|
||||
# ifdef ZmqttDiscovery
|
||||
// If zmqtt discovery is enabled, create a sensor topic for each DS18b20 sensor found on the bus, using addr as uniqueID
|
||||
if (disc) {
|
||||
Log.notice(F("CreateDiscoverySensor - Found %d" CR), ds1820_count);
|
||||
for (int index = 0; index < ds1820_count; index++) {
|
||||
createDiscovery("sensor",
|
||||
(char*)(String(OW_TOPIC) + "/" + ds1820_addr[index]).c_str(),
|
||||
|
||||
@@ -29,8 +29,31 @@
|
||||
|
||||
extern String getUniqueId(String name, String sufix);
|
||||
extern void pubMqttDiscovery();
|
||||
extern void createDiscoveryFromList(const char* mac, const char* sensorList[][8], int sensorCount,
|
||||
const char* device_name, const char* device_manufacturer, const char* device_model);
|
||||
|
||||
/**
|
||||
* Create a discover messages form a list of attribute
|
||||
*
|
||||
* @param mac the mac adres
|
||||
* @param sensorList[][0] = component type
|
||||
* @param sensorList[][1] = name
|
||||
* @param sensorList[][2] = availability topic
|
||||
* @param sensorList[][3] = device class
|
||||
* @param sensorList[][4] = value template
|
||||
* @param sensorList[][5] = payload on
|
||||
* @param sensorList[][6] = payload off
|
||||
* @param sensorList[][7] = unit of measurement
|
||||
* @param sensorList[][8] = unit of measurement
|
||||
* @param sensorCount number of sensor
|
||||
* @param device_name name of sensors
|
||||
* @param device_manufacturer name of manufacturer
|
||||
* @param device_model the model
|
||||
* */
|
||||
extern void createDiscoveryFromList(const char* mac,
|
||||
const char* sensorList[][9],
|
||||
int sensorCount,
|
||||
const char* device_name,
|
||||
const char* device_manufacturer,
|
||||
const char* device_model);
|
||||
|
||||
/**
|
||||
* @brief Generate message and publish it on an mqtt discovery exploiter. For HA @see https://www.home-assistant.io/docs/mqtt/discovery/
|
||||
|
||||
+104
-91
@@ -286,190 +286,200 @@ bool to_bool(String const& s) { // thanks Chris Jester-Young from stackoverflow
|
||||
return s != "0";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Publish the payload on default MQTT topic.
|
||||
*
|
||||
* @param topicori suffix to add on default MQTT Topic
|
||||
* @param payload the message to sends
|
||||
* @param retainFlag true if you what a retain
|
||||
*/
|
||||
void pub(const char* topicori, const char* payload, bool retainFlag) {
|
||||
String topic = String(mqtt_topic) + String(gateway_name) + String(topicori);
|
||||
pubMQTT(topic.c_str(), payload, retainFlag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Publish the payload on default MQTT topic
|
||||
*
|
||||
* @param topicori suffix to add on default MQTT Topic
|
||||
* @param data The Json Object that rapresent the message
|
||||
*/
|
||||
void pub(const char* topicori, JsonObject& data) {
|
||||
Log.notice(F("Subject: %s" CR), topicori);
|
||||
String dataAsString = "";
|
||||
serializeJson(data, dataAsString);
|
||||
Log.notice(F("Send on %s msg %s" CR), topicori, dataAsString.c_str());
|
||||
digitalWrite(LED_SEND_RECEIVE, LED_SEND_RECEIVE_ON);
|
||||
logJson(data);
|
||||
if (client.connected()) {
|
||||
String topic = String(mqtt_topic) + String(gateway_name) + String(topicori);
|
||||
String topic = String(mqtt_topic) + String(gateway_name) + String(topicori);
|
||||
#ifdef valueAsASubject
|
||||
# ifdef ZgatewayPilight
|
||||
String value = data["value"];
|
||||
String protocol = data["protocol"];
|
||||
if (value != 0) {
|
||||
topic = topic + "/" + protocol + "/" + value;
|
||||
}
|
||||
String value = data["value"];
|
||||
String protocol = data["protocol"];
|
||||
if (value != 0) {
|
||||
topic = topic + "/" + protocol + "/" + value;
|
||||
}
|
||||
# else
|
||||
SIGNAL_SIZE_UL_ULL value = data["value"];
|
||||
if (value != 0) {
|
||||
topic = topic + "/" + toString(value);
|
||||
}
|
||||
SIGNAL_SIZE_UL_ULL value = data["value"];
|
||||
if (value != 0) {
|
||||
topic = topic + "/" + toString(value);
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef jsonPublishing
|
||||
Log.trace(F("jsonPublishing" CR));
|
||||
# if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[measureJson(data) + 1];
|
||||
# else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
# endif
|
||||
serializeJson(data, JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
pubMQTT(topic, JSONmessageBuffer);
|
||||
Log.trace(F("jsonPubl - ON" CR));
|
||||
pubMQTT(topic, dataAsString.c_str());
|
||||
#endif
|
||||
|
||||
#ifdef simplePublishing
|
||||
Log.trace(F("simplePublishing" CR));
|
||||
// Loop through all the key-value pairs in obj
|
||||
for (JsonPair p : data) {
|
||||
Log.trace(F("simplePub - ON" CR));
|
||||
// Loop through all the key-value pairs in obj
|
||||
for (JsonPair p : data) {
|
||||
# if defined(ESP8266)
|
||||
yield();
|
||||
yield();
|
||||
# endif
|
||||
if (p.value().is<SIGNAL_SIZE_UL_ULL>() && strcmp(p.key().c_str(), "rssi") != 0) { //test rssi , bypass solution due to the fact that a int is considered as an SIGNAL_SIZE_UL_ULL
|
||||
if (strcmp(p.key().c_str(), "value") == 0) { // if data is a value we don't integrate the name into the topic
|
||||
pubMQTT(topic, p.value().as<SIGNAL_SIZE_UL_ULL>());
|
||||
} else { // if data is not a value we integrate the name into the topic
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<SIGNAL_SIZE_UL_ULL>());
|
||||
}
|
||||
} else if (p.value().is<int>()) {
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<int>());
|
||||
} else if (p.value().is<float>()) {
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<float>());
|
||||
} else if (p.value().is<char*>()) {
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<const char*>());
|
||||
if (p.value().is<SIGNAL_SIZE_UL_ULL>() && strcmp(p.key().c_str(), "rssi") != 0) { //test rssi , bypass solution due to the fact that a int is considered as an SIGNAL_SIZE_UL_ULL
|
||||
if (strcmp(p.key().c_str(), "value") == 0) { // if data is a value we don't integrate the name into the topic
|
||||
pubMQTT(topic, p.value().as<SIGNAL_SIZE_UL_ULL>());
|
||||
} else { // if data is not a value we integrate the name into the topic
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<SIGNAL_SIZE_UL_ULL>());
|
||||
}
|
||||
} else if (p.value().is<int>()) {
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<int>());
|
||||
} else if (p.value().is<float>()) {
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<float>());
|
||||
} else if (p.value().is<char*>()) {
|
||||
pubMQTT(topic + "/" + String(p.key().c_str()), p.value().as<const char*>());
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
Log.warning(F("client not connected can't pub" CR));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Publish the payload on default MQTT topic
|
||||
*
|
||||
* @param topicori suffix to add on default MQTT Topic
|
||||
* @param payload the message to sends
|
||||
*/
|
||||
void pub(const char* topicori, const char* payload) {
|
||||
if (client.connected()) {
|
||||
String topic = String(mqtt_topic) + String(gateway_name) + String(topicori);
|
||||
Log.trace(F("Pub ack %s into: %s" CR), payload, topic.c_str());
|
||||
pubMQTT(topic, payload);
|
||||
} else {
|
||||
Log.warning(F("client not connected can't pub" CR));
|
||||
}
|
||||
String topic = String(mqtt_topic) + String(gateway_name) + String(topicori);
|
||||
pubMQTT(topic, payload);
|
||||
}
|
||||
|
||||
void pub_custom_topic(const char* topicori, JsonObject& data, boolean retain) {
|
||||
if (client.connected()) {
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[measureJson(data) + 1];
|
||||
#else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
#endif
|
||||
serializeJson(data, JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
Log.trace(F("Pub json :%s into custom topic: %s" CR), JSONmessageBuffer, topicori);
|
||||
pubMQTT(topicori, JSONmessageBuffer, retain);
|
||||
} else {
|
||||
Log.warning(F("client not connected can't pub" CR));
|
||||
}
|
||||
/**
|
||||
* @brief Publish the payload on the topic with a retantion
|
||||
*
|
||||
* @param topic The topic where to publish
|
||||
* @param data The Json Object that rapresent the message
|
||||
* @param retain true if you what a retain
|
||||
*/
|
||||
void pub_custom_topic(const char* topic, JsonObject& data, boolean retain) {
|
||||
String buffer = "";
|
||||
serializeJson(data, buffer);
|
||||
pubMQTT(topic, buffer.c_str(), retain);
|
||||
}
|
||||
|
||||
// Low level MQTT functions
|
||||
/**
|
||||
* @brief Low level MQTT functions without retain
|
||||
*
|
||||
* @param topic the topic
|
||||
* @param payload the payload
|
||||
*/
|
||||
void pubMQTT(const char* topic, const char* payload) {
|
||||
client.publish(topic, payload);
|
||||
pubMQTT(topic, payload, false);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topicori, const char* payload, bool retainFlag) {
|
||||
client.publish(topicori, payload, retainFlag);
|
||||
/**
|
||||
* @brief Very Low level MQTT functions with retain Flag
|
||||
*
|
||||
* @param topic the topic
|
||||
* @param payload the payload
|
||||
* @param retainFlag true if retain the retain Flag
|
||||
*/
|
||||
void pubMQTT(const char* topic, const char* payload, bool retainFlag) {
|
||||
if (client.connected()) {
|
||||
Log.trace(F("[ OMG->MQTT ] topic: %s msg: %s " CR), topic, payload);
|
||||
client.publish(topic, payload, retainFlag);
|
||||
} else {
|
||||
Log.warning(F("Client not connected, aborting thes publication" CR));
|
||||
}
|
||||
}
|
||||
|
||||
void pubMQTT(String topic, const char* payload) {
|
||||
client.publish(topic.c_str(), payload);
|
||||
pubMQTT(topic.c_str(), payload);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, unsigned long payload) {
|
||||
char val[11];
|
||||
sprintf(val, "%lu", payload);
|
||||
client.publish(topic, val);
|
||||
pubMQTT(topic, val);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, unsigned long long payload) {
|
||||
char val[21];
|
||||
sprintf(val, "%llu", payload);
|
||||
client.publish(topic, val);
|
||||
pubMQTT(topic, val);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, String payload) {
|
||||
client.publish(topic, payload.c_str());
|
||||
pubMQTT(topic, payload.c_str());
|
||||
}
|
||||
|
||||
void pubMQTT(String topic, String payload) {
|
||||
client.publish(topic.c_str(), payload.c_str());
|
||||
pubMQTT(topic.c_str(), payload.c_str());
|
||||
}
|
||||
|
||||
void pubMQTT(String topic, int payload) {
|
||||
char val[12];
|
||||
sprintf(val, "%d", payload);
|
||||
client.publish(topic.c_str(), val);
|
||||
pubMQTT(topic.c_str(), val);
|
||||
}
|
||||
|
||||
void pubMQTT(String topic, unsigned long long payload) {
|
||||
char val[21];
|
||||
sprintf(val, "%llu", payload);
|
||||
client.publish(topic.c_str(), val);
|
||||
pubMQTT(topic.c_str(), val);
|
||||
}
|
||||
|
||||
void pubMQTT(String topic, float payload) {
|
||||
char val[12];
|
||||
dtostrf(payload, 3, 1, val);
|
||||
client.publish(topic.c_str(), val);
|
||||
pubMQTT(topic.c_str(), val);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, float payload) {
|
||||
char val[12];
|
||||
dtostrf(payload, 3, 1, val);
|
||||
client.publish(topic, val);
|
||||
pubMQTT(topic, val);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, int payload) {
|
||||
char val[6];
|
||||
char val[12];
|
||||
sprintf(val, "%d", payload);
|
||||
client.publish(topic, val);
|
||||
pubMQTT(topic, val);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, unsigned int payload) {
|
||||
char val[6];
|
||||
char val[12];
|
||||
sprintf(val, "%u", payload);
|
||||
client.publish(topic, val);
|
||||
pubMQTT(topic, val);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, long payload) {
|
||||
char val[11];
|
||||
sprintf(val, "%ld", payload);
|
||||
client.publish(topic, val);
|
||||
pubMQTT(topic, val);
|
||||
}
|
||||
|
||||
void pubMQTT(const char* topic, double payload) {
|
||||
char val[16];
|
||||
sprintf(val, "%f", payload);
|
||||
client.publish(topic, val);
|
||||
pubMQTT(topic, val);
|
||||
}
|
||||
|
||||
void pubMQTT(String topic, unsigned long payload) {
|
||||
char val[11];
|
||||
sprintf(val, "%lu", payload);
|
||||
client.publish(topic.c_str(), val);
|
||||
}
|
||||
|
||||
void logJson(JsonObject& jsondata) {
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[measureJson(jsondata) + 1];
|
||||
#else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
#endif
|
||||
serializeJson(jsondata, JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
Log.notice(F("Received json : %s" CR), JSONmessageBuffer);
|
||||
pubMQTT(topic.c_str(), val);
|
||||
}
|
||||
|
||||
bool cmpToMainTopic(const char* topicOri, const char* toAdd) {
|
||||
@@ -1652,7 +1662,10 @@ void receivingMQTT(char* topicOri, char* datacallback) {
|
||||
|
||||
if (!jsondata.isNull()) { // json object ok -> json decoding
|
||||
// log the received json
|
||||
logJson(jsondata);
|
||||
String buffer = "";
|
||||
serializeJson(jsondata, buffer);
|
||||
Log.notice(F("[ MQTT->OMG ]: %s" CR), buffer.c_str());
|
||||
|
||||
#ifdef ZgatewayPilight // ZgatewayPilight is only defined with json publishing due to its numerous parameters
|
||||
MQTTtoPilight(topicOri, jsondata);
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user