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