]> git.basschouten.com Git - openhab-addons.git/blob
dd91385d61fd0af931559026a7553cb8a8e46c97
[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.persistence.jpa.internal;
14
15 import java.util.Locale;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.DateTimeType;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.PointType;
21 import org.openhab.core.types.State;
22
23 /**
24  * Helper class for dealing with State
25  *
26  * @author Manfred Bergmann - Initial contribution
27  *
28  */
29 @NonNullByDefault
30 public class StateHelper {
31
32     /**
33      * Converts the given State to a string that can be persisted in the database.
34      *
35      * @param state the state of the item to be persisted
36      * @return state converted as string
37      */
38     public static String toString(State state) {
39         if (state instanceof DateTimeType) {
40             return String.valueOf(((DateTimeType) state).getZonedDateTime().toInstant().toEpochMilli());
41         }
42         if (state instanceof DecimalType) {
43             return String.valueOf(((DecimalType) state).doubleValue());
44         }
45         if (state instanceof PointType) {
46             PointType pType = (PointType) state;
47             return String.format(Locale.ENGLISH, "%f;%f;%f", pType.getLatitude().doubleValue(),
48                     pType.getLongitude().doubleValue(), pType.getAltitude().doubleValue());
49         }
50
51         return state.toString();
52     }
53 }