Search in sources :

Example 1 with HueConfig

use of com.bwssystems.HABridge.api.hue.HueConfig in project ha-bridge by bwssytems.

the class HueMulator method setupServer.

// This function sets up the sparkjava rest calls for the hue api
public void setupServer() {
    log.info("Hue emulator service started....");
    before(HUE_CONTEXT + "/*", (request, response) -> {
        log.debug("HueMulator GET called on api/* with request <" + request.pathInfo() + ">");
        if (bridgeSettingMaster.getBridgeSecurity().isSecure()) {
            String pathInfo = request.pathInfo();
            if (pathInfo != null && pathInfo.contains(HUE_CONTEXT + "/devices")) {
                User authUser = bridgeSettingMaster.getBridgeSecurity().getAuthenticatedUser(request);
                if (authUser == null) {
                    halt(401, "{\"message\":\"User not authenticated\"}");
                }
            } else if (bridgeSettingMaster.getBridgeSecurity().isSecureHueApi()) {
                User authUser = bridgeSettingMaster.getBridgeSecurity().getAuthenticatedUser(request);
                if (authUser == null) {
                    halt(401, "{\"message\":\"User not authenticated\"}");
                }
            }
        }
    });
    // http://ip_address:port/api/{userId}/groups returns json objects of
    // all groups configured
    get(HUE_CONTEXT + "/:userid/groups", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return groupsListHandler(request.params(":userid"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/api/{userId}/groups/{groupId} returns json
    // object for specified group. Only 0 is supported
    get(HUE_CONTEXT + "/:userid/groups/:groupid", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return groupsIdHandler(request.params(":groupid"), request.params(":userid"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/:userid/groups CORS request
    options(HUE_CONTEXT + "/:userid/groups", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/:userid/groups
    // dummy handler
    post(HUE_CONTEXT + "/:userid/groups", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("group add requested from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        return "[{\"success\":{\"id\":\"1\"}}]";
    });
    // http://ip_address:port/api/:userid/groups/<groupid>/action
    // Dummy handler
    // Error forces Logitech Pop to fall back to individual light control
    // instead of scene-based control.
    put(HUE_CONTEXT + "/:userid/groups/:groupid/action", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("put action to groups API from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        return "[{\"error\":{\"address\": \"/groups/0/action/scene\", \"type\":7, \"description\": \"invalid value, dummy for parameter, scene\"}}]";
    });
    // http://ip_address:port/api/{userId}/scenes returns json objects of
    // all scenes configured
    get(HUE_CONTEXT + "/:userid/scenes", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return basicListHandler("scenes", request.params(":userid"), request.ip());
    });
    // http://ip_address:port/:userid/scenes CORS request
    options(HUE_CONTEXT + "/:userid/scenes", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/:userid/scenes
    // dummy handler
    post(HUE_CONTEXT + "/:userid/scenes", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("scene add requested from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        return "[{\"success\":{\"id\":\"1\"}}]";
    });
    // http://ip_address:port/api/{userId}/schedules returns json objects of
    // all schedules configured
    get(HUE_CONTEXT + "/:userid/schedules", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return basicListHandler("schedules", request.params(":userid"), request.ip());
    });
    // http://ip_address:port/:userid/schedules CORS request
    options(HUE_CONTEXT + "/:userid/schedules", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/:userid/schedules
    // dummy handler
    post(HUE_CONTEXT + "/:userid/schedules", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("schedules add requested from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        return "[{\"success\":{\"id\":\"1\"}}]";
    });
    // http://ip_address:port/api/{userId}/sensors returns json objects of
    // all sensors configured
    get(HUE_CONTEXT + "/:userid/sensors", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return basicListHandler("sensors", request.params(":userid"), request.ip());
    });
    // http://ip_address:port/:userid/sensors CORS request
    options(HUE_CONTEXT + "/:userid/sensors", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/:userid/sensors
    // dummy handler
    post(HUE_CONTEXT + "/:userid/sensors", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("sensors add requested from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        return "[{\"success\":{\"id\":\"1\"}}]";
    });
    // http://ip_address:port/api/{userId}/rules returns json objects of all
    // rules configured
    get(HUE_CONTEXT + "/:userid/rules", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return basicListHandler("rules", request.params(":userid"), request.ip());
    });
    // http://ip_address:port/:userid/rules CORS request
    options(HUE_CONTEXT + "/:userid/rules", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/:userid/rules
    // dummy handler
    post(HUE_CONTEXT + "/:userid/rules", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("rules add requested from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        return "[{\"success\":{\"id\":\"1\"}}]";
    });
    // http://ip_address:port/api/{userId}/resourcelinks returns json
    // objects of all resourcelinks configured
    get(HUE_CONTEXT + "/:userid/resourcelinks", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return basicListHandler("resourcelinks", request.params(":userid"), request.ip());
    });
    // http://ip_address:port/:userid/resourcelinks CORS request
    options(HUE_CONTEXT + "/:userid/resourcelinks", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/:userid/resourcelinks
    // dummy handler
    post(HUE_CONTEXT + "/:userid/resourcelinks", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("resourcelinks add requested from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        return "[{\"success\":{\"id\":\"1\"}}]";
    });
    // http://ip_address:port/api/{userId}/lights returns json objects of
    // all lights configured
    get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return lightsListHandler(request.params(":userid"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/api/{userId}/lights/ returns json objects of
    // all lights configured
    get(HUE_CONTEXT + "/:userid/lights/", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return lightsListHandler(request.params(":userid"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/api CORS request
    options(HUE_CONTEXT, "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/api with body of user request returns json
    // object for a success of user add
    post(HUE_CONTEXT, "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return userAdd(request.body(), request.ip(), false);
    });
    // http://ip_address:port/api/* CORS request
    options(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/api/* with body of user request returns json
    // object for a success of user add - This method is for Harmony Hub
    post(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return userAdd(request.body(), request.ip(), true);
    });
    // http://ip_address:port/api/config returns json objects for the public
    // config when no user is given
    get(HUE_CONTEXT + "/config", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return getConfig(null, request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/api/{userId}/config returns json objects for
    // the config
    get(HUE_CONTEXT + "/:userid/config", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return getConfig(request.params(":userid"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/:userid/config CORS request
    options(HUE_CONTEXT + "/:userid/config", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/:userid/config uses json
    // object to set the config. this is to handle swupdates
    put(HUE_CONTEXT + "/:userid/config", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        log.debug("Config change requested from " + request.ip() + " user " + request.params(":userid") + " with body " + request.body());
        HueConfig aConfig = aGsonHandler.fromJson(request.body(), HueConfig.class);
        if (aConfig.getPortalservices() != null) {
            return "[{\"success\":{\"/config/portalservices\":true}}]";
        }
        return "[{\"success\":{\"/config/name\":\"My bridge\"}}]";
    });
    // http://ip_address:port/api/{userId} returns json objects for the full
    // state
    get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return getFullState(request.params(":userid"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/api/{userId}/ returns json objects for the full
    // state
    get(HUE_CONTEXT + "/:userid/", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return getFullState(request.params(":userid"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/api/{userId}/lights/{lightId} returns json
    // object for a given light
    get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return getLight(request.params(":userid"), request.params(":id"), request.ip());
    }, new JsonTransformer());
    // http://ip_address:port/api/:userid/lights/:id/bridgeupdatestate CORS
    // request
    options(HUE_CONTEXT + "/:userid/lights/:id/bridgeupdatestate", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/api/{userId}/lights/{lightId}/bridgeupdatestate
    // uses json object to update the internal bridge lights state.
    // THIS IS NOT A HUE API CALL... It is for state management if so
    // desired.
    put(HUE_CONTEXT + "/:userid/lights/:id/bridgeupdatestate", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return updateState(request.params(":userid"), request.params(":id"), request.body(), request.ip());
    });
    // http://ip_address:port/api/:userid/lights/:id/state CORS request
    options(HUE_CONTEXT + "/:userid/lights/:id/state", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html");
        return "";
    });
    // http://ip_address:port/api/{userId}/lights/{lightId}/state uses json
    // object to set the lights state
    put(HUE_CONTEXT + "/:userid/lights/:id/state", "application/json", (request, response) -> {
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.type("application/json");
        response.status(HttpStatus.SC_OK);
        return changeState(request.params(":userid"), request.params(":id"), request.body(), request.ip());
    });
}
Also used : User(com.bwssystems.HABridge.User) HueConfig(com.bwssystems.HABridge.api.hue.HueConfig) JsonTransformer(com.bwssystems.HABridge.util.JsonTransformer)

Aggregations

User (com.bwssystems.HABridge.User)1 HueConfig (com.bwssystems.HABridge.api.hue.HueConfig)1 JsonTransformer (com.bwssystems.HABridge.util.JsonTransformer)1