Created new function to fill payload string for publish
This commit is contained in:
@@ -46,6 +46,7 @@ static void mqtt_sub_request_cb(void*, err_t);
|
|||||||
static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t);
|
static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t);
|
||||||
static void mosquitto_connect(mqtt_client_t*);
|
static void mosquitto_connect(mqtt_client_t*);
|
||||||
static uint32_t color_picker(char*);
|
static uint32_t color_picker(char*);
|
||||||
|
static void create_publish_string(char*, char*, llfs_file_t*, size_t, size_t);
|
||||||
|
|
||||||
// Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics
|
// Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics
|
||||||
static sFONT* font;
|
static sFONT* font;
|
||||||
@@ -87,17 +88,11 @@ static void publish_data(mqtt_client_t* client, void* arg) {
|
|||||||
num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp");
|
num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp");
|
||||||
|
|
||||||
if (num_files == 0) {
|
if (num_files == 0) {
|
||||||
strncpy(pub_payload, "No images found", sizeof(pub_payload));
|
strncat(pub_payload, "No images found", sizeof(pub_payload) - strlen(pub_payload) - 1);
|
||||||
|
LOG_INFO(TAG, "%s: No images found", __func__);
|
||||||
} else {
|
} else {
|
||||||
strncat(pub_payload, "Available images: ", sizeof(pub_payload) - strlen(pub_payload) - 1);
|
create_publish_string("image", pub_payload, file_list, sizeof(pub_payload), num_files);
|
||||||
for (size_t i = 0; i < num_files; i++) {
|
|
||||||
// Concatenate file names into the payload string
|
|
||||||
strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1);
|
|
||||||
strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names
|
|
||||||
}
|
|
||||||
strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
|
err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
LOG_DEBUG(TAG, "Publish err: %d", err);
|
LOG_DEBUG(TAG, "Publish err: %d", err);
|
||||||
@@ -108,15 +103,10 @@ static void publish_data(mqtt_client_t* client, void* arg) {
|
|||||||
num_files = llfs_file_list(file_list, MAX_FILES, "*.gif");
|
num_files = llfs_file_list(file_list, MAX_FILES, "*.gif");
|
||||||
|
|
||||||
if (num_files == 0) {
|
if (num_files == 0) {
|
||||||
strncpy(pub_payload, "No gifs found", sizeof(pub_payload));
|
strncat(pub_payload, "No gifs found", sizeof(pub_payload) - strlen(pub_payload) - 1);
|
||||||
|
LOG_INFO(TAG, "%s: No gifs found", __func__);
|
||||||
} else {
|
} else {
|
||||||
strncpy(pub_payload, "Available gifs: ", sizeof(pub_payload));
|
create_publish_string("gif", pub_payload, file_list, sizeof(pub_payload), num_files);
|
||||||
for (size_t i = 0; i < num_files; i++) {
|
|
||||||
// Concatenate file names into the payload string
|
|
||||||
strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1);
|
|
||||||
strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names
|
|
||||||
}
|
|
||||||
strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1);
|
|
||||||
}
|
}
|
||||||
err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
|
err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
@@ -397,3 +387,23 @@ uint32_t color_picker(char* color_in) {
|
|||||||
|
|
||||||
return LCD_BLACK;
|
return LCD_BLACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void create_publish_string(char* file_type, char* payload_buffer, llfs_file_t* file_list, size_t buffer_size, size_t files_amount) {
|
||||||
|
if (strcmp(file_type, "image") == 0) {
|
||||||
|
strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1);
|
||||||
|
} else if (strcmp(file_type, "gif") == 0) {
|
||||||
|
strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1);
|
||||||
|
} else {
|
||||||
|
LOG_WARN(TAG, "No application for given file type: %s", file_type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < files_amount; i++) {
|
||||||
|
// Concatenate file names into the payload string
|
||||||
|
strncat(payload_buffer, file_list[i].name, buffer_size - strlen(payload_buffer) - 1);
|
||||||
|
strncat(payload_buffer, ", ", buffer_size - strlen(payload_buffer) - 1); // Add a comma between file names
|
||||||
|
}
|
||||||
|
strncat(payload_buffer, "\0", buffer_size - strlen(payload_buffer) - 1);
|
||||||
|
LOG_DEBUG(TAG, "String: %s", payload_buffer);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user