]> git.basschouten.com Git - openhab-addons.git/blob
0f2b5b2ad7681e5039c67570612bdbc4a03aab0a
[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.mqtt.generic.values;
14
15 import java.math.BigDecimal;
16 import java.util.List;
17 import java.util.Locale;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.CoreItemFactory;
22 import org.openhab.core.library.types.PointType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.Command;
25
26 /**
27  * Implements a location value.
28  *
29  * @author David Graeff - Initial contribution
30  */
31 @NonNullByDefault
32 public class LocationValue extends Value {
33     public LocationValue() {
34         super(CoreItemFactory.LOCATION, List.of(PointType.class, StringType.class));
35     }
36
37     @Override
38     public String getMQTTpublishValue(@Nullable String pattern) {
39         String formatPattern = pattern;
40         PointType point = ((PointType) state);
41
42         if (formatPattern == null || "%s".equals(formatPattern)) {
43             if (point.getAltitude().toBigDecimal().equals(BigDecimal.ZERO)) {
44                 formatPattern = "%2$f,%3$f";
45             } else {
46                 formatPattern = "%2$f,%3$f,%1$f";
47             }
48         }
49         return String.format(Locale.ROOT, formatPattern, point.getAltitude().toBigDecimal(),
50                 point.getLatitude().toBigDecimal(), point.getLongitude().toBigDecimal());
51     }
52
53     @Override
54     public void update(Command command) throws IllegalArgumentException {
55         if (command instanceof PointType) {
56             state = ((PointType) command);
57         } else {
58             state = PointType.valueOf(command.toString());
59         }
60     }
61 }