]> git.basschouten.com Git - openhab-addons.git/blob
f486ac274a758858572c31664b7c5fc3231ff517
[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.avmfritz.internal.hardware.callbacks;
14
15 import static org.eclipse.jetty.http.HttpMethod.GET;
16
17 import java.io.StringReader;
18
19 import javax.xml.bind.JAXBException;
20 import javax.xml.bind.UnmarshalException;
21 import javax.xml.bind.Unmarshaller;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamReader;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.avmfritz.internal.dto.DeviceListModel;
27 import org.openhab.binding.avmfritz.internal.handler.AVMFritzBaseBridgeHandler;
28 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaWebInterface;
29 import org.openhab.binding.avmfritz.internal.util.JAXBUtils;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Callback implementation for updating multiple numbers decoded from a xml
37  * response. Supports reauthorization.
38  *
39  * @author Robert Bausdorf - Initial contribution
40  * @author Christoph Weitkamp - Added support for groups
41  */
42 @NonNullByDefault
43 public class FritzAhaUpdateCallback extends FritzAhaReauthCallback {
44
45     private final Logger logger = LoggerFactory.getLogger(FritzAhaUpdateCallback.class);
46
47     private static final String WEBSERVICE_COMMAND = "switchcmd=getdevicelistinfos";
48
49     private final AVMFritzBaseBridgeHandler handler;
50
51     /**
52      * Constructor
53      *
54      * @param webIface Webinterface to FRITZ!Box
55      * @param handler Bridge handler that will update things.
56      */
57     public FritzAhaUpdateCallback(FritzAhaWebInterface webIface, AVMFritzBaseBridgeHandler handler) {
58         super(WEBSERVICE_PATH, WEBSERVICE_COMMAND, webIface, GET, 1);
59         this.handler = handler;
60     }
61
62     @SuppressWarnings({ "null", "unused" })
63     @Override
64     public void execute(int status, String response) {
65         super.execute(status, response);
66         logger.trace("Received State response {}", response);
67         if (isValidRequest()) {
68             try {
69                 XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
70                 Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller();
71                 DeviceListModel model = unmarshaller.unmarshal(xsr, DeviceListModel.class).getValue();
72                 if (model != null) {
73                     handler.onDeviceListAdded(model.getDevicelist());
74                 } else {
75                     logger.debug("no model in response");
76                 }
77                 handler.setStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
78             } catch (UnmarshalException e) {
79                 logger.debug("Failed to unmarshal XML document: {}", e.getMessage());
80                 handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
81             } catch (JAXBException | XMLStreamException e) {
82                 logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
83                 handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
84                         e.getLocalizedMessage());
85             }
86         } else {
87             logger.debug("request is invalid: {}", status);
88             handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Request is invalid");
89         }
90     }
91 }