Add time in sd file

Add create command to  filesystem
Clean SD filesystem code
This commit is contained in:
Luc 2019-10-21 17:50:18 +02:00
parent bbadbc2be3
commit c37b90ea05
14 changed files with 191 additions and 61 deletions

View File

@ -191,14 +191,14 @@ Get will give type and settings only, not the protected T1/T2
[ESP720]<Root> pwd=<admin password>
* Action on ESP Filesystem
rmdir / remove / mkdir / exists
rmdir / remove / mkdir / exists / create
[ESP730]<Action>=<path> pwd=<admin password>
* List SD Filesystem
[ESP740]<Root> pwd=<admin password>
* Action on SD Filesystem
rmdir / remove / mkdir / exists
rmdir / remove / mkdir / exists / create
[ESP750]<Action>=<path> pwd=<admin password>
* FW Informations

View File

@ -25,7 +25,7 @@
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_filesystem.h"
// Action on ESP Filesystem
//rmdir / remove / mkdir / exists
//rmdir / remove / mkdir / exists / create
//[ESP730]<Action>=<path> pwd=<admin password>
bool Commands::ESP730(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
{
@ -79,6 +79,18 @@ bool Commands::ESP730(const char* cmd_params, level_authenticate_type auth_type,
}
return response;
}
parameter = get_param (cmd_params, "create=");
if (parameter.length() != 0) {
ESP_File f = ESP_FileSystem::open(parameter.c_str(), ESP_SD_FILE_WRITE);
if (f.isOpen()) {
f.close();
output->printMSG ("ok");
} else {
output->printERROR ("failed!");
response = false;
}
return response;
}
output->printERROR ("Incorrect command!");
return false;
}

View File

@ -24,6 +24,9 @@
#include "../settings_esp3d.h"
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_sd.h"
#ifdef SD_TIMESTAMP_FEATURE
#include "../../modules/time/time_server.h"
#endif //SD_TIMESTAMP_FEATURE
//List SD Filesystem
//[ESP740]<Root> pwd=<admin password>
bool Commands::ESP740(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
@ -86,9 +89,7 @@ bool Commands::ESP740(const char* cmd_params, level_authenticate_type auth_type,
output->print(ESP_SD::formatBytes(sub.size()).c_str());
output->print(" \t");
#ifdef SD_TIMESTAMP_FEATURE
time_t t = sub.getLastWrite();
struct tm * tmstruct = localtime(&t);
output->printf("%d-%02d-%02d %02d:%02d:%02d",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
output->print(timeserver.current_time(sub.getLastWrite()));
output->print(" \t");
#endif //SD_TIMESTAMP_FEATURE
output->printLN("");

View File

@ -25,7 +25,7 @@
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_sd.h"
// Action on SD Filesystem
//rmdir / remove / mkdir / exists
//rmdir / remove / mkdir / exists /create
//[ESP750]<Action>=<path> pwd=<admin password>
bool Commands::ESP750(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
{
@ -90,6 +90,18 @@ bool Commands::ESP750(const char* cmd_params, level_authenticate_type auth_type,
}
return response;
}
parameter = get_param (cmd_params, "create=");
if (parameter.length() != 0) {
ESP_SDFile f = ESP_SD::open(parameter.c_str(), ESP_SD_FILE_WRITE);
if (f.isOpen()) {
f.close();
output->printMSG ("ok");
} else {
output->printERROR ("failed!");
response = false;
}
return response;
}
output->printERROR ("Incorrect command!");
return false;
}

View File

@ -22,7 +22,7 @@
#define _VERSION_ESP3D_H
//version and sources location
#define FW_VERSION "3.0.0.a22"
#define FW_VERSION "3.0.0.a23"
#define REPOSITORY "https://github.com/luc-github/ESP3D"
#endif //_VERSION_ESP3D_H

View File

@ -98,7 +98,7 @@ ESP_File::ESP_File(const char * name, const char * filename, bool isdir, size_t
_filename = filename;
_name = name;
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
memset (&_lastwrite,0,sizeof(time_t));
_lastwrite = 0;
#endif //FILESYSTEM_TIMESTAMP_FEATURE
_iswritemode = false;
_size = size;
@ -211,7 +211,7 @@ ESP_File& ESP_File::operator=(const ESP_File & other)
_iswritemode = other._iswritemode;
_dirlist = other._dirlist;
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
memcpy(&_lastwrite, &(other._lastwrite), sizeof (time_t));
_lastwrite = other._lastwrite;
#endif //FILESYSTEM_TIMESTAMP_FEATURE
return *this;
}

View File

@ -85,12 +85,11 @@ ESP_SDFile::ESP_SDFile(const char * name, const char * filename, bool isdir, siz
{
_isdir = isdir;
_dirlist = "";
_isfakedir = isdir;
_index = -1;
_filename = filename;
_name = name;
#ifdef SD_TIMESTAMP_FEATURE
memset (&_lastwrite,0,sizeof(time_t));
_lastwrite = 0;
#endif //SD_TIMESTAMP_FEATURE
_iswritemode = false;
_size = size;
@ -195,7 +194,6 @@ ESP_SDFile& ESP_SDFile::operator=(const ESP_SDFile & other)
{
//log_esp3d("Copy %s", other._filename.c_str());
_isdir = other._isdir;
_isfakedir = other._isfakedir;
_index = other._index;
_filename = other._filename;
_name = other._name;
@ -203,7 +201,7 @@ ESP_SDFile& ESP_SDFile::operator=(const ESP_SDFile & other)
_iswritemode = other._iswritemode;
_dirlist = other._dirlist;
#ifdef SD_TIMESTAMP_FEATURE
memcpy(&_lastwrite, &(other._lastwrite), sizeof (time_t));
_lastwrite = other._lastwrite;
#endif //SD_TIMESTAMP_FEATURE
return *this;
}

View File

@ -60,7 +60,6 @@ public:
private:
String _dirlist;
bool _isdir;
bool _isfakedir;
bool _iswritemode;
int8_t _index;
String _filename;

View File

@ -207,13 +207,12 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
{
_isdir = isdir;
_dirlist = "";
_isfakedir = false;
_index = -1;
_filename = "";
_name = "";
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
memset (&_lastwrite,0,sizeof(time_t));
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#endif //SD_TIMESTAMP_FEATURE
_iswritemode = iswritemode;
_size = 0;
if (!handle) {
@ -228,7 +227,6 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
_filename = path;
if (_name.endsWith("/")) {
_name.remove( _name.length() - 1,1);
_isfakedir = true;
_isdir = true;
}
if (_name[0] == '/') {
@ -241,9 +239,9 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
//size
_size = tSDFile_handle[i].size();
//time
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
_lastwrite = tSDFile_handle[i].getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#endif //SD_TIMESTAMP_FEATURE
_index = i;
//log_esp3d("Opening File at index %d",_index);
set = true;
@ -262,9 +260,9 @@ void ESP_SDFile::close()
File ftmp = SD.open(_filename.c_str());
if (ftmp) {
_size = ftmp.size();
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
_lastwrite = ftmp.getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#endif //SD_TIMESTAMP_FEATURE
ftmp.close();
}
}

View File

@ -29,9 +29,47 @@ sd_native_esp8266.cpp - ESP3D sd support class
extern sdfat::File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
using namespace sdfat;
SdFat SD;
#ifdef SD_TIMESTAMP_FEATURE
void dateTime (uint16_t* date, uint16_t* dtime)
{
struct tm tmstruct;
time_t now;
time (&now);
localtime_r (&now, &tmstruct);
*date = FAT_DATE ( (tmstruct.tm_year) + 1900, ( tmstruct.tm_mon) + 1, tmstruct.tm_mday);
*dtime = FAT_TIME (tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
}
time_t getDateTimeFile(File & filehandle)
{
time_t dt = 0;
struct tm timefile;
memset((void *)&timefile, 0, sizeof(tm));
dir_t d;
if (filehandle.dirEntry(&d)) {
timefile.tm_year = FAT_YEAR(d.lastWriteDate) - 1900;
timefile.tm_mon = FAT_MONTH(d.lastWriteDate) - 1;
timefile.tm_mday = FAT_DAY(d.lastWriteDate);
timefile.tm_hour = FAT_HOUR(d.lastWriteTime);
timefile.tm_min = FAT_MINUTE(d.lastWriteTime);
timefile.tm_sec = FAT_SECOND(d.lastWriteTime);
timefile.tm_isdst = -1;
if (mktime(&timefile) != -1) {
dt = mktime(&timefile);
} else {
log_esp3d("mktime failed");
}
} else {
log_esp3d("stat file failed");
}
return dt;
}
#endif //SD_TIMESTAMP_FEATURE
uint8_t ESP_SD::getState(bool refresh)
{
#if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1
@ -70,6 +108,10 @@ bool ESP_SD::begin()
if (_spi_speed_divider <= 0) {
_spi_speed_divider = 1;
}
#ifdef SD_TIMESTAMP_FEATURE
//set callback to get time on files on SD
SdFile::dateTimeCallback (dateTime);
#endif //SD_TIMESTAMP_FEATURE
if (getState(true) == ESP_SDCARD_IDLE) {
freeBytes();
}
@ -108,6 +150,7 @@ uint64_t ESP_SD::freeBytes()
bool ESP_SD::format()
{
//not available yet
//SDFat has a feature for this
return false;
}
@ -222,13 +265,12 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
{
_isdir = isdir;
_dirlist = "";
_isfakedir = false;
_index = -1;
_filename = "";
_name = "";
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
memset (&_lastwrite,0,sizeof(time_t));
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#endif //SD_TIMESTAMP_FEATURE
_iswritemode = iswritemode;
_size = 0;
if (!handle) {
@ -246,7 +288,6 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
_name = tmp;
if (_name.endsWith("/")) {
_name.remove( _name.length() - 1,1);
_isfakedir = true;
_isdir = true;
}
if (_name[0] == '/') {
@ -259,9 +300,15 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
//size
_size = tSDFile_handle[i].size();
//time
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
_lastwrite = tSDFile_handle[i].getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
if (!_isdir) {
_lastwrite = getDateTimeFile(tSDFile_handle[i]);
} else {
//no need date time for directory
_lastwrite = 0;
}
#endif //SD_TIMESTAMP_FEATURE
_index = i;
//log_esp3d("Opening File at index %d",_index);
set = true;
@ -277,7 +324,9 @@ const char* ESP_SDFile::shortname() const
ftmp.getSFN(sname);
ftmp.close();
return sname;
} else return _name.c_str();
} else {
return _name.c_str();
}
}
void ESP_SDFile::close()
@ -291,9 +340,9 @@ void ESP_SDFile::close()
sdfat::File ftmp = SD.open(_filename.c_str());
if (ftmp) {
_size = ftmp.size();
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
_lastwrite = ftmp.getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
_lastwrite = getDateTimeFile(ftmp);
#endif //SD_TIMESTAMP_FEATURE
ftmp.close();
}
}
@ -328,7 +377,7 @@ ESP_SDFile ESP_SDFile::openNextFile()
const char * ESP_SD::FilesystemName()
{
return "SD native";
return "SDFat";
}
#endif //SD_DEVICE == ESP_SD_NATIVE

View File

@ -30,6 +30,45 @@ extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
#define FREQMZ 40
SdFat SD;
#ifdef SD_TIMESTAMP_FEATURE
void dateTime (uint16_t* date, uint16_t* dtime)
{
struct tm tmstruct;
time_t now;
time (&now);
localtime_r (&now, &tmstruct);
*date = FAT_DATE ( (tmstruct.tm_year) + 1900, ( tmstruct.tm_mon) + 1, tmstruct.tm_mday);
*dtime = FAT_TIME (tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
}
time_t getDateTimeFile(File & filehandle)
{
time_t dt = 0;
struct tm timefile;
memset((void *)&timefile, 0, sizeof(tm));
dir_t d;
if (filehandle.dirEntry(&d)) {
timefile.tm_year = FAT_YEAR(d.lastWriteDate) - 1900;
timefile.tm_mon = FAT_MONTH(d.lastWriteDate) - 1;
timefile.tm_mday = FAT_DAY(d.lastWriteDate);
timefile.tm_hour = FAT_HOUR(d.lastWriteTime);
timefile.tm_min = FAT_MINUTE(d.lastWriteTime);
timefile.tm_sec = FAT_SECOND(d.lastWriteTime);
timefile.tm_isdst = -1;
if (mktime(&timefile) != -1) {
dt = mktime(&timefile);
} else {
log_esp3d("mktime failed");
}
} else {
log_esp3d("stat file failed");
}
return dt;
}
#endif //SD_TIMESTAMP_FEATURE
uint8_t ESP_SD::getState(bool refresh)
{
#if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1
@ -68,6 +107,10 @@ bool ESP_SD::begin()
if (_spi_speed_divider <= 0) {
_spi_speed_divider = 1;
}
#ifdef SD_TIMESTAMP_FEATURE
//set callback to get time on files on SD
SdFile::dateTimeCallback (dateTime);
#endif //SD_TIMESTAMP_FEATURE
if (getState(true) == ESP_SDCARD_IDLE) {
freeBytes();
}
@ -106,6 +149,7 @@ uint64_t ESP_SD::freeBytes()
bool ESP_SD::format()
{
//not available yet
//SDFat has a feature for this
return false;
}
@ -220,13 +264,12 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
{
_isdir = isdir;
_dirlist = "";
_isfakedir = false;
_index = -1;
_filename = "";
_name = "";
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
memset (&_lastwrite,0,sizeof(time_t));
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
_lastwrite = 0 ;
#endif //SD_TIMESTAMP_FEATURE
_iswritemode = iswritemode;
_size = 0;
if (!handle) {
@ -244,7 +287,6 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
_name = tmp;
if (_name.endsWith("/")) {
_name.remove( _name.length() - 1,1);
_isfakedir = true;
_isdir = true;
}
if (_name[0] == '/') {
@ -257,9 +299,15 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
//size
_size = tSDFile_handle[i].size();
//time
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
_lastwrite = tSDFile_handle[i].getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
if (!_isdir) {
_lastwrite = getDateTimeFile(tSDFile_handle[i]);
} else {
//no need date time for directory
_lastwrite = 0;
}
#endif //SD_TIMESTAMP_FEATURE
_index = i;
//log_esp3d("Opening File at index %d",_index);
set = true;
@ -275,7 +323,9 @@ const char* ESP_SDFile::shortname() const
ftmp.getSFN(sname);
ftmp.close();
return sname;
} else return _name.c_str();
} else {
return _name.c_str();
}
}
void ESP_SDFile::close()
@ -289,9 +339,9 @@ void ESP_SDFile::close()
File ftmp = SD.open(_filename.c_str());
if (ftmp) {
_size = ftmp.size();
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
_lastwrite = ftmp.getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
_lastwrite = getDateTimeFile(ftmp);
#endif //SD_TIMESTAMP_FEATURE
ftmp.close();
}
}
@ -326,7 +376,7 @@ ESP_SDFile ESP_SDFile::openNextFile()
const char * ESP_SD::FilesystemName()
{
return "SDfat";
return "SDFat";
}
#endif //SD_DEVICE == ESP_SD_NATIVE

View File

@ -196,13 +196,12 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
{
_isdir = isdir;
_dirlist = "";
_isfakedir = false;
_index = -1;
_filename = "";
_name = "";
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
memset (&_lastwrite,0,sizeof(time_t));
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#endif //SD_TIMESTAMP_FEATURE
_iswritemode = iswritemode;
_size = 0;
if (!handle) {
@ -217,7 +216,6 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
_filename = path;
if (_name.endsWith("/")) {
_name.remove( _name.length() - 1,1);
_isfakedir = true;
_isdir = true;
}
if (_name[0] == '/') {
@ -230,9 +228,9 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
//size
_size = tSDFile_handle[i].size();
//time
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
_lastwrite = tSDFile_handle[i].getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#endif //SD_TIMESTAMP_FEATURE
_index = i;
//log_esp3d("Opening File at index %d",_index);
set = true;
@ -251,9 +249,9 @@ void ESP_SDFile::close()
File ftmp = SD_MMC.open(_filename.c_str());
if (ftmp) {
_size = ftmp.size();
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#ifdef SD_TIMESTAMP_FEATURE
_lastwrite = ftmp.getLastWrite();
#endif //FILESYSTEM_TIMESTAMP_FEATURE
#endif //SD_TIMESTAMP_FEATURE
ftmp.close();
}
}

View File

@ -114,14 +114,26 @@ bool TimeServer::begin()
return _started;
}
const char * TimeServer::current_time()
const char * TimeServer::current_time(time_t t)
{
static String stmp;
struct tm tmstruct;
time_t now;
stmp = "";
time(&now);
localtime_r(&now, &tmstruct);
//get current time
if (t == 0) {
time(&now);
localtime_r(&now, &tmstruct);
} else {
/* struct tm * tmstructtmp = localtime(&t);
tmstruct.tm_year = tmstructtmp->tm_year;
tmstruct.tm_mon = tmstructtmp->tm_mon;
tmstruct.tm_mday = tmstructtmp->tm_mday;
tmstruct.tm_hour = tmstructtmp->tm_hour;
tmstruct.tm_min = tmstructtmp->tm_min;
tmstruct.tm_sec = tmstructtmp->tm_sec;*/
localtime_r(&t, &tmstruct);
}
stmp = String((tmstruct.tm_year)+1900) + "-";
if (((tmstruct.tm_mon)+1) < 10) {
stmp +="0";

View File

@ -23,6 +23,7 @@
#ifndef _TIME_SERVER_H
#define _TIME_SERVER_H
#include <time.h>
class TimeServer
{
@ -32,7 +33,7 @@ public:
bool begin();
void end();
void handle();
const char * current_time();
const char * current_time(time_t t = 0);
bool setTime(const char* stime);
bool started();
bool is_internet_time(bool readfromsettings = false);