From b511a1abac5e5b9ddf6e775693f3db6f652db1d9 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Sun, 27 Dec 2020 16:18:27 +0100
Subject: [PATCH] No access to Filesystem without AMPEL_CSV

---
 mqtt.cpp       |  8 ++++++--
 mqtt.h         |  4 +++-
 util.cpp       |  4 +++-
 util.h         |  5 +++--
 web_server.cpp | 23 +++++++++++++++++------
 5 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/mqtt.cpp b/mqtt.cpp
index e7e6e33..98eefc8 100644
--- a/mqtt.cpp
+++ b/mqtt.cpp
@@ -77,6 +77,7 @@ namespace mqtt {
     LedEffects::showKITTWheel(color::green, 1);
   }
 
+#ifdef AMPEL_CSV
   void setCSVinterval(String messageString) {
     messageString.replace("csv ", "");
     config::csv_interval = messageString.toInt();
@@ -85,6 +86,7 @@ namespace mqtt {
     Serial.println("s.");
     LedEffects::showKITTWheel(color::green, 1);
   }
+#endif
 
   void calibrateSensorToSpecificPPM(String messageString) {
     messageString.replace("calibrate ", "");
@@ -149,14 +151,16 @@ namespace mqtt {
       calibrateSensorToSpecificPPM(messageString);
     } else if (messageString.startsWith("mqtt ")) {
       setMQTTinterval(messageString);
-    } else if (messageString.startsWith("csv ")) {
-      setCSVinterval(messageString);
     } else if (messageString == "publish") {
       Serial.println(F("Forcing MQTT publish now."));
       publish(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
+#ifdef AMPEL_CSV
+    } else if (messageString.startsWith("csv ")) {
+      setCSVinterval(messageString);
     } else if (messageString == "format_filesystem") {
       FS_LIB.format();
       LedEffects::showKITTWheel(color::blue, 2);
+#endif
     } else if (messageString == "night_mode") {
       LedEffects::toggleNightMode();
     } else if (messageString == "local_ip") {
diff --git a/mqtt.h b/mqtt.h
index 9af46c8..8b520cc 100644
--- a/mqtt.h
+++ b/mqtt.h
@@ -4,7 +4,9 @@
 #include <Arduino.h>
 #include "config.h"
 #include "led_effects.h"
-#include "csv_writer.h"
+#ifdef AMPEL_CSV
+#  include "csv_writer.h"
+#endif
 #include "co2_sensor.h"
 #include "src/lib/PubSubClient/src/PubSubClient.h"
 #include "wifi_util.h"
diff --git a/util.cpp b/util.cpp
index cd7c53b..c32a5c5 100644
--- a/util.cpp
+++ b/util.cpp
@@ -39,8 +39,10 @@ namespace ntp {
 }
 
 void resetAmpel() {
-  Serial.print("Resetting");
+  Serial.print(F("Resetting"));
+#ifdef AMPEL_CSV
   FS_LIB.end();
+#endif
   LedEffects::LEDsOff();
   delay(1000);
   ESP.restart();
diff --git a/util.h b/util.h
index 5a46bd1..c9a8a94 100644
--- a/util.h
+++ b/util.h
@@ -3,8 +3,9 @@
 #include <Arduino.h>
 #include "config.h"
 #include "wifi_util.h" // To get MAC
-//FIXME: Only if CSV
-#include "csv_writer.h" // To close filesystem before reset
+#ifdef AMPEL_CSV
+#  include "csv_writer.h" // To close filesystem before reset
+#endif
 
 #include <WiFiUdp.h> //required for NTP
 #include "src/lib/NTPClient-master/NTPClient.h" // NTP
diff --git a/web_server.cpp b/web_server.cpp
index c96b2f4..ae2ad1f 100644
--- a/web_server.cpp
+++ b/web_server.cpp
@@ -21,9 +21,12 @@ namespace web_server {
   const char *body_template;
   const char *script_template;
   void handleWebServerRoot();
-  void handleWebServerCSV();
   void handlePageNotFound();
+
+#ifdef AMPEL_CSV
   void handleDeleteCSV();
+  void handleWebServerCSV();
+#endif
 
 #if defined(ESP8266)
   ESP8266WebServer http(80); // Create a webserver object that listens for HTTP request on port 80
@@ -198,8 +201,6 @@ namespace web_server {
     ss -= hh * 3600;
     uint8_t mm = ss / 60;
     ss -= mm * 60;
-    //FIXME: only if AMPEL_CSV
-    uint16_t available_fs_space = csv_writer::getAvailableSpace() / 1024;
 
     //NOTE: Splitting in multiple parts in order to use less RAM
     char content[2000]; // Update if needed
@@ -207,7 +208,11 @@ namespace web_server {
 
     // Header
     snprintf_P(content, sizeof(content), header_template, sensor::co2, SENSOR_ID.c_str(),
-        WiFi.localIP().toString().c_str(), csv_writer::filename.c_str());
+        WiFi.localIP().toString().c_str()
+#ifdef AMPEL_CSV
+        , csv_writer::filename.c_str()
+#endif
+        );
 
     http.setContentLength(CONTENT_LENGTH_UNKNOWN);
     http.send_P(200, PSTR("text/html"), content);
@@ -216,7 +221,7 @@ namespace web_server {
     snprintf_P(content, sizeof(content), body_template, SENSOR_ID.c_str(), sensor::co2, sensor::temperature,
         sensor::humidity, sensor::timestamp.c_str(), config::measurement_timestep,
 #ifdef AMPEL_CSV
-        csv_writer::last_successful_write.c_str(), config::csv_interval, available_fs_space,
+        csv_writer::last_successful_write.c_str(), config::csv_interval, csv_writer::getAvailableSpace() / 1024,
 #endif
 #ifdef AMPEL_MQTT
         mqtt::last_successful_publish.c_str(), config::sending_interval,
@@ -231,11 +236,16 @@ namespace web_server {
     http.sendContent(content);
 
     // Script
-    snprintf_P(content, sizeof(content), script_template, csv_writer::filename.c_str(), SENSOR_ID.c_str());
+    snprintf_P(content, sizeof(content), script_template
+#ifdef AMPEL_CSV
+        , csv_writer::filename.c_str(), SENSOR_ID.c_str()
+#endif
+        );
 
     http.sendContent(content);
   }
 
+#ifdef AMPEL_CSV
   void handleWebServerCSV() {
     if (!shouldBeAllowed()) {
       return http.requestAuthentication(DIGEST_AUTH);
@@ -260,6 +270,7 @@ namespace web_server {
     http.sendHeader("Location", "/");
     http.send(303);
   }
+#endif
 
   void handlePageNotFound() {
     http.send(404, F("text/plain"), F("404: Not found"));
-- 
GitLab