update sendDeviceInfo follow model special and RAM, flash
This commit is contained in:
parent
ae8c0dea08
commit
2a33901778
|
|
@ -14,10 +14,11 @@ export const sendDeviceInfora = async () => {
|
|||
const regexVersion = /sh.*? ver.*/;
|
||||
const regexMemory = /(\d+)K/g;
|
||||
const date = moment(Date.now()).format("YYYYMMDD");
|
||||
const memDefault = (await KeyValue.all())
|
||||
const allKeyValue = await KeyValue.all();
|
||||
const memDefault = allKeyValue
|
||||
.filter((i) => i.$attributes.key === "MEMORY_DEFAULT")
|
||||
.map((obj) => obj.$attributes.value);
|
||||
const listInformation = [];
|
||||
let listInformation = [];
|
||||
let dataFile = await LogDetectFile.all();
|
||||
|
||||
let html = "";
|
||||
|
|
@ -38,300 +39,283 @@ export const sendDeviceInfora = async () => {
|
|||
});
|
||||
|
||||
//Read file in listFile
|
||||
await listFile.map(async (file) => {
|
||||
fs.readFile(
|
||||
Env.get("FOLDER_LOGS") + "/" + file,
|
||||
"utf8",
|
||||
async (err, data) => {
|
||||
if (err) {
|
||||
console.log(`Error reading file: ${err}`);
|
||||
} else {
|
||||
//Array line
|
||||
const lines = data?.split("\n");
|
||||
const linesInventory = [];
|
||||
let DBFileCheck = dataFile.filter(
|
||||
(i) => i.$attributes.file_name === file
|
||||
)[0];
|
||||
if (DBFileCheck !== undefined) {
|
||||
if (lines.length > DBFileCheck?.$extras.last_check_SN) {
|
||||
const DBFile = await LogDetectFile.find(
|
||||
DBFileCheck?.$attributes.id_ldf
|
||||
);
|
||||
|
||||
// console.log(DBFile)
|
||||
|
||||
DBFile.last_check_SN = lines.length;
|
||||
await DBFile.save();
|
||||
}
|
||||
const filePromises = listFile.map((file, index) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(
|
||||
Env.get("FOLDER_LOGS") + "/" + file,
|
||||
"utf8",
|
||||
async (err, data) => {
|
||||
if (err) {
|
||||
console.log(`Error reading file: ${err}`);
|
||||
} else {
|
||||
await LogDetectFile.firstOrCreate(
|
||||
{ file_name: file },
|
||||
{ file_name: file }
|
||||
);
|
||||
|
||||
dataFile = await LogDetectFile.all();
|
||||
DBFileCheck = dataFile.filter(
|
||||
//Array line
|
||||
const lines = data?.split("\n");
|
||||
const linesInventory = [];
|
||||
let DBFileCheck = dataFile.filter(
|
||||
(i) => i.$attributes.file_name === file
|
||||
)[0];
|
||||
}
|
||||
|
||||
//Get index of "lines" with show inv
|
||||
lines
|
||||
?.slice(DBFileCheck?.$extras.last_check_SN, lines.length - 1)
|
||||
.map((line, index) => {
|
||||
if (line.match(regexInventory) !== null) {
|
||||
linesInventory.push(index);
|
||||
}
|
||||
});
|
||||
|
||||
//cut content with content1 = [linesInventory[index],linesInventory[index+1]] ...
|
||||
linesInventory?.map((line, index) => {
|
||||
const deviceContent = lines?.slice(
|
||||
linesInventory[index],
|
||||
linesInventory[index + 1]
|
||||
);
|
||||
|
||||
let backgroundColor = "rgb(200 200 200 / 30%)";
|
||||
const showInventory = [];
|
||||
let check = true;
|
||||
let begin = 0;
|
||||
let end = 4;
|
||||
|
||||
//get showInventory content
|
||||
while (check === true) {
|
||||
if (
|
||||
deviceContent
|
||||
.slice(begin, end)
|
||||
.filter(
|
||||
(i) =>
|
||||
i.includes("PID:") &&
|
||||
i.includes("VID:") &&
|
||||
i.includes("SN:")
|
||||
).length > 0
|
||||
) {
|
||||
showInventory.push(
|
||||
deviceContent.slice(begin, end).join("\n")
|
||||
if (DBFileCheck !== undefined) {
|
||||
if (lines.length > DBFileCheck?.$extras.last_check_SN) {
|
||||
const DBFile = await LogDetectFile.find(
|
||||
DBFileCheck?.$attributes.id_ldf
|
||||
);
|
||||
begin = end;
|
||||
end = end + 4;
|
||||
} else {
|
||||
check = false;
|
||||
|
||||
// console.log(DBFile)
|
||||
|
||||
DBFile.last_check_SN = lines.length;
|
||||
await DBFile.save();
|
||||
}
|
||||
} else {
|
||||
await LogDetectFile.firstOrCreate(
|
||||
{ file_name: file },
|
||||
{ file_name: file }
|
||||
);
|
||||
|
||||
dataFile = await LogDetectFile.all();
|
||||
DBFileCheck = dataFile.filter(
|
||||
(i) => i.$attributes.file_name === file
|
||||
)[0];
|
||||
}
|
||||
|
||||
const showInventoryContent = showInventory
|
||||
.join("\n")
|
||||
.split("\n")
|
||||
.filter(
|
||||
(i) =>
|
||||
i.includes("PID:") &&
|
||||
i.includes("VID:") &&
|
||||
i.includes("SN:")
|
||||
//Get index of "lines" with show inv
|
||||
lines
|
||||
?.slice(DBFileCheck?.$extras.last_check_SN, lines.length - 1)
|
||||
.map((line, index) => {
|
||||
if (line.match(regexInventory) !== null) {
|
||||
linesInventory.push(index);
|
||||
}
|
||||
});
|
||||
|
||||
//cut content with content1 = [linesInventory[index],linesInventory[index+1]] ...
|
||||
linesInventory?.map((line, index) => {
|
||||
const deviceContent = lines?.slice(
|
||||
linesInventory[index],
|
||||
linesInventory[index + 1]
|
||||
);
|
||||
|
||||
//show version exists
|
||||
if (
|
||||
deviceContent.filter(
|
||||
(line) => line.match(regexVersion) !== null
|
||||
).length > 0
|
||||
) {
|
||||
const lineShowver = deviceContent.indexOf(
|
||||
let backgroundColor = "rgb(200 200 200 / 30%)";
|
||||
const showInventory = [];
|
||||
let check = true;
|
||||
let begin = 0;
|
||||
let end = 4;
|
||||
|
||||
//get showInventory content
|
||||
while (check === true) {
|
||||
if (
|
||||
deviceContent
|
||||
.slice(begin, end)
|
||||
.filter(
|
||||
(i) =>
|
||||
i.includes("PID:") &&
|
||||
i.includes("VID:") &&
|
||||
i.includes("SN:")
|
||||
).length > 0
|
||||
) {
|
||||
showInventory.push(
|
||||
deviceContent.slice(begin, end).join("\n")
|
||||
);
|
||||
begin = end;
|
||||
end = end + 4;
|
||||
} else {
|
||||
check = false;
|
||||
}
|
||||
}
|
||||
|
||||
const showInventoryContent = showInventory
|
||||
.join("\n")
|
||||
.split("\n")
|
||||
.filter(
|
||||
(i) =>
|
||||
i.includes("PID:") &&
|
||||
i.includes("VID:") &&
|
||||
i.includes("SN:")
|
||||
);
|
||||
|
||||
//show version exists
|
||||
if (
|
||||
deviceContent.filter(
|
||||
(line) => line.match(regexVersion) !== null
|
||||
)[0]
|
||||
);
|
||||
|
||||
const showVersion = deviceContent.slice(
|
||||
lineShowver,
|
||||
deviceContent.indexOf(
|
||||
).length > 0
|
||||
) {
|
||||
const lineShowver = deviceContent.indexOf(
|
||||
deviceContent.filter(
|
||||
(line) => line.search("Configuration register") !== -1
|
||||
(line) => line.match(regexVersion) !== null
|
||||
)[0]
|
||||
) + 1
|
||||
);
|
||||
);
|
||||
|
||||
showInventoryContent.map((u, index) => {
|
||||
const PID = u
|
||||
?.split("VID:")[0]
|
||||
?.split("PID:")[1]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const VID = u
|
||||
?.split("VID:")[1]
|
||||
?.split("SN:")[0]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const SN = u?.split("SN:")[1]?.replace(/,/g, "").trim();
|
||||
// let memory =
|
||||
if (index > 0) {
|
||||
if (PID !== "" && SN !== "") {
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: "",
|
||||
flash: "",
|
||||
extra_item: "yes",
|
||||
});
|
||||
const showVersion = deviceContent.slice(
|
||||
lineShowver,
|
||||
deviceContent.indexOf(
|
||||
deviceContent.filter(
|
||||
(line) => line.search("Configuration register") !== -1
|
||||
)[0]
|
||||
) + 1
|
||||
);
|
||||
|
||||
html += `<tr><td>${PID}</td>
|
||||
<td>${VID}</td>
|
||||
<td>${SN}</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>yes</td></tr>`;
|
||||
}
|
||||
} else {
|
||||
if (PID !== "" && SN !== "") {
|
||||
const memDefaultForPID =
|
||||
memDefault.filter((i) =>
|
||||
PID.includes(i.split(":")[0])
|
||||
)[0] !== undefined
|
||||
? memDefault.filter((i) =>
|
||||
PID.includes(i.split(":")[0])
|
||||
)[0]
|
||||
: PID + ":N/A:N/A";
|
||||
let RAM =
|
||||
showVersion
|
||||
.filter(
|
||||
(line) =>
|
||||
line.includes("bytes of memory") ||
|
||||
line.includes("bytes of physical memory")
|
||||
)
|
||||
.join("<br>")
|
||||
.match(regexMemory) !== null
|
||||
? (
|
||||
parseInt(
|
||||
showVersion
|
||||
.filter(
|
||||
(line) =>
|
||||
line.includes("bytes of memory") ||
|
||||
line.includes("bytes of physical memory")
|
||||
)
|
||||
.join("<br>")
|
||||
.match(regexMemory)[0]
|
||||
) /
|
||||
1024 /
|
||||
1024
|
||||
).toFixed(2) +
|
||||
"G (D: " +
|
||||
memDefaultForPID.split(":")[1] +
|
||||
")"
|
||||
: "";
|
||||
|
||||
let flash = showVersion
|
||||
.filter((line) =>
|
||||
line.toLocaleLowerCase().includes("compactflash")
|
||||
)
|
||||
.join("<br>");
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: RAM,
|
||||
flash:
|
||||
flash.match(regexMemory) !== null
|
||||
showInventoryContent.map((u, index) => {
|
||||
const PID = u
|
||||
?.split("VID:")[0]
|
||||
?.split("PID:")[1]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const VID = u
|
||||
?.split("VID:")[1]
|
||||
?.split("SN:")[0]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const SN = u?.split("SN:")[1]?.replace(/,/g, "").trim();
|
||||
if (index > 0) {
|
||||
if (PID !== "" && SN !== "") {
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: "",
|
||||
flash: "",
|
||||
extra_item: "yes",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (PID !== "" && SN !== "") {
|
||||
const memDefaultForPID =
|
||||
memDefault.filter((i) =>
|
||||
PID.includes(i.split(":")[0])
|
||||
)[0] !== undefined
|
||||
? memDefault.filter((i) =>
|
||||
PID.includes(i.split(":")[0])
|
||||
)[0]
|
||||
: PID + ":N/A:N/A";
|
||||
let RAM =
|
||||
showVersion
|
||||
.filter(
|
||||
(line) =>
|
||||
line.includes("bytes of memory") ||
|
||||
line.includes("bytes of physical memory")
|
||||
)
|
||||
.join("<br>")
|
||||
.match(regexMemory) !== null
|
||||
? (
|
||||
parseInt(flash.match(regexMemory)[0]) /
|
||||
parseInt(
|
||||
showVersion
|
||||
.filter(
|
||||
(line) =>
|
||||
line.includes("bytes of memory") ||
|
||||
line.includes(
|
||||
"bytes of physical memory"
|
||||
)
|
||||
)
|
||||
.join("<br>")
|
||||
.match(regexMemory)[0]
|
||||
) /
|
||||
1024 /
|
||||
1024
|
||||
).toFixed(2) +
|
||||
"G (D: " +
|
||||
memDefaultForPID.split(":")[2] +
|
||||
memDefaultForPID.split(":")[1] +
|
||||
")"
|
||||
: "",
|
||||
extra_item: "no",
|
||||
});
|
||||
: "";
|
||||
|
||||
html += `<tr style='background-color:${backgroundColor}'><td>${PID}</td>
|
||||
<td>${VID}</td>
|
||||
<td>${SN}</td>
|
||||
<td>${RAM}</td>
|
||||
<td>${
|
||||
flash.match(regexMemory) !== null
|
||||
? (
|
||||
parseInt(flash.match(regexMemory)[0]) /
|
||||
1024 /
|
||||
1024
|
||||
).toFixed(2) +
|
||||
"G (D: " +
|
||||
memDefaultForPID.split(":")[2] +
|
||||
")"
|
||||
: ""
|
||||
}</td>
|
||||
<td>no</td></tr>`;
|
||||
let flash = showVersion
|
||||
.filter((line) =>
|
||||
line.toLocaleLowerCase().includes("compactflash")
|
||||
)
|
||||
.join("<br>");
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: RAM,
|
||||
flash:
|
||||
flash.match(regexMemory) !== null
|
||||
? (
|
||||
parseInt(flash.match(regexMemory)[0]) /
|
||||
1024 /
|
||||
1024
|
||||
).toFixed(2) +
|
||||
"G (D: " +
|
||||
memDefaultForPID.split(":")[2] +
|
||||
")"
|
||||
: "",
|
||||
extra_item: "no",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
//show version not exists --> RAM = N/A, Flash = N/A
|
||||
showInventoryContent.map((u, index) => {
|
||||
const PID = u
|
||||
?.split("VID:")[0]
|
||||
?.split("PID:")[1]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const VID = u
|
||||
?.split("VID:")[1]
|
||||
?.split("SN:")[0]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const SN = u?.split("SN:")[1]?.replace(/,/g, "").trim();
|
||||
});
|
||||
} else {
|
||||
//show version not exists --> RAM = N/A, Flash = N/A
|
||||
showInventoryContent.map((u, index) => {
|
||||
const PID = u
|
||||
?.split("VID:")[0]
|
||||
?.split("PID:")[1]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const VID = u
|
||||
?.split("VID:")[1]
|
||||
?.split("SN:")[0]
|
||||
?.replace(/,/g, "")
|
||||
.trim();
|
||||
const SN = u?.split("SN:")[1]?.replace(/,/g, "").trim();
|
||||
|
||||
if (index > 0) {
|
||||
if (PID !== "" && SN !== "") {
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: "",
|
||||
flash: "",
|
||||
extra_item: "yes",
|
||||
});
|
||||
|
||||
html += `<tr><td>${PID}</td>
|
||||
<td>${VID}</td>
|
||||
<td>${SN}</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>yes</td></tr>`;
|
||||
if (index > 0) {
|
||||
if (PID !== "" && SN !== "") {
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: "",
|
||||
flash: "",
|
||||
extra_item: "yes",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (PID !== "" && SN !== "") {
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: "",
|
||||
flash: "",
|
||||
extra_item: "no",
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (PID !== "" && SN !== "") {
|
||||
listInformation.push({
|
||||
PID: PID,
|
||||
VID: VID,
|
||||
SN: SN,
|
||||
RAM: "",
|
||||
flash: "",
|
||||
extra_item: "no",
|
||||
});
|
||||
|
||||
html += `<tr style='background-color:${backgroundColor}'><td>${PID}</td>
|
||||
<td>${VID}</td>
|
||||
<td>${SN}</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>no</td></tr>`;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
resolve(0);
|
||||
}
|
||||
}
|
||||
);
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(async () => {
|
||||
if (listInformation.length > 0) {
|
||||
console.log(listInformation);
|
||||
const options = {
|
||||
from: "admin@apactech.io",
|
||||
to: "joseph@apactech.io, ips@ipsupply.com.au",
|
||||
subject: "(AUTO-REPORT) SERIAL NUMBER",
|
||||
html:
|
||||
"<table border='1'>\
|
||||
await Promise.all(filePromises);
|
||||
if (listInformation.length > 0) {
|
||||
console.log(listInformation);
|
||||
let specialModel = allKeyValue
|
||||
.filter((i) => i.key === "MODEL_SPECIAL")
|
||||
.map((obj) => obj.$attributes.value);
|
||||
listInformation = listInformation.filter(
|
||||
(i) =>
|
||||
i.RAM !== "" ||
|
||||
i.flash !== "" ||
|
||||
specialModel.filter((m) => i.PID.includes(m)).length > 0
|
||||
);
|
||||
listInformation.map((inf) => {
|
||||
html += `<tr><td>${inf.PID}</td>
|
||||
<td>${inf.VID}</td>
|
||||
<td>${inf.SN}</td>
|
||||
<td>${inf.RAM}</td>
|
||||
<td>${inf.flash}</td>
|
||||
<td>${inf.extra_item}</td></tr>`;
|
||||
});
|
||||
const options = {
|
||||
from: "admin@apactech.io",
|
||||
to: "joseph@apactech.io, ips@ipsupply.com.au",
|
||||
// to: "joseph@apactech.io",
|
||||
subject: "(AUTO-REPORT) SERIAL NUMBER",
|
||||
html:
|
||||
"<table border='1'>\
|
||||
<thead>\
|
||||
<tr>\
|
||||
<th>PID</th>\
|
||||
|
|
@ -343,15 +327,13 @@ export const sendDeviceInfora = async () => {
|
|||
</tr>\
|
||||
</thead>\
|
||||
<tbody>" +
|
||||
html +
|
||||
"</tbody>\
|
||||
html +
|
||||
"</tbody>\
|
||||
</table>",
|
||||
};
|
||||
transporter.sendMail(options);
|
||||
|
||||
await InfoDevice.createMany(listInformation);
|
||||
}
|
||||
}, 5000);
|
||||
};
|
||||
transporter.sendMail(options);
|
||||
await InfoDevice.createMany(listInformation);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue