2 * Copyright (c) 2010-2024 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.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;
32 * implements the dynamicCurrent api call of the circuit.
34 * @author Alexander Friese - initial contribution
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;
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\\}",
52 * helper that transforms channelId + commandvalue in a JSON string that can be added as content to a POST request.
54 * @return converted JSON string
55 * @throws ValidationException
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]);
67 throw new ValidationException(
68 "malformed command string, expected: '<phase1>;<phase2>;<phase3>', actual: " + rawCommand);
70 return gson.toJson(content);
74 protected Request prepareWriteRequest(Request requestToPrepare) throws ValidationException {
75 requestToPrepare.method(HttpMethod.POST);
76 StringContentProvider cp = new StringContentProvider(getJsonContent());
77 requestToPrepare.content(cp);
79 return requestToPrepare;
83 protected String getURL() {