Fixed not connected bug, added version string

This commit is contained in:
Till Zimmermann
2018-06-20 18:58:34 +02:00
parent 3bfafa48a2
commit 3d1086ea2e
2 changed files with 22 additions and 1 deletions
+3
View File
@@ -1128,6 +1128,9 @@ char *get_version_string() {
#ifdef CONFIG_METADATA
strcat(version_string, "-metadata");
#endif
#ifdef CONFIG_MQTT
strcat(version_string, "-mqtt");
#endif
#ifdef HAVE_DBUS
strcat(version_string, "-dbus");
#endif
+19 -1
View File
@@ -17,6 +17,7 @@
//this holds the mosquitto client
struct mosquitto *global_mosq = NULL;
char *topic = NULL;
int connected = 0;
//mosquitto logging
void _cb_log( __attribute__((unused)) struct mosquitto *mosq,
@@ -74,12 +75,14 @@ void on_message( __attribute__((unused)) struct mosquitto* mosq,
void on_disconnect( __attribute__((unused)) struct mosquitto* mosq,
__attribute__((unused)) void* userdata,
__attribute__((unused)) int rc){
connected = 0;
debug(1, "[MQTT]: disconnected");
}
void on_connect(struct mosquitto* mosq,
__attribute__((unused)) void* userdata,
__attribute__((unused)) int rc){
connected = 1;
debug(1, "[MQTT]: connected");
//subscribe if requested
@@ -95,11 +98,26 @@ void mqtt_publish(char* topic, char* data, uint32_t length){
char fulltopic[strlen(config.mqtt_topic)+strlen(topic)+3];
snprintf(fulltopic, strlen(config.mqtt_topic)+strlen(topic)+2, "%s/%s", config.mqtt_topic, topic);
debug(1, "[MQTT]: publishing under %s",fulltopic);
mosquitto_publish(global_mosq, NULL, fulltopic, length, data, 0, 0);
int rc;
if((rc=mosquitto_publish(global_mosq, NULL, fulltopic, length, data, 0, 0))!=MOSQ_ERR_SUCCESS) {
switch(rc){
case MOSQ_ERR_NO_CONN:
debug(1, "[MQTT]: Publish failed: not connected to broker");
break;
default:
debug(1, "[MQTT]: Publish failed: unknown error");
break;
}
}
}
//handler for incoming metadata
void mqtt_process_metadata(uint32_t type, uint32_t code, char *data, uint32_t length){
if(global_mosq==NULL || connected!=1){
debug(3, "[MQTT]: Client not connected, skipping metadata handling");
return;
}
if(config.mqtt_publish_raw){
uint32_t val;
char topic[] = "____/____";