]> git.basschouten.com Git - openhab-addons.git/blob
03ccd471fb26441c3ff0d5c400a1a1e86fb11672
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.nibeuplink.internal.command;
14
15 import static org.openhab.binding.nibeuplink.internal.NibeUplinkBindingConstants.*;
16
17 import java.nio.charset.StandardCharsets;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.api.Request;
22 import org.eclipse.jetty.client.api.Result;
23 import org.eclipse.jetty.client.util.FormContentProvider;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.eclipse.jetty.http.HttpStatus;
27 import org.eclipse.jetty.util.Fields;
28 import org.openhab.binding.nibeuplink.internal.callback.AbstractUplinkCommandCallback;
29 import org.openhab.binding.nibeuplink.internal.handler.NibeUplinkHandler;
30 import org.openhab.binding.nibeuplink.internal.model.DataResponse;
31 import org.openhab.binding.nibeuplink.internal.model.DataResponseTransformer;
32 import org.openhab.core.thing.Channel;
33
34 /**
35  * generic command that retrieves status values for all channels defined in VVM320Channels
36  *
37  * @author Alexander Friese - initial contribution
38  */
39 @NonNullByDefault
40 public class GenericStatusUpdate extends AbstractUplinkCommandCallback implements NibeUplinkCommand {
41     private final NibeUplinkHandler handler;
42     private final DataResponseTransformer transformer;
43     private int retries = 0;
44
45     public GenericStatusUpdate(NibeUplinkHandler handler) {
46         super(handler.getConfiguration());
47         this.handler = handler;
48         this.transformer = new DataResponseTransformer(handler);
49     }
50
51     @Override
52     protected Request prepareRequest(Request requestToPrepare) {
53         Fields fields = new Fields();
54         fields.add(DATA_API_FIELD_LAST_DATE, DATA_API_FIELD_LAST_DATE_DEFAULT_VALUE);
55         fields.add(DATA_API_FIELD_ID, config.getNibeId());
56
57         for (Channel channel : handler.getChannels()) {
58             if (!handler.getDeadChannels().contains(channel)) {
59                 fields.add(DATA_API_FIELD_DATA, channel.getUID().getIdWithoutGroup());
60             }
61         }
62
63         fields.add(DATA_API_FIELD_DATA, DATA_API_FIELD_DATA_DEFAULT_VALUE);
64         FormContentProvider cp = new FormContentProvider(fields);
65
66         requestToPrepare.header(HttpHeader.ACCEPT, "application/json");
67         requestToPrepare.header(HttpHeader.ACCEPT_ENCODING, StandardCharsets.UTF_8.name());
68         requestToPrepare.content(cp);
69         requestToPrepare.followRedirects(false);
70         requestToPrepare.method(HttpMethod.POST);
71
72         return requestToPrepare;
73     }
74
75     @Override
76     protected String getURL() {
77         return DATA_API_URL;
78     }
79
80     @Override
81     public void onComplete(@Nullable Result result) {
82         logger.debug("onComplete()");
83
84         if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode()) && retries++ < MAX_RETRIES) {
85             updateListenerStatus();
86             handler.getWebInterface().enqueueCommand(this);
87         } else {
88             String json = getContentAsString(StandardCharsets.UTF_8);
89             if (json != null && !json.isEmpty()) {
90                 logger.debug("JSON String: {}", json);
91                 DataResponse jsonObject = fromJson(json);
92                 if (jsonObject != null) {
93                     handler.updateChannelStatus(transformer.transform(jsonObject));
94                 }
95             }
96         }
97     }
98 }