2 * Copyright (c) 2010-2022 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.ArrayList;
20 import java.util.List;
22 import org.openhab.core.items.Item;
23 import org.openhab.core.library.items.ContactItem;
24 import org.openhab.core.library.items.DateTimeItem;
25 import org.openhab.core.library.items.DimmerItem;
26 import org.openhab.core.library.items.LocationItem;
27 import org.openhab.core.library.items.NumberItem;
28 import org.openhab.core.library.items.RollershutterItem;
29 import org.openhab.core.library.items.SwitchItem;
30 import org.openhab.core.library.types.DateTimeType;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.OpenClosedType;
34 import org.openhab.core.library.types.PercentType;
35 import org.openhab.core.library.types.PointType;
36 import org.openhab.core.library.types.StringListType;
37 import org.openhab.core.library.types.StringType;
38 import org.openhab.core.persistence.HistoricItem;
39 import org.openhab.core.types.State;
40 import org.openhab.persistence.jpa.internal.model.JpaPersistentItem;
43 * The historic item as returned when querying the service.
45 * @author Manfred Bergmann - Initial contribution
48 public class JpaHistoricItem implements HistoricItem {
50 private final String name;
51 private final State state;
52 private final ZonedDateTime timestamp;
54 public JpaHistoricItem(String name, State state, ZonedDateTime timestamp) {
57 this.timestamp = timestamp;
61 public String getName() {
66 public ZonedDateTime getTimestamp() {
71 public State getState() {
76 public String toString() {
77 return DateFormat.getDateTimeInstance().format(timestamp) + ": " + name + " -> " + state.toString();
81 * This method maps a jpa result item to this historic item.
83 * @param jpaQueryResult the result which jpa items
84 * @param item used for query information, like the state (State)
85 * @return list of historic items
87 public static List<HistoricItem> fromResultList(List<JpaPersistentItem> jpaQueryResult, Item item) {
88 List<HistoricItem> ret = new ArrayList<>();
89 for (JpaPersistentItem i : jpaQueryResult) {
90 HistoricItem hi = fromPersistedItem(i, item);
97 * Converts the string value of the persisted item to the state of a HistoricItem.
99 * @param pItem the persisted JpaPersistentItem
100 * @param item the source reference Item
101 * @return historic item
103 public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
105 if (item instanceof NumberItem) {
106 state = new DecimalType(Double.valueOf(pItem.getValue()));
107 } else if (item instanceof DimmerItem) {
108 state = new PercentType(Integer.valueOf(pItem.getValue()));
109 } else if (item instanceof SwitchItem) {
110 state = OnOffType.valueOf(pItem.getValue());
111 } else if (item instanceof ContactItem) {
112 state = OpenClosedType.valueOf(pItem.getValue());
113 } else if (item instanceof RollershutterItem) {
114 state = PercentType.valueOf(pItem.getValue());
115 } else if (item instanceof DateTimeItem) {
116 state = new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.valueOf(pItem.getValue())),
117 ZoneId.systemDefault()));
118 } else if (item instanceof LocationItem) {
119 PointType pType = null;
120 String[] comps = pItem.getValue().split(";");
121 if (comps.length >= 2) {
122 pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
124 if (comps.length == 3) {
125 pType.setAltitude(new DecimalType(comps[2]));
129 } else if (item instanceof StringListType) {
130 state = new StringListType(pItem.getValue());
132 state = new StringType(pItem.getValue());
135 return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());