2 * Copyright (c) 2010-2021 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.apache.commons.lang.StringUtils;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.io.neeo.NeeoService;
26 import org.openhab.io.neeo.internal.NeeoBrainServlet;
27 import org.openhab.io.neeo.internal.NeeoUtil;
28 import org.openhab.io.neeo.internal.models.BrainStatus;
29 import org.openhab.io.neeo.internal.servletservices.models.BrainInfo;
30 import org.openhab.io.neeo.internal.servletservices.models.ReturnStatus;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.gson.Gson;
35 import com.google.gson.JsonParseException;
38 * A subclass of {@link DefaultServletService} that handles brain status update for the web pages
40 * @author Tim Roberts - Initial Contribution
43 public class BrainDashboardService extends DefaultServletService {
46 private final Logger logger = LoggerFactory.getLogger(BrainDashboardService.class);
48 /** The gson used for json operations */
49 private final Gson gson = NeeoUtil.createGson();
52 private final NeeoService service;
55 * Create a new brain status service using the specified {@link NeeoService}
57 * @param service the non-null service
59 public BrainDashboardService(NeeoService service) {
60 Objects.requireNonNull(service, "service cannot be null");
62 this.service = service;
66 * Returns true if the first part of the path is 'brainstatus'.
68 * @see DefaultServletService#canHandleRoute(String[])
71 public boolean canHandleRoute(String[] paths) {
72 return paths.length >= 1 && (StringUtils.equalsIgnoreCase(paths[0], "brainstatus")
73 || StringUtils.equalsIgnoreCase(paths[0], "addbrain")
74 || StringUtils.equalsIgnoreCase(paths[0], "removebrain")
75 || StringUtils.equalsIgnoreCase(paths[0], "getlog")
76 || StringUtils.equalsIgnoreCase(paths[0], "blinkled"));
80 * Handles the get by looking at all brain servlets and getting the status of each
82 * @see DefaultServletService#handleGet(HttpServletRequest, String[], HttpServletResponse)
85 public void handleGet(HttpServletRequest req, String[] paths, HttpServletResponse resp) throws IOException {
86 Objects.requireNonNull(req, "req cannot be null");
87 Objects.requireNonNull(paths, "paths cannot be null");
88 Objects.requireNonNull(resp, "resp cannot be null");
91 if (StringUtils.equalsIgnoreCase(paths[0], "brainstatus")) {
92 final List<BrainStatus> status = new ArrayList<>();
93 for (NeeoBrainServlet servlet : service.getServlets()) {
94 status.add(servlet.getBrainStatus());
96 NeeoUtil.write(resp, gson.toJson(status));
97 } else if (StringUtils.equalsIgnoreCase(paths[0], "blinkled")) {
98 final String brainId = req.getParameter("brainid");
99 if (brainId == null) {
100 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID is null")));
102 final NeeoBrainServlet servlet = service.getServlet(brainId);
103 if (servlet == null) {
104 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BrainID: " + brainId)));
107 servlet.getBrainApi().blinkLed();
108 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
109 } catch (IOException e) {
110 NeeoUtil.write(resp, gson
111 .toJson(new ReturnStatus("Exception occurred blinking LED: " + e.getMessage())));
115 } else if (StringUtils.equalsIgnoreCase(paths[0], "getlog")) {
116 final String brainId = req.getParameter("brainid");
117 if (brainId == null) {
118 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID is null")));
120 final NeeoBrainServlet servlet = service.getServlet(brainId);
121 if (servlet == null) {
122 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BraidID: " + brainId)));
125 final String log = servlet.getBrainApi().getLog();
126 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true, log)));
127 } catch (IOException e) {
129 gson.toJson(new ReturnStatus("Exception occurred getting log: " + e.getMessage())));
134 logger.debug("Unknown get path: {}", StringUtils.join(paths, ','));
136 } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
137 logger.debug("Exception handling get: {}", e.getMessage(), e);
138 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));
143 * Handles the post for the 'updatedevice', 'restoredevice' or 'refreshdevice'.
145 * @see DefaultServletService#handlePost(HttpServletRequest, String[], HttpServletResponse)
148 public void handlePost(HttpServletRequest req, String[] paths, HttpServletResponse resp) throws IOException {
149 Objects.requireNonNull(req, "req cannot be null");
150 Objects.requireNonNull(paths, "paths cannot be null");
151 Objects.requireNonNull(resp, "resp cannot be null");
152 if (paths.length == 0) {
153 throw new IllegalArgumentException("paths cannot be empty");
157 if (StringUtils.equalsIgnoreCase(paths[0], "removebrain")) {
158 final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
159 final String brainId = info.getBrainId();
160 if (brainId == null) {
161 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID not specified")));
162 } else if (service.removeBrain(brainId)) {
163 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
166 gson.toJson(new ReturnStatus("BrainID (" + brainId + ") could not be removed")));
168 } else if (StringUtils.equalsIgnoreCase(paths[0], "addbrain")) {
169 final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
170 final String brainIp = info.getBrainIp();
171 if (brainIp == null) {
172 NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainIP not specified")));
173 } else if (service.addBrain(brainIp)) {
174 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
176 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(
177 "Brain (" + brainIp + ") could not be added - no brain at that IP Address")));
180 logger.debug("Unknown get path: {}", StringUtils.join(paths, ','));
182 } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
183 logger.debug("Exception handling get: {}", e.getMessage(), e);
184 NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));