2 * Copyright (c) 2010-2023 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.easee.internal.command.circuit;
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.DYNAMIC_CIRCUIT_CURRENT_URL;
17 import java.util.HashMap;
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;
31 * implements the dynamicCurrent api call of the circuit.
33 * @author Alexander Friese - initial contribution
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;
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\\}",
50 * helper that transforms channelId + commandvalue in a JSON string that can be added as content to a POST request.
52 * @return converted JSON string
53 * @throws ValidationException
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]);
65 throw new ValidationException(
66 "malformed command string, expected: '<phase1>;<phase2>;<phase3>', actual: " + rawCommand);
68 return gson.toJson(content);
72 protected Request prepareWriteRequest(Request requestToPrepare) throws ValidationException {
73 requestToPrepare.method(HttpMethod.POST);
74 StringContentProvider cp = new StringContentProvider(getJsonContent());
75 requestToPrepare.content(cp);
77 return requestToPrepare;
81 protected String getURL() {