2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nibeuplink.internal.command;
15 import static org.openhab.binding.nibeuplink.internal.NibeUplinkBindingConstants.*;
17 import java.nio.charset.StandardCharsets;
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;
36 * generic command that retrieves status values for all channels defined in {@link VVM320Channels}
38 * @author Alexander Friese - initial contribution
41 public class GenericStatusUpdate extends AbstractUplinkCommandCallback implements NibeUplinkCommand {
42 private final NibeUplinkHandler handler;
43 private final DataResponseTransformer transformer;
44 private int retries = 0;
46 public GenericStatusUpdate(NibeUplinkHandler handler) {
47 super(handler.getConfiguration());
48 this.handler = handler;
49 this.transformer = new DataResponseTransformer(handler);
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());
58 for (Channel channel : handler.getChannels()) {
59 if (!handler.getDeadChannels().contains(channel)) {
60 fields.add(DATA_API_FIELD_DATA, channel.getUID().getIdWithoutGroup());
64 fields.add(DATA_API_FIELD_DATA, DATA_API_FIELD_DATA_DEFAULT_VALUE);
65 FormContentProvider cp = new FormContentProvider(fields);
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);
73 return requestToPrepare;
77 protected String getURL() {
82 public void onComplete(@Nullable Result result) {
83 logger.debug("onComplete()");
85 if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode()) && retries++ < MAX_RETRIES) {
86 StatusUpdateListener listener = getListener();
87 if (listener != null) {
88 listener.update(getCommunicationStatus());
90 handler.getWebInterface().enqueueCommand(this);
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));