2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.neeo.internal.servletservices;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Objects;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.io.neeo.NeeoService;
25 import org.openhab.io.neeo.internal.NeeoBrainServlet;
26 import org.openhab.io.neeo.internal.NeeoUtil;
27 import org.openhab.io.neeo.internal.models.BrainStatus;
28 import org.openhab.io.neeo.internal.servletservices.models.BrainInfo;
29 import org.openhab.io.neeo.internal.servletservices.models.ReturnStatus;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.gson.Gson;
34 import com.google.gson.JsonParseException;
37 * A subclass of {@link DefaultServletService} that handles brain status update for the web pages
39 * @author Tim Roberts - Initial Contribution
42 public class BrainDashboardService extends DefaultServletService {
45 private final Logger logger = LoggerFactory.getLogger(BrainDashboardService.class);
47 /** The gson used for json operations */
48 private final Gson gson = NeeoUtil.createGson();
51 private final NeeoService service;
54 * Create a new brain status service using the specified {@link NeeoService}
56 * @param service the non-null service
58 public BrainDashboardService(NeeoService service) {
59 Objects.requireNonNull(service, "service cannot be null");
61 this.service = service;
65 * Returns true if the first part of the path is 'brainstatus'.
67 * @see DefaultServletService#canHandleRoute(String[])
70 public boolean canHandleRoute(String[] paths) {
71 return paths.length >= 1 && (paths[0].equalsIgnoreCase("brainstatus") || paths[0].equalsIgnoreCase("addbrain")
72 || paths[0].equalsIgnoreCase("removebrain") || paths[0].equalsIgnoreCase("getlog")
73 || paths[0].equalsIgnoreCase("blinkled"));
77 * Handles the get by looking at all brain servlets and getting the status of each
79 * @see DefaultServletService#handleGet(HttpServletRequest, String[], HttpServletResponse)
82 public void handleGet(HttpServletRequest req, String[] paths, HttpServletResponse resp) throws IOException {
83 Objects.requireNonNull(req, "req cannot be null");
84 Objects.requireNonNull(paths, "paths cannot be null");
85 Objects.requireNonNull(resp, "resp cannot be null");
88 if (paths[0].equalsIgnoreCase("brainstatus")) {
89 final List<BrainStatus> status = new ArrayList<>();
90 for (NeeoBrainServlet servlet : service.getServlets()) {
91 status.add(servlet.getBrainStatus());
93 NeeoUtil.write(resp, gson.toJson(status));
94 } else if (paths[0].equalsIgnoreCase("blinkled")) {
95 final String brainId = req.getParameter("brainid");
96 if (brainId == null) {
97 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID is null")));
99 final NeeoBrainServlet servlet = service.getServlet(brainId);
100 if (servlet == null) {
101 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BrainID: " + brainId)));
104 servlet.getBrainApi().blinkLed();
105 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
106 } catch (IOException e) {
107 NeeoUtil.write(resp, gson
108 .toJson(new ReturnStatus("Exception occurred blinking LED: " + e.getMessage())));
112 } else if (paths[0].equalsIgnoreCase("getlog")) {
113 final String brainId = req.getParameter("brainid");
114 if (brainId == null) {
115 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID is null")));
117 final NeeoBrainServlet servlet = service.getServlet(brainId);
118 if (servlet == null) {
119 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BraidID: " + brainId)));
122 final String log = servlet.getBrainApi().getLog();
123 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true, log)));
124 } catch (IOException e) {
126 gson.toJson(new ReturnStatus("Exception occurred getting log: " + e.getMessage())));
131 logger.debug("Unknown get path: {}", String.join(",", paths));
133 } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
134 logger.debug("Exception handling get: {}", e.getMessage(), e);
135 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));
140 * Handles the post for the 'updatedevice', 'restoredevice' or 'refreshdevice'.
142 * @see DefaultServletService#handlePost(HttpServletRequest, String[], HttpServletResponse)
145 public void handlePost(HttpServletRequest req, String[] paths, HttpServletResponse resp) throws IOException {
146 Objects.requireNonNull(req, "req cannot be null");
147 Objects.requireNonNull(paths, "paths cannot be null");
148 Objects.requireNonNull(resp, "resp cannot be null");
149 if (paths.length == 0) {
150 throw new IllegalArgumentException("paths cannot be empty");
154 if (paths[0].equalsIgnoreCase("removebrain")) {
155 final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
156 final String brainId = info.getBrainId();
157 if (brainId == null) {
158 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID not specified")));
159 } else if (service.removeBrain(brainId)) {
160 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
163 gson.toJson(new ReturnStatus("BrainID (" + brainId + ") could not be removed")));
165 } else if (paths[0].equalsIgnoreCase("addbrain")) {
166 final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
167 final String brainIp = info.getBrainIp();
168 if (brainIp == null) {
169 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainIP not specified")));
170 } else if (service.addBrain(brainIp)) {
171 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
173 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(
174 "Brain (" + brainIp + ") could not be added - no brain at that IP Address")));
177 logger.debug("Unknown get path: {}", String.join(",", paths));
179 } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
180 logger.debug("Exception handling get: {}", e.getMessage(), e);
181 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));