Skip to content

Commit

Permalink
Downloading of content files
Browse files Browse the repository at this point in the history
  • Loading branch information
dawiduniec committed Jan 15, 2024
1 parent 656ba9d commit a8f07a6
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 31 deletions.
Empty file removed data/data.csv
Empty file.
9 changes: 0 additions & 9 deletions data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,6 @@

<body>
<div id="app">


<form v-if="false" action="/set" method="get">
<label for="wifiSsid">Wifi SSID</label>
<input type="text" name="wifiSsid"><br /><br />
<label for="wifiPassword">Wifi Pass</label>
<input type="text" name="wifiPassword"><br /><br />
<input type="submit" value="Ok">
</form>
<div class="container-fluid" v-cloak>
<div class="row">
<div class="col">
Expand Down
18 changes: 0 additions & 18 deletions data/indexAP.html

This file was deleted.

32 changes: 32 additions & 0 deletions src/FileService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/FileService.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/FlashDataHelper.cpp
Original file line number Diff line number Diff line change
@@ -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", "");
}
15 changes: 15 additions & 0 deletions src/FlashDataHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __FLASHDATAHELPER_H__
#define __FLASHDATAHELPER_H__

#include <WiFi.h>
#include "FileService.h"
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <TaskManagerIO.h>

class FlashDataHelperClass
{
public:
static void downloadContentFiles(FileServiceClass &fileService);
};
#endif // __FLASHDATAHELPER_H__
8 changes: 5 additions & 3 deletions src/WifiServer.cpp
Original file line number Diff line number Diff line change
@@ -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("*"));
Expand Down Expand Up @@ -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 = "<html> <head> <meta charset=\"UTF-8\"> </head> <body> <form v-if=\"false\" action=\"/set\" method=\"get\"> <label for=\"wifiSsid\">Wifi SSID</label> <input type=\"text\" name=\"wifiSsid\"><br /><br /> <label for=\"wifiPassword\">Wifi Pass</label> <input type=\"text\" name=\"wifiPassword\"><br /><br /> <input type=\"submit\" value=\"Ok\"> </form> </body> </html>";
request->send_P(200, "text/html", index_html);
});
}

Expand Down
9 changes: 8 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,6 +80,7 @@ void setup()
WaterName);

auto *fileService = new FileServiceClass();

auto *configurationService = new ConfigurationServiceClass(*fileService, *context, *settings);
configurationService->loadConfiguration();

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a8f07a6

Please # to comment.