rewrite backend in rust #32

Merged
lbailly merged 14 commits from rewrite into master 2024-06-13 13:20:05 +02:00
3 changed files with 61 additions and 40 deletions
Showing only changes of commit 4f7dda439a - Show all commits

View file

@ -250,13 +250,35 @@ fn tracked_events<'a>(
}
}
#[put("/track/<id>?<lat>&<long>")]
#[put("/track/<id>?<lat>&<long>", rank = 0)]
fn store_pos(id: &str, lat: f32, long: f32, tracking: &State<Tracking>) {
if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) {
tracked.write().unwrap().pos = (lat, long);
}
}
#[put("/track/<id>?<inv>&<col>", rank = 1)]
fn set_state(id: &str, inv: bool, col: u8, tracking: &State<Tracking>) -> Option<()> {
let tracking_lock = tracking.read().unwrap();
let state = &mut tracking_lock
.get(&id.to_string())
.unwrap()
.write()
.unwrap()
.state;
if let Vieux {
ref mut invisible,
ref mut color,
} = state
{
*invisible = inv;
*color = col;
Some(())
} else {
None
}
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
@ -285,6 +307,9 @@ fn rocket() -> _ {
rocket::build()
.attach(Template::fairing())
.manage(RwLock::new(tracking))
.mount("/", routes![index, store_pos, tracked_view, tracked_events])
.mount(
"/",
routes![index, store_pos, tracked_view, tracked_events, set_state],
)
.mount("/", FileServer::from(relative!("static")))
}

View file

@ -44,6 +44,7 @@
//////////////////////////////////////////////////////////////////////////////
// EVENT LISTENNING
id = "{{id}}";
evtSource = new EventSource("/track/{{id}}/events");
setup_evtlisten_common();

View file

@ -13,9 +13,6 @@
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
crossorigin=""></script>
<!-- SOCKET.IO INCLUDE -->
<script src="/socket.io/socket.io.js"></script>
<style type="text/css">
#map { height: 600px; }
#below {
@ -37,28 +34,25 @@
<input type="checkbox" id="invisibility" name="invisibility" checked onclick='sendState()' />
<label for="invisibility">Invisible</label>
<input type="checkbox" id="blurred" name="blurred" onclick='sendState()' />
<label for="blurred">Brouillé</label></br>
<input type="radio" id="npc0" name="npc" value=0 onclick='sendState()' />
<input type="radio" id="npc0" name="color" value=1 onclick='sendState()' />
<label for="npc0">Traqueur</label>
<input type="radio" id="npc1" name="npc" value=1 checked onclick='sendState()' />
<input type="radio" id="npc1" name="color" value=3 checked onclick='sendState()' />
<label for="npc1"><img src="/icons/3.png" height=15px></label>
<input type="radio" id="npc2" name="npc" value=2 checked onclick='sendState()' />
<input type="radio" id="npc2" name="color" value=4 checked onclick='sendState()' />
<label for="npc2"><img src="/icons/4.png" height=15px></label>
<input type="radio" id="npc3" name="npc" value=3 checked onclick='sendState()' />
<input type="radio" id="npc3" name="color" value=5 checked onclick='sendState()' />
<label for="npc3"><img src="/icons/5.png" height=15px></label>
<input type="radio" id="npc4" name="npc" value=4 checked onclick='sendState()' />
<input type="radio" id="npc4" name="color" value=6 checked onclick='sendState()' />
<label for="npc4"><img src="/icons/6.png" height=15px></label>
<input type="radio" id="npc5" name="npc" value=5 checked onclick='sendState()' />
<input type="radio" id="npc5" name="color" value=7 checked onclick='sendState()' />
<label for="npc5"><img src="/icons/7.png" height=15px></label>
<input type="radio" id="npc6" name="npc" value=6 checked onclick='sendState()' />
<input type="radio" id="npc6" name="color" value=8 checked onclick='sendState()' />
<label for="npc6"><img src="/icons/8.png" height=15px></label>
<input type="radio" id="npc7" name="npc" value=7 checked onclick='sendState()' />
<input type="radio" id="npc7" name="color" value=9 checked onclick='sendState()' />
<label for="npc7"><img src="/icons/9.png" height=15px></label>
<input type="radio" id="npc8" name="npc" value=8 checked onclick='sendState()' />
<input type="radio" id="npc8" name="color" value=10 checked onclick='sendState()' />
<label for="npc8"><img src="/icons/10.png" height=15px></label>
<input type="radio" id="npc9" name="npc" value=9 checked onclick='sendState()' />
<input type="radio" id="npc9" name="color" value=11 checked onclick='sendState()' />
<label for="npc9"><img src="/icons/11.png" height=15px></label>
</form><br/>
@ -68,32 +62,33 @@
</div>
<script type="text/javascript">
// //////////////////////////////////////////////////////////////////////////////
// // SETUP MAP
//////////////////////////////////////////////////////////////////////////////
// SETUP MAP
// setup_map();
//
// //////////////////////////////////////////////////////////////////////////////
// // UPDATE MAP
setup_map();
// id = "{{id}}";
// socket = io({rejectUnauthorized: false, auth: {id: id, type:"vieux"}});
// setup_socket_common();
//////////////////////////////////////////////////////////////////////////////
// EVENT LISTENNING
// //////////////////////////////////////////////////////////////////////////////
// // SETTINGS -- State
id = "{{id}}";
evtSource = new EventSource("/track/{{id}}/events");
setup_evtlisten_common();
// var form = document.querySelector("#state");
// function sendState(){
// const data = new FormData(form);
// var nState = {};
// for(entry of data)
// nState[entry[0]] = entry[1];
// nState.invisibility = "invisibility" in nState;
// nState.blurred = "blurred" in nState;
// nState.tracker = nState.npc == 0;
// socket.emit('changeState', nState);
// }
//////////////////////////////////////////////////////////////////////////////
// SETTINGS -- State
var form = document.querySelector("#state");
function sendState(){
const data = new FormData(form);
var nState = {};
for(entry of data)
nState[entry[0]] = entry[1];
nState.invisibility = "invisibility" in nState;
fetch("/track/{{id}}?inv="+nState.invisibility+
"&col="+nState.color,
{ method: 'PUT' }
);
}
// socket.on('newState', function(state){
// document.querySelector("#invisibility").checked = state.invisibility;