#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <PubSubClientTools.h>
#include "MyTools.h"
#include "ESPAsyncWebServer.h"


boolean readCard();
boolean checkAccess();
void mac_subscriber(String topic,String message);
void sendLateMessage();
void publishUserAcces(uint64_t user_id, uint64_t card);
void publishErrorCard(uint64_t card);
void listDir(fs::FS &fs, const char *dirname, uint8_t levels);
void writeUsersCardsInFile(fs::FS &fs,const char* );
void readFile(fs::FS &fs, const char *path);
void readUsers();
void writeUserToRAM(char *buffer);
void deleteFile(fs::FS &fs, const char *path);
uint8_t print_wakeup_reason();
void activate_AP();
String processor(const String& var);
void saveConfiguration();
void loadConfiguration();
void goSleep();
void closedoor();
bool isTimeToSleep();
void readSector();
void writeCard();
void tests();
void setConfig(const String& var);
void startServer();
void factory_reset();
void restart();
void connectBroker();
void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void onMqttUnsubscribe(uint16_t packetId);
void connectToMqtt();
void onMqttPublish(uint16_t packetId);
void confirmOkReceiving();
void confirmNokReceiving(char* message);
void opendoor(int seconds);
void goTest();
void initSPI();
void initGPIO();
void connectWiFi();
void printLocalTime();
void readBlock(byte block, byte *buffer);
void update(AsyncWebServerRequest *request);
void messageLoop();
bool readUserSlots(uint64_t user, uint8_t *slots);
uint16_t getUserSlot(char *buffer, uint64_t user);
void test();
void encrypt(char * plainText, const char * key, unsigned char * outputBuffer);
void decrypt(char * chipherText, const char * key, unsigned char * outputBuffer);
uint64_t checkOfflineCardAccess();
void activateUpdate();
bool corectTime();
void saveEvent(uint64_t user_id);
void sendLog();
void setRoomHotelGroup(String payload);
void setClock(String msg);
void relayLoop();
void wakeup_by_time();
void wakeup_by_proxy();
void wakeup_on_reset();
void cardLoop();
void activateAP();
bool groupAcces(uint64_t group);
void activateTimeUpdate();
void setSound(uint16_t time);
void clearLog();
void prepare_and_sleep();
void sendLastEvent();
void soundExit();
void changeUpdateKey();
bool compareTime();