]> git.basschouten.com Git - openhab-addons.git/blob
d976c3b35e014219d6005fa11adc0547344d2e0a
[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.templates.TemplateListModel;
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.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Callback implementation for updating templates from a xml response.
34  *
35  * @author Christoph Weitkamp - Initial contribution
36  */
37 @NonNullByDefault
38 public class FritzAhaUpdateTemplatesCallback extends FritzAhaReauthCallback {
39
40     private final Logger logger = LoggerFactory.getLogger(FritzAhaUpdateTemplatesCallback.class);
41
42     private static final String WEBSERVICE_COMMAND = "switchcmd=gettemplatelistinfos";
43
44     private final AVMFritzBaseBridgeHandler handler;
45
46     /**
47      * Constructor
48      *
49      * @param webInterface web interface to FRITZ!Box
50      * @param handler handler that will update things
51      */
52     public FritzAhaUpdateTemplatesCallback(FritzAhaWebInterface webInterface, AVMFritzBaseBridgeHandler handler) {
53         super(WEBSERVICE_PATH, WEBSERVICE_COMMAND, webInterface, GET, 1);
54         this.handler = handler;
55     }
56
57     @Override
58     public void execute(int status, String response) {
59         super.execute(status, response);
60         logger.trace("Received response '{}'", response);
61         if (isValidRequest()) {
62             try {
63                 XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
64                 Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
65                 TemplateListModel model = (TemplateListModel) unmarshaller.unmarshal(xsr);
66                 if (model != null) {
67                     handler.addTemplateList(model.getTemplates());
68                 } else {
69                     logger.debug("no template in response");
70                 }
71             } catch (JAXBException | XMLStreamException e) {
72                 logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
73             }
74         } else {
75             logger.debug("request is invalid: {}", status);
76         }
77     }
78 }