-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapiESP.js
107 lines (92 loc) · 3.81 KB
/
apiESP.js
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
const utils= require ('./utils.js');
// Existing routes remain the same...
function check_rfid(req,res){
const tagHash = req.query.taghash;
const tag = req.query.tag;
if (!tagHash) {
utils.logEvent('Request to /check-rfid without tag hash');
return res.status(400).json({ error: 'Tag hash missing' });
}
const cleanTagHash = tagHash.replace(/[^0-9A-Fa-f]/g, '');
utils.logEvent('RFID tag verification', { tagHash: cleanTagHash, ip: req.ip });
utils.db.get(
'SELECT authorized, coffee_count FROM tags WHERE tag_hash = ?',
[cleanTagHash],
(err, row) => {
if (err) {
utils.logEvent('Database error during tag verification:', { error: err.message });
return res.status(500).json({ error: 'Database error' });
}
if (row && row.authorized) {
utils.logEvent('Access authorized', { tagHash: cleanTagHash, coffeeCount: row.coffee_count });
res.json({
status: 'authorized',
coffeeCount: row.coffee_count
});
} else {
if (utils.eventsync) {
// Store the tag ID in pendingTagId
pendingTagId = tag.replace(/[^0-9A-Fa-f]/g, '');
utils.eventsync = false; // Exit sync mode
utils.logEvent('Tag read in sync mode', { tagId: pendingTagId });
res.json({ status: 'sync', coffeeCount: 0 });
} else {
utils.logEvent('Access denied', { tagHash: cleanTagHash });
res.json({
status: 'denied',
coffeeCount: 0
});
}
}
}
);
}
function increment_coffe(req,res){
const { tagId } = req.body; // tagId is the hashed tag ID
if (!tagId) {
utils.logEvent('Richiesta increment-coffee senza tag hash');
return res.status(400).json({ error: 'Tag hash mancante' });
}
const cleanTagHash = tagId.replace(/[^0-9A-Fa-f]/g, '');
utils.logEvent('Incremento conteggio caffè', {
tagHash: cleanTagHash,
ip: req.ip
});
utils.db.run(
'UPDATE tags SET coffee_count = coffee_count + 1, last_used = datetime(\'now\',\'localtime\') WHERE tag_hash = ?',
[cleanTagHash],
function(err) {
if (err) {
utils.logEvent('Errore incremento caffè:', { error: err.message });
return res.status(500).json({ error: 'Errore database' });
}
utils.db.get(
'SELECT coffee_count FROM tags WHERE tag_hash = ?',
[cleanTagHash],
(err, row) => {
if (err || !row) {
utils.logEvent('Errore lettura conteggio dopo incremento:', { error: err?.message });
return res.status(500).json({ error: 'Errore lettura conteggio' });
}
utils.logEvent('Caffè incrementato con successo', {
tagHash: cleanTagHash,
newCount: row.coffee_count
});
res.json({ coffeeCount: row.coffee_count });
}
);
}
);
const now = new Date();
utils.db.run(`INSERT INTO coffee_consumptions (tag_id) VALUES (?);`,[now],function(err) {
if (err) {
utils.logEvent('Errore incremento caffè:', { error: err.message });
return res.status(500).json({ error: 'Errore database' });
}else{
utils.logEvent('consumazione caffè incrementata con successo', {
date: now
});
}
});
}
module.exports={increment_coffe,check_rfid};