]> git.basschouten.com Git - openhab-addons.git/blob
7b198b206a82429cee370bae78ac3ae1fc271efa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.ArrayList;
20 import java.util.List;
21
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;
41
42 /**
43  * The historic item as returned when querying the service.
44  *
45  * @author Manfred Bergmann - Initial contribution
46  *
47  */
48 public class JpaHistoricItem implements HistoricItem {
49
50     private final String name;
51     private final State state;
52     private final ZonedDateTime timestamp;
53
54     public JpaHistoricItem(String name, State state, ZonedDateTime timestamp) {
55         this.name = name;
56         this.state = state;
57         this.timestamp = timestamp;
58     }
59
60     @Override
61     public String getName() {
62         return name;
63     }
64
65     @Override
66     public ZonedDateTime getTimestamp() {
67         return timestamp;
68     }
69
70     @Override
71     public State getState() {
72         return state;
73     }
74
75     @Override
76     public String toString() {
77         return DateFormat.getDateTimeInstance().format(timestamp) + ": " + name + " -> " + state.toString();
78     }
79
80     /**
81      * This method maps a jpa result item to this historic item.
82      *
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
86      */
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);
91             ret.add(hi);
92         }
93         return ret;
94     }
95
96     /**
97      * Converts the string value of the persisted item to the state of a HistoricItem.
98      *
99      * @param pItem the persisted JpaPersistentItem
100      * @param item the source reference Item
101      * @return historic item
102      */
103     public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
104         State state;
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]));
123
124                 if (comps.length == 3) {
125                     pType.setAltitude(new DecimalType(comps[2]));
126                 }
127             }
128             state = pType;
129         } else if (item instanceof StringListType) {
130             state = new StringListType(pItem.getValue());
131         } else {
132             state = new StringType(pItem.getValue());
133         }
134
135         return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
136     }
137 }