2 * Copyright (c) 2010-2023 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.binding.ipobserver.internal;
15 import static org.openhab.binding.ipobserver.internal.IpObserverBindingConstants.SERVER_UPDATE_URL;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
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;
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.
37 * @author Matthew Skinner - Initial contribution
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);
45 public IpObserverUpdateReceiver(HttpService httpService) {
47 httpService.registerServlet(SERVER_UPDATE_URL, this, null, httpService.createDefaultHttpContext());
48 } catch (NamespaceException | ServletException e) {
49 logger.warn("Registering servlet failed:{}", e.getMessage());
54 protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp) throws IOException {
58 String stationUpdate = req.getQueryString();
59 if (stationUpdate == null) {
62 logger.debug("Weather station packet received from {}", req.getRemoteHost());
63 for (IpObserverHandler ipObserverHandler : listOfHandlers) {
64 ipObserverHandler.processServerQuery(stationUpdate);
68 public void addStation(IpObserverHandler ipObserverHandler) {
69 listOfHandlers.add(ipObserverHandler);
72 public void removeStation(IpObserverHandler ipObserverHandler) {
73 listOfHandlers.remove(ipObserverHandler);