Skip to content

Commit

Permalink
Add 'Sync/Async' switch to automate the acceptance of incoming logs
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaj00 committed Feb 15, 2024
1 parent 1d36644 commit 8d278f0
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 3 deletions.
80 changes: 79 additions & 1 deletion frontend/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,82 @@ button {
border-radius: 5px;
margin-top: 5px;
overflow: auto;
}
}

/* Sync/Async connection switch */
.switch {
position: relative;
display: inline-block;
width: 90px;
height: 34px;
}

.switch input {display:none;}

.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}

.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}

input:checked + .slider {
background-color: #2ab934;
}

input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
-webkit-transform: translateX(55px);
-ms-transform: translateX(55px);
transform: translateX(55px);
}

.on
{
display: none;
}

.on, .off
{
color: white;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 14px;
font-family: Verdana, sans-serif;
}

input:checked+ .slider .on
{display: block;}

input:checked + .slider .off
{display: none;}

/* Rounded sliders */
.slider.round {
border-radius: 34px;
}

.slider.round:before {
border-radius: 50%;}
56 changes: 54 additions & 2 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
const [incomingMessage, setIncomingMessage] = useState('');
const [errors, setErrors] = useState([]);
const [loggedMessages, setLoggedMessages] = useState([]);
const [syncConnection, setSyncConnection] = useState(true);

// After the element rendering, connect to the given websocket port
useEffect(() => {
Expand All @@ -50,6 +51,19 @@
};
}, []);

// Need a different useEffect, due to the different dependency set
useEffect(() => {
if (ws) {
ws.addEventListener('message', handleMessageWebSocket);
}

return () => {
if (ws) {
ws.removeEventListener('message', handleMessageWebSocket);
}
};
}, [syncConnection]);

function handleOpenWebSocket(){
const message = `WebSocket connection for Port ${port} established.`
setLoggedMessages(prevMessages => [...prevMessages, message]);
Expand All @@ -61,8 +75,14 @@

function handleMessageWebSocket(event){
const message = decomposeJSON(event.data);
console.log("currently in sync and waiting", port);
setIncomingMessage(message);
if( syncConnection === true ){
// Sync mode
setIncomingMessage(message);
}
else {
// Async mode
acceptAsyncIncomingLog(message);
}
}

function handleErrorWebSocket(event) {
Expand Down Expand Up @@ -110,6 +130,22 @@
ws.send(webSocketResponseJSON);
};

const acceptAsyncIncomingLog = (message) => {
console.log("new message async accepted on port: ", port);

// Accept Log and clear the input
setLoggedMessages(prevMessages => [...prevMessages, message]);
setIncomingMessage("");

// Send the response on the WebSocket
let webSocketResponse = {
"Type": "accept",
"Value": ""
};
const webSocketResponseJSON = JSON.stringify(webSocketResponse);
ws.send(webSocketResponseJSON);
};

const declineIncomingLog = () => {
if(incomingMessage === ""){
return;
Expand All @@ -127,8 +163,24 @@
ws.send(webSocketResponseJSON);
};

const changeSyncronization = () => {
console.log("changing syncronization of port: ", port);
if (syncConnection) {
// Changing to Async mode
acceptIncomingLog(); // Accept also the existing incoming log
}
setSyncConnection(!syncConnection);
}

return (
<div id="sub-screen" className="sub-screen">
<label className="switch">
<input type="checkbox" id="sync-switch-input" checked={syncConnection} onChange={changeSyncronization} />
<div className="slider round">
<span className="on">Sync</span>
<span className="off">Async</span>
</div>
</label>
<p className="websocket-console-title">
WebSocket Console for Port {port}:
</p>
Expand Down

0 comments on commit 8d278f0

Please sign in to comment.