forked from esprfid/esp-rfid
-
Notifications
You must be signed in to change notification settings - Fork 8
/
log.esp
103 lines (99 loc) · 2.84 KB
/
log.esp
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
void extern mqtt_publish_event(JsonObject *root);
void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String data) {
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = type;
root["src"] = src;
root["desc"] = desc;
root["data"] = data;
root["time"] = now();
if ((mqttEvents) && (mqttenabled==1)){ // log to MQTT
root["cmd"] = "event";
root["door"] = deviceHostname;
mqtt_publish_event(&root);
}
else // log to file
{
File eventlog = SPIFFS.open("/eventlog.json", "a");
root.printTo(eventlog);
eventlog.print("\n");
eventlog.close();
}
}
void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype) {
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["uid"] = uid;
root["username"] = username;
root["acctype"] = acctype;
root["timestamp"] = now();
if ((mqttEvents) && (mqttenabled==1)){ // log to MQTT
//root["cmd"] = "access";
//mqtt_publish_event(&root);
}
else // log to file
{
File latestlog = SPIFFS.open("/latestlog.json", "a");
root.printTo(latestlog);
latestlog.print("\n");
latestlog.close();
}
}
void ICACHE_FLASH_ATTR sendEventLog(int page) {
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["command"] = "eventlist";
root["page"] = page;
JsonArray &items = root.createNestedArray("list");
File eventlog = SPIFFS.open("/eventlog.json", "r");
int first = (page - 1) * 10;
int last = page * 10;
int i = 0;
while (eventlog.available()) {
String item = String();
item = eventlog.readStringUntil('\n');
if (i >= first && i < last) {
items.add(item);
}
i++;
}
eventlog.close();
float pages = i / 10.0;
root["haspages"] = ceil(pages);
size_t len = root.measureLength();
AsyncWebSocketMessageBuffer *buffer = ws.makeBuffer(len);
if (buffer) {
root.printTo((char *)buffer->get(), len + 1);
ws.textAll(buffer);
ws.textAll("{\"command\":\"result\",\"resultof\":\"eventlist\",\"result\": true}");
}
}
void ICACHE_FLASH_ATTR sendLatestLog(int page) {
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["command"] = "latestlist";
root["page"] = page;
JsonArray &items = root.createNestedArray("list");
File latestlog = SPIFFS.open("/latestlog.json", "r");
int first = (page - 1) * 10;
int last = page * 10;
int i = 0;
while (latestlog.available()) {
String item = String();
item = latestlog.readStringUntil('\n');
if (i >= first && i < last) {
items.add(item);
}
i++;
}
latestlog.close();
float pages = i / 10.0;
root["haspages"] = ceil(pages);
size_t len = root.measureLength();
AsyncWebSocketMessageBuffer *buffer = ws.makeBuffer(len);
if (buffer) {
root.printTo((char *)buffer->get(), len + 1);
ws.textAll(buffer);
ws.textAll("{\"command\":\"result\",\"resultof\":\"latestlist\",\"result\": true}");
}
}