From a8f07a66bf76c46393044e909d7c1d2c9603161d Mon Sep 17 00:00:00 2001 From: Dawid Duniec Date: Mon, 15 Jan 2024 18:58:00 +0100 Subject: [PATCH] Downloading of content files --- data/data.csv | 0 data/index.html | 9 --------- data/indexAP.html | 18 ------------------ src/FileService.cpp | 32 ++++++++++++++++++++++++++++++++ src/FileService.h | 1 + src/FlashDataHelper.cpp | 17 +++++++++++++++++ src/FlashDataHelper.h | 15 +++++++++++++++ src/WifiServer.cpp | 8 +++++--- src/main.cpp | 9 ++++++++- 9 files changed, 78 insertions(+), 31 deletions(-) delete mode 100644 data/data.csv delete mode 100644 data/indexAP.html create mode 100644 src/FlashDataHelper.cpp create mode 100644 src/FlashDataHelper.h diff --git a/data/data.csv b/data/data.csv deleted file mode 100644 index e69de29..0000000 diff --git a/data/index.html b/data/index.html index 6c12fe9..151dddb 100644 --- a/data/index.html +++ b/data/index.html @@ -122,15 +122,6 @@
- - -
- -

- -

- -
diff --git a/data/indexAP.html b/data/indexAP.html deleted file mode 100644 index 0bf6273..0000000 --- a/data/indexAP.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - -
- -

- -

- -
- - - diff --git a/src/FileService.cpp b/src/FileService.cpp index 6d92f81..807cfc2 100644 --- a/src/FileService.cpp +++ b/src/FileService.cpp @@ -22,6 +22,10 @@ String FileServiceClass::openFile(String path) { Serial.println(path); Serial.println(F("Failed to read configuration file - FS error or file missing.")); + + file.close(); + _fileLock = false; + return ""; } @@ -34,6 +38,34 @@ String FileServiceClass::openFile(String path) return result; } +bool FileServiceClass::fileExists(String path) +{ + while (_fileLock) + { + } + + _fileLock = true; + + File file = _fs.open(path, "r"); + + if (!file || file.size() == 0) + { + Serial.println(path); + Serial.println(F("Failed to read file - FS error or file missing.")); + + file.close(); + _fileLock = false; + + return false; + } + + file.close(); + + _fileLock = false; + + return true; +} + void FileServiceClass::saveFile(String path, String content) { while (_fileLock) diff --git a/src/FileService.h b/src/FileService.h index ba6910b..a893cf3 100644 --- a/src/FileService.h +++ b/src/FileService.h @@ -11,6 +11,7 @@ class FileServiceClass String openFile(String path); void saveFile(String path, String content); void removeFile(String path); + bool fileExists(String path); private: FS& _fs; bool _fileLock = false; diff --git a/src/FlashDataHelper.cpp b/src/FlashDataHelper.cpp new file mode 100644 index 0000000..7b49fd3 --- /dev/null +++ b/src/FlashDataHelper.cpp @@ -0,0 +1,17 @@ + +#include "FlashDataHelper.h" + +void FlashDataHelperClass::downloadContentFiles(FileServiceClass &fileService) +{ + WiFiClientSecure client; + client.setInsecure(); + + HTTPClient https; + https.begin(client, "raw.githubusercontent.com", 443, "/stefunkk/OpenStill/master/data/index.html"); + + https.GET(); + String indexFile = https.getString(); + + fileService.saveFile("/index.html", indexFile); + fileService.saveFile("/data.csv", ""); +} diff --git a/src/FlashDataHelper.h b/src/FlashDataHelper.h new file mode 100644 index 0000000..7d56aaa --- /dev/null +++ b/src/FlashDataHelper.h @@ -0,0 +1,15 @@ +#ifndef __FLASHDATAHELPER_H__ +#define __FLASHDATAHELPER_H__ + +#include +#include "FileService.h" +#include +#include +#include + +class FlashDataHelperClass +{ +public: + static void downloadContentFiles(FileServiceClass &fileService); +}; +#endif // __FLASHDATAHELPER_H__ \ No newline at end of file diff --git a/src/WifiServer.cpp b/src/WifiServer.cpp index b0f04ab..fd31624 100644 --- a/src/WifiServer.cpp +++ b/src/WifiServer.cpp @@ -1,7 +1,8 @@ #include "WifiServer.h" WifiServerClass::WifiServerClass(AsyncWebServer &server, SettingsClass &wifiSettings, - SensorDataClass &sensorData, StillDataContextClass &context, ConfigurationServiceClass &configurationService) : _server(server), _settings(wifiSettings), _sensorData(sensorData), _context(context), _configurationService(configurationService) + SensorDataClass &sensorData, StillDataContextClass &context, ConfigurationServiceClass &configurationService) + : _server(server), _settings(wifiSettings), _sensorData(sensorData), _context(context), _configurationService(configurationService) { DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Origin"), F("*")); @@ -47,14 +48,15 @@ void WifiServerClass::setupAccessPoint() _server.begin(); - configurePages(); + configureAPPages(); configureInputs(); } void WifiServerClass::configureAPPages() { _server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(SPIFFS, "/indexAP.html", String(), false); + const char index_html[] PROGMEM = "




"; + request->send_P(200, "text/html", index_html); }); } diff --git a/src/main.cpp b/src/main.cpp index b3ad36b..d5be51e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "StillControllerTask.h" #include "NotificationTask.h" #include "WifiServer.h" +#include "FlashDataHelper.h" // // DS18B20 sensor settings const uint8_t sensorPin = GPIO_NUM_15; @@ -79,6 +80,7 @@ void setup() WaterName); auto *fileService = new FileServiceClass(); + auto *configurationService = new ConfigurationServiceClass(*fileService, *context, *settings); configurationService->loadConfiguration(); @@ -109,8 +111,13 @@ void setup() } else { - Serial.print("Connecting to WiFi"); + wifiClass->connectToWifi(); + + if (!fileService->fileExists("/index.html")) + { + FlashDataHelperClass::downloadContentFiles(*fileService); + } } taskManager.registerEvent(lcdTask);