2 * Copyright (c) 2010-2021 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.robonect.internal.model.cmd;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
19 import org.openhab.binding.robonect.internal.RobonectClient;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * The command allows to set or retrieve the mower name.
26 * @author Marco Meyer - Initial contribution
28 public class NameCommand implements Command {
30 private final Logger logger = LoggerFactory.getLogger(NameCommand.class);
32 private String newName;
35 * sets the mower name.
37 * @param newName - the mower name.
38 * @return - the command instance.
40 public NameCommand withNewName(String newName) {
41 this.newName = newName != null ? newName : "";
46 * @param baseURL - will be passed by the {@link RobonectClient} in the form
47 * http://xxx.xxx.xxx/json?
51 public String toCommandURL(String baseURL) {
52 if (newName == null) {
53 return baseURL + "?cmd=name";
56 return baseURL + "?cmd=name&name="
57 + URLEncoder.encode(newName, StandardCharsets.ISO_8859_1.displayName());
58 } catch (UnsupportedEncodingException e) {
59 logger.error("Could not encode name {} ", newName, e);
60 return baseURL + "?cmd=name";