removed old classes, included pwa for testing
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include "Config.h"
|
||||
|
||||
Config::Config()
|
||||
{
|
||||
if (!LittleFS.begin())
|
||||
{
|
||||
Serial.println("An Error has occurred while mounting LittleFS");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Config::loadConfig()
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
File config_file = LittleFS.open("/config.json","r");
|
||||
deserializeJson(doc, config_file);
|
||||
this->SSID=doc["SSID"].as<const char*>();
|
||||
this->PASS=doc["PASS"].as<const char*>();
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include "LittleFS.h"
|
||||
#include "ArduinoJson.h"
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
Config();
|
||||
const char *SSID;
|
||||
const char *PASS;
|
||||
int THEME;
|
||||
bool loadConfig();
|
||||
};
|
||||
@@ -1,54 +0,0 @@
|
||||
#include "Persistence.h"
|
||||
|
||||
Persistence::Persistence()
|
||||
{
|
||||
if (!LittleFS.begin())
|
||||
{
|
||||
Serial.println("An Error has occurred while mounting LittleFS");
|
||||
return;
|
||||
}
|
||||
}
|
||||
String Persistence::TestRead()
|
||||
{
|
||||
File file = LittleFS.open("/users.json", "r");
|
||||
String result = "";
|
||||
if (!file)
|
||||
{
|
||||
Serial.println("Failed to open file for reading");
|
||||
return result;
|
||||
}
|
||||
Serial.println("File Content:");
|
||||
while (file.available())
|
||||
{
|
||||
result += (char)file.read();
|
||||
}
|
||||
file.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
Persistence::Configuration Persistence::loadConfig()
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
File config_file = LittleFS.open("/config.json","r");
|
||||
deserializeJson(doc, config_file);
|
||||
Configuration res;
|
||||
res.SSID=doc["SSID"].as<const char*>();
|
||||
res.PASS=doc["PASS"].as<const char*>();
|
||||
return res;
|
||||
}
|
||||
DynamicJsonDocument Persistence::readUsers(){
|
||||
File user_file = LittleFS.open("/users.json","r");
|
||||
size_t buffersize = user_file.size()+512;
|
||||
Serial.printf("Reserved %i for reading UserDB File", buffersize);
|
||||
DynamicJsonDocument doc(user_file.size()+512);
|
||||
deserializeJson(doc, user_file);
|
||||
user_file.close();
|
||||
return doc;
|
||||
}
|
||||
bool Persistence::saveUsers(DynamicJsonDocument &doc){
|
||||
File user_file = LittleFS.open("/users.json","w");
|
||||
serializeJson(doc, user_file);
|
||||
user_file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
#include "LittleFS.h"
|
||||
#include "ArduinoJson.h"
|
||||
class Persistence
|
||||
{
|
||||
public:
|
||||
Persistence();
|
||||
String TestRead();
|
||||
typedef struct Configuration{
|
||||
const char* SSID;
|
||||
const char* PASS;
|
||||
int THEME;
|
||||
} config;
|
||||
Configuration loadConfig();
|
||||
DynamicJsonDocument readUsers();
|
||||
bool saveUsers(DynamicJsonDocument &doc);
|
||||
};
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "Persistence.h"
|
||||
#include "Config.h"
|
||||
#include "LittleFS.h"
|
||||
#include <sstream>
|
||||
#include <iterator>
|
||||
|
||||
-118
@@ -1,118 +0,0 @@
|
||||
#include "Users.h"
|
||||
|
||||
|
||||
Users::Users(Persistence &p): persistence(p){
|
||||
}
|
||||
Users::~Users()
|
||||
{
|
||||
}
|
||||
bool Users::ImportFromPersistence(){
|
||||
Serial.println("Importing Users from Persistence");
|
||||
DynamicJsonDocument user_persistence = persistence.readUsers();
|
||||
JsonArray array = user_persistence.as<JsonArray>();
|
||||
for(JsonObject userdata : array){
|
||||
User *imported = new User();
|
||||
imported->uid= userdata["ID"].as<unsigned long>();
|
||||
if(_userdb.count(*imported))
|
||||
continue;
|
||||
imported->first_name = userdata["FIRST_NAME"].as<String>();
|
||||
imported->last_name = userdata["LAST_NAME"].as<String>();
|
||||
imported->rfid_uid = userdata["RFID_UID"].as<String>();
|
||||
imported->user_pin = userdata["USER_PIN"].as<String>();
|
||||
imported->enabled = userdata["ENABLED"].as<bool>();
|
||||
this->_userdb.insert(*imported);
|
||||
}
|
||||
Serial.println("User import is done!");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Users::ExportToPersistence(){
|
||||
Serial.println("Exporting Users to Persistence");
|
||||
size_t user_amount = _userdb.size()+2;
|
||||
Serial.print( " User Amount: " );
|
||||
Serial.print(user_amount);
|
||||
size_t capacity = JSON_ARRAY_SIZE(user_amount) + user_amount*JSON_OBJECT_SIZE(6);
|
||||
Serial.print(" JSON Capacity");
|
||||
Serial.print(capacity);
|
||||
Serial.print(" Free Heap: ");
|
||||
Serial.print(ESP.getFreeHeap());
|
||||
//DynamicJsonDocument<CAPACITY> doc;
|
||||
DynamicJsonDocument doc(capacity);
|
||||
|
||||
for(Users::User u : _userdb){
|
||||
JsonObject exported_user = doc.createNestedObject();
|
||||
exported_user["ID"] = u.uid;
|
||||
exported_user["FIRST_NAME"] = u.first_name;
|
||||
exported_user["LAST_NAME"] = u.last_name;
|
||||
exported_user["RFID_UID"] = u.rfid_uid;
|
||||
exported_user["USER_PIN"] = u.user_pin;
|
||||
exported_user["ENABLED"] = u.enabled;
|
||||
}
|
||||
persistence.saveUsers(doc);
|
||||
Serial.println("User export is done!");
|
||||
return true;
|
||||
}
|
||||
bool Users::checkPin(String pin_code, std::vector<Users::User> *logon_users)
|
||||
{
|
||||
std::copy_if(this->_userdb.begin(), this->_userdb.end(), inserter(*logon_users, logon_users->end()), [=](user_account user)
|
||||
{ return pin_code == user.user_pin&&user.enabled==true; });
|
||||
return (logon_users->size() > 0);
|
||||
}
|
||||
|
||||
bool Users::checkRfid(String rfid_code, std::vector<Users::User> *logon_users)
|
||||
{
|
||||
std::copy_if(this->_userdb.begin(), this->_userdb.end(), inserter(*logon_users, logon_users->end()), [=](user_account user)
|
||||
{ return rfid_code == user.rfid_uid&&user.enabled==true; });
|
||||
return (logon_users->size() > 0);
|
||||
}
|
||||
|
||||
unsigned long Users::addUser(String first_name, String last_name, String rifd_uid, String user_pin)
|
||||
{
|
||||
Serial.println("Adding User:" + first_name);
|
||||
unsigned long new_id = this->_userdb.rbegin()->uid + 1;
|
||||
this->_userdb.insert(user_account{.uid = new_id, .first_name = first_name, .last_name = last_name, .rfid_uid = rifd_uid, .user_pin = user_pin});
|
||||
return new_id;
|
||||
}
|
||||
|
||||
bool Users::delUser(unsigned long id)
|
||||
{
|
||||
return this->_userdb.erase(user_account{.uid = id})>0;
|
||||
}
|
||||
|
||||
bool Users::updateUser(unsigned long id, ATTRIBUTES attr, String value)
|
||||
{
|
||||
auto modify = this->_userdb.find(user_account{.uid = id});
|
||||
switch (attr)
|
||||
{
|
||||
case FIRST_NAME:
|
||||
modify->first_name = value;
|
||||
break;
|
||||
case LAST_NAME:
|
||||
modify->last_name = value;
|
||||
break;
|
||||
case RFID_UID:
|
||||
modify->rfid_uid = value;
|
||||
break;
|
||||
case USER_PIN:
|
||||
modify->user_pin = value;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t Users::countUsers(){
|
||||
return _userdb.size();
|
||||
}
|
||||
String Users::toString(User *user)
|
||||
{
|
||||
return "UID: " + String(user->uid) + " Name: " + user->first_name + " " + user->last_name;
|
||||
}
|
||||
void Users::PrintAllToSerial(){
|
||||
Serial.println("#All Users:");
|
||||
for(Users::User u : _userdb){
|
||||
Serial.println(toString(&u));
|
||||
}
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include "Persistence.h"
|
||||
class Users
|
||||
{
|
||||
public:
|
||||
enum ATTRIBUTES{
|
||||
FIRST_NAME,
|
||||
LAST_NAME,
|
||||
RFID_UID,
|
||||
USER_PIN
|
||||
};
|
||||
typedef struct User
|
||||
{
|
||||
//User(unsigned long new_uid): uid(new_uid){}
|
||||
unsigned long uid;
|
||||
mutable String first_name;
|
||||
mutable String last_name;
|
||||
mutable String rfid_uid;
|
||||
mutable String user_pin;
|
||||
mutable bool enabled = true;
|
||||
bool operator < (const User &o) const { return uid <o.uid; }
|
||||
bool operator == (const User &o) const { return uid ==o.uid; }
|
||||
|
||||
} user_account;
|
||||
|
||||
private:
|
||||
std::set<User> _userdb;
|
||||
Persistence &persistence;
|
||||
/* data */
|
||||
public:
|
||||
Users(Persistence &persistence);
|
||||
~Users();
|
||||
bool checkRfid(String rfid_code, std::vector<Users::User> *logon_users);
|
||||
bool checkPin(String pin_code, std::vector<Users::User> *logon_users);
|
||||
unsigned long addUser(String first_name, String last_name, String rifd_uid, String user_pin);
|
||||
bool delUser(unsigned long id);
|
||||
bool updateUser(unsigned long id, ATTRIBUTES attr, String value);
|
||||
String toString(User *user);
|
||||
bool ImportFromPersistence();
|
||||
bool ExportToPersistence();
|
||||
void PrintAllToSerial();
|
||||
size_t countUsers();
|
||||
};
|
||||
+16
-28
@@ -23,8 +23,8 @@ bool WebConsole::init(userdb::UserDb *userdb)
|
||||
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_POST, std::bind(&WebConsole::_updateUser, this));
|
||||
_server->on(UriBraces("/api/config/{}"), std::bind(&WebConsole::_deleteUser, this));
|
||||
//_server->on("/bypin",std::bind(&WebConsole::_findPin,this));
|
||||
_server->serveStatic("/", LittleFS, "/static/index.html");
|
||||
//_server->onNotFound(std::bind(&WebConsole::_handleStatic, this));
|
||||
_server->serveStatic("/", LittleFS, "/s/");
|
||||
_server->onNotFound(std::bind(&WebConsole::_handleUnknown, this));
|
||||
return true;
|
||||
}
|
||||
void WebConsole::attachRfid(Rfid *rfid)
|
||||
@@ -45,34 +45,19 @@ bool WebConsole::isInterceptingRfid()
|
||||
{
|
||||
return catch_rfid;
|
||||
}
|
||||
void WebConsole::_handleStatic()
|
||||
void WebConsole::_handleUnknown()
|
||||
{
|
||||
String path = path_prefix + _server->uri();
|
||||
Serial.print("Request " + path);
|
||||
File src = LittleFS.open(path, "r");
|
||||
if (src.isDirectory())
|
||||
if (_server->method() == HTTP_OPTIONS)
|
||||
{
|
||||
path += "index.html";
|
||||
src.close();
|
||||
src = LittleFS.open(path, "r");
|
||||
}
|
||||
if (!src)
|
||||
{
|
||||
path = "/static/error404.html";
|
||||
src.close();
|
||||
src = LittleFS.open(path, "r");
|
||||
}
|
||||
|
||||
Serial.println(" resolved to " + path);
|
||||
if (src)
|
||||
{
|
||||
String content_type = "text/html";
|
||||
_server->streamFile(src, content_type);
|
||||
src.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
_server->send(500, "text/plain", "Internal error 500");
|
||||
_server->sendHeader("Access-Control-Allow-Origin", "*");
|
||||
_server->sendHeader("Access-Control-Max-Age", "10000");
|
||||
_server->sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
|
||||
_server->sendHeader("Access-Control-Allow-Headers", "*");
|
||||
_server->send(204);
|
||||
}else {
|
||||
File src = LittleFS.open("s/index.html", "r");
|
||||
_server->streamFile(src, "text/html");
|
||||
src.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,3 +223,6 @@ void WebConsole::_catchRFID()
|
||||
_server->send(500, "text/json", "{\"ok\":\"now activated\"}");
|
||||
}
|
||||
}
|
||||
void WebConsole::_updateAdmin()
|
||||
{
|
||||
}
|
||||
+3
-2
@@ -9,7 +9,7 @@
|
||||
#include "Rfid.h"
|
||||
namespace webconsole
|
||||
{
|
||||
static const char path_prefix[] PROGMEM = "/static";
|
||||
static const char path_prefix[] PROGMEM = "/s";
|
||||
static ESP8266WebServer _server(80);
|
||||
class WebConsole
|
||||
{
|
||||
@@ -23,7 +23,7 @@ namespace webconsole
|
||||
bool isInterceptingRfid();
|
||||
|
||||
private:
|
||||
void _handleStatic();
|
||||
void _handleUnknown();
|
||||
void _getUserDb();
|
||||
void _deleteUser();
|
||||
void _getUser();
|
||||
@@ -31,6 +31,7 @@ namespace webconsole
|
||||
void _createUser();
|
||||
void _dropUserDb();
|
||||
void _catchRFID();
|
||||
void _updateAdmin();
|
||||
void _print_db_raw()
|
||||
{
|
||||
File f = LittleFS.open("userdb.csv", "r");
|
||||
|
||||
+4
-6
@@ -7,9 +7,9 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "WebConsole.h"
|
||||
#include "UserDb.h"
|
||||
#include "Persistence.h"
|
||||
// File persistence
|
||||
Persistence persistence;
|
||||
#include "Config.h"
|
||||
// File config
|
||||
Config config;
|
||||
userdb::UserDb userdatabase("userdb.csv");
|
||||
webconsole::WebConsole web;
|
||||
// Rfid
|
||||
@@ -26,12 +26,10 @@ IPAddress local_IP(192, 168, 4, 22);
|
||||
IPAddress gateway(192, 168, 4, 9);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
|
||||
// User DB
|
||||
// Users users(persistence);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Persistence::Configuration config = persistence.loadConfig();
|
||||
config.loadConfig();
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting System");
|
||||
Serial.print("\t1. Network config ->");
|
||||
|
||||
Reference in New Issue
Block a user