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.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.UnmarshalException;
21 import javax.xml.bind.Unmarshaller;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamReader;
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;
36 * Callback implementation for updating multiple numbers decoded from a xml
37 * response. Supports reauthorization.
39 * @author Robert Bausdorf - Initial contribution
40 * @author Christoph Weitkamp - Added support for groups
43 public class FritzAhaUpdateCallback extends FritzAhaReauthCallback {
45 private final Logger logger = LoggerFactory.getLogger(FritzAhaUpdateCallback.class);
47 private static final String WEBSERVICE_COMMAND = "switchcmd=getdevicelistinfos";
49 private final AVMFritzBaseBridgeHandler handler;
54 * @param webIface Webinterface to FRITZ!Box
55 * @param handler Bridge handler that will update things.
57 public FritzAhaUpdateCallback(FritzAhaWebInterface webIface, AVMFritzBaseBridgeHandler handler) {
58 super(WEBSERVICE_PATH, WEBSERVICE_COMMAND, webIface, GET, 1);
59 this.handler = handler;
62 @SuppressWarnings({ "null", "unused" })
64 public void execute(int status, String response) {
65 super.execute(status, response);
66 logger.trace("Received State response {}", response);
67 if (isValidRequest()) {
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();
73 handler.onDeviceListAdded(model.getDevicelist());
75 logger.debug("no model in response");
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());
87 logger.debug("request is invalid: {}", status);
88 handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Request is invalid");