-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard_database.cpp
More file actions
154 lines (128 loc) · 3.61 KB
/
card_database.cpp
File metadata and controls
154 lines (128 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "card_database.h"
CardDatabase::CardDatabase() : mutex(NULL) {
mutex = xSemaphoreCreateMutex();
if (mutex == NULL) {
Serial.println("Error creating card database mutex");
}
}
CardDatabase::~CardDatabase() {
if (mutex != NULL) {
vSemaphoreDelete(mutex);
}
}
bool CardDatabase::begin() {
if (!LittleFS.begin(false)) { // Don't format if mount fails
Serial.println("LittleFS mount failed");
return false;
}
return initializeFile();
}
bool CardDatabase::takeMutex() {
if (mutex == NULL) return false;
return xSemaphoreTake(mutex, pdMS_TO_TICKS(1000)) == pdTRUE;
}
void CardDatabase::giveMutex() {
if (mutex != NULL) {
xSemaphoreGive(mutex);
}
}
bool CardDatabase::initializeFile() {
if (!takeMutex()) return false;
bool success = true;
if (!LittleFS.exists(DATABASE_PATH)) {
File file = LittleFS.open(DATABASE_PATH, FILE_WRITE);
if (!file) {
Serial.println("Failed to create card database file");
success = false;
}
file.close();
}
giveMutex();
return success;
}
bool CardDatabase::addCard(unsigned long cardNumber) {
if (!takeMutex()) return false;
bool success = writeCardToFile(cardNumber);
giveMutex();
return success;
}
bool CardDatabase::removeCard(unsigned long cardNumber) {
if (!takeMutex()) return false;
bool success = removeCardFromFile(cardNumber);
giveMutex();
return success;
}
bool CardDatabase::hasCard(unsigned long cardNumber) {
if (!takeMutex()) return false;
bool found = false;
File file = LittleFS.open(DATABASE_PATH, FILE_READ);
if (file) {
while (file.available()) {
String line = file.readStringUntil('\n');
if (line.toInt() == cardNumber) {
found = true;
break;
}
}
file.close();
}
giveMutex();
return found;
}
String CardDatabase::getAllCards() {
if (!takeMutex()) return "";
String cards;
File file = LittleFS.open(DATABASE_PATH, FILE_READ);
if (file) {
while (file.available()) {
cards += file.readStringUntil('\n') + "\n";
}
file.close();
}
giveMutex();
return cards;
}
bool CardDatabase::writeCardToFile(unsigned long cardNumber) {
// First check if card already exists
if (hasCard(cardNumber)) {
return true; // Card already exists, consider it a success
}
File file = LittleFS.open(DATABASE_PATH, FILE_APPEND);
if (!file) {
Serial.println("Failed to open card database for writing");
return false;
}
file.println(String(cardNumber));
file.close();
return true;
}
bool CardDatabase::removeCardFromFile(unsigned long cardNumber) {
String cards;
bool found = false;
// Read all cards except the one to remove
File file = LittleFS.open(DATABASE_PATH, FILE_READ);
if (file) {
while (file.available()) {
String line = file.readStringUntil('\n');
if (line.toInt() != cardNumber) {
cards += line + "\n";
} else {
found = true;
}
}
file.close();
}
// If card wasn't found, return success
if (!found) {
return true;
}
// Write back all cards except the removed one
file = LittleFS.open(DATABASE_PATH, FILE_WRITE);
if (!file) {
Serial.println("Failed to open card database for writing");
return false;
}
file.print(cards);
file.close();
return true;
}