Update function sendDeviceInfor to mail
This commit is contained in:
		
							parent
							
								
									ca18146b23
								
							
						
					
					
						commit
						297ca11585
					
				| 
						 | 
				
			
			@ -0,0 +1,34 @@
 | 
			
		|||
import { DateTime } from 'luxon'
 | 
			
		||||
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
 | 
			
		||||
 | 
			
		||||
export default class InfoDevice extends BaseModel {
 | 
			
		||||
  public static table = 'info_devices'
 | 
			
		||||
 | 
			
		||||
  @column({ isPrimary: true })
 | 
			
		||||
  public id_info: number
 | 
			
		||||
 | 
			
		||||
  @column()
 | 
			
		||||
  public PID: string
 | 
			
		||||
 | 
			
		||||
  @column()
 | 
			
		||||
  public SN: string
 | 
			
		||||
 | 
			
		||||
  @column()
 | 
			
		||||
  public VID: string
 | 
			
		||||
 | 
			
		||||
  @column()
 | 
			
		||||
  public RAM: string
 | 
			
		||||
 | 
			
		||||
  @column()
 | 
			
		||||
  public flash: string
 | 
			
		||||
 | 
			
		||||
  @column()
 | 
			
		||||
  public extraItem: string
 | 
			
		||||
 | 
			
		||||
  @column.dateTime({ autoCreate: true })
 | 
			
		||||
  public created_at: DateTime
 | 
			
		||||
 | 
			
		||||
  @column.dateTime({ autoCreate: true, autoUpdate: true })
 | 
			
		||||
  public updated_at: DateTime
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -11,6 +11,9 @@ export default class LogDetectFile extends BaseModel {
 | 
			
		|||
  @column()
 | 
			
		||||
  public file_name: string
 | 
			
		||||
 | 
			
		||||
  @column()
 | 
			
		||||
  public last_check_SN: number
 | 
			
		||||
 | 
			
		||||
  @column.dateTime({ autoCreate: true })
 | 
			
		||||
  public created_at: DateTime
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,9 @@ import fs from "fs";
 | 
			
		|||
import moment from "moment/moment";
 | 
			
		||||
import dotenv from "dotenv";
 | 
			
		||||
import nodeMailer from "nodemailer";
 | 
			
		||||
import LogDetectFile from "App/Models/LogDetectFile";
 | 
			
		||||
import InfoDevice from "App/Models/InfoDevice";
 | 
			
		||||
import KeyValue from "App/Models/KeyValue";
 | 
			
		||||
dotenv.config();
 | 
			
		||||
 | 
			
		||||
export const sendDeviceInfora = async () => {
 | 
			
		||||
| 
						 | 
				
			
			@ -13,11 +16,16 @@ 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())
 | 
			
		||||
      .filter((i) => i.$attributes.key === "MEMORY_DEFAULT")
 | 
			
		||||
      .map((obj) => obj.$attributes.value);
 | 
			
		||||
    const listInformation = [];
 | 
			
		||||
    let dataFile = await LogDetectFile.all();
 | 
			
		||||
    // console.log(dataFile)
 | 
			
		||||
    // console.log(dataFile)
 | 
			
		||||
    let html = "";
 | 
			
		||||
    //List file today
 | 
			
		||||
    const listFile = fs
 | 
			
		||||
      .readdirSync(Env.get("FOLDER_LOGS"))
 | 
			
		||||
    const listFile = fs.readdirSync(Env.get("FOLDER_LOGS"));
 | 
			
		||||
 | 
			
		||||
    //Configure mail
 | 
			
		||||
    const transporter = nodeMailer.createTransport({
 | 
			
		||||
| 
						 | 
				
			
			@ -32,28 +40,60 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
    });
 | 
			
		||||
 | 
			
		||||
    //Read file in listFile
 | 
			
		||||
    await listFile.map((file) => {
 | 
			
		||||
      fs.readFile(Env.get("FOLDER_LOGS") + "/" + file, "utf8", (err, data) => {
 | 
			
		||||
    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();
 | 
			
		||||
              }
 | 
			
		||||
            } else {
 | 
			
		||||
              await LogDetectFile.firstOrCreate(
 | 
			
		||||
                { file_name: file },
 | 
			
		||||
                { file_name: file }
 | 
			
		||||
              );
 | 
			
		||||
 | 
			
		||||
              dataFile = await LogDetectFile.all();
 | 
			
		||||
              DBFileCheck = dataFile.filter(
 | 
			
		||||
                (i) => i.$attributes.file_name === file
 | 
			
		||||
              )[0];
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            //Get index of "lines" with show inv
 | 
			
		||||
          lines?.map((line, index) => {
 | 
			
		||||
            lines
 | 
			
		||||
              ?.slice(DBFileCheck?.$extras.last_check_SN, lines.length - 1)
 | 
			
		||||
              .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 = "rgb(200 200 200 / 30%)"
 | 
			
		||||
 | 
			
		||||
              let backgroundColor = "rgb(200 200 200 / 30%)";
 | 
			
		||||
              const showInventory = [];
 | 
			
		||||
              let check = true;
 | 
			
		||||
              let begin = 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -71,7 +111,9 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                        i.includes("SN:")
 | 
			
		||||
                    ).length > 0
 | 
			
		||||
                ) {
 | 
			
		||||
                showInventory.push(deviceContent.slice(begin, end).join("\n"));
 | 
			
		||||
                  showInventory.push(
 | 
			
		||||
                    deviceContent.slice(begin, end).join("\n")
 | 
			
		||||
                  );
 | 
			
		||||
                  begin = end;
 | 
			
		||||
                  end = end + 4;
 | 
			
		||||
                } else {
 | 
			
		||||
| 
						 | 
				
			
			@ -84,13 +126,16 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                .split("\n")
 | 
			
		||||
                .filter(
 | 
			
		||||
                  (i) =>
 | 
			
		||||
                  i.includes("PID:") && i.includes("VID:") && i.includes("SN:")
 | 
			
		||||
                    i.includes("PID:") &&
 | 
			
		||||
                    i.includes("VID:") &&
 | 
			
		||||
                    i.includes("SN:")
 | 
			
		||||
                );
 | 
			
		||||
 | 
			
		||||
              //show version exists
 | 
			
		||||
              if (
 | 
			
		||||
              deviceContent.filter((line) => line.match(regexVersion) !== null)
 | 
			
		||||
                .length > 0
 | 
			
		||||
                deviceContent.filter(
 | 
			
		||||
                  (line) => line.match(regexVersion) !== null
 | 
			
		||||
                ).length > 0
 | 
			
		||||
              ) {
 | 
			
		||||
                const lineShowver = deviceContent.indexOf(
 | 
			
		||||
                  deviceContent.filter(
 | 
			
		||||
| 
						 | 
				
			
			@ -152,7 +197,7 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                        SN: SN,
 | 
			
		||||
                        RAM: "",
 | 
			
		||||
                        flash: "",
 | 
			
		||||
                      extraItem: "yes",
 | 
			
		||||
                        extra_item: "yes",
 | 
			
		||||
                      });
 | 
			
		||||
 | 
			
		||||
                      html += `<tr><td>${PID}</td>
 | 
			
		||||
| 
						 | 
				
			
			@ -164,23 +209,40 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                    }
 | 
			
		||||
                  } 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"))
 | 
			
		||||
                          .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")
 | 
			
		||||
                                  .filter(
 | 
			
		||||
                                    (line) =>
 | 
			
		||||
                                      line.includes("bytes of memory") ||
 | 
			
		||||
                                      line.includes("bytes of physical memory")
 | 
			
		||||
                                  )
 | 
			
		||||
                                  .join("<br>")
 | 
			
		||||
                                  .match(regexMemory)[0]
 | 
			
		||||
                              ) /
 | 
			
		||||
                              1024 /
 | 
			
		||||
                              1024
 | 
			
		||||
                          ).toFixed(2) + "G"
 | 
			
		||||
                            ).toFixed(2) +
 | 
			
		||||
                            "G (D: " +
 | 
			
		||||
                            memDefaultForPID.split(":")[1] +
 | 
			
		||||
                            ")"
 | 
			
		||||
                          : "";
 | 
			
		||||
 | 
			
		||||
                      let flash = showVersion
 | 
			
		||||
| 
						 | 
				
			
			@ -199,9 +261,12 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                                parseInt(flash.match(regexMemory)[0]) /
 | 
			
		||||
                                1024 /
 | 
			
		||||
                                1024
 | 
			
		||||
                            ).toFixed(2) + "G"
 | 
			
		||||
                              ).toFixed(2) +
 | 
			
		||||
                              "G (D: " +
 | 
			
		||||
                              memDefaultForPID.split(":")[2] +
 | 
			
		||||
                              ")"
 | 
			
		||||
                            : "",
 | 
			
		||||
                      extraItem: "no",
 | 
			
		||||
                        extra_item: "no",
 | 
			
		||||
                      });
 | 
			
		||||
 | 
			
		||||
                      html += `<tr style='background-color:${backgroundColor}'><td>${PID}</td>
 | 
			
		||||
| 
						 | 
				
			
			@ -214,7 +279,10 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                          parseInt(flash.match(regexMemory)[0]) /
 | 
			
		||||
                          1024 /
 | 
			
		||||
                          1024
 | 
			
		||||
                        ).toFixed(2) + "G"
 | 
			
		||||
                        ).toFixed(2) +
 | 
			
		||||
                        "G (D: " +
 | 
			
		||||
                        memDefaultForPID.split(":")[2] +
 | 
			
		||||
                        ")"
 | 
			
		||||
                      : ""
 | 
			
		||||
                  }</td>
 | 
			
		||||
                  <td>no</td></tr>`;
 | 
			
		||||
| 
						 | 
				
			
			@ -244,7 +312,7 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                        SN: SN,
 | 
			
		||||
                        RAM: "",
 | 
			
		||||
                        flash: "",
 | 
			
		||||
                      extraItem: "yes",
 | 
			
		||||
                        extra_item: "yes",
 | 
			
		||||
                      });
 | 
			
		||||
 | 
			
		||||
                      html += `<tr><td>${PID}</td>
 | 
			
		||||
| 
						 | 
				
			
			@ -262,7 +330,7 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
                        SN: SN,
 | 
			
		||||
                        RAM: "",
 | 
			
		||||
                        flash: "",
 | 
			
		||||
                      extraItem: "no",
 | 
			
		||||
                        extra_item: "no",
 | 
			
		||||
                      });
 | 
			
		||||
 | 
			
		||||
                      html += `<tr style='background-color:${backgroundColor}'><td>${PID}</td>
 | 
			
		||||
| 
						 | 
				
			
			@ -277,10 +345,12 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
              }
 | 
			
		||||
            });
 | 
			
		||||
          }
 | 
			
		||||
      });
 | 
			
		||||
        }
 | 
			
		||||
      );
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    setTimeout(() => {
 | 
			
		||||
    setTimeout(async () => {
 | 
			
		||||
      if (listInformation.length > 0) {
 | 
			
		||||
        console.log(listInformation);
 | 
			
		||||
        const options = {
 | 
			
		||||
          from: "admin@apactech.io",
 | 
			
		||||
| 
						 | 
				
			
			@ -304,6 +374,9 @@ export const sendDeviceInfora = async () => {
 | 
			
		|||
        </table>",
 | 
			
		||||
        };
 | 
			
		||||
        transporter.sendMail(options);
 | 
			
		||||
 | 
			
		||||
        await InfoDevice.createMany(listInformation);
 | 
			
		||||
      }
 | 
			
		||||
    }, 5000);
 | 
			
		||||
    // const match = "show inventory".match(regexInventory);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,21 @@
 | 
			
		|||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
 | 
			
		||||
 | 
			
		||||
export default class extends BaseSchema {
 | 
			
		||||
  protected tableName = 'info_devices'
 | 
			
		||||
 | 
			
		||||
  public async up () {
 | 
			
		||||
    this.schema.createTable(this.tableName, (table) => {
 | 
			
		||||
      table.increments('id')
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
 | 
			
		||||
       */
 | 
			
		||||
      table.timestamp('created_at', { useTz: true })
 | 
			
		||||
      table.timestamp('updated_at', { useTz: true })
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async down () {
 | 
			
		||||
    this.schema.dropTable(this.tableName)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue