by
1,488 15

Simple RPC call

Simple function to call RPC (only 1k uncompressed).  Use this instead of JQuery.  Alternatively, use zepto.min.js from zeptojs.com if you have 26K to spare.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/3424692/Simple-RPC-call?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/* Addapted from Paolo Manna git pmanna/mongoose_os_playground browser_rpc_service.js */ var platform = ''; var host = ''; var defCallBack = function(response) { if ((response) && (response.error)) { alert(response.message); } }; // Common call to RPC services on the board function callRPCService(cmd, params, callback) { if (!callback) { callback = defCallBack; } var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { callback(this.response); } }; xhttp.open('POST', 'rpc/' + cmd + '?' + new Date().getTime(), true); xhttp.responseType = 'json'; xhttp.send(JSON.stringify(params)); } // Discover which platform we're using, to enable/disable features function startup() { callRPCService('Config.Get',{}, function(response) { if (response) { platform = response.device.id; console.log('Platform is: ' + platform); var mac_id = (response.device.id.split("_"))[1]; host = mac_id + '.local'; document.getElementById("hostname").innerHTML = host; } }); } // Reboots the microcontroller function rebootDevice() { callRPCService('Sys.Reboot',{delay_ms:500}); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus