update sendDeviceInfo follow model special and RAM, flash

This commit is contained in:
joseph le 2023-10-12 10:10:20 +07:00
parent ae8c0dea08
commit 2a33901778
1 changed files with 258 additions and 276 deletions

View File

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