2 * Copyright (c) 2010-2021 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.persistence.jpa.internal;
15 import java.util.Locale;
17 import org.openhab.core.library.types.DateTimeType;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.PointType;
20 import org.openhab.core.types.State;
23 * Helper class for dealing with State
25 * @author Manfred Bergmann - Initial contribution
28 public class StateHelper {
31 * Converts the given State to a string that can be persisted in db
33 * @param state the state of the item to be persisted
34 * @return state converted as string
37 public static String toString(State state) throws Exception {
38 if (state instanceof DateTimeType) {
39 return String.valueOf(((DateTimeType) state).getZonedDateTime().toInstant().toEpochMilli());
41 if (state instanceof DecimalType) {
42 return String.valueOf(((DecimalType) state).doubleValue());
44 if (state instanceof PointType) {
45 PointType pType = (PointType) state;
46 return String.format(Locale.ENGLISH, "%f;%f;%f", pType.getLatitude().doubleValue(),
47 pType.getLongitude().doubleValue(), pType.getAltitude().doubleValue());
50 return state.toString();