2 * Copyright (c) 2010-2021 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.avmfritz.internal.hardware.callbacks;
15 import static org.eclipse.jetty.http.HttpMethod.GET;
17 import java.io.StringReader;
19 import javax.xml.bind.JAXBException;
20 import javax.xml.bind.Unmarshaller;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamReader;
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;
35 * Callback implementation for updating multiple numbers decoded from a xml
36 * response. Supports reauthorization.
38 * @author Robert Bausdorf - Initial contribution
39 * @author Christoph Weitkamp - Added support for groups
42 public class FritzAhaUpdateCallback extends FritzAhaReauthCallback {
44 private final Logger logger = LoggerFactory.getLogger(FritzAhaUpdateCallback.class);
46 private static final String WEBSERVICE_COMMAND = "switchcmd=getdevicelistinfos";
48 private final AVMFritzBaseBridgeHandler handler;
53 * @param webIface Webinterface to FRITZ!Box
54 * @param handler Bridge handler that will update things.
56 public FritzAhaUpdateCallback(FritzAhaWebInterface webIface, AVMFritzBaseBridgeHandler handler) {
57 super(WEBSERVICE_PATH, WEBSERVICE_COMMAND, webIface, GET, 1);
58 this.handler = handler;
62 public void execute(int status, String response) {
63 super.execute(status, response);
64 logger.trace("Received State response {}", response);
65 if (isValidRequest()) {
67 XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
68 Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller();
69 DeviceListModel model = (DeviceListModel) unmarshaller.unmarshal(xsr);
71 handler.onDeviceListAdded(model.getDevicelist());
73 logger.debug("no model in response");
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());
82 logger.debug("request is invalid: {}", status);
83 handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Request is invalid");