Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.softfun.hue;
- import java.awt.Color;
- import java.io.IOException;
- import java.util.Locale;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.entity.EntityBuilder;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPut;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- public class AmbiHueControl {
- HttpGet getAmbilight = new HttpGet("http://philips-tv.home:1925/1/ambilight/measured");
- HttpPut putHue = new HttpPut("http://philips-hue.home/api/raspberry/lights/1/state");
- private Color parseColor(String measured, String layer, String side, int light) {
- int layerPos = measured.indexOf(layer);
- int sidePos = measured.indexOf(side, layerPos);
- int lightPos = measured.indexOf("\"" + light + "\":", sidePos);
- String rgb = measured.substring(measured.indexOf("{", lightPos + 1), measured.indexOf("}", lightPos + 1));
- String[] parts = rgb.split(",");
- return new Color(
- Integer.parseInt(parts[0].substring(parts[0].indexOf(":") + 1)),
- Integer.parseInt(parts[1].substring(parts[1].indexOf(":") + 1)),
- Integer.parseInt(parts[2].substring(parts[2].indexOf(":") + 1))
- );
- }
- private String fetchFromTV() throws IOException {
- final CloseableHttpClient http = HttpClients.createMinimal();
- CloseableHttpResponse response = http.execute(getAmbilight);
- try {
- HttpEntity entity = response.getEntity();
- return EntityUtils.toString(entity).replaceAll("\\s+", "");
- } finally {
- response.close();
- }
- }
- private void setAmbilightColor(Color color) throws IOException {
- final CloseableHttpClient http = HttpClients.createMinimal();
- float xy[] = convertColor(color);
- String json = String.format(new Locale("en", "US"), "{\"xy\":[%f,%f]}", xy[0], xy[1]);
- putHue.setEntity(EntityBuilder.create().setText(json).build());
- CloseableHttpResponse response = http.execute(putHue);
- try {
- EntityUtils.consume(response.getEntity());
- } finally {
- response.close();
- }
- }
- private float[] convertColor(Color color) {
- float red = color.getRed() / 255f;
- float green = color.getGreen() / 255f;
- float blue = color.getBlue() / 255f;
- float x = red * 0.649926f + green * 0.103455f + blue * 0.197109f;
- float y = red * 0.234327f + green * 0.743075f + blue * 0.022598f;
- float z = red * 0.0000000f + green * 0.053077f + blue * 1.035763f;
- float[] xy = new float[2];
- xy[0] = x / (x + y + z);
- xy[1] = y / (x + y + z);
- return xy;
- }
- private void turnAmbilightOn() throws IOException {
- final CloseableHttpClient http = HttpClients.createMinimal();
- String json = "{\"on\":true,\"bri\":254}";
- putHue.setEntity(EntityBuilder.create().setText(json).build());
- CloseableHttpResponse response = http.execute(putHue);
- try {
- EntityUtils.consume(response.getEntity());
- } finally {
- response.close();
- }
- }
- private void turnAmbilightOff() throws IOException {
- final CloseableHttpClient http = HttpClients.createMinimal();
- String json = "{\"on\":false}";
- putHue.setEntity(EntityBuilder.create().setText(json).build());
- CloseableHttpResponse response = http.execute(putHue);
- try {
- EntityUtils.consume(response.getEntity());
- } finally {
- response.close();
- }
- }
- @SuppressWarnings("SleepWhileInLoop")
- public static void main(String[] args) throws IOException, InterruptedException {
- AmbiHueControl control = new AmbiHueControl();
- try {
- control.turnAmbilightOn();
- Thread.sleep(100);
- while (true) {
- long time = System.currentTimeMillis();
- String measured = control.fetchFromTV();
- Color color = control.parseColor(measured, "layer1", "left", 0);
- control.setAmbilightColor(color);
- // System.out.println("Worked for " + (System.currentTimeMillis()-time) + " milliseconds...");
- while (time + 125 > System.currentTimeMillis()) {
- Thread.sleep(25);
- }
- }
- } finally {
- control.turnAmbilightOff();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement