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