Add uziom generation script and update wet input documents
- Created a new Python script `uziom.py` for generating DXF drawings of grounding systems based on user input parameters. - Added detailed documentation in `wet_input_15.docx` and `wet_input_3.docx` for the design of grounding systems, including calculations for resistance and resistivity measurements. - Included placeholders for dynamic data insertion in the documents to facilitate project-specific customization.
This commit is contained in:
56
pages/api/external.js
Normal file
56
pages/api/external.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import Cors from "cors";
|
||||
|
||||
import fs from "fs";
|
||||
|
||||
// Initializing the cors middleware
|
||||
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
|
||||
const cors = Cors({
|
||||
methods: ["POST"],
|
||||
});
|
||||
|
||||
// Helper method to wait for a middleware to execute before continuing
|
||||
// And to throw an error when an error happens in a middleware
|
||||
function runMiddleware(req, res, fn) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fn(req, res, (result) => {
|
||||
if (result instanceof Error) {
|
||||
return reject(result);
|
||||
}
|
||||
|
||||
return resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default async function handler(req, res) {
|
||||
// Run the middleware
|
||||
await runMiddleware(req, res, cors);
|
||||
|
||||
const id = Math.floor(Math.random() * 90000) + 10000;
|
||||
const textData = req.body;
|
||||
|
||||
let data = textData.split(",");
|
||||
|
||||
let text = "Próbkowanie: 1\nSegment 0: w dół\nX: Y, Z\n";
|
||||
let i = 1;
|
||||
for (let snippet of data) {
|
||||
if (i % 3 != 0) {
|
||||
text += snippet + ",";
|
||||
} else {
|
||||
text += snippet + "\n";
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// console.log(text);
|
||||
|
||||
fs.writeFile(`externals/${id}.txt`, text, (err) => {
|
||||
if (err) {
|
||||
res.status(418);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
// console.log(textData);
|
||||
res.status(200).send({ url: `https://test.wastpol.pl/?external=tru&id=${id}` });
|
||||
}
|
||||
86
pages/api/generateDocx.js
Normal file
86
pages/api/generateDocx.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import PizZip from "pizzip";
|
||||
import Docxtemplater from "docxtemplater";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default async (req, res) => {
|
||||
if (req.method === "POST") {
|
||||
// Get the data from the request body
|
||||
const data = req.body;
|
||||
|
||||
// log stats
|
||||
let ip = req.body.ip;
|
||||
if (ip === undefined)
|
||||
ip = req.headers["x-forwarded-for"] + " on " + req.headers["user-agent"];
|
||||
let date_time = new Date();
|
||||
let string =
|
||||
fs.readFileSync("ground_log.txt", "utf8") +
|
||||
"\n" +
|
||||
"[" +
|
||||
date_time +
|
||||
"]" +
|
||||
ip +
|
||||
" : " +
|
||||
data.geo_data +
|
||||
" : " +
|
||||
data.in_city;
|
||||
fs.writeFile("ground_log.txt", string, function (err) {
|
||||
if (err) {
|
||||
return console.error(err);
|
||||
}
|
||||
});
|
||||
|
||||
//get wet coefficient
|
||||
let input_docx;
|
||||
if (data.wet_coef == 1.6) {
|
||||
if (data.rod_len == 2) input_docx = "wet_input_15.docx";
|
||||
else if (data.rod_len == 3) input_docx = "wet_input_3.docx";
|
||||
} else {
|
||||
if (data.rod_len == 2) input_docx = "dry_input_15.docx";
|
||||
else if (data.rod_len == 3) input_docx = "dry_input_3.docx";
|
||||
}
|
||||
|
||||
// Load the docx file as binary content
|
||||
const content = fs.readFileSync(
|
||||
path.resolve(process.cwd(), input_docx),
|
||||
"binary"
|
||||
);
|
||||
|
||||
const zip = new PizZip(content);
|
||||
|
||||
const doc = new Docxtemplater(zip, {
|
||||
paragraphLoop: true,
|
||||
linebreaks: true,
|
||||
});
|
||||
|
||||
try {
|
||||
// Render the document with data
|
||||
console.log(data);
|
||||
doc.render(data);
|
||||
|
||||
// Generate the document as a buffer
|
||||
const buf = doc.getZip().generate({
|
||||
type: "nodebuffer",
|
||||
compression: "DEFLATE",
|
||||
});
|
||||
|
||||
// Set headers for downloading the file
|
||||
res.setHeader("Content-Disposition", "attachment; filename=opis.docx");
|
||||
res.setHeader(
|
||||
"Content-Type",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
);
|
||||
|
||||
// Send the buffer as a response
|
||||
res.send(buf);
|
||||
} catch (error) {
|
||||
// Handle errors
|
||||
console.error(error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
} else {
|
||||
// Handle non-POST requests
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
};
|
||||
39
pages/api/generateDxf.js
Normal file
39
pages/api/generateDxf.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { exec } from 'child_process';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export default function handler(req, res) {
|
||||
if (req.method === 'POST') {
|
||||
// Get the input data from the request body
|
||||
const data = req.body;
|
||||
|
||||
// Prepare arguments to pass to the Python script if needed
|
||||
const scriptArgs = data.args || [];
|
||||
console.log(scriptArgs)
|
||||
const Arguments = "\"" + scriptArgs.join('" "') + "\"";
|
||||
|
||||
// Run the Python script and pass the arguments
|
||||
exec(`python3 uziom.py ${Arguments}`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
// Handle execution errors
|
||||
console.error(`exec error: ${error}`);
|
||||
return res.status(500).json({ message: 'Internal server error' });
|
||||
}
|
||||
|
||||
// The output file needs to have been created by the Python script
|
||||
const filePath = path.resolve('./public/uziom.dxf');
|
||||
const fileName = 'uziom.dxf';
|
||||
|
||||
// Set headers for file download
|
||||
res.setHeader('Content-Disposition', `attachment; filename=${fileName}`);
|
||||
res.setHeader('Content-Type', 'application/dxf');
|
||||
|
||||
// Read and send the file data
|
||||
const fileStream = fs.createReadStream(filePath);
|
||||
fileStream.pipe(res);
|
||||
});
|
||||
} else {
|
||||
// Handle any other HTTP methods if necessary
|
||||
res.status(405).json({ message: 'Method not allowed' });
|
||||
}
|
||||
}
|
||||
11
pages/api/readtext.js
Normal file
11
pages/api/readtext.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import fs from "fs";
|
||||
|
||||
export default function (req, res) {
|
||||
fs.readFile(`externals/${req.body.id}.txt`, (err, data) => {
|
||||
if (err) throw err;
|
||||
|
||||
res
|
||||
.status(200)
|
||||
.send({ data: data.toString().slice(0, data.toString().length - 1) });
|
||||
});
|
||||
}
|
||||
@@ -4,7 +4,7 @@ export default function (req, res) {
|
||||
//console.log(req.body);
|
||||
//file
|
||||
let textData = req.body.profil;
|
||||
let replacedData = textData.replace("<EFBFBD>", "o").replace("<EFBFBD>", "e");
|
||||
let replacedData = textData.replace(" ", "o").replace(" ", "e");
|
||||
console.log(replacedData);
|
||||
|
||||
var fs = require("fs");
|
||||
@@ -14,10 +14,32 @@ export default function (req, res) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// log stats
|
||||
let ip = req.body.ip;
|
||||
if (ip === undefined)
|
||||
ip = req.headers["x-forwarded-for"] + " on " + req.headers["user-agent"];
|
||||
let date_time = new Date();
|
||||
let string =
|
||||
fs.readFileSync("profile_log.txt", "utf8") +
|
||||
"\n" +
|
||||
"[" +
|
||||
date_time +
|
||||
"]" +
|
||||
ip +
|
||||
" : " +
|
||||
replacedData
|
||||
fs.writeFile("profile_log.txt", string, function (err) {
|
||||
if (err) {
|
||||
return console.error(err);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//py
|
||||
let fileName = Math.floor(Math.random() * 9999) + 1000;
|
||||
|
||||
const python = spawn("python3", ["a.py", req.body.arguments.scale, fileName]);
|
||||
const python = spawn("python3", ["a.py", req.body.arguments.scale, req.body.arguments.elementOne, req.body.arguments.elementTwo]);
|
||||
|
||||
let dataToSend;
|
||||
python.stdout.on("data", function (data) {
|
||||
|
||||
105
pages/api/upload.js
Normal file
105
pages/api/upload.js
Normal file
@@ -0,0 +1,105 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
// import nextConnect from 'next-connect';
|
||||
// import { sendFile } from 'next/dist/server/send-file';
|
||||
|
||||
import formidable from "formidable";
|
||||
import { spawn } from "child_process";
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: false,
|
||||
},
|
||||
};
|
||||
|
||||
export default async function (req, res) {
|
||||
const form = new formidable.IncomingForm();
|
||||
try {
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
if (err) {
|
||||
return res.status(400).json({ message: err.message });
|
||||
}
|
||||
|
||||
console.log('doing the saving')
|
||||
|
||||
|
||||
const { filepath, originalFilename } = files.file;
|
||||
const dateNow = Date.now()
|
||||
const newFilename = `${Date.now()}-${originalFilename}`;
|
||||
const newPath = path.join(
|
||||
process.cwd(),
|
||||
"public",
|
||||
"uploads",
|
||||
newFilename
|
||||
);
|
||||
|
||||
console.log('i tried so hart')
|
||||
|
||||
const readStream = fs.createReadStream(filepath);
|
||||
const writeStream = fs.createWriteStream(newPath);
|
||||
|
||||
readStream.on('error', function(err) {
|
||||
console.error('Error while reading file.', err);
|
||||
res.status(500).send('Error while uploading file.');
|
||||
});
|
||||
|
||||
writeStream.on('error', function(err) {
|
||||
console.error('Error while writing file.', err);
|
||||
res.status(500).send('Error while uploading file.');
|
||||
});
|
||||
|
||||
writeStream.on('finish', function() {
|
||||
console.log('File copied successfully');
|
||||
let dataToSend;
|
||||
python.stdout.on("data", function (data) {
|
||||
console.log("Pipe data from python script ...");
|
||||
dataToSend = data.toString();
|
||||
console.log(dataToSend);
|
||||
});
|
||||
python.stderr.on("data", (data) => {
|
||||
console.error(`stderr: ${data}`);
|
||||
res.send("Py Error: " + data);
|
||||
});
|
||||
// in close event we are sure that stream from child process is closed
|
||||
python.on("close", (code) => {
|
||||
console.log(`child process close all stdio with code ${code}`);
|
||||
// send data to browser
|
||||
console.log(dataToSend);
|
||||
console.log("done");
|
||||
try {
|
||||
// const handler = nextConnect();
|
||||
|
||||
// handler.get(async (req, res) => {
|
||||
const filename = newFilename; // get the filename from the query string
|
||||
const filePath = path.join(process.cwd(), 'public', 'uploads', filename);
|
||||
|
||||
console.log(newFilename, filename, filePath, dataToSend)
|
||||
|
||||
res.status(200).send({ filename: newFilename, filepath:filePath, data: dataToSend });
|
||||
// sendFile(req, filePath); // send the file as a response
|
||||
// });
|
||||
} catch (e) {
|
||||
console.log("child process end");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
readStream.pipe(writeStream);
|
||||
console.log('and got so far')
|
||||
|
||||
let fileName = Math.floor(Math.random() * 9999) + 1000;
|
||||
|
||||
console.log('starting the engines', newPath)
|
||||
|
||||
const python = spawn("python3", ["cross.py", 50, newPath]);
|
||||
console.log('oui')
|
||||
|
||||
|
||||
|
||||
// return res.status(200).json({ message: "File uploaded successfully" });
|
||||
});
|
||||
} catch (err) {
|
||||
console.log("err: ", err)
|
||||
return res.status(500).json({ message: err.message });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user