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