From d33ef4bd97c20c845319dfb2850a36fb208ea919 Mon Sep 17 00:00:00 2001 From: Nogard Date: Sat, 22 Mar 2025 23:56:16 +0100 Subject: [PATCH] PYTHON AAAAAAAAAAAAAAAA --- algo.js | 4 ++-- curl.py | 39 +++++++++++++++++++++++++++++++++ script.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 curl.py diff --git a/algo.js b/algo.js index 8216873..4e89762 100644 --- a/algo.js +++ b/algo.js @@ -18,11 +18,11 @@ class Noeud { } addCheminPossible(chemin){ - this.chemins.push(chemin) + this.chemins.push(chemin); } getCheminsPossibles(){ - return this.chemins + return this.chemins; } } diff --git a/curl.py b/curl.py new file mode 100644 index 0000000..c569c7b --- /dev/null +++ b/curl.py @@ -0,0 +1,39 @@ +import requests + +import aiohttp +from aiohttp import web, WSCloseCode +import asyncio + +async def http_handler(request): + payload = await request.text() + print(payload) + + url = 'http://192.168.185.166/turtle/send' + headers = {'content-type': 'application/json'} + r = requests.post(url, data=payload, headers=headers) + + print(r.text) + + return web.Response(text=r.text) + + +def create_runner(): + app = web.Application() + app.add_routes([ + web.post('/', http_handler), + ]) + return web.AppRunner(app) + + +async def start_server(host="127.0.0.1", port=4444): + runner = create_runner() + await runner.setup() + site = web.TCPSite(runner, host, port) + await site.start() + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(start_server()) + loop.run_forever() + diff --git a/script.js b/script.js index e82e94e..9c436fe 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,5 @@ -const URL = "192.168.185.166"; +const ROBOT_URL = "192.168.185.166"; +const CURL_URL = "localhost:4444"; const INFO_POSITION = 0x01; const INFO_LED = 0x02; @@ -10,14 +11,15 @@ let moteur = undefined; let led = undefined; let position = undefined; let pid = undefined; +let turtle = undefined; const connectSockets = () => { if ((socketMoteur != undefined) || (socketInfos != undefined)) { disconnectSocket(); } - socketMoteur = new WebSocket("ws://" + URL + "/motors.ws"); - socketInfos = new WebSocket("ws://" + URL + "/infos.ws"); + socketMoteur = new WebSocket("ws://" + ROBOT_URL + "/motors.ws"); + socketInfos = new WebSocket("ws://" + ROBOT_URL + "/infos.ws"); socketInfos.binaryType = "arraybuffer"; socketMoteur.onclose = function(event) { @@ -36,6 +38,9 @@ const connectSockets = () => { console.log("[socketInfos] Connection established"); position.reset(); led.green(); + new Promise(r => setTimeout(r, 2000)).then(() => { + led.off(); + }); }; socketInfos.onclose = function(event) { @@ -180,7 +185,7 @@ class Led { http = undefined; async send(color){ - await fetch('http://' + URL + '/led/set_color', { + await fetch('http://' + ROBOT_URL + '/led/set_color', { method: 'post', headers: { "Content-Type": "application/x-www-form-urlencoded" }, mode: "no-cors", @@ -236,7 +241,7 @@ class Position { } async reset() { - await fetch("http://" + URL + "/position/reset", { + await fetch("http://" + ROBOT_URL + "/position/reset", { method: 'get', headers: { "Content-Type": "application/x-www-form-urlencoded" }, mode: "no-cors" @@ -244,17 +249,49 @@ class Position { } } +class Turtle { + + async avancer(distance) { + const data = { + 'type': 'dist', + 'dist': distance, + } + await fetch("http://" + CURL_URL, { + method: "POST", + mode: "no-cors", + body: JSON.stringify(data), + }); + } + + async tourner(angle) { + const data = { + 'type': 'angle', + 'angle': angle, + } + await fetch("http://" + CURL_URL, { + method: "POST", + mode: "no-cors", + body: JSON.stringify(data), + }); + + + } + + + +} + async function run() { - - connectSockets(); moteur = new Moteur(); led = new Led(); position = new Position(socketInfos); pid = new PID(); + turtle = new Turtle(); led.red(); + /* @@ -286,3 +323,16 @@ async function run() { } +async function test() { + let d = 190; + const data = { + 'type': 'dist', + 'dist': d, + } + await fetch("http://127.0.0.1:4444/", { + method: "POST", + mode: "no-cors", + body: JSON.stringify(data), + }); +} +