]> git.basschouten.com Git - openhab-addons.git/blob
937c89158bb418217cc6ef5a5d2303d8414e23ed
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.robonect.internal.model.cmd;
14
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
18
19 import org.openhab.binding.robonect.internal.RobonectClient;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The command allows to set or retrieve the mower name.
25  * 
26  * @author Marco Meyer - Initial contribution
27  */
28 public class NameCommand implements Command {
29
30     private final Logger logger = LoggerFactory.getLogger(NameCommand.class);
31
32     private String newName;
33
34     /**
35      * sets the mower name.
36      * 
37      * @param newName - the mower name.
38      * @return - the command instance.
39      */
40     public NameCommand withNewName(String newName) {
41         this.newName = newName != null ? newName : "";
42         return this;
43     }
44
45     /**
46      * @param baseURL - will be passed by the {@link RobonectClient} in the form
47      *            http://xxx.xxx.xxx/json?
48      * @return
49      */
50     @Override
51     public String toCommandURL(String baseURL) {
52         if (newName == null) {
53             return baseURL + "?cmd=name";
54         } else {
55             try {
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";
61             }
62         }
63     }
64 }