298 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			298 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
import fs from "fs";
 | 
						|
import type { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
 | 
						|
import axios from "axios";
 | 
						|
 | 
						|
export default class ErpsController {
 | 
						|
  /**
 | 
						|
   * Controller do tim cac serial number trong cac log trong khoang thoi gian xac dinh
 | 
						|
   * @param {Integer} from thoi gian bat dau YYYYMMDD (vd: 20230130)
 | 
						|
   * @param {Integer} to thoi gian ket thuc YYYYMMDD (vd: 20230230)
 | 
						|
   */
 | 
						|
  public async getIndexSerialNumber({
 | 
						|
    request,
 | 
						|
    response,
 | 
						|
  }: HttpContextContract) {
 | 
						|
    const { from, to } = request.all();
 | 
						|
    const getListLog = async (from, to) => {
 | 
						|
      try {
 | 
						|
        // console.log("check!")
 | 
						|
        const listLog:string[] = []
 | 
						|
        const response = await axios.get("http://192.168.5.7:8080/");
 | 
						|
        const data = response.data;
 | 
						|
        const arrayLine = data
 | 
						|
          .split("\n")
 | 
						|
          .filter((i) => i.search("<a href") !== -1 && i.search(".log") !== -1);
 | 
						|
        arrayLine.map((u) => {
 | 
						|
          let temp = u
 | 
						|
            .slice(u.search("<a ") + 9, u.search("</a>"))
 | 
						|
            .split(">")[1];
 | 
						|
 | 
						|
          if (
 | 
						|
            parseInt(temp?.split("-")[0]) >= from &&
 | 
						|
            parseInt(temp?.split("-")[0]) <= to
 | 
						|
          ) {
 | 
						|
            listLog.push(
 | 
						|
              "http://192.168.5.7:8080/" +
 | 
						|
                u.slice(u.search("<a ") + 9, u.search("</a>")).split(">")[1] +
 | 
						|
                " "
 | 
						|
            );
 | 
						|
          }
 | 
						|
        });
 | 
						|
 | 
						|
        return listLog;
 | 
						|
      } catch (error) {
 | 
						|
        console.log(error);
 | 
						|
      }
 | 
						|
    };
 | 
						|
 | 
						|
    const fetchFiles = async (from, to) => {
 | 
						|
      try {
 | 
						|
        const urls = await getListLog(from, to);
 | 
						|
        let report = [];
 | 
						|
        const fileContents = await Promise.all(
 | 
						|
          urls.map(async (url) => {
 | 
						|
            const maxRetries = 10;
 | 
						|
            let retries = 0;
 | 
						|
            while (retries < maxRetries) {
 | 
						|
              try {
 | 
						|
                const response = await axios.get(url?.split(" ")[0]);
 | 
						|
                return response.data;
 | 
						|
              } catch (error) {
 | 
						|
                if (error.code !== "") {
 | 
						|
                  //=== "ETIMEDOUT" || error.code === "ECONNRESET"
 | 
						|
                  // console.log("Connection timed out. Retrying...");
 | 
						|
                  retries++;
 | 
						|
                } else {
 | 
						|
                  console.error("Error fetching file:", error);
 | 
						|
                  return;
 | 
						|
                }
 | 
						|
              }
 | 
						|
            }
 | 
						|
          })
 | 
						|
        );
 | 
						|
 | 
						|
        // Handle the file contents
 | 
						|
        fileContents.forEach((content, index) => {
 | 
						|
          console.log(`Content of file ${index + 1}:`);
 | 
						|
          const arrayLine = content?.split("\n");
 | 
						|
          let output = [];
 | 
						|
          if (arrayLine !== undefined) {
 | 
						|
            for (let i = 0; i < arrayLine.length; i++) {
 | 
						|
              if (
 | 
						|
                arrayLine[i].search("PID:") !== -1 &&
 | 
						|
                arrayLine[i].search("SN:") !== -1 &&
 | 
						|
                arrayLine[i].search("%") === -1 &&
 | 
						|
                arrayLine[i]
 | 
						|
                  ?.split(",")[2]
 | 
						|
                  ?.split(":")[1]
 | 
						|
                  ?.replace("\r", "")
 | 
						|
                  .trim() !== "" &&
 | 
						|
                arrayLine[i]
 | 
						|
                  ?.split(",")[2]
 | 
						|
                  ?.split(":")[1]
 | 
						|
                  ?.replace("\r", "")
 | 
						|
                  .trim() !== "N/A"
 | 
						|
              ) {
 | 
						|
                if (
 | 
						|
                  output.some(
 | 
						|
                    (u) =>
 | 
						|
                      u.SN ===
 | 
						|
                      arrayLine[i]
 | 
						|
                        ?.split(",")[2]
 | 
						|
                        ?.split(":")[1]
 | 
						|
                        ?.replace("\r", "")
 | 
						|
                        .trim()
 | 
						|
                  )
 | 
						|
                ) {
 | 
						|
                  output.map((u, index) => {
 | 
						|
                    if (
 | 
						|
                      u.SN === arrayLine[i]?.split("SN:")[1]?.trim()
 | 
						|
                    ) {
 | 
						|
                      output[index].PID = arrayLine[i]?.split("VID:")[0] !== undefined ? arrayLine[i]?.split("VID:")[0]?.split(":")[1]?.split(",")[0]?.trim() : "";
 | 
						|
                      output[index].VID = arrayLine[i]?.split("SN:")[0] !== undefined ? arrayLine[i]?.split("SN:")[0]?.split("VID:")[1]?.split(",")[0]?.trim() : "",
 | 
						|
                      output[index].line = output[index].line.concat([i + 1]);
 | 
						|
                    }
 | 
						|
                  });
 | 
						|
                } else {
 | 
						|
                  let fName = urls[index]
 | 
						|
                    ?.split("/")
 | 
						|
                    [urls[index]?.split("/")?.length - 1]?.trim();
 | 
						|
                  output.push({
 | 
						|
                    PID:
 | 
						|
                      arrayLine[i]?.split("VID:")[0] !== undefined
 | 
						|
                        ? arrayLine[i]?.split("VID:")[0]?.split(":")[1]?.split(",")[0]?.trim()
 | 
						|
                          : "",
 | 
						|
                    VID:
 | 
						|
                      arrayLine[i]?.split("SN:")[0] !== undefined
 | 
						|
                        ? arrayLine[i]?.split("SN:")[0]?.split("VID:")[1]?.split(",")[0]?.trim()
 | 
						|
                          : "",
 | 
						|
                    SN:
 | 
						|
                      arrayLine[i].split("SN:")[1] !== undefined
 | 
						|
                        ? arrayLine[i]?.split("SN:")[1]?.trim()
 | 
						|
                        : "",
 | 
						|
                    line: [i + 1],
 | 
						|
                    fileName: fName,
 | 
						|
                    warehouse:
 | 
						|
                      fName.search("-US") !== -1 ||
 | 
						|
                      fName.search(".US") !== -1 ||
 | 
						|
                      fName.search("US-") !== -1
 | 
						|
                        ? "US"
 | 
						|
                        : "AU",
 | 
						|
                  });
 | 
						|
                }
 | 
						|
              }
 | 
						|
 | 
						|
              if (arrayLine[i].search("PCB Serial Number") !== -1) {
 | 
						|
                if (
 | 
						|
                  //Neu SN da nam trong output
 | 
						|
                  output.some(
 | 
						|
                    (u) =>
 | 
						|
                      u.SN ===
 | 
						|
                      arrayLine[i]?.split(":")[1]?.replace("\r", "").trim()
 | 
						|
                  )
 | 
						|
                ) {
 | 
						|
                  output.map((u, index) => {
 | 
						|
                    if (
 | 
						|
                      u.SN ===
 | 
						|
                      arrayLine[i]?.split(":")[1]?.replace("\r", "").trim()
 | 
						|
                    ) {
 | 
						|
                      output[index].line = output[index].line.concat([i + 1]);
 | 
						|
                    }
 | 
						|
                  });
 | 
						|
                } else {
 | 
						|
                  let fName = urls[index]
 | 
						|
                    ?.split("/")
 | 
						|
                    [urls[index]?.split("/")?.length - 1]?.trim();
 | 
						|
                  output.push({
 | 
						|
                    PID: "",
 | 
						|
                    VID: "",
 | 
						|
                    SN: arrayLine[i]?.split(":")[1]?.replace("\r", "").trim(),
 | 
						|
                    line: [i + 1],
 | 
						|
                    fileName: fName,
 | 
						|
                    warehouse:
 | 
						|
                      fName.search("-US") !== -1 ||
 | 
						|
                      fName.search(".US") !== -1 ||
 | 
						|
                      fName.search("US-") !== -1
 | 
						|
                        ? "US"
 | 
						|
                        : "AU",
 | 
						|
                  });
 | 
						|
                }
 | 
						|
              }
 | 
						|
 | 
						|
              if (arrayLine[i].search("Processor board ID") !== -1) {
 | 
						|
                if (
 | 
						|
                  //Neu SN da nam trong output
 | 
						|
                  output.some(
 | 
						|
                    (u) =>
 | 
						|
                      u.SN ===
 | 
						|
                      arrayLine[i]
 | 
						|
                        ?.split(" ")
 | 
						|
                        [arrayLine[i]?.split(" ").length - 1]?.replace("\r", "")
 | 
						|
                        .trim()
 | 
						|
                  )
 | 
						|
                ) {
 | 
						|
                  output.map((u, index) => {
 | 
						|
                    if (
 | 
						|
                      u.SN ===
 | 
						|
                      arrayLine[i]
 | 
						|
                        ?.split(" ")
 | 
						|
                        [arrayLine[i]?.split(" ").length - 1]?.replace("\r", "")
 | 
						|
                        .trim()
 | 
						|
                    ) {
 | 
						|
                      output[index].line = output[index].line.concat([i + 1]);
 | 
						|
                    }
 | 
						|
                  });
 | 
						|
                } else {
 | 
						|
                  let fName = urls[index]
 | 
						|
                    ?.split("/")
 | 
						|
                    [urls[index]?.split("/")?.length - 1]?.trim();
 | 
						|
                  output.push({
 | 
						|
                    PID: "",
 | 
						|
                    VID: "",
 | 
						|
                    SN: arrayLine[i]
 | 
						|
                      ?.split(" ")
 | 
						|
                      [arrayLine[i]?.split(" ").length - 1]?.replace("\r", "")
 | 
						|
                      .trim(),
 | 
						|
                    line: [i + 1],
 | 
						|
                    fileName: fName,
 | 
						|
                    warehouse:
 | 
						|
                      fName.search("-US") !== -1 ||
 | 
						|
                      fName.search(".US") !== -1 ||
 | 
						|
                      fName.search("US-") !== -1
 | 
						|
                        ? "US"
 | 
						|
                        : "AU",
 | 
						|
                  });
 | 
						|
                }
 | 
						|
              }
 | 
						|
            }
 | 
						|
            report = report.concat(output);
 | 
						|
          }
 | 
						|
        });
 | 
						|
 | 
						|
        fs.writeFile(
 | 
						|
          "./app/utils/indexSN.txt",
 | 
						|
          JSON.stringify(report)
 | 
						|
            .replace(/,{/g, "\n,{")
 | 
						|
            .replace(/\\u0000/g, ""),
 | 
						|
          function (err) {
 | 
						|
            if (err) {
 | 
						|
              return console.error(err);
 | 
						|
            }
 | 
						|
            console.log("Write loggg !");
 | 
						|
          }
 | 
						|
        );
 | 
						|
        // console.log(report);
 | 
						|
        return report;
 | 
						|
        // }, 15000);
 | 
						|
      } catch (error) {
 | 
						|
        response
 | 
						|
          .status(500)
 | 
						|
          .send({ mess: "GET INFORMATION FAIL", error: error });
 | 
						|
      }
 | 
						|
    };
 | 
						|
    const result = await fetchFiles(from, to);
 | 
						|
    response.status(200).json(result);
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Controller lay noi dung file log theo so dong
 | 
						|
   * @param {String} fileName url file log tren server log
 | 
						|
   * @param {Integer} line dong chua serial number trong log
 | 
						|
   * @param {Integer} range khoang dong truoc/sau line
 | 
						|
   * @author {Bearer Token} req.headers.authorization //token xac thuc
 | 
						|
   */
 | 
						|
  public async getParagraph({ request, response }: HttpContextContract) {
 | 
						|
    const { fileName, line, range } = request.all();
 | 
						|
 | 
						|
    try {
 | 
						|
      const res = await axios.get("http://192.168.5.7:8080/" + fileName);
 | 
						|
      const arrayLine = res?.data?.split("\n");
 | 
						|
      // console.log(arrayLine)
 | 
						|
      if (range >= line) {
 | 
						|
        response.status(200).json({
 | 
						|
          content: arrayLine?.slice(0, line + range)?.join("\n"),
 | 
						|
        });
 | 
						|
      } else {
 | 
						|
        response.status(200).json({
 | 
						|
          content: arrayLine?.slice(line - range - 1, line + range)?.join("\n"),
 | 
						|
        });
 | 
						|
      }
 | 
						|
    } catch (error) {
 | 
						|
      console.log(error);
 | 
						|
      response
 | 
						|
        .status(500)
 | 
						|
        .send({ mess: "GET CONTENT FILE FAIL", error: error });
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  public async store({}: HttpContextContract) {}
 | 
						|
 | 
						|
  public async show({}: HttpContextContract) {}
 | 
						|
 | 
						|
  public async edit({}: HttpContextContract) {}
 | 
						|
 | 
						|
  public async update({}: HttpContextContract) {}
 | 
						|
 | 
						|
  public async destroy({}: HttpContextContract) {}
 | 
						|
}
 |