main/files.c

Mon, 22 Oct 2018 21:43:45 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 22 Oct 2018 21:43:45 +0200
changeset 6
e84200edc852
parent 3
2dcdf83248c8
child 54
7b134c27fadb
permissions
-rw-r--r--

Updated esp-ide. Removed VNC server corre encoding because no clients would use it. Enabled WiFi error logmessages. Write runtime record is now debug logging. Removed recipe.Record number, not usefull and was wrong too. Removed console print of json log data.

/**
 * @file files.c
 * @brief Files management.
 */

#include "config.h"


extern sButton                  Buttons[MAXBUTTONS];
extern int                      Main_Screen;

static const char		*TAG = "files";


/**
 * @brief Show files directory
 * @param path The path from which to list
 */
void Files_Dir(char *path)
{
    char		filename[256], tmp[42];
    uint16_t		y = 28;
    DIR			*dir;
    struct dirent	*de;
    struct stat		st;

    _bg = TFT_BLACK;
    TFT_fillScreen(_bg);
    TopMessage(path);
    _fg = TFT_WHITE;
    TFT_setFont(DEFAULT_FONT, NULL);

    if ((dir = opendir(path))) {
	de = readdir(dir);
	while (de) {
	    TFT_print(de->d_name, 2, y);
	    sprintf(filename, "%s/%s", path, de->d_name);
	    if (stat(filename, &st) == 0) {
		sprintf(tmp, "%ld", st.st_size);
		TFT_print(tmp, RIGHT, y);
	    }
	    de = readdir(dir);
	    y += 16;
	}
	closedir(dir);
    }

    Buttons_Add(130, 200, 60, 40, "Ok", 0);
    Buttons[0].dark = true;
    Buttons_Show();

    while (true) {
	if (Buttons_Scan() == 0) {
	    Buttons_Clear();
	    return;
	}
	vTaskDelay(50 / portTICK_PERIOD_MS);
    }
}



/**
 * @brief File copier.
 * @param fdir From directory
 * @param tdir To directory
 * @param fn Filename
 */
void FCopy(char *fdir, char *tdir, char *filename)
{
    char	fn[33], tn[33], msg[33];
    int		rc;

    snprintf(msg, 32, "Copy %s\r\n", filename);
    TFT_print(msg, 0, LASTY);
    snprintf(fn, 32, "%s%s", fdir, filename);
    snprintf(tn, 32, "%s%s", tdir, filename);
    rc = FileCopy(fn, tn);
    ESP_LOGI(TAG, "Copy %s to %s rc=%d", fn, tn, rc);
}



/*
 * Files init function, only runs once a new screen is entered.
 */
void Files_Init(void)
{
    bool	_loop;
    DIR		*dir;

    switch (Main_Screen) {
	case MAIN_TOOLS_FILES:
			TopMessage("Bestanden menu");
			Buttons_Add( 20, 40,120, 40, "Restore", 0);
			Buttons_Add(180, 40,120, 40, "Backup", 1);
			Buttons_Add( 20,120,120, 40, "Directory", 2);
			Buttons_Add(130, 200, 60, 40, "Ok", 3);
			Buttons[3].dark = true;
			Buttons_Show();
			break;

	case MAIN_TOOLS_FILES_DIR:
			break;

	case MAIN_TOOLS_FILES_RESTORE:
	case MAIN_TOOLS_FILES_BACKUP:
			if (Main_Screen == MAIN_TOOLS_FILES_RESTORE)
			    TopMessage("Restore database");
			else
			    TopMessage("Backup database");
			Buttons_Add( 40, 100, 80, 40, "Start", 0);
			Buttons_Add(200, 100, 80, 40, "Stop",  1);
			Buttons[1].dark = true;
			Buttons_Show();
			SoundPlay(SOUND_Prompt);
			_loop = true;
			while (_loop) {
			    switch (Buttons_Scan()) {
				case 0:
				    	_loop = false;
					break;
				case 1:
					_loop = false;
					Main_Screen = MAIN_TOOLS_FILES;
					break;
				default:
					break;
			    }
			    vTaskDelay(50 / portTICK_PERIOD_MS);
			}
			Buttons_Clear();
			if (Main_Screen == MAIN_TOOLS_FILES)
			    break;

			_fg = TFT_YELLOW;
			TFT_setFont(DEJAVU24_FONT, NULL);
			if (Main_Screen == MAIN_TOOLS_FILES_RESTORE) {
			    ESP_LOGI(TAG, "Restore database");
			    TFT_setFont(DEJAVU18_FONT, NULL);
			    TFT_print("Backup:\r\n", 0, 30);
			    _fg = TFT_CYAN;
			    FCopy("/sdcard/etc/", "/spiffs/etc/", "config.conf");
			    FCopy("/sdcard/etc/", "/spiffs/etc/", "recipe.conf");
			    FCopy("/sdcard/etc/", "/spiffs/etc/", "equipments.conf");
			    FCopy("/sdcard/etc/", "/spiffs/etc/", "stations.conf");
			    FCopy("/sdcard/etc/", "/spiffs/etc/", "runtime.conf");
			    _fg = TFT_YELLOW;
			    TFT_setFont(DEJAVU24_FONT, NULL);
			    TFT_print("Restore gereed, reset!", CENTER, LASTY + 12);
			    vTaskDelay(2000 / portTICK_PERIOD_MS);
			    esp_restart();
			} else {
			    dir = opendir("/sdcard/etc");
			    if (dir == NULL) {
				mkdir("/sdcard/etc",  0755);
				dir = opendir("/sdcard/etc");
			    }
			    if (dir == NULL) {
				TFT_print("SD kaart fout", CENTER, CENTER);
			    } else {
				closedir(dir);
				ESP_LOGI(TAG, "Backup database");
				TFT_setFont(DEJAVU18_FONT, NULL);
				TFT_print("Backup:\r\n", 0, 30);
				_fg = TFT_CYAN;
				FCopy("/spiffs/etc/", "/sdcard/etc/", "config.conf");
				FCopy("/spiffs/etc/", "/sdcard/etc/", "recipe.conf");
				FCopy("/spiffs/etc/", "/sdcard/etc/", "equipments.conf");
				FCopy("/spiffs/etc/", "/sdcard/etc/", "stations.conf");
				FCopy("/spiffs/etc/", "/sdcard/etc/", "runtime.conf");
				_fg = TFT_YELLOW;
				TFT_setFont(DEJAVU24_FONT, NULL);
			    	TFT_print("Backup gereed.", CENTER, LASTY + 12);
			    }
			}

			Buttons_Add(130, 200, 60, 40, "Ok", 0);
			Buttons[0].dark = true;
			Buttons_Show();
			break;

	default:	break;
    }
}



/*
 * Recipes management loop, non-blocking.
 */
void Files_Loop(void)
{
    switch (Main_Screen) {

	case MAIN_TOOLS_FILES:
			switch (Buttons_Scan()) {
			    case 0:	Main_Screen = MAIN_TOOLS_FILES_RESTORE;
					break;

			    case 1:	Main_Screen = MAIN_TOOLS_FILES_BACKUP;
					break;

			    case 2:	Main_Screen = MAIN_TOOLS_FILES_DIR;
					break;

			    case 3:	Main_Screen = MAIN_TOOLS;
					break;
			}
			break;

	case MAIN_TOOLS_FILES_DIR:
			Files_Dir("/sdcard/recipe");
			Files_Dir("/sdcard/w/log");
			Main_Screen = MAIN_TOOLS_FILES;
			break;

	case MAIN_TOOLS_FILES_RESTORE:
	case MAIN_TOOLS_FILES_BACKUP:
			if (Buttons_Scan() == 0)
			    Main_Screen = MAIN_TOOLS_FILES;
			break;

	default:	break;
    }
}

mercurial