Search in sources :

Example 1 with ReturnStatus

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

the class NeeoBrainService method handleSubscribe.

/**
 * Handle subscribe to a device by adding the device key to the API for the related {@link ThingUID}
 *
 * @param resp the non-null response to write to
 * @param adapterName the non-empty adapter name
 * @param deviceKey the non-empty device key
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void handleSubscribe(HttpServletResponse resp, String adapterName, String deviceKey) throws IOException {
    Objects.requireNonNull(resp, "resp cannot be null");
    NeeoUtil.requireNotEmpty(adapterName, "adapterName cannot be empty");
    NeeoUtil.requireNotEmpty(deviceKey, "deviceKey cannot be empty");
    logger.debug("handleSubscribe {}/{}", adapterName, deviceKey);
    try {
        final NeeoThingUID uid = new NeeoThingUID(adapterName);
        api.getDeviceKeys().put(uid, deviceKey);
        NeeoUtil.write(resp, gson.toJson(ReturnStatus.SUCCESS));
    } catch (IllegalArgumentException e) {
        logger.debug("AdapterName {} is not a valid thinguid - ignoring", adapterName);
        NeeoUtil.write(resp, gson.toJson(new ReturnStatus("AdapterName not a valid ThingUID: " + adapterName)));
    }
}
Also used : NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) ReturnStatus(org.openhab.io.neeo.internal.servletservices.models.ReturnStatus)

Example 2 with ReturnStatus

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

the class ThingDashboardService method handleGet.

/**
 * Handles the get for the 'thingstatus' and 'getchannel' URL (all other URLs do posts)
 *
 * @see DefaultServletService#handleGet(HttpServletRequest, String[], HttpServletResponse)
 */
@Override
public void handleGet(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");
    try {
        if (paths[0].equalsIgnoreCase("thingstatus")) {
            final List<NeeoDevice> devices = context.getDefinitions().getAllDevices();
            NeeoUtil.write(resp, gson.toJson(devices));
        } else if (paths[0].equalsIgnoreCase("getchannel")) {
            final String itemName = NeeoUtil.decodeURIComponent(req.getParameter("itemname"));
            final List<NeeoDeviceChannel> channels = context.getDefinitions().getNeeoDeviceChannel(itemName);
            if (channels == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Channel no longer exists")));
            } else {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus(channels)));
            }
        } else if (paths[0].equalsIgnoreCase("getvirtualdevice")) {
            final NeeoThingUID uid = context.generate(NeeoConstants.VIRTUAL_THING_TYPE);
            final NeeoDevice device = new NeeoDevice(uid, 0, NeeoDeviceType.EXCLUDE, "NEEO Integration", "New Virtual Thing", new ArrayList<>(), null, null, null, null);
            NeeoUtil.write(resp, gson.toJson(new ReturnStatus(device)));
        } 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 : NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) ReturnStatus(org.openhab.io.neeo.internal.servletservices.models.ReturnStatus) ArrayList(java.util.ArrayList) List(java.util.List) JsonParseException(com.google.gson.JsonParseException) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice)

Example 3 with ReturnStatus

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

the class ThingDashboardService 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("updatedevice")) {
            final NeeoDevice device = gson.fromJson(req.getReader(), NeeoDevice.class);
            context.getDefinitions().put(device);
            for (NeeoBrainServlet servlet : service.getServlets()) {
                // restart so brain will query changes
                servlet.getBrainApi().restart();
            }
            NeeoUtil.write(resp, gson.toJson(ReturnStatus.SUCCESS));
        } else if (paths[0].equalsIgnoreCase("restoredevice")) {
            final NeeoThingUID uid = new NeeoThingUID(new String(req.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
            context.getDefinitions().remove(uid);
            final NeeoDevice device = context.getDefinitions().getDevice(uid);
            if (device == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Device no longer exists in openHAB!")));
            } else {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus(device)));
            }
        } else if (paths[0].equalsIgnoreCase("refreshdevice")) {
            final NeeoThingUID uid = new NeeoThingUID(new String(req.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
            final NeeoDevice device = context.getDefinitions().getDevice(uid);
            if (device == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Device no longer exists in openHAB!")));
            } else {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus(device)));
            }
        } else if (paths[0].equalsIgnoreCase("deletedevice")) {
            final NeeoThingUID uid = new NeeoThingUID(new String(req.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
            final boolean deleted = context.getDefinitions().remove(uid);
            NeeoUtil.write(resp, gson.toJson(new ReturnStatus(deleted ? null : "Device " + uid + " was not found (possibly already deleted?)")));
        } else if (paths[0].equalsIgnoreCase("exportrules")) {
            final NeeoThingUID uid = new NeeoThingUID(new String(req.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
            final NeeoDevice device = context.getDefinitions().getDevice(uid);
            if (device == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Device " + uid + " was not found")));
            } else {
                writeExampleRules(resp, device);
            }
        } else {
            logger.debug("Unknown post path: {}", String.join(",", paths));
        }
    } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
        logger.debug("Exception handling post: {}", e.getMessage(), e);
        NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));
    }
}
Also used : NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) ReturnStatus(org.openhab.io.neeo.internal.servletservices.models.ReturnStatus) NeeoBrainServlet(org.openhab.io.neeo.internal.NeeoBrainServlet) JsonParseException(com.google.gson.JsonParseException) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice)

Example 4 with ReturnStatus

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

the class NeeoBrainService method handleUnsubscribe.

/**
 * Handle unsubscribing from a device by removing all device keys for the related {@link ThingUID}
 *
 * @param resp the non-null response to write to
 * @param adapterName the non-empty adapter name
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void handleUnsubscribe(HttpServletResponse resp, String adapterName) throws IOException {
    Objects.requireNonNull(resp, "resp cannot be null");
    NeeoUtil.requireNotEmpty(adapterName, "adapterName cannot be empty");
    logger.debug("handleUnsubscribe {}", adapterName);
    try {
        final NeeoThingUID uid = new NeeoThingUID(adapterName);
        api.getDeviceKeys().remove(uid);
        NeeoUtil.write(resp, gson.toJson(ReturnStatus.SUCCESS));
    } catch (IllegalArgumentException e) {
        logger.debug("AdapterName {} is not a valid thinguid - ignoring", adapterName);
        NeeoUtil.write(resp, gson.toJson(new ReturnStatus("AdapterName not a valid ThingUID: " + adapterName)));
    }
}
Also used : NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) ReturnStatus(org.openhab.io.neeo.internal.servletservices.models.ReturnStatus)

Example 5 with ReturnStatus

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

the class BrainDashboardService method handleGet.

/**
 * Handles the get by looking at all brain servlets and getting the status of each
 *
 * @see DefaultServletService#handleGet(HttpServletRequest, String[], HttpServletResponse)
 */
@Override
public void handleGet(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");
    try {
        if (paths[0].equalsIgnoreCase("brainstatus")) {
            final List<BrainStatus> status = new ArrayList<>();
            for (NeeoBrainServlet servlet : service.getServlets()) {
                status.add(servlet.getBrainStatus());
            }
            NeeoUtil.write(resp, gson.toJson(status));
        } else if (paths[0].equalsIgnoreCase("blinkled")) {
            final String brainId = req.getParameter("brainid");
            if (brainId == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID is null")));
            } else {
                final NeeoBrainServlet servlet = service.getServlet(brainId);
                if (servlet == null) {
                    NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BrainID: " + brainId)));
                } else {
                    try {
                        servlet.getBrainApi().blinkLed();
                        NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
                    } catch (IOException e) {
                        NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Exception occurred blinking LED: " + e.getMessage())));
                    }
                }
            }
        } else if (paths[0].equalsIgnoreCase("getlog")) {
            final String brainId = req.getParameter("brainid");
            if (brainId == null) {
                NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID is null")));
            } else {
                final NeeoBrainServlet servlet = service.getServlet(brainId);
                if (servlet == null) {
                    NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BraidID: " + brainId)));
                } else {
                    try {
                        final String log = servlet.getBrainApi().getLog();
                        NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true, log)));
                    } catch (IOException e) {
                        NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Exception occurred getting log: " + e.getMessage())));
                    }
                }
            }
        } 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 : ReturnStatus(org.openhab.io.neeo.internal.servletservices.models.ReturnStatus) ArrayList(java.util.ArrayList) NeeoBrainServlet(org.openhab.io.neeo.internal.NeeoBrainServlet) IOException(java.io.IOException) BrainStatus(org.openhab.io.neeo.internal.models.BrainStatus) JsonParseException(com.google.gson.JsonParseException)

Aggregations

ReturnStatus (org.openhab.io.neeo.internal.servletservices.models.ReturnStatus)6 JsonParseException (com.google.gson.JsonParseException)4 NeeoThingUID (org.openhab.io.neeo.internal.models.NeeoThingUID)4 ArrayList (java.util.ArrayList)2 NeeoBrainServlet (org.openhab.io.neeo.internal.NeeoBrainServlet)2 NeeoDevice (org.openhab.io.neeo.internal.models.NeeoDevice)2 IOException (java.io.IOException)1 List (java.util.List)1 BrainStatus (org.openhab.io.neeo.internal.models.BrainStatus)1 BrainInfo (org.openhab.io.neeo.internal.servletservices.models.BrainInfo)1