From 6b0ce137b7b66072d048e2dde6826e8cb8a24778 Mon Sep 17 00:00:00 2001 From: Brosef Date: Tue, 2 Jun 2026 16:45:54 +0100 Subject: [PATCH] Added transmit frontend call --- www/main.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/www/main.js b/www/main.js index e69a55f..e717800 100644 --- a/www/main.js +++ b/www/main.js @@ -1 +1,37 @@ +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}); +} + console.debug('Main loaded!');