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.persistence.jpa.internal;
15 import java.text.DateFormat;
16 import java.time.Instant;
17 import java.time.ZoneId;
18 import java.time.ZonedDateTime;
19 import java.util.List;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.core.items.Item;
24 import org.openhab.core.library.items.ContactItem;
25 import org.openhab.core.library.items.DateTimeItem;
26 import org.openhab.core.library.items.DimmerItem;
27 import org.openhab.core.library.items.LocationItem;
28 import org.openhab.core.library.items.NumberItem;
29 import org.openhab.core.library.items.RollershutterItem;
30 import org.openhab.core.library.items.SwitchItem;
31 import org.openhab.core.library.types.DateTimeType;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.library.types.OpenClosedType;
35 import org.openhab.core.library.types.PercentType;
36 import org.openhab.core.library.types.PointType;
37 import org.openhab.core.library.types.StringListType;
38 import org.openhab.core.library.types.StringType;
39 import org.openhab.core.persistence.HistoricItem;
40 import org.openhab.core.types.State;
41 import org.openhab.core.types.UnDefType;
42 import org.openhab.persistence.jpa.internal.model.JpaPersistentItem;
45 * The historic item as returned when querying the service.
47 * @author Manfred Bergmann - Initial contribution
51 public class JpaHistoricItem implements HistoricItem {
53 private final String name;
54 private final State state;
55 private final ZonedDateTime timestamp;
57 public JpaHistoricItem(String name, State state, ZonedDateTime timestamp) {
60 this.timestamp = timestamp;
64 public String getName() {
69 public ZonedDateTime getTimestamp() {
74 public State getState() {
79 public String toString() {
80 return DateFormat.getDateTimeInstance().format(timestamp) + ": " + name + " -> " + state.toString();
84 * This method maps {@link JpaPersistentItem}s to {@link HistoricItem}s.
86 * @param jpaQueryResult the result with jpa items
87 * @param item used for query information, like the state (State)
88 * @return list of historic items
90 public static List<HistoricItem> fromResultList(List<JpaPersistentItem> jpaQueryResult, Item item) {
91 return jpaQueryResult.stream().map(pItem -> fromPersistedItem(pItem, item)).collect(Collectors.toList());
95 * Converts the string value of the persisted item to the state of a {@link HistoricItem}.
97 * @param pItem the persisted {@link JpaPersistentItem}
98 * @param item the source reference Item
99 * @return historic item
101 public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
103 if (item instanceof NumberItem) {
104 state = new DecimalType(Double.valueOf(pItem.getValue()));
105 } else if (item instanceof DimmerItem) {
106 state = new PercentType(Integer.parseInt(pItem.getValue()));
107 } else if (item instanceof SwitchItem) {
108 state = OnOffType.valueOf(pItem.getValue());
109 } else if (item instanceof ContactItem) {
110 state = OpenClosedType.valueOf(pItem.getValue());
111 } else if (item instanceof RollershutterItem) {
112 state = PercentType.valueOf(pItem.getValue());
113 } else if (item instanceof DateTimeItem) {
114 state = new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(pItem.getValue())),
115 ZoneId.systemDefault()));
116 } else if (item instanceof LocationItem) {
117 PointType pType = null;
118 String[] comps = pItem.getValue().split(";");
119 if (comps.length >= 2) {
120 pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
122 if (comps.length == 3) {
123 pType.setAltitude(new DecimalType(comps[2]));
126 state = pType == null ? UnDefType.UNDEF : pType;
127 } else if (item instanceof StringListType) {
128 state = new StringListType(pItem.getValue());
130 state = new StringType(pItem.getValue());
133 return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());