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