Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2013 George Reese Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.imaginary.home.device.hue;
 
 import  org.json.JSONException;
 import  org.json.JSONObject;
 
 import  javax.annotation.Nonnull;
 
 public class Hue implements HomeAutomationSystemLightingService {
     static private @Nonnull String getLastItem(@Nonnull String name) {
         int idx = name.lastIndexOf('.');
 
         ifidx < 0 ) {
             return name;
         }
         else ifidx == (name.length()-1) ) {
             return "";
         }
         return name.substring(idx+1);
     }
 
     static public @Nonnull Logger getLogger(@Nonnull Class<?> cls) {
         return getLogger(cls"std");
     }
 
     static public @Nonnull Logger getLogger(@Nonnull Class<?> cls, @Nonnull String type) {
         String pkg = getLastItem(cls.getPackage().getName());
 
         ifpkg.equals("hue") ) {
             pkg = "";
         }
         else {
             pkg = pkg + ".";
         }
         return Logger.getLogger("imaginary.hue." + type + "." + pkg + getLastItem(cls.getName()));
     }
 
     static public @Nonnull Logger getWireLogger(@Nonnull Class<?> cls) {
         return getLogger(cls"wire");
     }
 
     private String     accessKey;
     private Properties customProperties;
     private String     endpoint;
     private String     id;
     private String     ipAddress;
 
     public Hue() { }
 
     public Hue(@Nonnull String ipAddress, @Nonnull String accessKey) {
         this(ipAddressaccessKeynew Properties());
     }
 
     public Hue(@Nonnull String ipAddress, @Nonnull String accessKey, @Nonnull Properties customProperties) {
         this. = ipAddress;
         this. = accessKey;
         this. = customProperties;
         ifaccessKey.equals("") ) {
             this. = "http://" + ipAddress + "/api";
         }
         else {
             this. = "http://" + ipAddress + "/api/" + accessKey + "/";
         }
     }
 
     @Override
     public @Nonnull String getAPIEndpoint() {
         return ;
     }
 
     @Override
     public @Nonnull Properties getAuthenticationProperties() {
         Properties p = new Properties();
 
        p.put("accessKey");
        return p;
    }
    public @Nonnull Properties getCustomProperties() {
        return ;
    }
    @Override
    public @Nonnull String getId() {
        return ;
    }
    @Override
    public @Nonnull String getName() {
        return "Hue";
    }
    @Override
    public @Nonnull String getVendor() {
        return "Philips";
    }
    @Override
    public void init(@Nonnull String id, @Nonnull Properties auth, @Nonnull Properties custom) {
        this. = id;
         = auth.getProperty("accessKey""");
         = auth.getProperty("ipAddress");
         = custom;
        if.equals("") ) {
            this. = "http://" +  + "/api";
        }
        else {
            this. = "http://" +  + "/api/" +  + "/";
        }
    }
    @Override
    public @Nonnull Iterable<LightlistLights() throws CommunicationException {
        HueMethod method = new HueMethod(this);
        JSONObject list = method.get("lights");
        iflist == null ) {
            return Collections.emptyList();
        }
        ArrayList<Lightmatches = new ArrayList<Light>();
        forString id : JSONObject.getNames(list) ) {
            try {
                JSONObject item = list.getJSONObject(id);
                String name = (item.has("name") ? item.getString("name") : id);
                matches.add(new HueBulb(thisidname));
            }
            catch( JSONException e ) {
                throw new HueException(e);
            }
        }
        return matches;
    }
    private String generateKey() {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        int len = 20 + random.nextInt(10);
        whilestr.length() < len ) {
            char c = (char)random.nextInt(255);
            ifc >= 'a' && c <= 'z' ) {
                ifc != 'l' && c != 'i' && c != 'o' ) {
                    str.append(c);
                }
            }
            else ifc >= 'A' && c <= 'Z' ) {
                ifc != 'I' && c != 'O' ) {
                    str.append(c);
                }
            }
            else ifc >= '2' && c <= '9' ) {
                str.append(c);
            }
        }
        return str.toString();
    }
    @Override
    public @Nonnull Iterable<ColorModelistNativeColorModes() {
        ArrayList<ColorModemodes = new ArrayList<ColorMode>();
        modes.add(.);
        modes.add(.);
        modes.add(.);
        return modes;
    }
    @Override
    public @Nonnull Properties pair(@Nonnull String applicationNamethrows CommunicationException {
        HueMethod method = new HueMethod(this);
        HashMap<String,Objectauth = new HashMap<StringObject>();
        auth.put("username"generateKey());
        auth.put("devicetype"applicationName);
        try {
            JSONObject result = method.post(""new JSONObject(auth));
            Properties properties = new Properties();
            properties.put("accessKey");
            ifresult != null && result.has("success") ) {
                result = result.getJSONObject("success");
                ifresult.has("username") ) {
                     = result.getString("username");
                     = "http://" +  + "/api/" +  + "/";
                    properties.put("accessKey");
                    return properties;
                }
            }
            throw new HueException("Failed to receive authentication key");
        }
        catch( JSONException e ) {
            throw new HueException(e);
        }
    }
New to GrepCode? Check out our FAQ X