Skip to content

Commit f7f4b76

Browse files
authored
PersistenceExtensions: Support state as string for persist method (#4268)
Signed-off-by: Florian Hotze <[email protected]>
1 parent d092c51 commit f7f4b76

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java

+37
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.openhab.core.persistence.QueryablePersistenceService;
4242
import org.openhab.core.types.State;
4343
import org.openhab.core.types.TimeSeries;
44+
import org.openhab.core.types.TypeParser;
4445
import org.osgi.service.component.annotations.Activate;
4546
import org.osgi.service.component.annotations.Component;
4647
import org.osgi.service.component.annotations.Reference;
@@ -147,6 +148,42 @@ private static void internalPersist(Item item, ZonedDateTime timestamp, State st
147148
.warn("There is no modifiable persistence service registered with the id '{}'", effectiveServiceId);
148149
}
149150

151+
/**
152+
* Persists a <code>state</code> at a given <code>timestamp</code> of an <code>item</code> through the default
153+
* persistence service.
154+
*
155+
* @param item the item to store
156+
* @param timestamp the date for the item state to be stored
157+
* @param stateString the state to be stored
158+
*/
159+
public static void persist(Item item, ZonedDateTime timestamp, String stateString) {
160+
internalPersist(item, timestamp, stateString, null);
161+
}
162+
163+
/**
164+
* Persists a <code>state</code> at a given <code>timestamp</code> of an <code>item</code> through a
165+
* {@link PersistenceService} identified by the <code>serviceId</code>.
166+
*
167+
* @param item the item
168+
* @param timestamp the date for the item state to be stored
169+
* @param stateString the state to be stored
170+
* @param serviceId the name of the {@link PersistenceService} to use
171+
*/
172+
public static void persist(Item item, ZonedDateTime timestamp, String stateString, @Nullable String serviceId) {
173+
internalPersist(item, timestamp, stateString, serviceId);
174+
}
175+
176+
private static void internalPersist(Item item, ZonedDateTime timestamp, String stateString,
177+
@Nullable String serviceId) {
178+
State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
179+
if (state != null) {
180+
internalPersist(item, timestamp, state, serviceId);
181+
} else {
182+
LoggerFactory.getLogger(PersistenceExtensions.class).warn("State '{}' cannot be parsed for item '{}'.",
183+
stateString, item.getName());
184+
}
185+
}
186+
150187
/**
151188
* Persists a <code>timeSeries</code> of an <code>item</code> through the default persistence service.
152189
*

0 commit comments

Comments
 (0)