]> git.basschouten.com Git - openhab-addons.git/blob
b1c00fe11db313bdd5c1d6d9cd948b0a5eaf502d
[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.binding.ipobserver.internal;
14
15 import static org.openhab.binding.ipobserver.internal.IpObserverBindingConstants.SERVER_UPDATE_URL;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.osgi.service.http.HttpService;
29 import org.osgi.service.http.NamespaceException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link IpObserverUpdateReceiver} captures any updates sent to the openHAB Jetty server if the weather station is
35  * setup to direct the weather updates to the HTTP server of openHAB which is normally port 8080.
36  *
37  * @author Matthew Skinner - Initial contribution
38  */
39 @NonNullByDefault
40 public class IpObserverUpdateReceiver extends HttpServlet {
41     private static final long serialVersionUID = -234658674L;
42     private final Logger logger = LoggerFactory.getLogger(this.getClass());
43     private List<IpObserverHandler> listOfHandlers = new ArrayList<>(1);
44
45     public IpObserverUpdateReceiver(HttpService httpService) {
46         try {
47             httpService.registerServlet(SERVER_UPDATE_URL, this, null, httpService.createDefaultHttpContext());
48         } catch (NamespaceException | ServletException e) {
49             logger.warn("Registering servlet failed:{}", e.getMessage());
50         }
51     }
52
53     @Override
54     protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp) throws IOException {
55         if (req == null) {
56             return;
57         }
58         String stationUpdate = req.getQueryString();
59         if (stationUpdate == null) {
60             return;
61         }
62         logger.debug("Weather station packet received from {}", req.getRemoteHost());
63         for (IpObserverHandler ipObserverHandler : listOfHandlers) {
64             ipObserverHandler.processServerQuery(stationUpdate);
65         }
66     }
67
68     public void addStation(IpObserverHandler ipObserverHandler) {
69         listOfHandlers.add(ipObserverHandler);
70     }
71
72     public void removeStation(IpObserverHandler ipObserverHandler) {
73         listOfHandlers.remove(ipObserverHandler);
74     }
75 }