-
Notifications
You must be signed in to change notification settings - Fork 18
/
door_lock_rfid.h
207 lines (160 loc) · 5.23 KB
/
door_lock_rfid.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* ESPhome RC522 RFID plugin with access control
*
* Created on: 17.03.2019
*
* Copyright (c) 2019 Jakub Pachciarek. All rights reserved.
*
*/
#include "esphome.h"
#include <FS.h>
#include <SPI.h>
#include <MFRC522.h>
using namespace esphome;
using namespace std;
class RfidSensorsComponent : public Component {
private:
MFRC522 rfid;
typedef struct {
String id;
String description;
} user;
vector<user> users_list;
unsigned long last_tag_read_milis = 0;
//https://www.esphome.io/api/custom__api__device_8h_source.html
void send_notification(String title, String message, String notification_id) {
api::HomeassistantServiceResponse resp;
resp.service = "persistent_notification.create";
HomeassistantServiceMap kv1;
kv1.key = "title";
kv1.value = title.c_str();
resp.data.push_back(kv1);
HomeassistantServiceMap kv2;
kv2.key = "message";
kv2.value = message.c_str();
resp.data.push_back(kv2);
HomeassistantServiceMap kv3;
kv3.key = "notification_id";
kv3.value = notification_id.c_str();
resp.data.push_back(kv3);
api::global_api_server->send_homeassistant_service_call(resp);
}
void send_event(String name) {
api::HomeassistantServiceResponse resp;
resp.service = name.c_str();
resp.is_event = true;
api::global_api_server->send_homeassistant_service_call(resp);
}
bool load_users_list() {
ESP_LOGD("rfid", "Loading users list from file...");
File f = SPIFFS.open("/rfid_users_list.db", "r");
if (!f) {
ESP_LOGE("rfid", "Failed to open users list file!");
return false;
}
ESP_LOGD("rfid", "Parsing users list...");
users_list.clear();
while (f.available()) {
String line;
char tmp = 0;
while (f.available() && tmp != '\n'){
tmp = f.read();
line += tmp;
}
line.trim();
size_t split_pos = line.indexOf('=');
if (split_pos == 11) { //Naive ID check: XX:XX:XX:XX=DESCRIPTION
user row;
row.id = line.substring(0, split_pos);
row.description = line.substring(split_pos + 1);
users_list.push_back(row);
}
}
f.close();
return true;
}
bool save_users_list() {
ESP_LOGD("rfid", "Saving users list to file...");
File f = SPIFFS.open("/rfid_users_list.db", "w");
if (!f) {
ESP_LOGE("rfid", "Failed to open users list file!");
return false;
}
for (auto &user : users_list) {
f.print(user.id + "=" + user.description + '\n');
}
f.close();
return true;
}
void handle_tag_read() {
if (last_tag_read_milis != 0 && (last_tag_read_milis + 5000) > millis()) return;
valid_tag_sensor->publish_state(false);
invalid_tag_sensor->publish_state(false);
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
user current_tag;
bool known_tag = false;
char tag_id_buffer[12];
sprintf(tag_id_buffer, "%02X:%02X:%02X:%02X", rfid.uid.uidByte[0], rfid.uid.uidByte[1], rfid.uid.uidByte[2], rfid.uid.uidByte[3]);
current_tag.id = tag_id_buffer;
current_tag.description = "UNKNOWN";
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
for (size_t i = 0; i < users_list.size(); i++) {
if (users_list[i].id == current_tag.id) {
current_tag = users_list[i];
known_tag = true;
break;
}
}
ESP_LOGD("rfid", "Tag with id %s found!", current_tag.id.c_str());
ESP_LOGD("rfid", "Setting sensor states...");
last_tag_read_milis = millis();
valid_tag_sensor->publish_state(known_tag);
invalid_tag_sensor->publish_state(!known_tag);
last_tag_id_sensor->publish_state(current_tag.id.c_str());
last_tag_description_sensor->publish_state(current_tag.description.c_str());
}
public:
binary_sensor::BinarySensor *valid_tag_sensor = new binary_sensor::BinarySensor();
binary_sensor::BinarySensor *invalid_tag_sensor = new binary_sensor::BinarySensor();
text_sensor::TextSensor *last_tag_id_sensor = new text_sensor::TextSensor();
text_sensor::TextSensor *last_tag_description_sensor = new text_sensor::TextSensor();
void send_users_list_notification() {
bool first = true;
String message;
message += "{\"list\":[\n";
if (users_list.size() == 0) {
message += "\"XX:XX:XX:XX=Example 1\",\n\"XX:XX:XX:XX=Example 2\"";
}
for (auto &user : users_list) {
if (!first) message += ",\n";
first = false;
message += "\"" + user.id + "=" + user.description + "\"";
}
message += "\n]}";
send_notification((App.get_name() + ": users list").c_str(), message, (App.get_name() + "_users_list_print").c_str());
}
void upload_users_list(vector<string> list) {
users_list.clear();
for (auto &line : list) {
size_t split_pos = line.find('=');
if (split_pos == 11) { //Naive ID check: XX:XX:XX:XX=DESCRIPTION
user row;
row.id = line.substr(0, split_pos).c_str();
row.description = line.substr(split_pos + 1).c_str();
users_list.push_back(row);
}
}
save_users_list();
send_users_list_notification();
}
void setup() override {
SPIFFS.begin();
SPI.begin();
rfid.PCD_Init(D8, D3);
load_users_list();
}
void loop() override {
handle_tag_read();
}
};