-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·83 lines (69 loc) · 2.7 KB
/
server.js
File metadata and controls
executable file
·83 lines (69 loc) · 2.7 KB
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
const express = require("express");
const cors = require("cors");
const fs = require("fs");
const path = require("path");
const dgram = require("dgram");
const app = express();
const PORT = 3000;
const CONFIG_FILE = path.join(__dirname, "config.json");
app.use(cors());
app.use(express.static("public"));
const PPT_PORT = 61000; // Порт управления PowerPoint
const udpClient = dgram.createSocket("udp4");
// Функция загрузки конфигурации
function loadConfig() {
if (fs.existsSync(CONFIG_FILE)) {
try {
return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf8"));
} catch (error) {
console.error("❌ Ошибка чтения config.json:", error);
}
}
return { ip: "192.168.1.100" }; // Значение по умолчанию
}
let config = loadConfig();
// API для получения текущих настроек
app.get("/api/get-settings", (req, res) => {
config = loadConfig();
res.json(config);
});
// API для обновления IP-адреса
app.get("/api/set-ip", (req, res) => {
const newIP = req.query.ip;
if (!newIP) {
return res.status(400).json({ error: "IP-адрес не указан!" });
}
config.ip = newIP;
try {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 4));
console.log(`✅ IP обновлён: ${newIP}`);
res.json({ success: true, ip: newIP });
} catch (error) {
console.error("❌ Ошибка записи в config.json:", error);
res.status(500).json({ error: "Ошибка сохранения IP-адреса!" });
}
});
// Функция отправки команды в PowerPoint
function sendCommand(command) {
const message = Buffer.from(command);
udpClient.send(message, 0, message.length, PPT_PORT, config.ip, (err) => {
if (err) {
console.error("❌ Ошибка отправки команды:", err);
} else {
console.log(`📡 Отправлена команда: "${command}" на ${config.ip}:${PPT_PORT}`);
}
});
}
// API для отправки команд "NEXT" и "PREV"
app.get("/api/command", (req, res) => {
const action = req.query.action; // Получаем параметр action (NEXT или PREV)
if (!action || (action !== "NEXT" && action !== "PREV")) {
return res.status(400).json({ error: "Некорректная команда! Используйте ?action=NEXT или ?action=PREV" });
}
sendCommand(action);
res.json({ success: true, command: action });
});
// Запуск сервера
app.listen(PORT, () => {
console.log(`🚀 Server is running on port ${PORT}`);
});