]> git.basschouten.com Git - openhab-addons.git/blob
e10139f0e67053b30096282216e308f90a18c05d
[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 type) {
40             return String.valueOf(type.getZonedDateTime().toInstant().toEpochMilli());
41         }
42         if (state instanceof DecimalType type) {
43             return String.valueOf(type.doubleValue());
44         }
45         if (state instanceof PointType pType) {
46             return String.format(Locale.ENGLISH, "%f;%f;%f", pType.getLatitude().doubleValue(),
47                     pType.getLongitude().doubleValue(), pType.getAltitude().doubleValue());
48         }
49
50         return state.toString();
51     }
52 }