200 lines
7.7 KiB
JavaScript
200 lines
7.7 KiB
JavaScript
const WebSocket = require("ws");
|
|
const { URL } = require('url');
|
|
const sqlite3 = require('sqlite3');
|
|
const path = require('path');
|
|
|
|
const dbPath = process.env.DATABASE_PATH || '../';
|
|
const db = new sqlite3.Database(path.join(dbPath, 'database.sqlite'));
|
|
|
|
const port = 4001;
|
|
const api_version = '1.0.0';
|
|
|
|
const wss = new WebSocket.Server({
|
|
port: port
|
|
}, () => console.log(`Server started on port ${port}`));
|
|
|
|
|
|
wss.on('connection', async (ws, request) => {
|
|
const url = new URL(request.url, `http://${request.headers.host}`)
|
|
const params = Object.fromEntries(url.searchParams.entries());
|
|
const uuid = params.uuid;
|
|
|
|
if (!uuid) return
|
|
|
|
let check = await checkUUID(uuid);
|
|
|
|
console.log(check);
|
|
|
|
|
|
if (!check && (check.can_view_territory || check.administrator.id || check.moderator.id)) return
|
|
|
|
// Periodic ping to maintain a connection
|
|
const pingInterval = setInterval(() => {
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
ws.ping('ping');
|
|
}
|
|
}, 30000);
|
|
|
|
|
|
ws.send(JSON.stringify({ "connection": "success", "api_version": api_version }));
|
|
|
|
ws.on('message', (message) => {
|
|
message = JSON.parse(message);
|
|
|
|
console.log(message.username, check.name);
|
|
|
|
switch (message.event) {
|
|
case "connection":
|
|
broadcastMessage(message);
|
|
break;
|
|
|
|
case "message":
|
|
updateDatabase(message, check.sheep_id);
|
|
broadcastMessage(message);
|
|
break;
|
|
};
|
|
});
|
|
|
|
ws.on('pong', (data) => {
|
|
console.log('PONG received from the client:', data.toString());
|
|
});
|
|
|
|
ws.on('close', () => {
|
|
console.log('Client close');
|
|
});
|
|
|
|
ws.on('error', (err) => {
|
|
console.error('ERROR WebSocket:', err);
|
|
});
|
|
});
|
|
|
|
|
|
function broadcastMessage(message) {
|
|
wss.clients.forEach(client => {
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
client.send(JSON.stringify(message));
|
|
}
|
|
});
|
|
}
|
|
|
|
function updateDatabase(message, sheep_id) {
|
|
let sql = 'UPDATE apartments SET status = ?, description = ?, sheep_id = ?, updated_at = ? WHERE id = ?';
|
|
|
|
db.run(sql, [
|
|
Number(message.data.status),
|
|
message.data.description,
|
|
sheep_id,
|
|
message.data.updated_at,
|
|
message.data.id
|
|
], function (err) {
|
|
if (err) {
|
|
console.error(err.message);
|
|
} else if (this.changes === 0) {
|
|
console.error('Product not found');
|
|
} else {
|
|
console.log({ "update": "ok", "id": message.data.id });
|
|
|
|
let sql = `INSERT INTO apartments_history (apartments_id, status, description, sheep_id, created_at) VALUES (?, ?, ?, ?, ?)`;
|
|
|
|
db.run(sql, [
|
|
Number(message.data.id),
|
|
Number(message.data.status),
|
|
message.data.description,
|
|
sheep_id,
|
|
message.data.updated_at
|
|
], function (err) {
|
|
if (err) {
|
|
console.error(err.message);
|
|
} else if (this.changes === 0) {
|
|
console.error('Apartments not found');
|
|
} else {
|
|
console.log({ "insert": "ok", "id": this.lastID });
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
async function checkUUID(uuid) {
|
|
return new Promise((res, rej) => {
|
|
db.get(`
|
|
SELECT sheeps.*, administrators.* FROM administrators JOIN sheeps ON sheeps.id = administrators.sheep_id WHERE administrators.uuid = ?`,
|
|
[uuid],
|
|
(err, administrator) => {
|
|
if (administrator) {
|
|
let data = {
|
|
"id": Number(administrator.id),
|
|
"group_id": Number(administrator.group_id),
|
|
"name": administrator.name,
|
|
"icon": administrator.icon,
|
|
"uuid": administrator.uuid,
|
|
"appointment": administrator.appointment,
|
|
"can_view_stand": administrator.can_view_stand == 0 ? false : true,
|
|
"can_view_schedule": administrator.can_view_schedule == 0 ? false : true,
|
|
"can_view_territory": administrator.can_view_territory == 0 ? false : true,
|
|
"administrator": {
|
|
"id": administrator.administrators_id ? administrator.administrators_id : false
|
|
},
|
|
"moderator": {
|
|
"id": administrator.administrators_id ? administrator.administrators_id : false
|
|
}
|
|
}
|
|
return res(data);
|
|
}
|
|
|
|
db.get(`
|
|
SELECT sheeps.*, moderators.* FROM moderators JOIN sheeps ON sheeps.id = moderators.sheep_id WHERE moderators.uuid = ?`,
|
|
[uuid],
|
|
(err, moderator) => {
|
|
if (moderator) {
|
|
let data = {
|
|
"id": Number(moderator.id),
|
|
"group_id": Number(moderator.group_id),
|
|
"name": moderator.name,
|
|
"icon": moderator.icon,
|
|
"uuid": moderator.uuid,
|
|
"appointment": moderator.appointment,
|
|
"can_view_stand": moderator.can_view_stand == 0 ? false : true,
|
|
"can_view_schedule": moderator.can_view_schedule == 0 ? false : true,
|
|
"can_view_territory": moderator.can_view_territory == 0 ? false : true,
|
|
"administrator": {
|
|
"id": moderator.administrators_id ? moderator.administrators_id : false
|
|
},
|
|
"moderator": {
|
|
"id": moderator.moderators_id ? moderator.moderators_id : false
|
|
}
|
|
}
|
|
return res(data);
|
|
}
|
|
|
|
db.get(`SELECT sheeps.* FROM sheeps WHERE sheeps.uuid = ?`, [uuid],(err, sheep) => {
|
|
if (sheep) {
|
|
let data = {
|
|
"id": Number(sheep.id),
|
|
"group_id": Number(sheep.group_id),
|
|
"name": sheep.name,
|
|
"icon": sheep.icon,
|
|
"uuid": sheep.uuid,
|
|
"appointment": sheep.appointment,
|
|
"can_view_stand": sheep.can_view_stand == 0 ? false : true,
|
|
"can_view_schedule": sheep.can_view_schedule == 0 ? false : true,
|
|
"can_view_territory": sheep.can_view_territory == 0 ? false : true,
|
|
"administrator": {
|
|
"id": sheep.administrators_id ? sheep.administrators_id : false
|
|
},
|
|
"moderator": {
|
|
"id": sheep.moderators_id ? sheep.moderators_id : false
|
|
}
|
|
}
|
|
return res(sheep);
|
|
}
|
|
|
|
return res(false);
|
|
}
|
|
);
|
|
}
|
|
);
|
|
}
|
|
);
|
|
});
|
|
} |