Page 1 of 1

API toggle function

Posted: Thu Mar 28, 2024 10:28 am
by SMNDVC
Hi,

I've been playing around with QLC+ APIs, and I've noticed, that there was no way to toggle a function on and off with one button. So i've created one:

Code: Select all

function toggleFunction(functionId) {
    // Send the request to get function status
    websocket.send("QLC+API|getFunctionStatus|" + functionId);
    
    // WebSocket message handler to process the response
    websocket.onmessage = function(event) {
        // Parse and handle the response here
        var message = event.data;
        
        // Check if the message is related to function status
        if (message.startsWith("QLC+API|getFunctionStatus|")) {
            var functionStatus = message.split("|")[2];
            
            // Toggle the function status
            if (functionStatus === "Running") {
                websocket.send('QLC+API|setFunctionStatus|' + functionId + '|0');
                // console.log("function ID", functionId, "has stopped");
            } else {
                websocket.send('QLC+API|setFunctionStatus|' + functionId + '|1');
                // console.log("function ID", functionId, "is running");
            }
        }
    };
}
Example usage case (I'm using bootstrap, but you can use it without it)

Code: Select all

<button class="btn btn-primary" onclick="toggleFunction(5)">Toggle dimmer on and off/button>
Hope someone finds this usefull :D

Re: API toggle function

Posted: Thu Mar 28, 2024 3:31 pm
by Yestalgia
Thanks for the contribution - I can see why something like this would be useful.