Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
srwi committed Feb 27, 2021
1 parent 7846baa commit 3a35289
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 32 deletions.
6 changes: 3 additions & 3 deletions ESPEssentials.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef _ESPESSENTIALS_H_1436481
#define _ESPESSENTIALS_H_1436481
#ifndef ESPESSENTIALS_H
#define ESPESSENTIALS_H

#include "Wifi.h"
#include "WebServer.h"
#include "OTA.h"
#include "WebServer.h"

#define ESSENTIALS_BAUD 115200

Expand Down
24 changes: 19 additions & 5 deletions OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,25 @@ void OTAClass::init(char const *hostname, char const *password, uint16_t port)
onError([](ota_error_t error)
{
Serial.printf("[OTA] Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");

switch(error)
{
case OTA_AUTH_ERROR:
Serial.println("Auth Failed");
break;
case OTA_BEGIN_ERROR:
Serial.println("Begin Failed");
break;
case OTA_CONNECT_ERROR:
Serial.println("Connect Failed");
break;
case OTA_RECEIVE_ERROR:
Serial.println("Receive Failed");
break;
case OTA_END_ERROR:
Serial.println("End Failed");
break;
}
});

begin();
Expand Down
7 changes: 4 additions & 3 deletions OTA.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef _OTA_H_714124218
#define _OTA_H_714124218
#ifndef ESPESSENTIALS_OTA_H
#define ESPESSENTIALS_OTA_H

#include <ArduinoOTA.h>
#include "Wifi.h"

#include <ArduinoOTA.h>

class OTAClass : public ArduinoOTAClass
{
public:
Expand Down
31 changes: 15 additions & 16 deletions WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void WebServerClass::init()
Serial.println("[Storage] Couldn't mount file system.");
return;
}

on("/edit", HTTP_GET, [&]()
{
if(!handleFileRead("/edit.htm"))
Expand All @@ -32,7 +32,7 @@ void WebServerClass::init()
});
on("/edit", HTTP_PUT, _handle_file_create);
on("/edit", HTTP_DELETE, _handle_file_delete);
on("/edit", HTTP_POST, [&](){ send(200, "text/plain", ""); }, _handle_file_upload);
on("/edit", HTTP_POST, [&]() { send(200, "text/plain", ""); }, _handle_file_upload);
on("/list", HTTP_GET, _handle_file_list);
on("/update", HTTP_GET, [&]()
{
Expand Down Expand Up @@ -64,7 +64,7 @@ void WebServerClass::init()
send(404, "text/plain", "Oops, file not found!");
}
});

begin();
}

Expand Down Expand Up @@ -106,7 +106,7 @@ String WebServerClass::getContentType(String filename)
bool WebServerClass::handleFileRead(String path)
{
Serial.println("[Storage] File read: " + path);

webserverBusy = true;
if(path.endsWith("/")) path += "index.htm";
String contentType = getContentType(path);
Expand Down Expand Up @@ -135,7 +135,7 @@ void WebServerClass::handleFileUpload()
return;

HTTPUpload& _upload = upload();

if(_upload.status == UPLOAD_FILE_START)
{
String filename = _upload.filename;
Expand All @@ -148,7 +148,6 @@ void WebServerClass::handleFileUpload()
}
else if(_upload.status == UPLOAD_FILE_WRITE)
{
//Serial.print("[Storage] Receiving..." + _upload.currentSize);
if(fsUploadFile)
fsUploadFile.write(_upload.buf, _upload.currentSize);
}
Expand All @@ -164,17 +163,17 @@ void WebServerClass::handleFileDelete()
{
if(args() == 0)
return send(500, "text/plain", "BAD ARGS");

String path = arg(0);
Serial.println("[Storage] Deleting file: " + path);

if(path == "/")
return send(500, "text/plain", "BAD PATH");
if(!SPIFFS.exists(path))
return send(404, "text/plain", "Oops, file not found (3)!");

SPIFFS.remove(path);

send(200, "text/plain", "");
path = String();
}
Expand All @@ -183,22 +182,22 @@ void WebServerClass::handleFileCreate()
{
if(args() == 0)
return send(500, "text/plain", "BAD ARGS");

String path = arg(0);
Serial.println("[Storage] Creating file: " + path);

if(path == "/")
return send(500, "text/plain", "BAD PATH");
if(SPIFFS.exists(path))
return send(500, "text/plain", "File already exists!");

File file = SPIFFS.open(path, "w");

if(file)
file.close();
else
return send(500, "text/plain", "Oops, creating file failed!");

send(200, "text/plain", "");
path = String();
}
Expand All @@ -210,7 +209,7 @@ void WebServerClass::handleFileList()
send(500, "text/plain", "BAD ARGS");
return;
}

String path = arg("dir");
Dir dir = SPIFFS.openDir(path);
path = String();
Expand Down
6 changes: 3 additions & 3 deletions WebServer.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef _WEBSERVER_H_471412218
#define _WEBSERVER_H_471412218
#ifndef ESPESSENTIALS_WEBSERVER_H
#define ESPESSENTIALS_WEBSERVER_H

#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <WiFiUdp.h>

class WebServerClass : public ESP8266WebServer
{
Expand Down
4 changes: 2 additions & 2 deletions Wifi.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _WIFI_H_471412218
#define _WIFI_H_471412218
#ifndef ESPESSENTIALS_WIFI_H
#define ESPESSENTIALS_WIFI_H

#include <WiFiManager.h>

Expand Down

0 comments on commit 3a35289

Please sign in to comment.