]> git.basschouten.com Git - openhab-addons.git/blob
d327cc9a7d9c383bdc41e49fc2df0b0bddcec500
[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.circuit;
14
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.DYNAMIC_CIRCUIT_CURRENT_URL;
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.eclipse.jetty.client.util.StringContentProvider;
23 import org.eclipse.jetty.http.HttpMethod;
24 import org.openhab.binding.easee.internal.command.AbstractWriteCommand;
25 import org.openhab.binding.easee.internal.command.JsonResultProcessor;
26 import org.openhab.binding.easee.internal.handler.EaseeThingHandler;
27 import org.openhab.binding.easee.internal.model.ValidationException;
28 import org.openhab.core.thing.Channel;
29 import org.openhab.core.types.Command;
30
31 /**
32  * implements the dynamicCurrent api call of the circuit.
33  *
34  * @author Alexander Friese - initial contribution
35  */
36 @NonNullByDefault
37 public class SetDynamicCircuitCurrents extends AbstractWriteCommand {
38     private static final String PHASE1 = "phase1";
39     private static final String PHASE2 = "phase2";
40     private static final String PHASE3 = "phase3";
41     private final String url;
42
43     public SetDynamicCircuitCurrents(EaseeThingHandler handler, Channel channel, Command command, String circuitId,
44             JsonResultProcessor resultProcessor) {
45         super(handler, channel, command, RetryOnFailure.YES, ProcessFailureResponse.YES, resultProcessor);
46         String siteId = handler.getBridgeConfiguration().getSiteId();
47         this.url = DYNAMIC_CIRCUIT_CURRENT_URL.replaceAll("\\{siteId\\}", siteId).replaceAll("\\{circuitId\\}",
48                 circuitId);
49     }
50
51     /**
52      * helper that transforms channelId + commandvalue in a JSON string that can be added as content to a POST request.
53      *
54      * @return converted JSON string
55      * @throws ValidationException
56      */
57     @Override
58     protected String getJsonContent() throws ValidationException {
59         Map<String, String> content = new HashMap<String, String>(3);
60         String rawCommand = getCommandValue();
61         String[] tokens = rawCommand.split(";");
62         if (tokens.length == 3) {
63             content.put(PHASE1, tokens[0]);
64             content.put(PHASE2, tokens[1]);
65             content.put(PHASE3, tokens[2]);
66         } else {
67             throw new ValidationException(
68                     "malformed command string, expected: '<phase1>;<phase2>;<phase3>', actual: " + rawCommand);
69         }
70         return gson.toJson(content);
71     }
72
73     @Override
74     protected Request prepareWriteRequest(Request requestToPrepare) throws ValidationException {
75         requestToPrepare.method(HttpMethod.POST);
76         StringContentProvider cp = new StringContentProvider(getJsonContent());
77         requestToPrepare.content(cp);
78
79         return requestToPrepare;
80     }
81
82     @Override
83     protected String getURL() {
84         return url;
85     }
86 }