mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-28 13:46:36 +00:00
Add MQTT error codes
This commit is contained in:
parent
190f2d649e
commit
17550d7b66
@ -13,16 +13,24 @@
|
||||
#define __FlashStringHelper char
|
||||
#endif
|
||||
|
||||
enum hasp_mqtt_error_t {
|
||||
MQTT_ERR_OK = 0,
|
||||
MQTT_ERR_DISABLED = -1,
|
||||
MQTT_ERR_NO_CONN = -2,
|
||||
MQTT_ERR_SUB_FAIL = -3,
|
||||
MQTT_ERR_PUB_FAIL = -4,
|
||||
MQTT_ERR_UNKNOWN = -128
|
||||
};
|
||||
|
||||
void mqttSetup();
|
||||
void mqttLoop();
|
||||
void mqttEvery5Seconds(bool wifiIsConnected);
|
||||
void mqttStart();
|
||||
void mqttStop();
|
||||
|
||||
void mqtt_send_object_state(uint8_t pageid, uint8_t btnid, char* payload);
|
||||
void mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload);
|
||||
|
||||
bool mqttPublish(const char* topic, const char* payload, size_t len, bool retain);
|
||||
int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload);
|
||||
int mqtt_send_state(const char* subtopic, const char* payload);
|
||||
int mqttPublish(const char* topic, const char* payload, size_t len, bool retain);
|
||||
|
||||
bool mqttIsConnected();
|
||||
|
||||
|
@ -343,7 +343,7 @@ void mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload)
|
||||
mqttPublish(tmp_topic, payload, false);
|
||||
}
|
||||
|
||||
void mqtt_send_object_state(uint8_t pageid, uint8_t btnid, char* payload)
|
||||
void mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload)
|
||||
{
|
||||
char tmp_topic[strlen(mqttNodeTopic) + 20];
|
||||
snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR("%sstate/p%ub%u"), mqttNodeTopic, pageid, btnid);
|
||||
|
@ -106,7 +106,7 @@ int disc_finished = 0;
|
||||
int subscribed = 0;
|
||||
int connected = 0;
|
||||
|
||||
bool mqttPublish(const char* topic, const char* payload, size_t len, bool retain);
|
||||
int mqttPublish(const char* topic, const char* payload, size_t len, bool retain);
|
||||
|
||||
/* ===== Paho event callbacks ===== */
|
||||
|
||||
@ -207,9 +207,10 @@ void mqtt_subscribe(void* context, const char* topic)
|
||||
|
||||
/* ===== Local HASP MQTT functions ===== */
|
||||
|
||||
bool mqttPublish(const char* topic, const char* payload, size_t len, bool retain)
|
||||
int mqttPublish(const char* topic, const char* payload, size_t len, bool retain)
|
||||
{
|
||||
if(mqttIsConnected()) {
|
||||
if(!mqttIsConnected()) return MQTT_ERR_NO_CONN;
|
||||
|
||||
MQTTClient_message pubmsg = MQTTClient_message_initializer;
|
||||
MQTTClient_deliveryToken token;
|
||||
|
||||
@ -223,14 +224,11 @@ bool mqttPublish(const char* topic, const char* payload, size_t len, bool retain
|
||||
|
||||
if(rc != MQTTCLIENT_SUCCESS) {
|
||||
LOG_ERROR(TAG_MQTT_PUB, F(D_MQTT_FAILED " '%s' => %s"), topic, payload);
|
||||
return MQTT_ERR_PUB_FAIL;
|
||||
} else {
|
||||
LOG_TRACE(TAG_MQTT_PUB, F("'%s' => %s OK"), topic, payload);
|
||||
return true;
|
||||
return MQTT_ERR_OK;
|
||||
}
|
||||
} else {
|
||||
LOG_ERROR(TAG_MQTT, F(D_MQTT_NOT_CONNECTED));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// static bool mqttPublish(const char* topic, const char* payload, bool retain)
|
||||
@ -245,19 +243,19 @@ bool mqttIsConnected()
|
||||
return connected == 1;
|
||||
}
|
||||
|
||||
void mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload)
|
||||
int mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload)
|
||||
{
|
||||
char tmp_topic[strlen(mqttNodeTopic) + 20];
|
||||
// printf(("%sstate/%s\n"), mqttNodeTopic, subtopic);
|
||||
snprintf_P(tmp_topic, sizeof(tmp_topic), ("%sstate/%s"), mqttNodeTopic, subtopic);
|
||||
mqttPublish(tmp_topic, payload, strlen(payload), false);
|
||||
return mqttPublish(tmp_topic, payload, strlen(payload), false);
|
||||
}
|
||||
|
||||
void mqtt_send_object_state(uint8_t pageid, uint8_t btnid, char* payload)
|
||||
int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, char* payload)
|
||||
{
|
||||
char tmp_topic[strlen(mqttNodeTopic) + 20];
|
||||
snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR("%sstate/p%ub%u"), mqttNodeTopic, pageid, btnid);
|
||||
mqttPublish(tmp_topic, payload, strlen(payload), false);
|
||||
return mqttPublish(tmp_topic, payload, strlen(payload), false);
|
||||
}
|
||||
|
||||
static void onConnect(void* context)
|
||||
|
@ -89,25 +89,21 @@ char mqttGroupName[16] = MQTT_GROUPNAME;
|
||||
uint16_t mqttPort = MQTT_PORT;
|
||||
PubSubClient mqttClient(mqttNetworkClient);
|
||||
|
||||
bool mqttPublish(const char* topic, const char* payload, size_t len, bool retain)
|
||||
int mqttPublish(const char* topic, const char* payload, size_t len, bool retain)
|
||||
{
|
||||
if(mqttIsConnected()) {
|
||||
if(!mqttEnabled) return MQTT_ERR_DISABLED;
|
||||
if(!mqttClient.connected()) return MQTT_ERR_NO_CONN;
|
||||
|
||||
if(mqttClient.beginPublish(topic, len, retain)) {
|
||||
mqttClient.write((uint8_t*)payload, len);
|
||||
mqttClient.endPublish();
|
||||
|
||||
LOG_TRACE(TAG_MQTT_PUB, F("%s => %s"), topic, payload);
|
||||
return true;
|
||||
} else {
|
||||
LOG_ERROR(TAG_MQTT_PUB, F(D_MQTT_FAILED " %s => %s"), topic, payload);
|
||||
}
|
||||
} else {
|
||||
LOG_ERROR(TAG_MQTT, F(D_MQTT_NOT_CONNECTED));
|
||||
}
|
||||
return false;
|
||||
return MQTT_ERR_OK;
|
||||
}
|
||||
|
||||
static bool mqttPublish(const char* topic, const char* payload, bool retain)
|
||||
return MQTT_ERR_PUB_FAIL;
|
||||
}
|
||||
|
||||
int mqttPublish(const char* topic, const char* payload, bool retain)
|
||||
{
|
||||
return mqttPublish(topic, payload, strlen(payload), retain);
|
||||
}
|
||||
@ -132,18 +128,18 @@ void mqtt_send_lwt(bool online)
|
||||
bool res = mqttPublish(tmp_topic, tmp_payload, len, true);
|
||||
}
|
||||
|
||||
void mqtt_send_object_state(uint8_t pageid, uint8_t btnid, char* payload)
|
||||
void mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload)
|
||||
{
|
||||
char tmp_topic[strlen(mqttNodeTopic) + 16];
|
||||
snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR("%sstate/" HASP_OBJECT_NOTATION), mqttNodeTopic, pageid, btnid);
|
||||
mqttPublish(tmp_topic, payload, false);
|
||||
}
|
||||
|
||||
void mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload)
|
||||
int mqtt_send_state(const char* subtopic, const char* payload)
|
||||
{
|
||||
char tmp_topic[strlen(mqttNodeTopic) + 20];
|
||||
snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR("%sstate/%s"), mqttNodeTopic, subtopic);
|
||||
mqttPublish(tmp_topic, payload, false);
|
||||
return mqttPublish(tmp_topic, payload, false);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
Loading…
x
Reference in New Issue
Block a user