Last active
May 21, 2018 03:27
-
-
Save dj0001/6abc0cbcd62c5d715779137aed9c9de1 to your computer and use it in GitHub Desktop.
forked from hueSimulator/public_html/js/main.js simulate bulb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var logError = function(response) { | |
$("#log").prepend("<div class='error log'>" + response.status + " " + response.statusText + "</div>"); | |
}; | |
$("#linkbutton").on("click", function(event) { | |
event.preventDefault(); | |
$.ajax({ | |
url: '/linkbutton' | |
}).done(function(response) { | |
$("#log").prepend("<div class='info log'>" + response + "</div>"); | |
}).fail(logError); | |
}); | |
$("#commands button").on("click", function() { | |
var $this = $(this); | |
var body = $this.attr("data-body"); | |
var url = $this.attr("data-url"); | |
var method = $this.attr("data-method"); | |
$("input[name='req-method'][value='"+method+"']").prop("checked", true); | |
$("#req-url").val(url); | |
$("#req-body").val(body); | |
}); | |
$("#request").on("submit", function(event) { | |
event.preventDefault(); | |
var url = $("#req-url").val(); | |
var method = $("input[name='req-method']:checked").val(); | |
var body = $("#req-body").val(); | |
var data; | |
try { | |
if (body !== "") { | |
data = JSON.parse(body); | |
data = JSON.stringify(data, null, 2); | |
body = "<pre>" + data + "</pre>"; | |
} | |
} catch (e) { | |
alert('malformed json: ' + e); | |
return; | |
} | |
$("#log").prepend("<div class='request log'>" + method + " " + url + " " + body + "</div>"); | |
var req = $.ajax({ | |
url: url, | |
method: method, | |
data: data, | |
contentType: 'application/json; charset=UTF-8', | |
accepts: 'application/json; charset=utf-8', | |
dataType: "json" | |
}); | |
req.done(function(response) { | |
$("#log").prepend("<div class='response log'><pre>" + JSON.stringify(response, null, 2) + "</pre></div>"); | |
if(response.state) {var bd=response.state //simulate bulb | |
var l = (2 - bd.sat/255) * bd.bri/255 / 2; var s = l<1 ? bd.sat*bd.bri/(l<0.5 ? l*2 : 2-l*2) : 0; if (isNaN(s)) s = 0; //l&& | |
document.querySelector('button[data-url$="1"]').style.background=bd.on?"hsl("+(bd.hue||0)/182+","+s*100+"%,"+(l||0)*100+"%)":"hsl(0,0%,50%)" //bd.on!=false | |
}); | |
req.fail(logError); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment