]> git.basschouten.com Git - openhab-addons.git/blob
a603e4df61aa9d8d73ea92c5e0743342415b5631
[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.easee.internal.command;
14
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.CHANNEL_GROUP_NONE;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jetty.client.api.Request;
22 import org.openhab.binding.easee.internal.Utils;
23 import org.openhab.binding.easee.internal.handler.EaseeThingHandler;
24 import org.openhab.binding.easee.internal.model.ValidationException;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * base class for all write commands. common logic should be implemented here
34  *
35  * @author Alexander Friese - initial contribution
36  */
37 @NonNullByDefault
38 public abstract class AbstractWriteCommand extends AbstractCommand {
39     private final Logger logger = LoggerFactory.getLogger(AbstractWriteCommand.class);
40
41     protected final Channel channel;
42     protected Command command;
43
44     /**
45      * the constructor
46      */
47     public AbstractWriteCommand(EaseeThingHandler handler, Channel channel, Command command,
48             RetryOnFailure retryOnFailure, ProcessFailureResponse processFailureResponse,
49             JsonResultProcessor resultProcessor) {
50         super(handler, retryOnFailure, processFailureResponse, resultProcessor);
51         this.channel = channel;
52         this.command = command;
53     }
54
55     /**
56      * helper method for write commands that extracts value from command.
57      *
58      * @return value as String without unit.
59      */
60     protected String getCommandValue() {
61         if (command instanceof QuantityType<?> quantityCommand) {
62             // this is necessary because we must not send the unit to the backend
63             return String.valueOf(quantityCommand.doubleValue());
64         } else if (command instanceof OnOffType) {
65             // this is necessary because we must send booleans and not ON/OFF to the backend
66             return String.valueOf(command.equals(OnOffType.ON));
67         } else {
68             return command.toString();
69         }
70     }
71
72     /**
73      * helper that transforms channelId + commandvalue in a JSON string that can be added as content to a POST request.
74      *
75      * @return converted JSON string
76      * @throws ValidationException
77      */
78     protected String getJsonContent() throws ValidationException {
79         Map<String, String> content = new HashMap<String, String>(1);
80         content.put(channel.getUID().getIdWithoutGroup(), getCommandValue());
81
82         return gson.toJson(content);
83     }
84
85     @Override
86     protected Request prepareRequest(Request requestToPrepare) throws ValidationException {
87         String channelId = channel.getUID().getIdWithoutGroup();
88         String expr = Utils.getValidationExpression(channel);
89         String value = getCommandValue();
90
91         // quantity types are transformed to double and thus we might have decimals which could cause validation error.
92         // So we will shorten here in case no decimals are needed.
93         if (value.endsWith(".0")) {
94             value = value.substring(0, value.length() - 2);
95         }
96
97         if (value.matches(expr)) {
98             return prepareWriteRequest(requestToPrepare);
99         } else {
100             logger.info("channel '{}' does not allow value '{}' - validation rule '{}'", channelId, value, expr);
101             throw new ValidationException("channel (" + channelId + ") could not be updated due to a validation error");
102         }
103     }
104
105     @Override
106     protected String getChannelGroup() {
107         // this is a pure write command, thus no channel group needed.
108         return CHANNEL_GROUP_NONE;
109     }
110
111     /**
112      * concrete implementation has to prepare the write requests with additional parameters, etc
113      *
114      * @param requestToPrepare the request to prepare
115      * @return prepared Request object
116      * @throws ValidationException
117      */
118     protected abstract Request prepareWriteRequest(Request requestToPrepare) throws ValidationException;
119 }