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