58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
async function POST(path, data={}) {
|
|
return new Promise(function (onSuccess, onError) {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('POST', path, true);
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
xhr.onreadystatechange = function () {
|
|
if (xhr.readyState === 4) {
|
|
if (xhr.status === 200) {
|
|
onSuccess(xhr);
|
|
} else {
|
|
onError(xhr);
|
|
}
|
|
}
|
|
};
|
|
|
|
xhr.onerror = () => onError(xhr);
|
|
xhr.send(JSON.stringify(data));
|
|
});
|
|
}
|
|
|
|
function assert(condition, message) {
|
|
if (!condition) {
|
|
throw message || "Assertion failed";
|
|
}
|
|
}
|
|
|
|
async function transmit(transmitterID, channel, action, intensity=0, lucalEncoded=false) {
|
|
assert(typeof transmitterID === 'number');
|
|
assert(typeof channel === 'number');
|
|
assert(['shock', 'vibrate', 'beep'].includes(action));
|
|
assert(typeof intensity === 'number');
|
|
assert(typeof lucalEncoded === 'boolean');
|
|
POST('/transmit', {transmitterID: transmitterID, channel: channel, action: action, intensity: intensity, lucalEncoded: lucalEncoded});
|
|
}
|
|
|
|
|
|
|
|
async function txFromUI(action, intensity=0) {
|
|
let transmitterID = Number(document.getElementById('transmitterIDInput').value);
|
|
let channel = Number(document.getElementById('channelIDInput').value);
|
|
let lucalEncoded = document.getElementById('lucalEncodedInput').checked;
|
|
transmit(transmitterID, channel, action, intensity, lucalEncoded);
|
|
}
|
|
|
|
async function shock() {
|
|
let intensity = Number(document.getElementById('shockIntensity').value);
|
|
txFromUI('shock', intensity);
|
|
}
|
|
|
|
async function vibrate() {
|
|
let intensity = Number(document.getElementById('vibrateIntensity').value);
|
|
txFromUI('vibrate', intensity);
|
|
}
|
|
|
|
async function beep() {
|
|
txFromUI('beep');
|
|
}
|