2 * Copyright (c) 2010-2022 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.mqtt.generic.values;
15 import java.math.BigDecimal;
16 import java.util.Locale;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.library.CoreItemFactory;
24 import org.openhab.core.library.types.PointType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.Command;
29 * Implements a location value.
31 * @author David Graeff - Initial contribution
34 public class LocationValue extends Value {
35 public LocationValue() {
36 super(CoreItemFactory.LOCATION, Stream.of(PointType.class, StringType.class).collect(Collectors.toList()));
40 public @NonNull String getMQTTpublishValue(@Nullable String pattern) {
41 String formatPattern = pattern;
42 PointType point = ((PointType) state);
44 if (formatPattern == null || "%s".equals(formatPattern)) {
45 if (point.getAltitude().toBigDecimal().equals(BigDecimal.ZERO)) {
46 formatPattern = "%2$f,%3$f";
48 formatPattern = "%2$f,%3$f,%1$f";
51 return String.format(Locale.ROOT, formatPattern, point.getAltitude().toBigDecimal(),
52 point.getLatitude().toBigDecimal(), point.getLongitude().toBigDecimal());
56 public void update(Command command) throws IllegalArgumentException {
57 if (command instanceof PointType) {
58 state = ((PointType) command);
60 state = PointType.valueOf(command.toString());