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 java.nio.charset.StandardCharsets;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.api.Request;
20 import org.eclipse.jetty.client.api.Result;
21 import org.eclipse.jetty.client.util.FormContentProvider;
22 import org.eclipse.jetty.http.HttpHeader;
23 import org.eclipse.jetty.http.HttpMethod;
24 import org.eclipse.jetty.http.HttpStatus;
25 import org.eclipse.jetty.util.Fields;
26 import org.openhab.binding.nibeuplink.internal.NibeUplinkBindingConstants;
27 import org.openhab.binding.nibeuplink.internal.callback.AbstractUplinkCommandCallback;
28 import org.openhab.binding.nibeuplink.internal.handler.ChannelUtil;
29 import org.openhab.binding.nibeuplink.internal.handler.NibeUplinkHandler;
30 import org.openhab.binding.nibeuplink.internal.model.ValidationException;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.QuantityType;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.type.ChannelTypeUID;
35 import org.openhab.core.types.Command;
38 * allows update of writable channels
40 * @author Alexander Friese - initial contribution
43 public class UpdateSetting extends AbstractUplinkCommandCallback implements NibeUplinkCommand {
45 private final NibeUplinkHandler handler;
46 private final Channel channel;
48 private int retries = 0;
50 public UpdateSetting(NibeUplinkHandler handler, Channel channel, Command command) {
51 super(handler.getConfiguration());
52 this.handler = handler;
53 this.channel = channel;
54 this.value = extractValue(command);
57 private String extractValue(Command command) {
58 // this is necessary because we must not send the unit to the nibe backend
59 if (command instanceof QuantityType<?> quantityCommand) {
60 return String.valueOf(quantityCommand.doubleValue());
61 } else if (command instanceof OnOffType onOffCommand) {
62 return ChannelUtil.mapValue(channel, onOffCommand);
64 return command.toString();
69 protected Request prepareRequest(Request requestToPrepare) {
70 ChannelTypeUID typeUID = channel.getChannelTypeUID();
71 String channelId = channel.getUID().getIdWithoutGroup();
73 if (typeUID == null || !typeUID.getId().startsWith(NibeUplinkBindingConstants.RW_CHANNEL_PREFIX)) {
74 logger.info("channel '{}' does not support write access - value to set '{}'", channelId, value);
75 throw new UnsupportedOperationException("channel (" + channelId + ") does not support write access");
78 // although we have integers openhab often transfers decimals which will then cause a validation error. So we
80 if (value.endsWith(".0")) {
81 value = value.substring(0, value.length() - 2);
84 String expr = ChannelUtil.getValidationExpression(channel);
86 if (value.matches(expr)) {
87 Fields fields = new Fields();
88 fields.add(channelId, value);
90 FormContentProvider cp = new FormContentProvider(fields);
92 requestToPrepare.header(HttpHeader.ACCEPT_ENCODING, StandardCharsets.UTF_8.name());
93 requestToPrepare.content(cp);
94 requestToPrepare.followRedirects(false);
95 requestToPrepare.method(HttpMethod.POST);
97 return requestToPrepare;
99 logger.info("channel '{}' does not allow value '{}' - validation rule '{}'", channelId, value, expr);
100 throw new ValidationException("channel (" + channelId + ") could not be updated due to a validation error");
105 protected String getURL() {
106 return NibeUplinkBindingConstants.MANAGE_API_BASE_URL + config.getNibeId()
107 + ChannelUtil.getWriteApiUrlSuffix(channel);
111 public void onComplete(@Nullable Result result) {
112 logger.debug("onComplete()");
114 if (!HttpStatus.Code.FOUND.equals(getCommunicationStatus().getHttpCode()) && retries++ < MAX_RETRIES) {
115 logger.debug("Could not set value '{}' for channel '{}' ({})", value, channel.getUID().getId(),
117 handler.getWebInterface().enqueueCommand(this);