229 lines
7.7 KiB
JavaScript
229 lines
7.7 KiB
JavaScript
const express = require("express");
|
|
const router = express.Router();
|
|
const { spawn, exec } = require("child_process");
|
|
const DeviceController = require("../controllers/DeviceController");
|
|
const AccountController = require("../controllers/AccountController");
|
|
const ERP_Controller = require("../controllers/ERP_Controller");
|
|
const { io } = require("socket.io-client");
|
|
const fs = require("fs");
|
|
|
|
// router.post("/api/testDevices", DeviceController.testDevice);
|
|
router.post("/api/autoTest", DeviceController.autoTest);
|
|
router.post("/api/manualTest", DeviceController.manualTest);
|
|
router.post("/api/installIOS", DeviceController.installIOS);
|
|
router.post("/api/powerControl", DeviceController.powerControl);
|
|
router.post("/api/breakPassword_36_38", DeviceController.breakPassword_36_38);
|
|
router.post(
|
|
"/api/breakPassword_2960_3560_3750",
|
|
DeviceController.breakPassword_2960_3560_3750
|
|
);
|
|
router.post("/api/updateIOS", DeviceController.updateIOS);
|
|
|
|
router.post("/api/device/getAllError", DeviceController.getAllExcludeError);
|
|
router.post("/api/device/addError", DeviceController.addExcludeError);
|
|
router.post("/api/device/deleteError", DeviceController.deleteExcludeError);
|
|
router.post("/api/device/getAllValue", DeviceController.getAllValue);
|
|
router.post("/api/device/addValue", DeviceController.addValue);
|
|
router.post("/api/device/deleteValue", DeviceController.deleteValue);
|
|
|
|
router.post("/api/item/addItem", DeviceController.addItem);
|
|
router.post("/api/item/getAllItem", DeviceController.getAllItem);
|
|
router.post(
|
|
"/api/item/getAllItemBySNParent",
|
|
DeviceController.getAllItemBySNParent
|
|
);
|
|
|
|
router.post("/api/station/getAllStation", DeviceController.getAllStation);
|
|
router.post(
|
|
"/api/station/addLineForStation",
|
|
DeviceController.addLineForStation
|
|
);
|
|
router.post(
|
|
"/api/station/getAllLineOfStation",
|
|
DeviceController.getAllLineOfStation
|
|
);
|
|
router.post("/api/station/editLine", DeviceController.editLine);
|
|
router.post("/api/station/enableLine", DeviceController.enableLine);
|
|
router.post("/api/station/editStation", DeviceController.editStation);
|
|
|
|
router.post("/api/group/addGroup", DeviceController.addGroup);
|
|
router.post("/api/group/addModel", DeviceController.addModel);
|
|
router.post("/api/group/getAllGroup", DeviceController.getAllGroup);
|
|
router.post("/api/group/getAllModel", DeviceController.getAllModel);
|
|
router.post(
|
|
"/api/group/getAllModelOfGroup",
|
|
DeviceController.getAllModelOfGroup
|
|
);
|
|
router.post(
|
|
"/api/group/getAllGroupOfModel",
|
|
DeviceController.getAllGroupOfModel
|
|
);
|
|
router.post("/api/group/getAllCmdOfGroup", DeviceController.getAllCmdOfGroup);
|
|
router.post("/api/group/getAllCmdOfModel", DeviceController.getAllCmdOfModel);
|
|
router.post("/api/group/editGroup", DeviceController.editGroup);
|
|
router.post("/api/group/editModel", DeviceController.editModel);
|
|
router.post("/api/group/deleteModel", DeviceController.deleteModel);
|
|
router.post("/api/group/deleteGroup", DeviceController.deleteGroup);
|
|
router.post("/api/group/checkActionModel", DeviceController.checkActionModel);
|
|
|
|
router.post("/api/log/getListLastLog", DeviceController.getListLastLog);
|
|
router.post("/api/log/getAllLogOfStation", DeviceController.getAllLogOfStation);
|
|
router.post("/api/log/changeCommand", DeviceController.changeCommand);
|
|
|
|
router.post("/api/account/addAccount", AccountController.addAccount);
|
|
router.post("/api/account/checkLogin", AccountController.checkLogin);
|
|
router.post("/api/account/checkToken", AccountController.checkToken);
|
|
|
|
router.post("/api/readLog", (req, res) => {
|
|
try {
|
|
const data = fs.readFileSync("./utils/listIOS.text", "utf8");
|
|
res.json(data.split("\n"));
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
});
|
|
router.post("/api/downloadFile", (req, res) => {
|
|
res.download(req.body.fileName);
|
|
});
|
|
|
|
router.post("/api/test/getCommandByModel", DeviceController.getCommandByModel);
|
|
|
|
router.post("/api/cancelProcess", (req, res) => {
|
|
// let socket = io(process.env.SOCKET_SERVER);
|
|
// socket?.emit("newUser", {email:"api", time: 9999999});
|
|
console.log(req.body);
|
|
let listPID = [];
|
|
exec(
|
|
"ps -eo pid,command | grep -E 'python3|index.js'",
|
|
(error, stdout, stderr) => {
|
|
console.log("Output:", stdout);
|
|
stdout.split("\n").map((u) => {
|
|
if (
|
|
(u.trim().split(" ").includes(req.body.line_port.toString()) &&
|
|
u.split(" ").includes(req.body.sta_ip)) ||
|
|
(u.trim().split(" ").includes(req.body.clear_line.toString()) &&
|
|
u.split(" ").includes(req.body.sta_ip))
|
|
) {
|
|
listPID.push(u.trim().split(" ")[0]);
|
|
}
|
|
});
|
|
listPID.map((u) => {
|
|
exec("kill -9 " + u, (error, stdout, stderr) => {
|
|
console.log("kill " + u);
|
|
});
|
|
});
|
|
console.log(listPID);
|
|
// console.error('Errors:', stderr);
|
|
}
|
|
);
|
|
res.json({ status: "done" });
|
|
});
|
|
|
|
router.post("/api/getListCisco", (req, res) => {
|
|
const fileContents = fs.readFileSync(
|
|
"/home/ct/ManagementNetworkDevice/netmiko_Api/utils/list_cisco.txt",
|
|
"utf8"
|
|
);
|
|
|
|
const line = fileContents.split("\n");
|
|
res.json({ line });
|
|
});
|
|
|
|
router.post("/api/openTerminal", (req, res) => {
|
|
const { infoStation, listLine } = req.body;
|
|
spawn(
|
|
process.env.PYTHON_FORMAT,
|
|
[
|
|
"./utils/netmiko_test.py",
|
|
"clear line " + listLine.line_clear,
|
|
infoStation.sta_ip,
|
|
infoStation.sta_port_telnet,
|
|
"clear",
|
|
],
|
|
{ timeout: 14000 }
|
|
);
|
|
|
|
setTimeout(() => {
|
|
spawn(
|
|
process.env.PYTHON_FORMAT,
|
|
[
|
|
"./utils/openTerminal.py",
|
|
"telnet " + infoStation.sta_ip + " " + infoStation.sta_port_telnet,
|
|
],
|
|
{ timeout: 14000 }
|
|
);
|
|
}, 5000);
|
|
|
|
res.json({ status: "success" });
|
|
});
|
|
|
|
router.post("/api/getIndexSerialNumber", ERP_Controller.getIndexSerialNumber);
|
|
router.post("/api/getParagraph", ERP_Controller.getParagraph);
|
|
|
|
router.get("/api/log", (req, res) => {
|
|
const { name } = req.query;
|
|
const fileContents = fs.readFileSync(
|
|
"/home/tools/ManagementNetworkDevice/netmiko_Api/public/filesTest/" + name,
|
|
"utf8"
|
|
);
|
|
|
|
res.send(
|
|
"<textarea style='width:100%;height:100%;'>" + fileContents + "</textarea>"
|
|
);
|
|
});
|
|
|
|
router.get("/api/dailyLog", (req, res) => {
|
|
try {
|
|
const { name } = req.query;
|
|
fs.access(
|
|
"/home/tools/ManagementNetworkDevice/netmiko_Api/public/dailyLogs/" + name,
|
|
fs.constants.F_OK,
|
|
(err) => {
|
|
if (err) {
|
|
res.send(
|
|
"<h2 style='text-align:center;'> - - - No station log for to day - - - </h2>"
|
|
);
|
|
} else {
|
|
const fileContents = fs.readFileSync(
|
|
"/home/tools/ManagementNetworkDevice/netmiko_Api/public/dailyLogs/" +
|
|
name,
|
|
"utf8"
|
|
);
|
|
|
|
res.send(
|
|
"<textarea style='width:100%;height:100%;'>" +
|
|
fileContents +
|
|
"</textarea>"
|
|
);
|
|
}
|
|
}
|
|
);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
});
|
|
|
|
router.post("/api/gitea-webhook", ( req, res ) => {
|
|
// Kiểm tra xem đó có phải sự kiện bạn muốn xử lý không
|
|
// if (request.headers['x-gitlab-event'] === 'push') {
|
|
// // Lấy dữ liệu sự kiện từ req.body
|
|
// const eventData = req.body;
|
|
// console.log('Received push event from GitLab:');
|
|
// console.log(JSON.stringify(eventData, null, 2));
|
|
// }
|
|
console.log("New event ---> ", req.body);
|
|
|
|
// exec(
|
|
// Env.get("URL_FILE_SHELL"),
|
|
// (error, stdout, stderr) => {
|
|
// if (error) {
|
|
// console.error(`Error executing command: ${error}`);
|
|
// return;
|
|
// }
|
|
|
|
// console.log(`Command output:\n${stdout}`);
|
|
// }
|
|
// );
|
|
});
|
|
module.exports = router;
|