]> git.basschouten.com Git - openhab-addons.git/blob
68c1af585bf2b2a4afb7b3a0cc8333af90cf535f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
32
33 import com.google.gson.Gson;
34 import com.google.gson.JsonParseException;
35
36 /**
37  * A subclass of {@link DefaultServletService} that handles brain status update for the web pages
38  *
39  * @author Tim Roberts - Initial Contribution
40  */
41 @NonNullByDefault
42 public class BrainDashboardService extends DefaultServletService {
43
44     /** The logger */
45     private final Logger logger = LoggerFactory.getLogger(BrainDashboardService.class);
46
47     /** The gson used for json operations */
48     private final Gson gson = NeeoUtil.createGson();
49
50     /** The service */
51     private final NeeoService service;
52
53     /**
54      * Create a new brain status service using the specified {@link NeeoService}
55      *
56      * @param service the non-null service
57      */
58     public BrainDashboardService(NeeoService service) {
59         Objects.requireNonNull(service, "service cannot be null");
60
61         this.service = service;
62     }
63
64     /**
65      * Returns true if the first part of the path is 'brainstatus'.
66      *
67      * @see DefaultServletService#canHandleRoute(String[])
68      */
69     @Override
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"));
74     }
75
76     /**
77      * Handles the get by looking at all brain servlets and getting the status of each
78      *
79      * @see DefaultServletService#handleGet(HttpServletRequest, String[], HttpServletResponse)
80      */
81     @Override
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");
86
87         try {
88             if (paths[0].equalsIgnoreCase("brainstatus")) {
89                 final List<BrainStatus> status = new ArrayList<>();
90                 for (NeeoBrainServlet servlet : service.getServlets()) {
91                     status.add(servlet.getBrainStatus());
92                 }
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")));
98                 } else {
99                     final NeeoBrainServlet servlet = service.getServlet(brainId);
100                     if (servlet == null) {
101                         NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BrainID: " + 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, gson
108                                     .toJson(new ReturnStatus("Exception occurred blinking LED: " + e.getMessage())));
109                         }
110                     }
111                 }
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")));
116                 } else {
117                     final NeeoBrainServlet servlet = service.getServlet(brainId);
118                     if (servlet == null) {
119                         NeeoUtil.write(resp, gson.toJson(new ReturnStatus("Unknown BraidID: " + brainId)));
120                     } else {
121                         try {
122                             final String log = servlet.getBrainApi().getLog();
123                             NeeoUtil.write(resp, gson.toJson(new ReturnStatus(true, log)));
124                         } catch (IOException e) {
125                             NeeoUtil.write(resp,
126                                     gson.toJson(new ReturnStatus("Exception occurred getting log: " + e.getMessage())));
127                         }
128                     }
129                 }
130             } else {
131                 logger.debug("Unknown get path: {}", String.join(",", paths));
132             }
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())));
136         }
137     }
138
139     /**
140      * Handles the post for the 'updatedevice', 'restoredevice' or 'refreshdevice'.
141      *
142      * @see DefaultServletService#handlePost(HttpServletRequest, String[], HttpServletResponse)
143      */
144     @Override
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");
151         }
152
153         try {
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)));
161                 } else {
162                     NeeoUtil.write(resp,
163                             gson.toJson(new ReturnStatus("BrainID (" + brainId + ") could not be removed")));
164                 }
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)));
172                 } else {
173                     NeeoUtil.write(resp, gson.toJson(new ReturnStatus(
174                             "Brain (" + brainIp + ") could not be added - no brain at that IP Address")));
175                 }
176             } else {
177                 logger.debug("Unknown get path: {}", String.join(",", paths));
178             }
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())));
182         }
183     }
184 }