Simulation activate/deactivate

This commit is contained in:
moxitech 2024-11-16 14:12:21 +07:00
parent 9395f71485
commit ef938ab84a
8 changed files with 107 additions and 23 deletions

View File

@ -73,7 +73,7 @@ services:
dockerfile: Dockerfile dockerfile: Dockerfile
env_file: ".env" env_file: ".env"
ports: ports:
- "10000:10000" - "10001:10000"
depends_on: depends_on:
- nats - nats
networks: networks:
@ -95,26 +95,22 @@ services:
networks: networks:
- dns_net - dns_net
restart: always restart: always
# For messaging
nats: #
image: nats math:
container_name: dns-nats container_name: dns-math
build:
context: ./src/microservices/py-simulation
dockerfile: Dockerfile
env_file: ".env"
ports: ports:
- "8222:8222" - "10000:10000"
- "6222:6222"
command: "--cluster_name NATS --cluster nats://0.0.0.0:6222 --http_port 8222 "
networks: networks:
- dns_net - dns_net
restart: always restart: always
nats-1:
image: nats
container_name: dns-nats-cluster-1
command: "--cluster_name NATS --cluster nats://0.0.0.0:6222 --routes=nats://ruser:T0pS3cr3t@nats:6222"
networks:
- dns_net
depends_on: ["nats"]
restart: always
# For messaging
volumes: volumes:
postgres_data: postgres_data:

BIN
docs/imgs/sim_process.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -114,7 +114,7 @@ class DroneNetworkSimulator:
return 5.0 return 5.0
return 1.0 return 1.0
@app.post("/start_simulation") @app.post("/simulation/forceRunCalc")
async def start_simulation(request: SimulationRequest): async def start_simulation(request: SimulationRequest):
try: try:
simulator = DroneNetworkSimulator( simulator = DroneNetworkSimulator(

View File

@ -0,0 +1,15 @@
annotated-types==0.7.0
anyio==4.6.2.post1
click==8.1.7
fastapi==0.115.5
h11==0.14.0
idna==3.10
numpy==2.1.3
pydantic==2.9.2
pydantic_core==2.23.4
PyWavefront==1.3.3
sniffio==1.3.1
starlette==0.41.2
typing_extensions==4.12.2
uvicorn==0.32.0
websockets==14.1

View File

@ -60,9 +60,13 @@ func SpawnServer() error {
// GET http://localhost:8080/get/server/state // GET http://localhost:8080/get/server/state
app.Get("/get/server/state", GetServerState) app.Get("/get/server/state", GetServerState)
// WebSocket маршрут // WebSocket маршрут
app.Get("/ws/connect/", websocket.New(CreateOrConnectSocket)) app.Get("/ws/connect/", websocket.New(CreateOrConnectSocket))
// Return scheme for request to start simulation
app.Get("/simulation/forceRunCalc", ForceRunCalcSim)
return app.Listen(os.Getenv("SERVER_BASE_ADDRESS")) return app.Listen(os.Getenv("SERVER_BASE_ADDRESS"))
} }

View File

@ -49,3 +49,12 @@ func GetActiveRooms(c *fiber.Ctx) error {
c.Write(res) c.Write(res)
return nil return nil
} }
// ForceRunCalcSim "заставляет" запустить симуляцию игнорируя очередь
func ForceRunCalcSim(c *fiber.Ctx) error {
return c.Status(1002).JSON(fiber.Map{
"message": "Wait minutes, prepare simulation...",
"link": "localhost:10000/run/sumulation",
})
}

View File

@ -7,7 +7,12 @@ import { PlayCircleIcon } from '@heroicons/react/24/outline';
import FormComponent from '../ObjectProps/FormComponent'; import FormComponent from '../ObjectProps/FormComponent';
import Modal from '../Modal/Modal'; import Modal from '../Modal/Modal';
const ThreeJsInstance = () => {
interface IThreeJsInstance {
Click: (value: boolean) => void; // клик по кнопке
}
const ThreeJsInstance : React.FC<IThreeJsInstance> = ({Click}) => {
const [drones, setDrones] = useState<Drone[]>([]); const [drones, setDrones] = useState<Drone[]>([]);
const [baseStations, setBaseStations] = useState<BaseStation[]>([]); const [baseStations, setBaseStations] = useState<BaseStation[]>([]);
const [selectedObject, setSelectedObject] = useState<{ type: 'drone' | 'baseStation'; id: number } | null>(null); const [selectedObject, setSelectedObject] = useState<{ type: 'drone' | 'baseStation'; id: number } | null>(null);
@ -58,6 +63,16 @@ const ThreeJsInstance = () => {
]); ]);
}; };
const runSimulation = () => {
// GET:
// baseStations
// drones
// SET TO PAYLOAD
const STANDART_TS = 300; // seconds
const STANDART_TIMESTEP = 1; // timestep
Click(true)
}
const handleObjectClick = (type: 'drone' | 'baseStation', id: number) => { const handleObjectClick = (type: 'drone' | 'baseStation', id: number) => {
setSelectedObject({ type, id }); setSelectedObject({ type, id });
setIsModalOpen(true); setIsModalOpen(true);
@ -181,7 +196,7 @@ const ThreeJsInstance = () => {
<button onClick={resetCamera} className="p-2 m-2 bg-red-500 text-white rounded transition duration-300 ease-in-out transform hover:scale-105 focus:scale-95">Сбросить камеру</button> <button onClick={resetCamera} className="p-2 m-2 bg-red-500 text-white rounded transition duration-300 ease-in-out transform hover:scale-105 focus:scale-95">Сбросить камеру</button>
<button onClick={() => document.getElementById("fileInput")?.click()} className="p-2 m-2 bg-orange-400 text-white rounded transition duration-300 ease-in-out transform hover:scale-105 focus:scale-95">Загрузка из JSON</button> <button onClick={() => document.getElementById("fileInput")?.click()} className="p-2 m-2 bg-orange-400 text-white rounded transition duration-300 ease-in-out transform hover:scale-105 focus:scale-95">Загрузка из JSON</button>
<button onClick={() => {}} className="p-1 m-2 w-9 h-9 bg-emerald-700 text-white rounded transition duration-300 ease-in-out transform hover:scale-105 focus:scale-95"> <button onClick={() => runSimulation()} className="p-1 m-2 w-9 h-9 bg-emerald-700 text-white rounded transition duration-300 ease-in-out transform hover:scale-105 focus:scale-95">
<PlayCircleIcon/> <PlayCircleIcon/>
</button> </button>
{selectedObject && ( {selectedObject && (
@ -270,3 +285,6 @@ const BaseStationModel = ({ position, onClick, isSelected }: { position: [number
}; };
export default ThreeJsInstance; export default ThreeJsInstance;
// /simulation/forceRunCalc => start simulation => REQUEST(DTO) => START_PLAYER()

View File

@ -1,17 +1,43 @@
"use client"; "use client";
import InitPlayer from '@/app/components/Player/Player';
import ThreeJsInstance from '@/app/components/Threejs/ThreeJsInstance'; import ThreeJsInstance from '@/app/components/Threejs/ThreeJsInstance';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
const Simulations: React.FC = () => { const Simulations: React.FC = () => {
const [activeSimalationWindow, setActiveSimalationWindow] = useState(false); const [activeSimulationWindow, setActiveSimulationWindow] = useState(false);
const [mathServer, setMathServer] = useState("");
const [simulationDto, setSimulationDto] = useState({});
const ActiveSimulation = (value: boolean) => setActiveSimulationWindow(value);
useEffect(() => {
// let value = SendRequestGetLink()
// value.then(e => {
// if (e.status == 1002){
// e.json().then( res =>{
// if (res.link) {
// setMathServer(res.link)
// }})}
// })
setMathServer("localhost:10000/run/sumulation")
}, [])
// => localhost:10000/run/sumulation
return ( return (
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]"> <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
{/* TEST : MATH SERVER ENDPOINT */}
{/* <p>{mathServer}</p> */}
<main id="modal-root"> <main id="modal-root">
{activeSimalationWindow ? <div> {activeSimulationWindow ?
</div> : <ThreeJsInstance/>} <div>
<InitPlayer
Click={() => {}}
TimeEnd={100}
TimeStep={1}
></InitPlayer>
</div> : <ThreeJsInstance Click={ActiveSimulation}/>}
</main> </main>
<footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center"> <footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center">
@ -19,4 +45,20 @@ const Simulations: React.FC = () => {
</div> </div>
); );
}; };
const SendRequestGetLink = async () => {
const requestOptions: RequestInit = {
method: "GET",
redirect: "follow"
};
try {
const response = await fetch(`http://api.localhost/simulation/forceRunCalc`, requestOptions);
return response;
} catch (error) {
throw new Error('Network error');
}
};
export default Simulations; export default Simulations;