improved rfid scan api->timeout
This commit is contained in:
parent
e81509a295
commit
d27a42c7e8
@ -21,7 +21,7 @@ bool WebConsole::init(userdb::UserDb *userdb)
|
||||
_server->on("/api/auth", HTTPMethod::HTTP_POST, std::bind(&WebConsole::_auth, this));
|
||||
_server->on("/api/userdb", HTTPMethod::HTTP_DELETE, std::bind(&WebConsole::_dropUserDb, this));
|
||||
_server->on("/api/userdb", HTTPMethod::HTTP_GET, std::bind(&WebConsole::_getUserDb, this));
|
||||
_server->on("/api/rfid", std::bind(&WebConsole::_catchRFID, this));
|
||||
_server->on("/api/rfid", HTTPMethod::HTTP_GET, std::bind(&WebConsole::_catchRFID, this));
|
||||
_server->on("/api/debug/printfile", std::bind(&WebConsole::_print_db_raw, this));
|
||||
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_DELETE, std::bind(&WebConsole::_deleteUser, this));
|
||||
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_GET, std::bind(&WebConsole::_getUser, this));
|
||||
@ -39,16 +39,26 @@ void WebConsole::attachRfid(Rfid *rfid)
|
||||
}
|
||||
void WebConsole::serve()
|
||||
{
|
||||
if (catch_rfid && rfid != nullptr && rfid->available())
|
||||
if (catch_rfid)
|
||||
{
|
||||
if (millis() - catch_rfid_millis > 2000){
|
||||
catch_rfid = 0;
|
||||
}
|
||||
else if (catch_rfid > 1 && rfid != nullptr && rfid->available())
|
||||
{
|
||||
rfid_buffer = rfid->getID();
|
||||
catch_rfid_updated = true;
|
||||
catch_rfid = false;
|
||||
catch_rfid = 1;
|
||||
}
|
||||
}
|
||||
_server->handleClient();
|
||||
}
|
||||
bool WebConsole::isInterceptingRfid()
|
||||
uint8_t WebConsole::isInterceptingRfid()
|
||||
{
|
||||
if(catch_rfid==3){
|
||||
catch_rfid=2;
|
||||
return 3;
|
||||
}
|
||||
|
||||
return catch_rfid;
|
||||
}
|
||||
bool WebConsole::_isAuth()
|
||||
@ -78,7 +88,7 @@ void WebConsole::_auth()
|
||||
{
|
||||
char *token = auth.login(_server->arg("username"), _server->arg("password"));
|
||||
if (token == nullptr)
|
||||
_server->send(401, "text/plain", "failed!");
|
||||
_server->send(401, "text/plain", "login_failed");
|
||||
else
|
||||
_server->send(200, "text/plain", token);
|
||||
}
|
||||
@ -94,7 +104,6 @@ void WebConsole::_auth()
|
||||
// return;
|
||||
bool res = auth.setAuth(_server->arg("username"), _server->arg("password"));
|
||||
_server->send(200, "text/plain", res ? "success" : "failed");
|
||||
|
||||
}
|
||||
else
|
||||
_server->send(404, "text/plain", "unknown action");
|
||||
@ -228,7 +237,7 @@ void WebConsole::_updateUser()
|
||||
updated.user_pin = doc["user_pin"].as<String>();
|
||||
if (doc.containsKey("enabled"))
|
||||
updated.enabled = doc["enabled"].as<bool>();
|
||||
bool res = userdb->update_user(&updated);
|
||||
userdb->update_user(&updated);
|
||||
_server->send(200, "text/json", updated.toJSONString().c_str());
|
||||
}
|
||||
void WebConsole::_createUser()
|
||||
@ -285,23 +294,20 @@ void WebConsole::_catchRFID()
|
||||
return;
|
||||
if (rfid == nullptr)
|
||||
{
|
||||
_server->send(500, "text/json", "{\"error\":\"RFID not attached.\"}");
|
||||
_server->send(500, "text/json", "{\"error\":\"RFID not attached.\",\"state\":\"" + String(catch_rfid) + "\"}");
|
||||
return;
|
||||
}
|
||||
if (catch_rfid_updated)
|
||||
if (catch_rfid == 1)
|
||||
{
|
||||
String response = "{\"rfid_uid\":\"" + rfid_buffer + "\"}";
|
||||
_server->send(500, "text/json", response);
|
||||
catch_rfid_updated = false;
|
||||
}
|
||||
else if (catch_rfid)
|
||||
{
|
||||
_server->send(500, "text/json", "{\"ok\":\"already activated\"}");
|
||||
String response = "{\"rfid_uid\":\"" + rfid_buffer + "\",\"state\":\"" + String(catch_rfid) + "\"}";
|
||||
_server->send(200, "text/json", response);
|
||||
catch_rfid = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
catch_rfid = true;
|
||||
_server->send(500, "text/json", "{\"ok\":\"now activated\"}");
|
||||
catch_rfid = 3; // 3 - Start listening
|
||||
catch_rfid_millis = millis();
|
||||
_server->send(200, "text/json", "{\"ok\":\"listening\",\"state\":\"" + String(catch_rfid) + "\"}");
|
||||
}
|
||||
}
|
||||
void WebConsole::_updateAdmin()
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "UserDb.h"
|
||||
#include "Rfid.h"
|
||||
#include "AdminAuth.h"
|
||||
#include "Interface.h"
|
||||
namespace webconsole
|
||||
{
|
||||
static const char path_prefix[] PROGMEM = "/s";
|
||||
@ -21,7 +22,7 @@ namespace webconsole
|
||||
bool init(userdb::UserDb *userdb);
|
||||
void attachRfid(Rfid *rfid);
|
||||
void serve();
|
||||
bool isInterceptingRfid();
|
||||
uint8_t isInterceptingRfid();
|
||||
|
||||
private:
|
||||
void _sendCORS();
|
||||
@ -48,10 +49,12 @@ namespace webconsole
|
||||
}
|
||||
ESP8266WebServer *_server;
|
||||
userdb::UserDb *userdb = nullptr;
|
||||
bool catch_rfid = false;
|
||||
unsigned long catch_rfid_millis = 0;
|
||||
uint8_t catch_rfid = 0;
|
||||
bool catch_rfid_updated = false;
|
||||
String rfid_buffer;
|
||||
Rfid *rfid = nullptr;
|
||||
Interface *iface = nullptr;
|
||||
AdminAuth auth;
|
||||
};
|
||||
|
||||
|
@ -62,9 +62,10 @@ void loop()
|
||||
web.serve();
|
||||
keyboard.scanAsync();
|
||||
userdb::User login_user;
|
||||
if(web.isInterceptingRfid()&&iface.getState()==0){
|
||||
iface.showMessage("WebUI connected", "Awaiting RFID", 3000);
|
||||
}
|
||||
if (web.isInterceptingRfid() ==3)
|
||||
iface.showMessage("WebUI connected", "Awaiting RFID", 2000);
|
||||
else if (web.isInterceptingRfid() == 1)
|
||||
iface.showMessage("WebUI connected", "RFID Found", 1000);
|
||||
else if (iface.pinAvailable() && iface.getState() != Interface::GREET)
|
||||
{
|
||||
unsigned long delta = millis();
|
||||
|
Loading…
x
Reference in New Issue
Block a user