removed old classes, included pwa for testing

This commit is contained in:
Jean Jacques Avril 2022-03-08 18:22:23 +01:00
parent 74da018c99
commit 2719314506
20 changed files with 86 additions and 303 deletions

1
data/admin Normal file
View File

@ -0,0 +1 @@
admin|admin

View File

@ -1,7 +0,0 @@
[
{
"NAME":"Toni",
"PASSWORD":"geheim",
"PIN":1231239
}
]

10
data/s/a.svg Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 25.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.2" baseProfile="tiny" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" viewBox="0 0 500 500" overflow="visible" xml:space="preserve">
<path fill="#CCCCCC" stroke="#000000" stroke-linecap="round" stroke-miterlimit="10" d="M349.5,421.5h-199c-8.28,0-15-6.72-15-15
v-255h229v255C364.5,414.78,357.78,421.5,349.5,421.5z"/>
<path fill="#CCCCCC" stroke="#000000" stroke-linecap="round" stroke-miterlimit="10" d="M386.81,99.5H314.5V80.38
c0-9.88-8.01-17.88-17.88-17.88h-92.24c-9.88,0-17.88,8.01-17.88,17.88V99.5h-73.31c-5.35,0-9.69,4.34-9.69,9.69v20.62
c0,5.35,4.34,9.69,9.69,9.69h273.62c5.35,0,9.69-4.34,9.69-9.69v-20.62C396.5,103.84,392.16,99.5,386.81,99.5z"/>
</svg>

After

Width:  |  Height:  |  Size: 874 B

12
data/s/b.svg Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 25.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.2" baseProfile="tiny" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" viewBox="0 0 500 500" overflow="visible" xml:space="preserve">
<polyline fill="#CCCCCC" stroke="#000000" stroke-miterlimit="10" points="181.5,395.5 86.5,395.5 86.5,300.5 "/>
<g>
<rect x="355.37" y="40.63" transform="matrix(0.7071 -0.7071 0.7071 0.7071 31.9147 294.7168)" fill="#CCCCCC" stroke="#000000" stroke-width="1" stroke-miterlimit="9.9999" width="32.69" height="136.4"/>
<rect x="94.59" y="168.11" transform="matrix(0.7071 -0.7071 0.7071 0.7071 -95.5641 241.9096)" fill="#CCCCCC" stroke="#000000" stroke-width="1" stroke-miterlimit="9.9999" width="299.29" height="136.4"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 881 B

1
data/s/bundle.45d14.css Normal file

File diff suppressed because one or more lines are too long

2
data/s/bundle.f9ad8.js Normal file

File diff suppressed because one or more lines are too long

1
data/s/index.html Normal file
View File

@ -0,0 +1 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>doorlock_pwa</title><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-capable" content="yes"><link rel="apple-touch-icon" href="/assets/icons/apple-touch-icon.png"><link rel="manifest" href="/manifest.json"><meta name="theme-color" content="#673ab8"><style>*{box-sizing:border-box}html{font-family:Helvetica,sans-serif;font-size:16px}body,html{height:100%}body{background-color:#fff;margin:0;padding:0;width:100%}</style><link href="/bundle.45d14.css" rel="stylesheet" media="only x" onload="this.media='all'"><noscript><link rel="stylesheet" href="/bundle.45d14.css"></noscript></head><body><script defer="defer" src="/bundle.f9ad8.js"></script><script nomodule="" src="/polyfills.058fb.js"></script></body></html>

File diff suppressed because one or more lines are too long

View File

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Error 404: Page not found.</h1>
</body>
</html>

View File

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Welcome on ESP8266</h1>
</body>
</html>

21
src/Config.cpp Normal file
View File

@ -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;
}

12
src/Config.h Normal file
View File

@ -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();
};

View File

@ -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;
}

View File

@ -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);
};

View File

@ -1,5 +1,5 @@
#pragma once
#include "Persistence.h"
#include "Config.h"
#include "LittleFS.h"
#include <sstream>
#include <iterator>

View File

@ -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));
}
}

View File

@ -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();
};

View File

@ -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";
_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();
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");
}
}
@ -238,3 +223,6 @@ void WebConsole::_catchRFID()
_server->send(500, "text/json", "{\"ok\":\"now activated\"}");
}
}
void WebConsole::_updateAdmin()
{
}

View File

@ -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");

View File

@ -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 ->");