added topic for the files operations, file list and delete implemented

This commit is contained in:
Bogdan Pilyugin 2024-03-11 14:41:32 +02:00
parent f007f15313
commit 6712ed4b42
3 changed files with 103 additions and 2 deletions

View File

@ -144,7 +144,7 @@ int ExecCommand(char *cmd)
static int ExecCommandParse(char *cmd)
{
char *obj = NULL, *com = NULL, *arg = NULL;
int err = 0;
//int err = 0;
int commlen = strlen(cmd);
if (commlen > CONFIG_WEBGUIAPP_MAX_COMMAND_STRING_LENGTH)
return 1;

View File

@ -28,6 +28,7 @@
#define TAG "MQTT"
#define SERVICE_NAME "SYSTEM" // Dedicated service name
#define FILE_SERVICE_NAME "FILE"
#define EXTERNAL_SERVICE_NAME "RS485"
#define UPLINK_SUBTOPIC "UPLINK" // Device publish to this topic
#define DOWNLINK_SUBTOPIC "DWLINK" // Device listen from this topic
@ -219,6 +220,14 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
ESP_LOGI(TAG, "Subscribe to %s", topic);
#endif
ComposeTopic(topic, idx, FILE_SERVICE_NAME, DOWNLINK_SUBTOPIC);
msg_id = esp_mqtt_client_subscribe(client, (char*) topic, 0);
#if MQTT_DEBUG_MODE > 0
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
ESP_LOGI(TAG, "Subscribe to %s", topic);
#endif
#ifdef CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE
if (GetSysConf()->serialSettings.Flags.IsBridgeEnabled)
{
@ -291,8 +300,15 @@ static void mqtt_system_event_handler(int idx, void *handler_args, esp_event_bas
}
else
ESP_LOGE(TAG, "Out of free RAM for MQTT API handle");
}
ComposeTopic(topic, idx, FILE_SERVICE_NAME, DOWNLINK_SUBTOPIC);
if (!memcmp(topic, event->topic, event->topic_len))
{
ESP_LOGI(TAG, "Got data for FILE_SYSTEM_SERVICE:%s", event->data);
//Here handler of file system operations
}
#ifdef CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE
if (GetSysConf()->serialSettings.Flags.IsBridgeEnabled)
{

View File

@ -30,6 +30,7 @@
#include "romfs.h"
#include "esp_idf_version.h"
#include "NetTransport.h"
#include "esp_vfs.h"
extern SYS_CONFIG SysConfig;
@ -371,6 +372,87 @@ static void funct_exec(char *argres, int rw)
snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"EXECUTED\"");
}
static const char *dirpath = "/data/";
static void funct_file_list(char *argres, int rw)
{
char entrypath[FILE_PATH_MAX];
char entrysize[16];
const char *entrytype = "file";
struct dirent *entry;
struct stat entry_stat;
DIR *dir = opendir(dirpath);
const size_t dirpath_len = strlen(dirpath);
strlcpy(entrypath, dirpath, sizeof(entrypath));
if (!dir)
{
ESP_LOGE("FILE_API", "Failed to stat dir : %s", dirpath);
snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\"");
return;
}
struct jWriteControl jwc;
jwOpen(&jwc, argres, VAR_MAX_VALUE_LENGTH, JW_ARRAY, JW_COMPACT);
while ((entry = readdir(dir)) != NULL)
{
strlcpy(entrypath + dirpath_len, entry->d_name, sizeof(entrypath) - dirpath_len);
entrytype = (entry->d_type == DT_DIR ? "directory" : "file");
if (stat(entrypath, &entry_stat) == -1)
{
ESP_LOGE("FILE_API", "Failed to stat %s : %s", entrytype, entry->d_name);
continue;
}
jwArr_object(&jwc);
jwObj_string(&jwc, "name", (char*) entry->d_name);
jwObj_string(&jwc, "type", entrytype);
jwObj_int(&jwc, "size", entry_stat.st_size);
jwEnd(&jwc);
}
jwClose(&jwc);
}
static void funct_file_delete(char *argres, int rw)
{
char filepath[FILE_PATH_MAX];
struct stat file_stat;
const char *filename = argres;
if (!filename)
{
snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\"");
return;
}
if (filename[strlen(filename) - 1] == '/')
{
ESP_LOGE("FILE_API", "Invalid filename : %s", filename);
snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\"");
return;
}
strcpy(filepath, dirpath);
strcat(filepath, filename);
ESP_LOGI("FILE_API", " filepath to delete : %s", filepath);
if (stat(filepath, &file_stat) == -1)
{
ESP_LOGE("FILE_API", "File does not exist : %s", filename);
snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"ERROR:DIR_NOT_FOUND\"");
/* Respond with 400 Bad Request */
return;
}
unlink(filepath);
}
const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION;
const bool VAR_TRUE = true;
const bool VAR_FALSE = false;
@ -557,6 +639,9 @@ const rest_var_t SystemVariables[] =
{ 0, "cronrecs", &funct_cronrecs, VAR_FUNCT, RW, 0, 0 },
{ 0, "objsinfo", &funct_objsinfo, VAR_FUNCT, R, 0, 0 },
{ 0, "file_list", &funct_file_list, VAR_FUNCT, R, 0, 0 },
{ 0, "file_delete", &funct_file_delete, VAR_FUNCT, R, 0, 0 },
};