2 * Copyright (c) 2010-2024 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 javax.measure.Unit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.core.items.Item;
26 import org.openhab.core.library.items.ContactItem;
27 import org.openhab.core.library.items.DateTimeItem;
28 import org.openhab.core.library.items.DimmerItem;
29 import org.openhab.core.library.items.LocationItem;
30 import org.openhab.core.library.items.NumberItem;
31 import org.openhab.core.library.items.RollershutterItem;
32 import org.openhab.core.library.items.SwitchItem;
33 import org.openhab.core.library.types.DateTimeType;
34 import org.openhab.core.library.types.DecimalType;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.library.types.OpenClosedType;
37 import org.openhab.core.library.types.PercentType;
38 import org.openhab.core.library.types.PointType;
39 import org.openhab.core.library.types.QuantityType;
40 import org.openhab.core.library.types.StringListType;
41 import org.openhab.core.library.types.StringType;
42 import org.openhab.core.persistence.HistoricItem;
43 import org.openhab.core.types.State;
44 import org.openhab.core.types.UnDefType;
45 import org.openhab.persistence.jpa.internal.model.JpaPersistentItem;
48 * The historic item as returned when querying the service.
50 * @author Manfred Bergmann - Initial contribution
54 public class JpaHistoricItem implements HistoricItem {
56 private final String name;
57 private final State state;
58 private final ZonedDateTime timestamp;
60 public JpaHistoricItem(String name, State state, ZonedDateTime timestamp) {
63 this.timestamp = timestamp;
67 public String getName() {
72 public ZonedDateTime getTimestamp() {
77 public State getState() {
82 public String toString() {
83 return DateFormat.getDateTimeInstance().format(timestamp) + ": " + name + " -> " + state.toString();
87 * This method maps {@link JpaPersistentItem}s to {@link HistoricItem}s.
89 * @param jpaQueryResult the result with jpa items
90 * @param item used for query information, like the state (State)
91 * @return list of historic items
93 public static List<HistoricItem> fromResultList(List<JpaPersistentItem> jpaQueryResult, Item item) {
94 return jpaQueryResult.stream().map(pItem -> fromPersistedItem(pItem, item)).collect(Collectors.toList());
98 * Converts the string value of the persisted item to the state of a {@link HistoricItem}.
100 * @param pItem the persisted {@link JpaPersistentItem}
101 * @param item the source reference Item
102 * @return historic item
104 public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
106 if (item instanceof NumberItem numberItem) {
107 Unit<?> unit = numberItem.getUnit();
108 double value = Double.parseDouble(pItem.getValue());
109 state = (unit == null) ? new DecimalType(value) : new QuantityType<>(value, unit);
110 } else if (item instanceof DimmerItem) {
111 state = new PercentType(Integer.parseInt(pItem.getValue()));
112 } else if (item instanceof SwitchItem) {
113 state = OnOffType.valueOf(pItem.getValue());
114 } else if (item instanceof ContactItem) {
115 state = OpenClosedType.valueOf(pItem.getValue());
116 } else if (item instanceof RollershutterItem) {
117 state = PercentType.valueOf(pItem.getValue());
118 } else if (item instanceof DateTimeItem) {
119 state = new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(pItem.getValue())),
120 ZoneId.systemDefault()));
121 } else if (item instanceof LocationItem) {
122 PointType pType = null;
123 String[] comps = pItem.getValue().split(";");
124 if (comps.length >= 2) {
125 pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
127 if (comps.length == 3) {
128 pType.setAltitude(new DecimalType(comps[2]));
131 state = pType == null ? UnDefType.UNDEF : pType;
132 } else if (item instanceof StringListType) {
133 state = new StringListType(pItem.getValue());
135 state = new StringType(pItem.getValue());
138 return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());