|
41 | 41 | import org.openhab.core.persistence.QueryablePersistenceService;
|
42 | 42 | import org.openhab.core.types.State;
|
43 | 43 | import org.openhab.core.types.TimeSeries;
|
| 44 | +import org.openhab.core.types.TypeParser; |
44 | 45 | import org.osgi.service.component.annotations.Activate;
|
45 | 46 | import org.osgi.service.component.annotations.Component;
|
46 | 47 | import org.osgi.service.component.annotations.Reference;
|
@@ -147,6 +148,42 @@ private static void internalPersist(Item item, ZonedDateTime timestamp, State st
|
147 | 148 | .warn("There is no modifiable persistence service registered with the id '{}'", effectiveServiceId);
|
148 | 149 | }
|
149 | 150 |
|
| 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 | + |
150 | 187 | /**
|
151 | 188 | * Persists a <code>timeSeries</code> of an <code>item</code> through the default persistence service.
|
152 | 189 | *
|
|
0 commit comments