Skip to content

Commit

Permalink
rationalisation of logs and removal of duplicate codes (it should fix…
Browse files Browse the repository at this point in the history
… also #931)  (#1093)

* Rationalisation of logs and removal of duplicate codes
  • Loading branch information
Odyno authored Nov 2, 2021
1 parent 10f0d4c commit bdda5df
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 116 deletions.
1 change: 1 addition & 0 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
24 changes: 2 additions & 22 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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],
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
1 change: 0 additions & 1 deletion main/ZsensorDS1820.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
27 changes: 25 additions & 2 deletions main/config_mqttDiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
Loading

0 comments on commit bdda5df

Please sign in to comment.