]> git.basschouten.com Git - openhab-addons.git/blob
cead06f6c6eaf2cde5bf05e1abb8d362e021100a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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                 final NeeoBrainServlet servlet = service.getServlet(brainId);
100                 if (servlet == null) {
101                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BraidID: " + brainId)));
102                 } else {
103                     try {
104                         servlet.getBrainApi().blinkLed();
105                         NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
106                     } catch (IOException e) {
107                         NeeoUtil.write(resp,
108                                 gson.toJson(new ReturnStatus("Exception occurred blinking LED: " + e.getMessage())));
109                     }
110                 }
111             } else if (StringUtils.equalsIgnoreCase(paths[0], "getlog")) {
112                 final String brainId = req.getParameter("brainid");
113                 final NeeoBrainServlet servlet = service.getServlet(brainId);
114                 if (servlet == null) {
115                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BraidID: " + brainId)));
116                 } else {
117                     try {
118                         final String log = servlet.getBrainApi().getLog();
119                         NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true, log)));
120                     } catch (IOException e) {
121                         NeeoUtil.write(resp,
122                                 gson.toJson(new ReturnStatus("Exception occurred getting log: " + e.getMessage())));
123                     }
124                 }
125
126             } else {
127                 logger.debug("Unknown get path: {}", StringUtils.join(paths, ','));
128             }
129         } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
130             logger.debug("Exception handling get: {}", e.getMessage(), e);
131             NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));
132         }
133     }
134
135     /**
136      * Handles the post for the 'updatedevice', 'restoredevice' or 'refreshdevice'.
137      *
138      * @see DefaultServletService#handlePost(HttpServletRequest, String[], HttpServletResponse)
139      */
140     @Override
141     public void handlePost(HttpServletRequest req, String[] paths, HttpServletResponse resp) throws IOException {
142         Objects.requireNonNull(req, "req cannot be null");
143         Objects.requireNonNull(paths, "paths cannot be null");
144         Objects.requireNonNull(resp, "resp cannot be null");
145         if (paths.length == 0) {
146             throw new IllegalArgumentException("paths cannot be empty");
147         }
148
149         try {
150             if (StringUtils.equalsIgnoreCase(paths[0], "removebrain")) {
151                 final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
152                 final String brainId = info.getBrainId();
153                 if (brainId == null) {
154                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainID not specified")));
155                 } else if (service.removeBrain(brainId)) {
156                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
157                 } else {
158                     NeeoUtil.write(resp,
159                             gson.toJson(new ReturnStatus("BrainID (" + brainId + ") could not be removed")));
160                 }
161             } else if (StringUtils.equalsIgnoreCase(paths[0], "addbrain")) {
162                 final BrainInfo info = gson.fromJson(req.getReader(), BrainInfo.class);
163                 final String brainIp = info.getBrainIp();
164                 if (brainIp == null) {
165                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus("BrainIP not specified")));
166                 } else if (service.addBrain(brainIp)) {
167                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true)));
168                 } else {
169                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus(
170                             "Brain (" + brainIp + ") could not be added - no brain at that IP Address")));
171                 }
172             } else {
173                 logger.debug("Unknown get path: {}", StringUtils.join(paths, ','));
174             }
175         } catch (JsonParseException | IllegalArgumentException | NullPointerException e) {
176             logger.debug("Exception handling get: {}", e.getMessage(), e);
177             NeeoUtil.write(resp, gson.toJson(new ReturnStatus(e.getMessage())));
178         }
179     }
180 }