thermferm/mqtt.c

changeset 612
452f79a5ad71
parent 610
5563ee815701
child 616
e2b82881c13e
--- a/thermferm/mqtt.c	Wed May 12 21:17:59 2021 +0200
+++ b/thermferm/mqtt.c	Thu May 13 13:49:04 2021 +0200
@@ -1561,6 +1561,10 @@
      * Initialize mosquitto communication
      */
     gethostname(my_hostname, 255);
+    if (strchr(my_hostname, '.')) {
+	char *p = strchr(my_hostname, '.');
+	*p = '\0';
+    }
     mosquitto_lib_init();
     id = xstrcpy((char *)"thermferm/");
     id = xstrcat(id, my_hostname);
@@ -1580,9 +1584,6 @@
            case EINVAL:
                syslog(LOG_NOTICE, "MQTT: mosquitto_new: Invalid id");
                break;
-	   default:
-		syslog(LOG_NOTICE, "MQTT: mosquitto_new: Unknown error %d", errno);
-		break;
        }
        mosquitto_lib_cleanup();
        return;
@@ -1611,20 +1612,18 @@
     mosquitto_message_callback_set(mosq, my_message_callback);
     mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
 
-    if (Config.mqtt_username && Config.mqtt_password) {
-	syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set(%s, %s)", Config.mqtt_username, Config.mqtt_password);
-	if ((rc = mosquitto_username_pw_set(mosq, Config.mqtt_username, Config.mqtt_password))) {
-	    switch(errno) {
-           	case ENOMEM:
-		    syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Out of memory");
-               	    break;
-           	case EINVAL:
-		    syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Invalid id");
-		    break;
-           	default:
-		    syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Unknown error %d", errno);
-		    break;
-       	    }
+    if (Config.mqtt_username) {
+	if (Config.mqtt_password) {
+	    rc = mosquitto_username_pw_set(mosq, Config.mqtt_username, Config.mqtt_password);
+	} else {
+	    rc = mosquitto_username_pw_set(mosq, Config.mqtt_username, NULL);
+	}
+	if (rc == MOSQ_ERR_INVAL) {
+	    syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Invalid id");
+	} else if (rc == MOSQ_ERR_NOMEM) {
+	    syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Out of memory");
+	}
+	if (rc != MOSQ_ERR_SUCCESS) {
        	    mosquitto_lib_cleanup();
        	    return;
 	}

mercurial