-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
84 lines (74 loc) · 2.68 KB
/
database.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
// controllers/deviceController.js
require('dotenv').config();
const deviceController = require('./controllers/deviceController');
const mysql = require('mysql2');
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
});
// Function to fetch devices from the cache
async function getAllDevices(callback) {
// Attempt to fetch devices from the cache
console.log('trying to get devices ')
deviceController.getCachedDevices(async (err, cachedDevices) => {
if (err || !cachedDevices) {
try {
// Fetch devices from the database using the getDevices function from database.js
// @todo use real data from database
// const devices = await pool.query('SELECT * FROM devices')
const devices = [
{
"ip": "192.168.86.175",
"login": "admin",
"password": "camera123"
},
{
"ip": "192.168.86.200",
"login": "admin",
"password": "admin123"
},
{
"ip": "192.168.86.121",
"login": "admin",
"password": "admin123"
},
{
"ip": "192.168.86.176",
"login": "admin",
"password": "camera123"
},
{
"ip": "192.168.86.91",
"login": "admin",
"password": "admin123"
},
{
"ip": "192.168.86.119",
"login": "admin",
"password": "admin"
}
]
// Cache the devices
deviceController.setCachedDevices(devices, (cacheErr) => {
if (cacheErr) {
console.error('Error caching devices:', cacheErr);
}
});
callback(devices);
} catch (dbErr) {
console.error('Database error:', dbErr);
callback([]);
}
} else {
// Devices found in the cache
callback(cachedDevices);
}
});
}
// Other controller functions for single device, create, update, and delete can remain the same
module.exports = {
getAllDevices,
// Other controller functions here
};