315 lines
10 KiB
TypeScript
315 lines
10 KiB
TypeScript
import Env from "@ioc:Adonis/Core/Env";
|
|
import fs from "fs";
|
|
import moment from "moment/moment";
|
|
import dotenv from "dotenv";
|
|
import nodeMailer from "nodemailer";
|
|
dotenv.config();
|
|
|
|
export const sendDeviceInfora = async () => {
|
|
try {
|
|
//Regex check show inventory
|
|
const regexInventory = /sh.*? inv.*/;
|
|
//Regex check show version
|
|
const regexVersion = /sh.*? ver.*/;
|
|
const regexMemory = /(\d+)K/g;
|
|
const date = moment(Date.now()).format("YYYYMMDD");
|
|
const listInformation = [];
|
|
let html = "";
|
|
//List file today
|
|
const listFile = fs
|
|
.readdirSync(Env.get("FOLDER_LOGS"))
|
|
|
|
//Configure mail
|
|
const transporter = nodeMailer.createTransport({
|
|
pool: true,
|
|
host: "mail.ipsupply.com.au",
|
|
port: 465,
|
|
secure: true,
|
|
auth: {
|
|
user: "admin@apactech.io",
|
|
pass: "BGK!dyt6upd2eax1bhz",
|
|
},
|
|
});
|
|
|
|
//Read file in listFile
|
|
await listFile.map((file) => {
|
|
fs.readFile(Env.get("FOLDER_LOGS") + "/" + file, "utf8", (err, data) => {
|
|
if (err) {
|
|
console.log(`Error reading file: ${err}`);
|
|
} else {
|
|
//Array line
|
|
const lines = data?.split("\n");
|
|
const linesInventory = [];
|
|
//Get index of "lines" with show inv
|
|
lines?.map((line, index) => {
|
|
if (line.match(regexInventory) !== null) {
|
|
linesInventory.push(index);
|
|
}
|
|
});
|
|
// console.log(linesInventory);
|
|
//cut content with content1 = [linesInventory[index],linesInventory[index+1]] ...
|
|
linesInventory?.map((line, index) => {
|
|
const deviceContent = lines?.slice(
|
|
linesInventory[index],
|
|
linesInventory[index + 1]
|
|
);
|
|
let backgroundColor = index%2===0?"rgb(200 200 200 / 39%)":"white"
|
|
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)
|
|
.length > 0
|
|
) {
|
|
const lineShowver = deviceContent.indexOf(
|
|
deviceContent.filter(
|
|
(line) => line.match(regexVersion) !== null
|
|
)[0]
|
|
);
|
|
|
|
const showVersion = deviceContent.slice(
|
|
lineShowver,
|
|
deviceContent.indexOf(
|
|
deviceContent.filter(
|
|
(line) => line.search("Configuration register") !== -1
|
|
)[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 !== "") {
|
|
// let RAM =
|
|
// showVersion
|
|
// .filter((line) => line.includes("bytes of memory"))
|
|
// .join("<br>")
|
|
// .match(regexMemory) !== null
|
|
// ? (
|
|
// parseInt(
|
|
// showVersion
|
|
// .filter((line) =>
|
|
// line.includes("bytes of memory")
|
|
// )
|
|
// .join("<br>")
|
|
// .match(regexMemory)[0]
|
|
// ) /
|
|
// 1024 /
|
|
// 1024
|
|
// ).toFixed(2) + "G"
|
|
// : "";
|
|
|
|
// let flash = showVersion
|
|
// .filter((line) =>
|
|
// line.toLocaleLowerCase().includes("compactflash")
|
|
// )
|
|
// .join("<br>");
|
|
listInformation.push({
|
|
PID: PID,
|
|
VID: VID,
|
|
SN: SN,
|
|
RAM: "",
|
|
flash: "",
|
|
extraItem: "yes",
|
|
});
|
|
|
|
html += `<tr><td>${PID}</td>
|
|
<td>${VID}</td>
|
|
<td>${SN}</td>
|
|
<td></td>
|
|
<td></td>
|
|
<td>yes</td></tr>`;
|
|
}
|
|
} else {
|
|
if (PID !== "" && SN !== "") {
|
|
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"
|
|
: "";
|
|
|
|
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"
|
|
: "",
|
|
extraItem: "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"
|
|
: ""
|
|
}</td>
|
|
<td>no</td></tr>`;
|
|
}
|
|
}
|
|
});
|
|
} 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: "",
|
|
extraItem: "yes",
|
|
});
|
|
|
|
html += `<tr><td>${PID}</td>
|
|
<td>${VID}</td>
|
|
<td>${SN}</td>
|
|
<td></td>
|
|
<td></td>
|
|
<td>yes</td></tr>`;
|
|
}
|
|
} else {
|
|
if (PID !== "" && SN !== "") {
|
|
listInformation.push({
|
|
PID: PID,
|
|
VID: VID,
|
|
SN: SN,
|
|
RAM: "",
|
|
flash: "",
|
|
extraItem: "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(() => {
|
|
console.log(listInformation);
|
|
const options = {
|
|
from: "admin@apactech.io",
|
|
to: "joseph@apactech.io",
|
|
subject: "SN AUTO REPORT",
|
|
html:
|
|
"<table border='1'>\
|
|
<thead>\
|
|
<tr>\
|
|
<th>PID</th>\
|
|
<th>VID</th>\
|
|
<th>SN</th>\
|
|
<th>RAM</th>\
|
|
<th>Flash</th>\
|
|
<th>Extra Item</th>\
|
|
</tr>\
|
|
</thead>\
|
|
<tbody>" +
|
|
html +
|
|
"</tbody>\
|
|
</table>",
|
|
};
|
|
transporter.sendMail(options);
|
|
}, 5000);
|
|
// const match = "show inventory".match(regexInventory);
|
|
|
|
// console.log(match)
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
};
|