Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PersistenceExtensions: Support state as string for persist method #4268

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -147,6 +148,42 @@ private static void internalPersist(Item item, ZonedDateTime timestamp, State st
.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.
*
Expand Down