2 * Copyright (c) 2010-2020 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;
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;
33 * Callback implementation for updating multiple numbers decoded from a xml
34 * response. Supports reauthorization.
36 * @author Robert Bausdorf - Initial contribution
37 * @author Christoph Weitkamp - Added support for groups
40 public class FritzAhaUpdateCallback extends FritzAhaReauthCallback {
42 private final Logger logger = LoggerFactory.getLogger(FritzAhaUpdateCallback.class);
44 private static final String WEBSERVICE_COMMAND = "switchcmd=getdevicelistinfos";
46 private final AVMFritzBaseBridgeHandler handler;
51 * @param webIface Webinterface to FRITZ!Box
52 * @param handler Bridge handler that will update things.
54 public FritzAhaUpdateCallback(FritzAhaWebInterface webIface, AVMFritzBaseBridgeHandler handler) {
55 super(WEBSERVICE_PATH, WEBSERVICE_COMMAND, webIface, GET, 1);
56 this.handler = handler;
60 public void execute(int status, String response) {
61 super.execute(status, response);
62 logger.trace("Received State response {}", response);
63 if (isValidRequest()) {
65 Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller();
66 DeviceListModel model = (DeviceListModel) unmarshaller.unmarshal(new StringReader(response));
68 handler.onDeviceListAdded(model.getDevicelist());
70 logger.debug("no model in response");
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());
79 logger.debug("request is invalid: {}", status);
80 handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Request is invalid");