first commit
This commit is contained in:
168
ESP8266_DOMOTICZ_OCTOPRINT_WEB/Files.cpp
Normal file
168
ESP8266_DOMOTICZ_OCTOPRINT_WEB/Files.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
/* Example showing timestamp support in LittleFS */
|
||||
/* Released into the public domain. */
|
||||
/* Earle F. Philhower, III <earlephilhower@yahoo.com> */
|
||||
|
||||
#include "Files.h"
|
||||
|
||||
bool Files::getLocalTime(struct tm * info, uint32_t ms) {
|
||||
uint32_t count = ms / 10;
|
||||
time_t now;
|
||||
|
||||
time(&now);
|
||||
localtime_r(&now, info);
|
||||
|
||||
if (info->tm_year > (2016 - 1900)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
while (count--) {
|
||||
delay(10);
|
||||
time(&now);
|
||||
localtime_r(&now, info);
|
||||
if (info->tm_year > (2016 - 1900)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Files::listDir(const char * dirname) {
|
||||
Serial.printf("Listing directory: %s\n", dirname);
|
||||
|
||||
Dir root = LittleFS.openDir(dirname);
|
||||
|
||||
while (root.next()) {
|
||||
File file = root.openFile("r");
|
||||
Serial.print(" FILE: ");
|
||||
Serial.print(root.fileName());
|
||||
Serial.print(" SIZE: ");
|
||||
Serial.print(file.size());
|
||||
time_t cr = file.getCreationTime();
|
||||
time_t lw = file.getLastWrite();
|
||||
file.close();
|
||||
struct tm * tmstruct = localtime(&cr);
|
||||
Serial.printf(" CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
|
||||
tmstruct = localtime(&lw);
|
||||
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Files::readFile(const char * path) {
|
||||
Serial.printf("Reading file: %s\n", path);
|
||||
|
||||
File file = LittleFS.open(path, "r");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Read from file: ");
|
||||
while (file.available()) {
|
||||
Serial.write(file.read());
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Files::writeFile(const char * path, const char * message) {
|
||||
Serial.printf("Writing file: %s\n", path);
|
||||
|
||||
File file = LittleFS.open(path, "w");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("File written");
|
||||
} else {
|
||||
Serial.println("Write failed");
|
||||
}
|
||||
delay(2000); // Make sure the CREATE and LASTWRITE times are different
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Files::appendFile(const char * path, const char * message) {
|
||||
Serial.printf("Appending to file: %s\n", path);
|
||||
|
||||
File file = LittleFS.open(path, "a");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for appending");
|
||||
return;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("Message appended");
|
||||
} else {
|
||||
Serial.println("Append failed");
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Files::renameFile(const char * path1, const char * path2) {
|
||||
Serial.printf("Renaming file %s to %s\n", path1, path2);
|
||||
if (LittleFS.rename(path1, path2)) {
|
||||
Serial.println("File renamed");
|
||||
} else {
|
||||
Serial.println("Rename failed");
|
||||
}
|
||||
}
|
||||
|
||||
void Files::deleteFile(const char * path) {
|
||||
Serial.printf("Deleting file: %s\n", path);
|
||||
if (LittleFS.remove(path)) {
|
||||
Serial.println("File deleted");
|
||||
} else {
|
||||
Serial.println("Delete failed");
|
||||
}
|
||||
}
|
||||
|
||||
void Files::init() {
|
||||
|
||||
// /////FTP Setup, ensure SPIFFS is started before ftp; /////////
|
||||
// #ifdef ESP32 //esp32 we send true to format spiffs if cannot mount
|
||||
// if (SPIFFS.begin(true)) {
|
||||
// #elif defined ESP8266
|
||||
// if (SPIFFS.begin()) {
|
||||
// #endif
|
||||
// Serial.println("SPIFFS opened!");
|
||||
// ftpSrv.begin("esp8266","esp8266"); //username, password for ftp. set ports in ESP8266FtpServer.h (default 21, 50009 for PASV)
|
||||
// }
|
||||
|
||||
LittleFSConfig cfg;
|
||||
cfg.setAutoFormat(false);
|
||||
LittleFS.setConfig(cfg);
|
||||
|
||||
// configTime(3600 * timezone, daysavetime * 3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
|
||||
// struct tm tmstruct ;
|
||||
// delay(2000);
|
||||
// tmstruct.tm_year = 0;
|
||||
// getLocalTime(&tmstruct, 5000);
|
||||
// Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
|
||||
// Serial.println("");
|
||||
// Serial.println("Formatting LittleFS filesystem");
|
||||
// LittleFS.format();
|
||||
Serial.println("Mount LittleFS");
|
||||
if (!LittleFS.begin()) {
|
||||
Serial.println("LittleFS mount failed");
|
||||
return;
|
||||
}
|
||||
// listDir("/");
|
||||
// deleteFile("/hello.txt");
|
||||
// writeFile("/hello.txt", "Hello ");
|
||||
// appendFile("/hello.txt", "World!\n");
|
||||
// listDir("/");
|
||||
|
||||
// Serial.println("The timestamp should be valid above");
|
||||
|
||||
// Serial.println("Now unmount and remount and perform the same operation.");
|
||||
// Serial.println("Timestamp should be valid, data should be good.");
|
||||
// LittleFS.end();
|
||||
// Serial.println("Now mount it");
|
||||
// if (!LittleFS.begin()) {
|
||||
// Serial.println("LittleFS mount failed");
|
||||
// return;
|
||||
// }
|
||||
// readFile("/hello.txt");
|
||||
// listDir("/");
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user