mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 13:21:53 +01:00
PersistenceExtensions: Support state as string for persist method (#4268)
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
parent
d092c517f0
commit
f7f4b7653c
@ -41,6 +41,7 @@ import org.openhab.core.persistence.PersistenceServiceRegistry;
|
||||
import org.openhab.core.persistence.QueryablePersistenceService;
|
||||
import org.openhab.core.types.State;
|
||||
import org.openhab.core.types.TimeSeries;
|
||||
import org.openhab.core.types.TypeParser;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
@ -147,6 +148,42 @@ public class PersistenceExtensions {
|
||||
.warn("There is no modifiable persistence service registered with the id '{}'", effectiveServiceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists a <code>state</code> at a given <code>timestamp</code> of an <code>item</code> through the default
|
||||
* persistence service.
|
||||
*
|
||||
* @param item the item to store
|
||||
* @param timestamp the date for the item state to be stored
|
||||
* @param stateString the state to be stored
|
||||
*/
|
||||
public static void persist(Item item, ZonedDateTime timestamp, String stateString) {
|
||||
internalPersist(item, timestamp, stateString, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists a <code>state</code> at a given <code>timestamp</code> of an <code>item</code> through a
|
||||
* {@link PersistenceService} identified by the <code>serviceId</code>.
|
||||
*
|
||||
* @param item the item
|
||||
* @param timestamp the date for the item state to be stored
|
||||
* @param stateString the state to be stored
|
||||
* @param serviceId the name of the {@link PersistenceService} to use
|
||||
*/
|
||||
public static void persist(Item item, ZonedDateTime timestamp, String stateString, @Nullable String serviceId) {
|
||||
internalPersist(item, timestamp, stateString, serviceId);
|
||||
}
|
||||
|
||||
private static void internalPersist(Item item, ZonedDateTime timestamp, String stateString,
|
||||
@Nullable String serviceId) {
|
||||
State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
|
||||
if (state != null) {
|
||||
internalPersist(item, timestamp, state, serviceId);
|
||||
} else {
|
||||
LoggerFactory.getLogger(PersistenceExtensions.class).warn("State '{}' cannot be parsed for item '{}'.",
|
||||
stateString, item.getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists a <code>timeSeries</code> of an <code>item</code> through the default persistence service.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user