]> git.basschouten.com Git - openhab-addons.git/blob
eaee4bb8caadb7f23c6288c592c3c925d5cf247b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.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;
21
22 import javax.measure.Unit;
23
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;
46
47 /**
48  * The historic item as returned when querying the service.
49  *
50  * @author Manfred Bergmann - Initial contribution
51  *
52  */
53 @NonNullByDefault
54 public class JpaHistoricItem implements HistoricItem {
55
56     private final String name;
57     private final State state;
58     private final ZonedDateTime timestamp;
59
60     public JpaHistoricItem(String name, State state, ZonedDateTime timestamp) {
61         this.name = name;
62         this.state = state;
63         this.timestamp = timestamp;
64     }
65
66     @Override
67     public String getName() {
68         return name;
69     }
70
71     @Override
72     public ZonedDateTime getTimestamp() {
73         return timestamp;
74     }
75
76     @Override
77     public State getState() {
78         return state;
79     }
80
81     @Override
82     public String toString() {
83         return DateFormat.getDateTimeInstance().format(timestamp) + ": " + name + " -> " + state.toString();
84     }
85
86     /**
87      * This method maps {@link JpaPersistentItem}s to {@link HistoricItem}s.
88      *
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
92      */
93     public static List<HistoricItem> fromResultList(List<JpaPersistentItem> jpaQueryResult, Item item) {
94         return jpaQueryResult.stream().map(pItem -> fromPersistedItem(pItem, item)).collect(Collectors.toList());
95     }
96
97     /**
98      * Converts the string value of the persisted item to the state of a {@link HistoricItem}.
99      *
100      * @param pItem the persisted {@link JpaPersistentItem}
101      * @param item the source reference Item
102      * @return historic item
103      */
104     public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
105         State state;
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]));
126
127                 if (comps.length == 3) {
128                     pType.setAltitude(new DecimalType(comps[2]));
129                 }
130             }
131             state = pType == null ? UnDefType.UNDEF : pType;
132         } else if (item instanceof StringListType) {
133             state = new StringListType(pItem.getValue());
134         } else {
135             state = new StringType(pItem.getValue());
136         }
137
138         return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
139     }
140 }