refactor(client): ♻️ resolve endpoint inside sendCommand
instead of redundant argument
This commit is contained in:
parent
4ccf981f12
commit
a269f45f23
|
@ -80,19 +80,19 @@
|
|||
// Keyboard handler
|
||||
document.addEventListener('keydown', ({ code }) => {
|
||||
if (code === 'ArrowUp') {
|
||||
return sendCommand(getEndpoint(), 'forward', 2)
|
||||
return sendCommand('forward', 2)
|
||||
}
|
||||
if (code === 'ArrowDown') {
|
||||
return sendCommand(getEndpoint(), 'backward', 2)
|
||||
return sendCommand('backward', 2)
|
||||
}
|
||||
if (code === 'ArrowLeft') {
|
||||
return sendCommand(getEndpoint(), 'left', 5)
|
||||
return sendCommand('left', 5)
|
||||
}
|
||||
if (code === 'ArrowRight') {
|
||||
return sendCommand(getEndpoint(), 'right', 5)
|
||||
return sendCommand('right', 5)
|
||||
}
|
||||
if (code === 'Space') {
|
||||
return sendCommand(getEndpoint(), 'stop', 0)
|
||||
return sendCommand('stop', 0)
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -124,13 +124,13 @@
|
|||
/**
|
||||
* Send command to the robot.
|
||||
*
|
||||
* @param {string} endpoint Address IP of the robot.
|
||||
* @param {Command} command Command to send.
|
||||
* @param {number} value Value of the command if needed.
|
||||
*
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function sendCommand(endpoint, command, value) {
|
||||
async function sendCommand(command, value) {
|
||||
const endpoint = getEndpoint()
|
||||
const url = new URL(
|
||||
`/get?command=${command}&value=${value}`,
|
||||
endpoint
|
||||
|
@ -202,11 +202,10 @@
|
|||
const target = event.target
|
||||
const value = target.valueAsNumber
|
||||
|
||||
const endpoint = getEndpoint()
|
||||
if (value < 0) {
|
||||
return sendCommand(endpoint, negativeCommand, Math.abs(value))
|
||||
return sendCommand(negativeCommand, Math.abs(value))
|
||||
}
|
||||
return sendCommand(endpoint, positiveCommand, value)
|
||||
return sendCommand(positiveCommand, value)
|
||||
}
|
||||
</script>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue