Search in sources :

Example 1 with User

use of com.bwssystems.HABridge.User 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)

Example 2 with User

use of com.bwssystems.HABridge.User in project ha-bridge by bwssytems.

the class DeviceResource method setupEndpoints.

private void setupEndpoints() {
    log.info("HABridge device management service started.... ");
    before(API_CONTEXT + "/*", (request, response) -> {
        // This never gets called as the HueMulator class covers this path. This is here for backup
        if (bridgeSettings.getBridgeSecurity().isSecure()) {
            User authUser = bridgeSettings.getBridgeSecurity().getAuthenticatedUser(request);
            if (authUser == null) {
                halt(401, "{\"message\":\"User not authenticated\"}");
            }
        }
    });
    // http://ip_address:port/api/devices CORS request
    options(API_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, DELETE");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    post(API_CONTEXT, "application/json", (request, response) -> {
        log.debug("Create a Device(s) - request body: " + request.body());
        DeviceDescriptor[] devices;
        if (request.body().substring(0, 1).equalsIgnoreCase("[") == true) {
            devices = new Gson().fromJson(request.body(), DeviceDescriptor[].class);
        } else {
            devices = new Gson().fromJson("[" + request.body() + "]", DeviceDescriptor[].class);
        }
        CallItem[] callItems = null;
        String errorMessage = null;
        for (int i = 0; i < devices.length; i++) {
            if (devices[i].getContentBody() != null) {
                if (devices[i].getContentType() == null || devices[i].getHttpVerb() == null || !supportedVerbs.contains(devices[i].getHttpVerb().toLowerCase())) {
                    response.status(HttpStatus.SC_BAD_REQUEST);
                    errorMessage = "Bad http verb in create device(s) for name: " + devices[i].getName() + " with verb: " + devices[i].getHttpVerb();
                    log.debug(errorMessage);
                    return new ErrorMessage(errorMessage);
                }
            }
            try {
                if (devices[i].getOnUrl() != null && !devices[i].getOnUrl().isEmpty())
                    callItems = aGsonHandler.fromJson(devices[i].getOnUrl(), CallItem[].class);
            } catch (JsonSyntaxException e) {
                response.status(HttpStatus.SC_BAD_REQUEST);
                errorMessage = "Bad on URL JSON in create device(s) for name: " + devices[i].getName() + " with on URL: " + devices[i].getOnUrl();
                log.debug(errorMessage);
                return new ErrorMessage(errorMessage);
            }
            try {
                if (devices[i].getDimUrl() != null && !devices[i].getDimUrl().isEmpty())
                    callItems = aGsonHandler.fromJson(devices[i].getDimUrl(), CallItem[].class);
            } catch (JsonSyntaxException e) {
                response.status(HttpStatus.SC_BAD_REQUEST);
                errorMessage = "Bad dim URL JSON in create device(s) for name: " + devices[i].getName() + " with dim URL: " + devices[i].getDimUrl();
                log.debug(errorMessage);
                return new ErrorMessage(errorMessage);
            }
            try {
                if (devices[i].getOffUrl() != null && !devices[i].getOffUrl().isEmpty())
                    callItems = aGsonHandler.fromJson(devices[i].getOffUrl(), CallItem[].class);
            } catch (JsonSyntaxException e) {
                response.status(HttpStatus.SC_BAD_REQUEST);
                errorMessage = "Bad off URL JSON in create device(s) for name: " + devices[i].getName() + " with off URL: " + devices[i].getOffUrl();
                log.debug(errorMessage);
                return new ErrorMessage(errorMessage);
            }
        }
        deviceRepository.save(devices);
        log.debug("Created a Device(s): " + request.body());
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.status(HttpStatus.SC_CREATED);
        return devices;
    }, new JsonTransformer());
    // http://ip_address:port/api/devices/:id CORS request
    options(API_CONTEXT + "/:id", "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, DELETE");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    put(API_CONTEXT + "/:id", "application/json", (request, response) -> {
        log.debug("Edit a Device - request body: " + request.body());
        DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
        if (deviceRepository.findOne(request.params(":id")) == null) {
            log.debug("Could not save an edited device, Device Id not found: " + request.params(":id"));
            response.status(HttpStatus.SC_BAD_REQUEST);
            return new ErrorMessage("Could not save an edited device, Device Id not found: " + request.params(":id") + " ");
        } else {
            log.debug("Saving an edited Device: " + device.getName());
            if (device.getDeviceType() != null)
                device.setDeviceType(device.getDeviceType());
            DeviceDescriptor[] theDevices = new DeviceDescriptor[1];
            theDevices[0] = device;
            deviceRepository.save(theDevices);
            response.status(HttpStatus.SC_OK);
        }
        return device;
    }, new JsonTransformer());
    get(API_CONTEXT, "application/json", (request, response) -> {
        List<DeviceDescriptor> deviceList = deviceRepository.findAll();
        log.debug("Get all devices");
        JsonTransformer aRenderer = new JsonTransformer();
        String theStream = aRenderer.render(deviceList);
        log.debug("The Device List: " + theStream);
        response.status(HttpStatus.SC_OK);
        return deviceList;
    }, new JsonTransformer());
    get(API_CONTEXT + "/:id", "application/json", (request, response) -> {
        log.debug("Get a device");
        DeviceDescriptor descriptor = deviceRepository.findOne(request.params(":id"));
        if (descriptor == null) {
            response.status(HttpStatus.SC_NOT_FOUND);
            return new ErrorMessage("Could not find, id: " + request.params(":id") + " ");
        } else
            response.status(HttpStatus.SC_OK);
        return descriptor;
    }, new JsonTransformer());
    delete(API_CONTEXT + "/:id", "application/json", (request, response) -> {
        String anId = request.params(":id");
        log.debug("Delete a device: " + anId);
        DeviceDescriptor deleted = deviceRepository.findOne(anId);
        if (deleted == null) {
            response.status(HttpStatus.SC_NOT_FOUND);
            return new ErrorMessage("Could not delete, id: " + anId + " not found. ");
        } else {
            deviceRepository.delete(deleted);
            response.status(HttpStatus.SC_OK);
        }
        return null;
    }, new JsonTransformer());
    get(API_CONTEXT + "/vera/devices", "application/json", (request, response) -> {
        log.debug("Get vera devices");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/vera/scenes", "application/json", (request, response) -> {
        log.debug("Get vera scenes");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.VERA_SCENE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/harmony/activities", "application/json", (request, response) -> {
        log.debug("Get harmony activities");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/harmony/show", "application/json", (request, response) -> {
        log.debug("Get harmony current activity");
        return homeManager.findResource(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]).getItems("current_activity");
    }, new JsonTransformer());
    get(API_CONTEXT + "/harmony/devices", "application/json", (request, response) -> {
        log.debug("Get harmony devices");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/nest/items", "application/json", (request, response) -> {
        log.debug("Get nest items");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/hue/devices", "application/json", (request, response) -> {
        log.debug("Get hue items");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/hal/devices", "application/json", (request, response) -> {
        log.debug("Get hal items");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.HAL_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HAL_DEVICE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/mqtt/devices", "application/json", (request, response) -> {
        log.debug("Get MQTT brokers");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/hass/devices", "application/json", (request, response) -> {
        log.debug("Get HomeAssistant Clients");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/domoticz/devices", "application/json", (request, response) -> {
        log.debug("Get Domoticz Clients");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/lifx/devices", "application/json", (request, response) -> {
        log.debug("Get LIFX devices");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.LIFX_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.LIFX_DEVICE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/somfy/devices", "application/json", (request, response) -> {
        log.debug("Get somfy devices");
        response.status(HttpStatus.SC_OK);
        return homeManager.findResource(DeviceMapTypes.SOMFY_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.SOMFY_DEVICE[DeviceMapTypes.typeIndex]);
    }, new JsonTransformer());
    get(API_CONTEXT + "/map/types", "application/json", (request, response) -> {
        log.debug("Get map types");
        return new DeviceMapTypes().getDeviceMapTypes();
    }, new JsonTransformer());
    // http://ip_address:port/api/devices/exec/renumber CORS request
    options(API_CONTEXT + "/exec/renumber", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "POST");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    post(API_CONTEXT + "/exec/renumber", "application/json", (request, response) -> {
        log.debug("Renumber devices.");
        deviceRepository.renumber();
        return null;
    }, new JsonTransformer());
    get(API_CONTEXT + "/backup/available", "application/json", (request, response) -> {
        log.debug("Get backup filenames");
        response.status(HttpStatus.SC_OK);
        return deviceRepository.getBackups();
    }, new JsonTransformer());
    // http://ip_address:port/api/devices/backup/create CORS request
    options(API_CONTEXT + "/backup/create", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    put(API_CONTEXT + "/backup/create", "application/json", (request, response) -> {
        log.debug("Create backup: " + request.body());
        BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
        BackupFilename returnFilename = new BackupFilename();
        returnFilename.setFilename(deviceRepository.backup(aFilename.getFilename()));
        return returnFilename;
    }, new JsonTransformer());
    // http://ip_address:port/api/devices/backup/delete CORS request
    options(API_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "POST");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    post(API_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
        log.debug("Delete backup: " + request.body());
        BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
        if (aFilename != null)
            deviceRepository.deleteBackup(aFilename.getFilename());
        else
            log.warn("No filename given for delete backup.");
        return null;
    }, new JsonTransformer());
    // http://ip_address:port/api/devices/backup/restore CORS request
    options(API_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "POST");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    post(API_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
        log.debug("Restore backup: " + request.body());
        BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
        if (aFilename != null) {
            deviceRepository.restoreBackup(aFilename.getFilename());
            deviceRepository.loadRepository();
        } else
            log.warn("No filename given for restore backup.");
        return null;
    }, new JsonTransformer());
}
Also used : User(com.bwssystems.HABridge.User) BackupFilename(com.bwssystems.HABridge.dao.BackupFilename) DeviceDescriptor(com.bwssystems.HABridge.dao.DeviceDescriptor) Gson(com.google.gson.Gson) DeviceMapTypes(com.bwssystems.HABridge.DeviceMapTypes) JsonSyntaxException(com.google.gson.JsonSyntaxException) CallItem(com.bwssystems.HABridge.api.CallItem) ErrorMessage(com.bwssystems.HABridge.dao.ErrorMessage) JsonTransformer(com.bwssystems.HABridge.util.JsonTransformer)

Aggregations

User (com.bwssystems.HABridge.User)2 JsonTransformer (com.bwssystems.HABridge.util.JsonTransformer)2 DeviceMapTypes (com.bwssystems.HABridge.DeviceMapTypes)1 CallItem (com.bwssystems.HABridge.api.CallItem)1 HueConfig (com.bwssystems.HABridge.api.hue.HueConfig)1 BackupFilename (com.bwssystems.HABridge.dao.BackupFilename)1 DeviceDescriptor (com.bwssystems.HABridge.dao.DeviceDescriptor)1 ErrorMessage (com.bwssystems.HABridge.dao.ErrorMessage)1 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1