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>) { fn store_pos(id: &str, lat: f32, long: f32, tracking: &State<Tracking>) {
if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) { if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) {
tracked.write().unwrap().pos = (lat, long); 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("/")] #[get("/")]
fn index() -> &'static str { fn index() -> &'static str {
"Hello, world!" "Hello, world!"
@ -285,6 +307,9 @@ fn rocket() -> _ {
rocket::build() rocket::build()
.attach(Template::fairing()) .attach(Template::fairing())
.manage(RwLock::new(tracking)) .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"))) .mount("/", FileServer::from(relative!("static")))
} }

View file

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

View file

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