]> git.basschouten.com Git - openhab-addons.git/blob
c0ba5686c475b562cd80ef872b25b68d8e5fbaf2
[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 java.nio.charset.StandardCharsets;
16
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;
36
37 /**
38  * allows update of writable channels
39  *
40  * @author Alexander Friese - initial contribution
41  */
42 @NonNullByDefault
43 public class UpdateSetting extends AbstractUplinkCommandCallback implements NibeUplinkCommand {
44
45     private final NibeUplinkHandler handler;
46     private final Channel channel;
47     private String value;
48     private int retries = 0;
49
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);
55     }
56
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);
63         } else {
64             return command.toString();
65         }
66     }
67
68     @Override
69     protected Request prepareRequest(Request requestToPrepare) {
70         ChannelTypeUID typeUID = channel.getChannelTypeUID();
71         String channelId = channel.getUID().getIdWithoutGroup();
72
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");
76         }
77
78         // although we have integers openhab often transfers decimals which will then cause a validation error. So we
79         // will shorten here.
80         if (value.endsWith(".0")) {
81             value = value.substring(0, value.length() - 2);
82         }
83
84         String expr = ChannelUtil.getValidationExpression(channel);
85
86         if (value.matches(expr)) {
87             Fields fields = new Fields();
88             fields.add(channelId, value);
89
90             FormContentProvider cp = new FormContentProvider(fields);
91
92             requestToPrepare.header(HttpHeader.ACCEPT_ENCODING, StandardCharsets.UTF_8.name());
93             requestToPrepare.content(cp);
94             requestToPrepare.followRedirects(false);
95             requestToPrepare.method(HttpMethod.POST);
96
97             return requestToPrepare;
98         } else {
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");
101         }
102     }
103
104     @Override
105     protected String getURL() {
106         return NibeUplinkBindingConstants.MANAGE_API_BASE_URL + config.getNibeId()
107                 + ChannelUtil.getWriteApiUrlSuffix(channel);
108     }
109
110     @Override
111     public void onComplete(@Nullable Result result) {
112         logger.debug("onComplete()");
113
114         if (!HttpStatus.Code.FOUND.equals(getCommunicationStatus().getHttpCode()) && retries++ < MAX_RETRIES) {
115             logger.debug("Could not set value '{}' for channel '{}' ({})", value, channel.getUID().getId(),
116                     channel.getLabel());
117             handler.getWebInterface().enqueueCommand(this);
118         }
119     }
120 }