2 * Copyright (c) 2010-2023 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.List;
17 import java.util.Locale;
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;
27 * Implements a location value.
29 * @author David Graeff - Initial contribution
32 public class LocationValue extends Value {
33 public LocationValue() {
34 super(CoreItemFactory.LOCATION, List.of(PointType.class, StringType.class));
38 public String getMQTTpublishValue(@Nullable String pattern) {
39 String formatPattern = pattern;
40 PointType point = ((PointType) state);
42 if (formatPattern == null || "%s".equals(formatPattern)) {
43 if (point.getAltitude().toBigDecimal().equals(BigDecimal.ZERO)) {
44 formatPattern = "%2$f,%3$f";
46 formatPattern = "%2$f,%3$f,%1$f";
49 return String.format(Locale.ROOT, formatPattern, point.getAltitude().toBigDecimal(),
50 point.getLatitude().toBigDecimal(), point.getLongitude().toBigDecimal());
54 public void update(Command command) throws IllegalArgumentException {
55 if (command instanceof PointType) {
56 state = ((PointType) command);
58 state = PointType.valueOf(command.toString());