Search in sources :

Example 1 with BrainInfo

use of org.openhab.io.neeo.internal.servletservices.models.BrainInfo in project openhab-addons by openhab.

the class BrainDashboardService method handlePost.

/**
 * Handles the post for the 'updatedevice', 'restoredevice' or 'refreshdevice'.
 *
 * @see DefaultServletService#handlePost(HttpServletRequest, String[], HttpServletResponse)
 */
@Override
public void handlePost(HttpServletRequest req, String[] paths, HttpServletResponse resp) throws IOException {
    Objects.requireNonNull(req, "req cannot be null");
    Objects.requireNonNull(paths, "paths cannot be null");
    Objects.requireNonNull(resp, "resp cannot be null");
    if (paths.length == 0) {
        throw new IllegalArgumentException("paths cannot be empty");
    }
    try {
        if (paths[0].equalsIgnoreCase("removebrain")) {
            final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
            final String brainId = info.getBrainId();
            if (brainId == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID not specified")));
            } else if (service.removeBrain(brainId)) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
            } else {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID (" + brainId + ") could not be removed")));
            }
        } else if (paths[0].equalsIgnoreCase("addbrain")) {
            final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
            final String brainIp = info.getBrainIp();
            if (brainIp == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainIP not specified")));
            } else if (service.addBrain(brainIp)) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
            } else {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Brain (" + brainIp + ") could not be added - no brain at that IP Address")));
            }
        } else {
            logger.debug("Unknown get path: {}", String.join(",", paths));
        }
    } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
        logger.debug("Exception handling get: {}", e.getMessage(), e);
        NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));
    }
}
Also used : BrainInfo(org.openhab.io.neeo.internal.servletservices.models.BrainInfo) ReturnStatus(org.openhab.io.neeo.internal.servletservices.models.ReturnStatus) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)1 BrainInfo (org.openhab.io.neeo.internal.servletservices.models.BrainInfo)1 ReturnStatus (org.openhab.io.neeo.internal.servletservices.models.ReturnStatus)1