Add initial support for MQTT logging

Add initial support for MQTT logging using command MqttLog <loglevel> (#6498)
This commit is contained in:
Theo Arends 2019-09-27 18:13:00 +02:00
parent 82b7225332
commit b7aa3cd884
6 changed files with 45 additions and 4 deletions

View File

@ -2,6 +2,7 @@
* 6.6.0.14 20190925
* Change command Tariffx to allow time entries like 23 (hours), 1320 (minutes) or 23:00. NOTE: As this is development branch previous tariffs are lost! (#6488)
* Remove support for define USE_DS18x20_LEGACY and legacy DS18x20 driver (#6486)
* Add initial support for MQTT logging using command MqttLog <loglevel> (#6498)
*
* 6.6.0.13 20190922
* Add command EnergyReset4 x,x to initialize total usage for two tarrifs

View File

@ -291,6 +291,7 @@
#define D_JSON_BASE "BASE"
// Commands xdrv_01_mqtt.ino
#define D_CMND_MQTTLOG "MqttLog"
#define D_CMND_MQTTHOST "MqttHost"
#define D_CMND_MQTTPORT "MqttPort"
#define D_CMND_MQTTRETRY "MqttRetry"

View File

@ -227,7 +227,8 @@ struct SYSCFG {
uint8_t weblog_level; // 1AC
uint8_t mqtt_fingerprint[2][20]; // 1AD
uint8_t adc_param_type; // 1D5
uint8_t register8[18]; // 1D6 - 18 x 8-bit registers indexed by enum SettingsRegister8
uint8_t register8[17]; // 1D6 - 17 x 8-bit registers indexed by enum SettingsRegister8
uint8_t mqttlog_level; // 1E7
uint8_t sps30_inuse_hours; // 1E8
char mqtt_host[33]; // 1E9 - Keep together with below as being copied as one chunck with reset 6
uint16_t mqtt_port; // 20A - Keep together

View File

@ -256,7 +256,7 @@ enum SettingsRegister8 { R8_SPARE00, R8_SPARE01, R8_SPARE02, R8_SPARE03,
R8_SPARE04, R8_SPARE05, R8_SPARE06, R8_SPARE07,
R8_SPARE08, R8_SPARE09, R8_SPARE10, R8_SPARE11,
R8_SPARE12, R8_SPARE13, R8_SPARE14, R8_SPARE15,
R8_SPARE16, R8_SPARE17 }; // Max size is 18 (Settings.register8[])
R8_SPARE16 }; // Max size is 17 (Settings.register8[])
enum DomoticzSensors {DZ_TEMP, DZ_TEMP_HUM, DZ_TEMP_HUM_BARO, DZ_POWER_ENERGY, DZ_ILLUMINANCE, DZ_COUNT, DZ_VOLTAGE, DZ_CURRENT, DZ_AIRQUALITY, DZ_P1_SMART_METER, DZ_MAX_SENSORS};

View File

@ -1571,6 +1571,7 @@ void AddLog(uint32_t loglevel)
if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string
}
#endif // USE_WEBSERVER
if (!global_state.mqtt_down && (loglevel <= Settings.mqttlog_level)) { MqttPublishLogging(mxtime); }
if (!global_state.wifi_down && (loglevel <= syslog_level)) { Syslog(); }
}

View File

@ -39,7 +39,7 @@ const char kMqttCommands[] PROGMEM = "|" // No prefix
D_CMND_TLSKEY "|"
#endif
D_CMND_MQTTHOST "|" D_CMND_MQTTPORT "|" D_CMND_MQTTRETRY "|" D_CMND_STATETEXT "|" D_CMND_MQTTCLIENT "|"
D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|"
D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|" D_CMND_MQTTLOG "|"
D_CMND_BUTTONTOPIC "|" D_CMND_SWITCHTOPIC "|" D_CMND_BUTTONRETAIN "|" D_CMND_SWITCHRETAIN "|" D_CMND_POWERRETAIN "|" D_CMND_SENSORRETAIN ;
void (* const MqttCommand[])(void) PROGMEM = {
@ -53,7 +53,7 @@ void (* const MqttCommand[])(void) PROGMEM = {
&CmndTlsKey,
#endif
&CmndMqttHost, &CmndMqttPort, &CmndMqttRetry, &CmndStateText, &CmndMqttClient,
&CmndFullTopic, &CmndPrefix, &CmndGroupTopic, &CmndTopic, &CmndPublish,
&CmndFullTopic, &CmndPrefix, &CmndGroupTopic, &CmndTopic, &CmndPublish, &CmndMqttlog,
&CmndButtonTopic, &CmndSwitchTopic, &CmndButtonRetain, &CmndSwitchRetain, &CmndPowerRetain, &CmndSensorRetain };
struct MQTT {
@ -305,6 +305,35 @@ void MqttUnsubscribe(const char *topic)
MqttUnsubscribeLib(topic);
}
void MqttPublishLogging(const char *mxtime)
{
if (Settings.flag.mqtt_enabled) {
if (MqttIsConnected()) {
char saved_mqtt_data[MESSZ];
memcpy(saved_mqtt_data, mqtt_data, sizeof(saved_mqtt_data));
// ResponseTime_P(PSTR(",\"Log\":{\"%s\"}}"), log_data); // Will fail as some messages contain JSON
Response_P(PSTR("%s%s"), mxtime, log_data); // No JSON and ugly!!
char romram[33];
char stopic[TOPSZ];
snprintf_P(romram, sizeof(romram), PSTR("LOGGING"));
GetTopic_P(stopic, STAT, mqtt_topic, romram);
char *me;
if (!strcmp(Settings.mqtt_prefix[0], Settings.mqtt_prefix[1])) {
me = strstr(stopic, Settings.mqtt_prefix[0]);
if (me == stopic) {
mqtt_cmnd_publish += 3;
}
}
MqttPublishLib(stopic, false);
memcpy(mqtt_data, saved_mqtt_data, sizeof(saved_mqtt_data));
}
}
}
void MqttPublishDirect(const char* topic, bool retained)
{
char sretained[CMDSZ];
@ -724,6 +753,14 @@ void CmndMqttPassword(void)
}
#endif // USE_MQTT_AWS_IOT
void CmndMqttlog(void)
{
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_ALL)) {
Settings.mqttlog_level = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings.mqttlog_level);
}
void CmndMqttHost(void)
{
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT)