]> git.basschouten.com Git - openhab-addons.git/blob
7186dbbe16f07ff19829a8f41d4aca3c7eff3ab2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.Locale;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
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;
27
28 /**
29  * Implements a location value.
30  *
31  * @author David Graeff - Initial contribution
32  */
33 @NonNullByDefault
34 public class LocationValue extends Value {
35     public LocationValue() {
36         super(CoreItemFactory.LOCATION, Stream.of(PointType.class, StringType.class).collect(Collectors.toList()));
37     }
38
39     @Override
40     public @NonNull String getMQTTpublishValue(@Nullable String pattern) {
41         String formatPattern = pattern;
42         PointType point = ((PointType) state);
43
44         if (formatPattern == null || "%s".equals(formatPattern)) {
45             if (point.getAltitude().toBigDecimal().equals(BigDecimal.ZERO)) {
46                 formatPattern = "%2$f,%3$f";
47             } else {
48                 formatPattern = "%2$f,%3$f,%1$f";
49             }
50         }
51         return String.format(Locale.ROOT, formatPattern, point.getAltitude().toBigDecimal(),
52                 point.getLatitude().toBigDecimal(), point.getLongitude().toBigDecimal());
53     }
54
55     @Override
56     public void update(Command command) throws IllegalArgumentException {
57         if (command instanceof PointType) {
58             state = ((PointType) command);
59         } else {
60             state = PointType.valueOf(command.toString());
61         }
62     }
63 }