implement first commands

This commit is contained in:
MReenen 2023-11-21 21:02:30 +01:00
parent b03ec14c0f
commit 39e26f96dd
3 changed files with 104 additions and 18 deletions

View File

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MBC rc bootjes - commander</title>
<style>
* {
--background: #000000;
--background-second: #444444;
--foreground: #CCCCCC;
}
body {
background-color: var(--background);
color: var(--foreground);
}
.login {
position: fixed;
top: 50px;
right: 50px;
bottom: 50px;
left: 50px;
background-color: var(--background-second);
text-align: center;
padding: calc(50vh - 150px) 50px;
height: 200px;
}
</style>
</head>
<body>
<div class="login">
<h1>Login</h1>
<input type="password" />
</div>
<main>
<div id="bootjes"></div>
<div id="clients"></div>
</main>
<script>
var bootjes = document.getElementById('bootjes');
var clients = document.getElementById('clients');
var connection = new WebSocket('ws://' + "10.254.0.1" + ':8080/', ['mbcRcRf']);
function addBoat(boat)
{
boat = boat.split(':')
}
</script>
</body>
</html>

View File

@ -1,9 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title> <title>MBC Bootjes</title>
Mousebot
</title>
<meta name="viewport" content="user-scalable=no"> <meta name="viewport" content="user-scalable=no">
</head> </head>
<body style="position: fixed; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif ; <body style="position: fixed; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif ;
@ -33,7 +31,7 @@ font-size: xx-large;">
connection.onopen = function () connection.onopen = function ()
{ {
connection.send('Connect ' + (new Date())); connection.send(clientId + ':' + (new Date()));
}; };
connection.onerror = function (error) connection.onerror = function (error)

View File

@ -31,30 +31,67 @@ def sendData(data):
global boats global boats
print("data to send: " + str(data)) print("data to send: " + str(data))
def onMessage(message): async def echoBoats(client):
global boats global boats
data = message.split(':') data = ""
if(data[0] == "cmd"):
onCMD(message)
else:
for boat in boats: for boat in boats:
if(data[0] == boat): if (boat['state'] != BOAT_STATE_LOCKED):
sendData(data) data += str(boat['id']) + ";" + str(boat['name']) + ";"
break if (boat["state"] == BOAT_STATE_AVAILABLE):
data += 'available:'
elif (boat["state"] == BOAT_STATE_INCTRL):
data += 'in:'
await client['ws'].send(data)
async def echoLockedBoats(client):
global boats
data = ""
for boat in boats:
if (boat['state'] == BOAT_STATE_LOCKED):
data += str(boat['id']) + ";" + str(boat['name']) + ";locked:"
await client['ws'].send(data + '\n')
async def onMessage(message, client):
global boats
data = message.replace('\n', '').split(';')
if(data[0] != client["id"]):
print("invalid id: " + str(data[0]) + " != " + str(client["id"]))
else:
if (data[1] == "boats"):
await echoBoats(client)
elif(data[1] == "ctrl"):
print("controll");
elif(data[1] == "end"):
print("end");
elif(data[1] == "free"):
print("free");
elif(data[1] == "lock"):
print("lock");
elif(data[1] == "locked"):
await echoLockedBoats(client);
elif(data[1] == "kick"):
print("kick");
elif(data[1] == "clients"):
print("clients");
else:
print("something else: '" + data[1] + "'")
def newClient(message, ws): def newClient(message, ws):
print("new client connected: " + message) message = message.split(';')
return None client = { "id": message[0], "boat": None, "ws": ws }
print("new client connected: " + client['id'])
clients.append(client)
return client
async def run(ws, path): async def run(ws, path):
client = {} client = None
async for message in ws: async for message in ws:
client = newClient(message, ws) client = newClient(message, ws)
if (client is None): if (client is not None):
return break
async for message in ws: async for message in ws:
onMessage(message, client) await onMessage(message, client)
async def main(): async def main():
async with serve(run, "0.0.0.0", 8080): async with serve(run, "0.0.0.0", 8080):