Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Ambilight + Hue in Java with minimal footprint

a guest Mar 6th, 2015 1,687 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. package de.softfun.hue;
  2.  
  3. import java.awt.Color;
  4. import java.io.IOException;
  5. import java.util.Locale;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.client.entity.EntityBuilder;
  8. import org.apache.http.client.methods.CloseableHttpResponse;
  9. import org.apache.http.client.methods.HttpGet;
  10. import org.apache.http.client.methods.HttpPut;
  11. import org.apache.http.impl.client.CloseableHttpClient;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.apache.http.util.EntityUtils;
  14.  
  15. public class AmbiHueControl {
  16.     HttpGet getAmbilight = new HttpGet("http://philips-tv.home:1925/1/ambilight/measured");
  17.     HttpPut putHue = new HttpPut("http://philips-hue.home/api/raspberry/lights/1/state");
  18.  
  19.     private Color parseColor(String measured, String layer, String side, int light) {
  20.         int layerPos = measured.indexOf(layer);
  21.         int sidePos = measured.indexOf(side, layerPos);
  22.         int lightPos = measured.indexOf("\"" + light + "\":", sidePos);
  23.         String rgb = measured.substring(measured.indexOf("{", lightPos + 1), measured.indexOf("}", lightPos + 1));
  24.         String[] parts = rgb.split(",");
  25.         return new Color(
  26.                 Integer.parseInt(parts[0].substring(parts[0].indexOf(":") + 1)),
  27.                 Integer.parseInt(parts[1].substring(parts[1].indexOf(":") + 1)),
  28.                 Integer.parseInt(parts[2].substring(parts[2].indexOf(":") + 1))
  29.         );
  30.     }
  31.  
  32.     private String fetchFromTV() throws IOException {
  33.         final CloseableHttpClient http = HttpClients.createMinimal();
  34.         CloseableHttpResponse response = http.execute(getAmbilight);
  35.         try {
  36.             HttpEntity entity = response.getEntity();
  37.             return EntityUtils.toString(entity).replaceAll("\\s+", "");
  38.         } finally {
  39.             response.close();
  40.         }
  41.     }
  42.  
  43.     private void setAmbilightColor(Color color) throws IOException {
  44.         final CloseableHttpClient http = HttpClients.createMinimal();
  45.         float xy[] = convertColor(color);
  46.         String json = String.format(new Locale("en", "US"), "{\"xy\":[%f,%f]}", xy[0], xy[1]);
  47.         putHue.setEntity(EntityBuilder.create().setText(json).build());
  48.         CloseableHttpResponse response = http.execute(putHue);
  49.         try {
  50.             EntityUtils.consume(response.getEntity());
  51.         } finally {
  52.             response.close();
  53.         }
  54.     }
  55.  
  56.     private float[] convertColor(Color color) {
  57.         float red = color.getRed() / 255f;
  58.         float green = color.getGreen() / 255f;
  59.         float blue = color.getBlue() / 255f;
  60.         float x = red * 0.649926f + green * 0.103455f + blue * 0.197109f;
  61.         float y = red * 0.234327f + green * 0.743075f + blue * 0.022598f;
  62.         float z = red * 0.0000000f + green * 0.053077f + blue * 1.035763f;
  63.         float[] xy = new float[2];
  64.         xy[0] = x / (x + y + z);
  65.         xy[1] = y / (x + y + z);
  66.         return xy;
  67.     }
  68.  
  69.     private void turnAmbilightOn() throws IOException {
  70.         final CloseableHttpClient http = HttpClients.createMinimal();
  71.         String json = "{\"on\":true,\"bri\":254}";
  72.         putHue.setEntity(EntityBuilder.create().setText(json).build());
  73.         CloseableHttpResponse response = http.execute(putHue);
  74.         try {
  75.             EntityUtils.consume(response.getEntity());
  76.         } finally {
  77.             response.close();
  78.         }
  79.     }
  80.  
  81.     private void turnAmbilightOff() throws IOException {
  82.         final CloseableHttpClient http = HttpClients.createMinimal();
  83.         String json = "{\"on\":false}";
  84.         putHue.setEntity(EntityBuilder.create().setText(json).build());
  85.         CloseableHttpResponse response = http.execute(putHue);
  86.         try {
  87.             EntityUtils.consume(response.getEntity());
  88.         } finally {
  89.             response.close();
  90.         }
  91.     }
  92.  
  93.     @SuppressWarnings("SleepWhileInLoop")
  94.     public static void main(String[] args) throws IOException, InterruptedException {
  95.         AmbiHueControl control = new AmbiHueControl();
  96.         try {
  97.             control.turnAmbilightOn();
  98.             Thread.sleep(100);
  99.             while (true) {
  100.                 long time = System.currentTimeMillis();
  101.                 String measured = control.fetchFromTV();
  102.                 Color color = control.parseColor(measured, "layer1", "left", 0);
  103.                 control.setAmbilightColor(color);
  104.                 // System.out.println("Worked for " + (System.currentTimeMillis()-time) + " milliseconds...");
  105.                 while (time + 125 > System.currentTimeMillis()) {
  106.                     Thread.sleep(25);
  107.                 }
  108.             }
  109.         } finally {
  110.             control.turnAmbilightOff();
  111.         }
  112.     }
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top