383 lines
12 KiB
JavaScript
383 lines
12 KiB
JavaScript
const Device = require("../../models/DeviceModel");
|
|
const { spawn } = require("child_process");
|
|
const { io } = require("socket.io-client");
|
|
const moment = require("moment");
|
|
const { detectFaulty } = require("./detectFaulty");
|
|
const { getInfoLog } = require("./getInfoLog");
|
|
const fs = require("fs");
|
|
const { checkPID } = require("./checkPID");
|
|
|
|
const SRW_test = async (infoStation, listLine, created_by) => {
|
|
try {
|
|
let socket = io(process.env.SOCKET_SERVER);
|
|
socket?.emit("newUser", { email: "api", time: 9999999 });
|
|
let result = [];
|
|
const specialItem = await Device.getAllValue();
|
|
const date = Date.now();
|
|
const time =
|
|
"./public/filesTest/" +
|
|
moment(date).format("YYYYMMDD").toString() +
|
|
"-AUTO-Session." +
|
|
infoStation.sta_name +
|
|
".Port" +
|
|
listLine.line_number +
|
|
"-" +
|
|
moment(date).format("HH-mm-ss").toString() +
|
|
".log";
|
|
|
|
let listLog = [];
|
|
const allLine = await Device.getAllLineOfStation(infoStation.id_station);
|
|
for (let i = 0; i < allLine.pData.length; i++) {
|
|
const allLog = await Device.getAllLogOfLine(allLine.pData[i].id_line);
|
|
for (let j = 0; j < allLog.pData.length; j++) {
|
|
const data = fs.readFileSync(allLog.pData[j].log_content, "utf8");
|
|
listLog.push({
|
|
id_log: allLog.pData[j].id_log,
|
|
fileName: allLog.pData[j].log_content,
|
|
log_status: allLog.pData[j].log_status,
|
|
log_line: allLog.pData[j].log_line,
|
|
log_content: data,
|
|
PID: allLog.pData[j].PID,
|
|
SN: allLog.pData[j].SN,
|
|
command: allLog.pData[j].command,
|
|
line: allLine.pData[i].line_number,
|
|
created_at: allLog.pData[j].created_at,
|
|
created_by: allLog.pData[j].created_by,
|
|
});
|
|
}
|
|
}
|
|
let groups = await Device.getAllGroup();
|
|
|
|
let group = groups.pData.filter(
|
|
(gr) =>
|
|
gr.group_name.search("SRW") !== -1 &&
|
|
gr.group_name.search("Test") !== -1
|
|
)[0];
|
|
const commands = await Device.getAllCmdOfGroup(group.id_group);
|
|
let command = commands.pData.map((obj) => obj.cmd_message);
|
|
console.log(group);
|
|
console.log(command);
|
|
const autoTest = spawn(
|
|
process.env.PYTHON_FORMAT,
|
|
[
|
|
"./utils/autoTest/SRW_test.py",
|
|
"",
|
|
infoStation.sta_ip,
|
|
infoStation.sta_port_telnet,
|
|
listLine.line_clear,
|
|
listLine.line_number,
|
|
command,
|
|
],
|
|
{ timeout: 150000 }
|
|
);
|
|
|
|
autoTest.stdout.on("data", async (data) => {
|
|
data = data.toString().split("\n").filter((line)=>line.search("More: ")===-1).join("\n").replace(/\n\n\n/g,"\n");
|
|
console.log("data scan: ", data);
|
|
if (data.search("xxError") !== -1) {
|
|
result.push({
|
|
id_line: listLine.id_line,
|
|
line: listLine.line_number,
|
|
line_ip: listLine.line_ip,
|
|
port: listLine.line_port,
|
|
sta_name: infoStation.sta_name,
|
|
output: data,
|
|
status: "Error",
|
|
});
|
|
fs.writeFile(time, data, function (err) {
|
|
if (err) {
|
|
return console.error(err);
|
|
}
|
|
console.log("Write log line " + listLine.line_number);
|
|
});
|
|
Device.addLog(
|
|
listLine.id_line,
|
|
time,
|
|
"Error",
|
|
Date.now(),
|
|
created_by,
|
|
"",
|
|
""
|
|
);
|
|
socket?.emit("send-process", {
|
|
nameFile: time,
|
|
id: listLine.id_line,
|
|
result,
|
|
infoStation,
|
|
listLine,
|
|
specialItem: specialItem.pData,
|
|
checkSendMail: "true",
|
|
});
|
|
} else {
|
|
if(data.search(command[0])!==-1){
|
|
// console.log("DATA: ",data
|
|
// .split("\n"))
|
|
// console.log("DATA2: ",data
|
|
// .split("\n")
|
|
// .filter(
|
|
// (i) =>
|
|
// i.search("PID:") !== -1 &&
|
|
// i.search("VID:") !== -1 &&
|
|
// i.search("SN:") !== -1
|
|
// )[0])
|
|
let PID = data
|
|
.split("\n")
|
|
.filter(
|
|
(i) =>
|
|
i.search("PID:") !== -1 &&
|
|
i.search("VID:") !== -1 &&
|
|
i.search("SN:") !== -1
|
|
)[0]
|
|
?.split("VID:")[0]
|
|
?.split(":")[1]
|
|
.replace(/,/g, "")
|
|
?.trim();
|
|
let VID = data
|
|
.split("\n")
|
|
.filter(
|
|
(i) =>
|
|
i.search("PID:") !== -1 &&
|
|
i.search("VID:") !== -1 &&
|
|
i.search("SN:") !== -1
|
|
)[0]
|
|
?.split("VID:")[1]
|
|
?.split("SN:")[0]
|
|
.replace(",", "")
|
|
.trim();
|
|
let SN = data
|
|
.split("\n")
|
|
.filter(
|
|
(i) =>
|
|
i.search("PID:") !== -1 &&
|
|
i.search("VID:") !== -1 &&
|
|
i.search("SN:") !== -1
|
|
)[0]
|
|
?.split("SN:")[1]
|
|
?.trim();
|
|
|
|
let report = await detectFaulty(data);
|
|
let Item =
|
|
report.modelCheck !== ""
|
|
? "\nExtra items: " + report.modelCheck + "\n"
|
|
: "";
|
|
if (report.fail.length > 15 && report.report.length > 20) {
|
|
result.push({
|
|
id_line: listLine.id_line,
|
|
line: listLine.line_number,
|
|
line_ip: listLine.line_ip,
|
|
port: listLine.line_port,
|
|
PID: PID,
|
|
SN: SN,
|
|
sta_name: infoStation.sta_name,
|
|
modelCheck: report.modelCheck,
|
|
RAMcheck: report.RAMcheck,
|
|
output: (await getInfoLog(data)) + Item + report.report,
|
|
status: "Faulty",
|
|
});
|
|
|
|
let checkSendMail = "";
|
|
if (
|
|
listLog
|
|
.filter((i) => i.SN === SN)
|
|
.sort(function (a, b) {
|
|
if (a.created_at > b.created_at) {
|
|
return -1;
|
|
}
|
|
if (a.created_at < b.created_at) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
})[0] !== undefined &&
|
|
listLog
|
|
.filter((i) => i.SN === SN)
|
|
.sort(function (a, b) {
|
|
if (a.created_at > b.created_at) {
|
|
return -1;
|
|
}
|
|
if (a.created_at < b.created_at) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
})[0].log_status === "Faulty"
|
|
) {
|
|
checkSendMail = checkSendMail + "false";
|
|
} else {
|
|
checkSendMail = checkSendMail + "true";
|
|
}
|
|
// console.log("check send mail*******", checkSendMail);
|
|
fs.writeFile(
|
|
time,
|
|
(await getInfoLog(data)) + Item + report.report,
|
|
function (err) {
|
|
if (err) {
|
|
return console.error(err);
|
|
}
|
|
console.log("Write log line " + listLine.line_number);
|
|
}
|
|
);
|
|
Device.addLog(
|
|
listLine.id_line,
|
|
time,
|
|
"Faulty",
|
|
Date.now(),
|
|
created_by,
|
|
PID,
|
|
SN
|
|
);
|
|
socket?.emit("send-process", {
|
|
nameFile: time,
|
|
id: listLine.id_line,
|
|
result,
|
|
infoStation,
|
|
listLine,
|
|
specialItem: specialItem.pData,
|
|
checkSendMail: checkSendMail,
|
|
});
|
|
} else {
|
|
if (data.length > 25 && report.uniqueArr.length === 0) {
|
|
const checkAction = checkPID(PID);
|
|
let status = "";
|
|
// console.log("checkAction: ", checkAction)
|
|
|
|
if (checkAction !== undefined) {
|
|
if (checkAction.familyProduct.search("Switche") !== -1) {
|
|
status = status + "DPEL";
|
|
} else {
|
|
status = status + "DPELP";
|
|
}
|
|
|
|
if (checkAction.LED === "Check") {
|
|
status = status + "--(On-site tester) Please check LED";
|
|
} else {
|
|
if (checkAction.POE === "Check") {
|
|
status = status + "--(On-site tester) Please check POE";
|
|
} else {
|
|
status =
|
|
status +
|
|
"--Auto test done. (On-site tester) Please test LED/Ports";
|
|
}
|
|
}
|
|
}
|
|
result.push({
|
|
id_line: listLine.id_line,
|
|
line: listLine.line_number,
|
|
line_ip: listLine.line_ip,
|
|
port: listLine.line_port,
|
|
PID: PID,
|
|
SN: SN,
|
|
sta_name: infoStation.sta_name,
|
|
modelCheck: report.modelCheck,
|
|
RAMcheck: report.RAMcheck,
|
|
output: (await getInfoLog(data)) + Item + data,
|
|
familyProduct: checkAction.familyProduct,
|
|
status: status.split("--")[0],
|
|
});
|
|
let checkSendMail = "";
|
|
if (
|
|
listLog
|
|
.filter((i) => i.SN === SN)
|
|
.sort(function (a, b) {
|
|
if (a.created_at > b.created_at) {
|
|
return -1;
|
|
}
|
|
if (a.created_at < b.created_at) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
})[0] !== undefined &&
|
|
listLog
|
|
.filter((i) => i.SN === SN)
|
|
.sort(function (a, b) {
|
|
if (a.created_at > b.created_at) {
|
|
return -1;
|
|
}
|
|
if (a.created_at < b.created_at) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
})[0]
|
|
.log_status.search("DPEL") !== -1
|
|
) {
|
|
checkSendMail = checkSendMail + "false";
|
|
} else {
|
|
checkSendMail = checkSendMail + "true";
|
|
}
|
|
fs.writeFile(
|
|
time,
|
|
(await getInfoLog(data)) + Item + data,
|
|
function (err) {
|
|
if (err) {
|
|
return console.error(err);
|
|
}
|
|
console.log("Write log line " + listLine.line_number);
|
|
}
|
|
);
|
|
console.log({ time, status, created_by, PID, SN });
|
|
Device.addLog(
|
|
listLine.id_line,
|
|
time,
|
|
status,
|
|
Date.now(),
|
|
created_by,
|
|
PID,
|
|
SN
|
|
);
|
|
socket?.emit("send-process", {
|
|
nameFile: time,
|
|
id: listLine.id_line,
|
|
result,
|
|
infoStation,
|
|
listLine,
|
|
specialItem: specialItem.pData,
|
|
checkSendMail: checkSendMail,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
autoTest.stderr.on("data", (data) => {
|
|
data = data.toString();
|
|
console.log("data scan: ", data);
|
|
result.push({
|
|
id_line: listLine.id_line,
|
|
line: listLine.line_number,
|
|
line_ip: listLine.line_ip,
|
|
port: listLine.line_port,
|
|
sta_name: infoStation.sta_name,
|
|
output: data,
|
|
status: "Error",
|
|
});
|
|
fs.writeFile(time, data, function (err) {
|
|
if (err) {
|
|
return console.error(err);
|
|
}
|
|
console.log("Write log line " + listLine.line_number);
|
|
});
|
|
Device.addLog(
|
|
listLine.id_line,
|
|
time,
|
|
"Error",
|
|
Date.now(),
|
|
created_by,
|
|
"",
|
|
""
|
|
);
|
|
socket?.emit("send-process", {
|
|
nameFile: time,
|
|
id: listLine.id_line,
|
|
result,
|
|
infoStation,
|
|
listLine,
|
|
specialItem: specialItem.pData,
|
|
checkSendMail: "true",
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
};
|
|
|
|
module.exports.SRW_test = SRW_test;
|