diff --git a/commander.html b/commander.html index c8c48ac..48bfe1d 100644 --- a/commander.html +++ b/commander.html @@ -73,9 +73,13 @@ const boatjesEl = document.getElementById('boatjes'); const clientsEl = document.getElementById('clients'); - const kickIcon = ""; + const kickIcon = "" + const banIcon = ""; const openIcon = ""; const lockIcon = ""; + const freeIcon = "" + const logIcon = " " + const serverURL = 'ws://10.254.0.1:8080/'; @@ -168,6 +172,11 @@ conn.send(passEl.value + ";lock;" + boat['id']); } + fucntion getBoatLog(e) + { + alert("loggy log"); + } + function reqBoats() { conn.send(passEl.value + ";boats"); @@ -220,17 +229,19 @@ boatCells[2].innerText = boat[2]; let lockBtn = document.createElement("div"); lockBtn.className = "btn"; - if (boatRow.dataset['state'] == "locked") - { - lockBtn.innerHTML = openIcon; - lockBtn.addEventListener('click', freeBoat); - } - else - { - lockBtn.innerHTML = lockIcon; - lockBtn.addEventListener('click', lockBoat); - } + lockBtn.innerHTML = lockIcon; + lockBtn.addEventListener('click', lockBoat); boatCells[3].append(lockBtn); + let freeBtn = document.createElement("div"); + freeBtn.className = "btn"; + freeBtn.innerHTML = freeIcon; + freeBtn.addEventListener('click', freeBoat); + boatCells[3].append(freeBtn); + let logBtn = document.createElement("div"); + logBtn.className = "btn"; + logBtn.innerHTML = logIcon; + logBtn.addEventListener('click', getBoatLog); + boatCells[3].append(logBtn); for (cell in boatCells) { boatRow.append(boatCells[cell]); diff --git a/ground-station.py b/ground-station.py index f6009ba..c510946 100644 --- a/ground-station.py +++ b/ground-station.py @@ -23,13 +23,14 @@ async def sendToBoat(boat, cmd): async def echo_boats(client): """echo list of all not locked boats to client""" data = "boats:" - for boat in Boats: - if boat['state'] != BOAT_STATE_LOCKED and boat['state'] != BOAT_STATE_TERMINATED: - data += str(boat['id']) + ";" + str(boat['name']) + ";" - if boat["state"] == BOAT_STATE_AVAILABLE: - data += 'available:' - elif boat["state"] == BOAT_STATE_INCTRL: - data += 'inctrl:' + if client['state'] != "kicked": + for boat in Boats: + if boat['state'] != BOAT_STATE_LOCKED and boat['state'] != BOAT_STATE_TERMINATED: + data += str(boat['id']) + ";" + str(boat['name']) + ";" + if boat["state"] == BOAT_STATE_AVAILABLE: + data += 'available:' + elif boat["state"] == BOAT_STATE_INCTRL: + data += 'inctrl:' await client['ws'].send(data) async def echo_locked_boats(client): @@ -111,6 +112,17 @@ async def sendcmd(data): await sendToBoat(b, data) return +async def kick_client(clientId): + """kick a client""" + for client in Clients: + if client['id'] == client and client['state'] == "terminated": + if client['state'] is not None: + client['boat']['state'] = BOAT_STATE_AVAILABLE + client['boat'] = None + client['state'] = "kicked" + await client['ws'].send("FAIL") + return + @@ -130,10 +142,10 @@ async def on_message(message, client): await free_boat(data[2]) elif data[1] == "lock" and client["id"] == ADMIN_ID: await lock_boat(data[2]) - elif data[1] == "locked": + elif data[1] == "locked" and client["id"] == ADMIN_ID: await echo_locked_boats(client) - elif data[1] == "kick": - print("kick") + elif data[1] == "kick" and client["id"] == ADMIN_ID: + await kick_client(data[2]) elif data[1] == "clients": await echo_clients(client) elif data[1] == "d": @@ -161,6 +173,8 @@ async def new_client(clientId, ws): print("new client connected: " + client['id']) for clie in Clients: if clie['id'] == client['id']: + if client['state'] == "kicked": + client['state'] = "kicked" clie['state'] = 'terminated' Clients.append(client) return client diff --git a/index.html b/index.html index c466a2c..fe708d6 100644 --- a/index.html +++ b/index.html @@ -29,60 +29,83 @@ var boatListEl = document.getElementById('boats'); var stickEl = document.getElementById('canvas'); - var connection = new WebSocket('ws://' + "10.254.0.1" + ':8080/', ['mbcRcRf']); + var connection = false; + var refresh_timer = false; + var state = "connecting"; - connection.onopen = function () + function conn_onopen() { connection.send(clientId + ';4675;' + Math.floor(new Date().getTime()/1000.0).toString()); setTimeout(() => { connection.send(clientId + ';boats'); }, 100); + state = "connected"; }; - connection.onerror = function (error) + function conn_onerror(error) { console.log('WebSocket Error ', error); alert('WebSocket Error ', error); - - //connection.onmessage({ data: "boats:50;test 1;available:164;tosti;available" }) }; - connection.onmessage = function (e) + function conn_onmessage(e) { let data = e.data.split(':'); if (data[0] == "boats") { - console.log("boats: ", data); - boatListEl.innerHTML = ""; - stickEl.style.display = 'none'; - boatListEl.style.display = 'block'; - for (let boat in data) + if (state == 'connected') { - boat = data[boat].split(';'); - if ((boat.length == 3) && (boat[2] == 'available')) + console.log("boats: ", data); + boatListEl.innerHTML = ""; + stickEl.style.display = 'none'; + boatListEl.style.display = 'block'; + for (let boat in data) { - console.log("render boat", boat); - let el = document.createElement("div"); - el.style.textAlign = "center"; - el.style.borderRadius = "10px"; - el.style.backgroundColor = "#242495"; - el.style.padding = "10px"; - el.style.margin = "5px 25%"; - el.style.color = "white"; - el.style.cursor = "pointer"; - el.id = "boat" + boat[0]; - el.innerHTML = boat[1]; - el.addEventListener('click', selectBoat); - boatListEl.appendChild(el); + boat = data[boat].split(';'); + if ((boat.length == 3) && (boat[2] == 'available')) + { + console.log("render boat", boat); + let el = document.createElement("div"); + el.style.textAlign = "center"; + el.style.borderRadius = "10px"; + el.style.backgroundColor = "#242495"; + el.style.padding = "10px"; + el.style.margin = "5px 25%"; + el.style.color = "white"; + el.style.cursor = "pointer"; + el.id = "boat" + boat[0]; + el.innerHTML = boat[1]; + el.addEventListener('click', selectBoat); + boatListEl.appendChild(el); + } } + if (refresh_timer) + { + clearTimeout(refresh_timer); + } + refresh_timer = setTimeout(()=>{connection.send(clientId + ';boats')}, 1000) } } + else if (data[0] == "FAIL") + { + console.error("ground station send FAIL"); + connection.send(clientId + ';boats'); + } else { console.log('Server: ', e.data); } }; + function connect() + { + connection = new WebSocket('ws://10.254.0.1:8080/', ['mbcRcRf']); + connection.onopen = conn_onopen; + connection.onerror = conn_onerror; + connection.onmessage = conn_onmessage; + } + function selectBoat(e) { + clearTimeout(refresh_timer); connection.send(clientId + ';ctrl;' + e.target.id.split('boat')[1]); console.log('ctrl:', e.target.id.split('boat')[1]); stickEl.style.display = 'block';