]> git.basschouten.com Git - openhab-addons.git/blob
c3e9713d80a1486a30c85868c650c85aee6a4de7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.neeo.internal.servletservices;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Objects;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
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;
33
34 import com.google.gson.Gson;
35 import com.google.gson.JsonParseException;
36
37 /**
38  * A subclass of {@link DefaultServletService} that handles brain status update for the web pages
39  *
40  * @author Tim Roberts - Initial Contribution
41  */
42 @NonNullByDefault
43 public class BrainDashboardService extends DefaultServletService {
44
45     /** The logger */
46     private final Logger logger = LoggerFactory.getLogger(BrainDashboardService.class);
47
48     /** The gson used for json operations */
49     private final Gson gson = NeeoUtil.createGson();
50
51     /** The service */
52     private final NeeoService service;
53
54     /**
55      * Create a new brain status service using the specified {@link NeeoService}
56      *
57      * @param service the non-null service
58      */
59     public BrainDashboardService(NeeoService service) {
60         Objects.requireNonNull(service, "service cannot be null");
61
62         this.service = service;
63     }
64
65     /**
66      * Returns true if the first part of the path is 'brainstatus'.
67      *
68      * @see DefaultServletService#canHandleRoute(String[])
69      */
70     @Override
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"));
77     }
78
79     /**
80      * Handles the get by looking at all brain servlets and getting the status of each
81      *
82      * @see DefaultServletService#handleGet(HttpServletRequest, String[], HttpServletResponse)
83      */
84     @Override
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");
89
90         try {
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());
95                 }
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")));
101                 } else {
102                     final NeeoBrainServlet servlet = service.getServlet(brainId);
103                     if (servlet == null) {
104                         NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BrainID: " + brainId)));
105                     } else {
106                         try {
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())));
112                         }
113                     }
114                 }
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")));
119                 } else {
120                     final NeeoBrainServlet servlet = service.getServlet(brainId);
121                     if (servlet == null) {
122                         NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BraidID: " + brainId)));
123                     } else {
124                         try {
125                             final String log = servlet.getBrainApi().getLog();
126                             NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true, log)));
127                         } catch (IOException e) {
128                             NeeoUtil.write(resp,
129                                     gson.toJson(new ReturnStatus("Exception occurred getting log: " + e.getMessage())));
130                         }
131                     }
132                 }
133             } else {
134                 logger.debug("Unknown get path: {}", StringUtils.join(paths, ','));
135             }
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())));
139         }
140     }
141
142     /**
143      * Handles the post for the 'updatedevice', 'restoredevice' or 'refreshdevice'.
144      *
145      * @see DefaultServletService#handlePost(HttpServletRequest, String[], HttpServletResponse)
146      */
147     @Override
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");
154         }
155
156         try {
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)));
164                 } else {
165                     NeeoUtil.write(resp,
166                             gson.toJson(new ReturnStatus("BrainID (" + brainId + ") could not be removed")));
167                 }
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)));
175                 } else {
176                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus(
177                             "Brain (" + brainIp + ") could not be added - no brain at that IP Address")));
178                 }
179             } else {
180                 logger.debug("Unknown get path: {}", StringUtils.join(paths, ','));
181             }
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())));
185         }
186     }
187 }