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