third time's a charm

This commit is contained in:
Chop
2022-02-18 00:14:48 +01:00
parent f7bd47f974
commit e4478d7841
7 changed files with 2258 additions and 267 deletions

2180
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,12 +10,15 @@
}, },
"dependencies": { "dependencies": {
"@supercharge/request-ip": "^1.1.2", "@supercharge/request-ip": "^1.1.2",
"axios": "^0.26.0",
"child_process": "^1.0.2", "child_process": "^1.0.2",
"evergreen-ui": "^6.8.2",
"next": "^12.0.10", "next": "^12.0.10",
"next-auth": "^4.2.1", "next-auth": "^4.2.1",
"nodemailer": "^6.7.2", "nodemailer": "^6.7.2",
"react": "17.0.2", "react": "17.0.2",
"react-dom": "17.0.2" "react-dom": "17.0.2",
"three": "^0.137.5"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "^10.4.0", "autoprefixer": "^10.4.0",

View File

@@ -1,38 +1,44 @@
import { spawn } from "child_process"; import { spawn } from "child_process";
export default function (req, res) { export default function (req, res) {
console.log("spawnin"); console.log("spawnin");
//console.log(req.body); //console.log(req.body);
//file //file
let textData = req.body.profil; let textData = req.body.profil;
let replacedData = textData.replace("<22>", "o").replace("<22>", "e"); let replacedData = textData.replace("<22>", "o").replace("<22>", "e");
console.log(replacedData); console.log(replacedData);
var fs = require("fs"); var fs = require("fs");
fs.writeFile("P.txt", replacedData, function (err) { fs.writeFile("P.txt", replacedData, function (err) {
if (err) { if (err) {
return console.error(err); return console.error(err);
} }
}); });
//py //py
let fileName = Math.floor(Math.random() * 9999) + 1000;
const python = spawn("python3", ["a.py"]); const python = spawn("python3", ["a.py", req.body.arguments.scale, fileName]);
let dataToSend; let dataToSend;
python.stdout.on("data", function (data) { python.stdout.on("data", function (data) {
console.log("Pipe data from python script ..."); console.log("Pipe data from python script ...");
dataToSend = data.toString(); dataToSend = data.toString();
//console.log(dataToSend) console.log(dataToSend);
}); });
python.stderr.on('data', (data) => { python.stderr.on("data", (data) => {
console.error(`stderr: ${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) => { // in close event we are sure that stream from child process is closed
console.log(`child process close all stdio with code ${code}`); python.on("close", (code) => {
// send data to browser console.log(`child process close all stdio with code ${code}`);
console.log(dataToSend); // send data to browser
console.log("done"); console.log(dataToSend);
res.send("dataToSend"); console.log("done");
}); try {
res.status(200).send({ filename: fileName, data: dataToSend });
} catch (e) {
console.log("child process end");
}
});
} }

View File

@@ -1,97 +1,64 @@
import Head from "next/head"; import Head from "next/head";
import styles from "../styles/Home.module.css"; import styles from "../styles/Home.module.css";
import Header from "./templates/header"; import Header from "./templates/header";
import Generator from "./templates/generator";
import Nav from "./templates/nav"; import Nav from "./templates/nav";
import Content from "./templates/content"; import Content from "./templates/content";
import Footer from "./templates/footer"; import Footer from "./templates/footer";
import { useState } from "react"; import { useState } from "react";
import { useSession, signIn, signOut } from "next-auth/react"; import { useSession, signIn, signOut } from "next-auth/react";
import { Button } from "evergreen-ui";
export default function Home() { export default function Home() {
const { data: session } = useSession(); const { data: session } = useSession();
const [profil, setProfil] = useState();
const py = (e, profil) => { if (session) {
e.preventDefault(); return (
fetch("/api/spawn", { <div className={styles.container}>
method: "POST", <Head>
headers: { <title>Wastpol</title>
Accept: "application/json, text/plain, */*", <meta name="description" content="Wastpol" />
"Content-Type": "application/json", <link rel="icon" href="/icon.png" />
}, </Head>
body: JSON.stringify({ profil: profil }), <div className="flex justify-between">
}).then((res) => { <Nav />
console.log("data from api: ", res); <div className="flex">
document.getElementById("down").click(); <h3 className="px-3 py-2 place-self-end">
}); Zalogowano jako {session.user.email}
}; </h3>
<Button onClick={() => signOut()} className="place-self-end">
Wyloguj
</Button>
</div>
</div>
if (session) { <main className={styles.main}>
return ( <Header />
<div className={styles.container}> <Generator />
<Head> </main>
<title>Wastpol</title> <Footer />
<meta name="description" content="Wastpol" /> </div>
<link rel="icon" href="/icon.png" /> );
</Head> }
<div className="flex justify-between">
<Nav />
<div className="flex">
<h3 className="px-3 py-2 place-self-end">
Zalogowano jako {session.user.email}
</h3>
<button
onClick={() => signOut()}
className="p-2 bg-slate-200 rounded-md place-self-end"
>
Wyloguj
</button>
</div>
</div>
<main className={styles.main}> return (
<Header /> <div className="grid place-items-center h-screen">
<textarea <Head>
id="profil" <title>Wastpol</title>
className="bg-gray-100 h-96 w-96" <meta name="description" content="Wastpol" />
onChange={(e) => { <link rel="icon" href="/icon.png" />
console.log(e.target.value); </Head>
setProfil(e.target.value); <div className="flex flex-col justify-center">
}} <h2 className="p-2">Nie zalogowano</h2>
></textarea> <br></br>
<button <Button
onClick={(e) => { onClick={() => signIn()}
py(e, profil); appearance="primary"
}} // className="p-2 bg-slate-200 rounded-md"
> >
Generuj Zaloguj
</button> </Button>
<a href="test.dxf" download="test.dxf" id="down"> </div>
{" "} </div>
</a> );
</main>
<Footer />
</div>
);
}
return (
<div className="grid place-items-center h-screen">
<Head>
<title>Wastpol</title>
<meta name="description" content="Wastpol" />
<link rel="icon" href="/icon.png" />
</Head>
<div className="flex justify-center">
<h2 className="p-2">Nie zalogowano</h2>
<br></br>
<button
onClick={() => signIn()}
className="p-2 bg-slate-200 rounded-md"
>
Zaloguj
</button>
</div>
</div>
);
} }

View File

@@ -0,0 +1,95 @@
import { useState } from "react";
import {
Pane,
TextInputField,
TextareaField,
Button,
BuildIcon,
toaster,
Alert,
} from "evergreen-ui";
import axios from "axios";
export default function Generator() {
const [profil, setProfil] = useState();
const [args, setArgs] = useState({ scale: 200 });
const parsePreview = (e) => {
// console.log(dxf);
};
const py = (e, profil, args) => {
e.preventDefault();
axios
.post("/api/spawn", {
profil: profil,
arguments: args,
})
.then(function (response) {
console.log(response);
if (response.data.toString().startsWith("Py Error")) {
toaster.danger(response.data);
return;
}
toaster.warning(response.data.data);
document.getElementById("down").download =
response.data.filename.toString() + ".dxf";
document.getElementById("down").click();
})
.catch(function (error) {
console.log(error);
});
};
return (
<div className="flex flex-col">
<Pane width={480}>
<TextareaField
id="textarea-1"
label="Dane z Geoportalu:"
placeholder="Próbkowanie: 1 ..."
onChange={(e) => {
console.log(e.target.value);
setProfil(e.target.value);
parsePreview(e);
}}
/>
<TextInputField
label="Skala:"
placeholder="200"
width={100}
onChange={(e) => {
console.log(e.target.value);
setArgs({ ...args, scale: e.target.value });
}}
/>
<Button
marginY={8}
marginRight={12}
appearance="default"
iconAfter={BuildIcon}
onClick={(e) => {
if (document.getElementById("textarea-1").value == "") {
toaster.danger("Pole danych nie może być puste");
} else if (
!document
.getElementById("textarea-1")
.value.startsWith("Próbkowanie")
) {
toaster.danger("Błędne dane");
} else {
py(e, profil, args);
}
}}
>
Generuj
</Button>
</Pane>
<a href="test.dxf" download="test.dxf" id="down">
{" "}
</a>
</div>
);
}

View File

@@ -1,9 +1,12 @@
import styles from "../../styles/Home.module.css"; import styles from "../../styles/Home.module.css";
import { Pane, Heading } from "evergreen-ui";
export default function Header() { export default function Header() {
return ( return (
<div> <Pane>
<h1 className="text-3xl p-6">Generuj profil przekroju terenu</h1> <Heading is="h1" size={800} marginBottom={42}>
</div> Generuj profil przekroju terenu
); </Heading>
</Pane>
);
} }

View File

@@ -1,19 +1,20 @@
import Link from "next/link"; import Link from "next/link";
import { Pane, Heading } from "evergreen-ui";
export default function Nav() { export default function Nav() {
return ( return (
<div className="flex"> <Pane display="flex">
<Link href="/"> <Link href="/">
<h2 className="px-2 place-self-center"> <h2 className="px-2 place-self-center">
<img src="logo.png" className="h-12" /> <img src="logo.png" className="h-12" />
</h2> </h2>
</Link> </Link>
<Link href="/onas"> <Link href="/onas">
<h3 className="px-3 py-2 place-self-end">O nas</h3> <h3 className="px-3 py-2 place-self-end">O nas</h3>
</Link> </Link>
<Link href="/kontakt"> <Link href="/kontakt">
<h3 className="px-3 py-2 place-self-end">Kontakt</h3> <h3 className="px-3 py-2 place-self-end">Kontakt</h3>
</Link> </Link>
</div> </Pane>
); );
} }