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