2 * Copyright (c) 2010-2024 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.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;
35 * generic command that retrieves status values for all channels defined in VVM320Channels
37 * @author Alexander Friese - initial contribution
40 public class GenericStatusUpdate extends AbstractUplinkCommandCallback implements NibeUplinkCommand {
41 private final NibeUplinkHandler handler;
42 private final DataResponseTransformer transformer;
43 private int retries = 0;
45 public GenericStatusUpdate(NibeUplinkHandler handler) {
46 super(handler.getConfiguration());
47 this.handler = handler;
48 this.transformer = new DataResponseTransformer(handler);
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());
57 for (Channel channel : handler.getChannels()) {
58 if (!handler.getDeadChannels().contains(channel)) {
59 fields.add(DATA_API_FIELD_DATA, channel.getUID().getIdWithoutGroup());
63 fields.add(DATA_API_FIELD_DATA, DATA_API_FIELD_DATA_DEFAULT_VALUE);
64 FormContentProvider cp = new FormContentProvider(fields);
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);
72 return requestToPrepare;
76 protected String getURL() {
81 public void onComplete(@Nullable Result result) {
82 logger.debug("onComplete()");
84 if (!HttpStatus.Code.OK.equals(getCommunicationStatus().getHttpCode()) && retries++ < MAX_RETRIES) {
85 updateListenerStatus();
86 handler.getWebInterface().enqueueCommand(this);
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));