feat(client): ✨ add function to test connection on ip update
This commit is contained in:
parent
994254a1a5
commit
6044a3e50c
|
@ -116,7 +116,7 @@
|
|||
|
||||
document
|
||||
.querySelector('#settings')
|
||||
.addEventListener('submit', (event) => {
|
||||
.addEventListener('submit', async (event) => {
|
||||
// Don't interrupt event if not form submitting
|
||||
if (!(event instanceof SubmitEvent)) return true
|
||||
if (event.target === null) return true
|
||||
|
@ -128,9 +128,38 @@
|
|||
|
||||
if (ipAddress === null) return
|
||||
|
||||
setEndpoint(`http://${ipAddress}`)
|
||||
const endpoint = `http://${ipAddress}`
|
||||
await testEndpoint(endpoint)
|
||||
setEndpoint(endpoint)
|
||||
})
|
||||
|
||||
/**
|
||||
* Test connection to endpoint.
|
||||
*
|
||||
* @param {string} endpoint Endpoint to test for.
|
||||
*
|
||||
* @returns {void}
|
||||
* @throws {Error} Endpoint unreachable.
|
||||
*/
|
||||
async function testEndpoint(endpoint) {
|
||||
try {
|
||||
const response = await fetch(endpoint)
|
||||
if (response.ok) return
|
||||
} catch (cause) {
|
||||
alert(`Impossible de joindre l'adresse "${endpoint}"`)
|
||||
throw new Error(`unable to connect to robot at ${endpoint}`, {
|
||||
cause,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('keydown', (event) => {
|
||||
const key = event.keyCode
|
||||
const { command, value } = keyMap[key]
|
||||
const endpoint = getEndpoint()
|
||||
sendCommand(endpoint, command, value)
|
||||
})
|
||||
|
||||
function storeInput() {
|
||||
ipInput = document.getElementById('ip').value
|
||||
valueRot = document.getElementById('valueRot').value
|
||||
|
|
Loading…
Reference in a new issue