Skip to content

Commit 66177cc

Browse files
authored
Added support for TimeSeries on Offset-Profile (#5119)
Signed-off-by: Christoph Weitkamp <[email protected]>
1 parent e39eb5d commit 66177cc

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/profiles/SystemOffsetProfile.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import org.openhab.core.thing.profiles.ProfileCallback;
2424
import org.openhab.core.thing.profiles.ProfileContext;
2525
import org.openhab.core.thing.profiles.ProfileTypeUID;
26-
import org.openhab.core.thing.profiles.StateProfile;
2726
import org.openhab.core.thing.profiles.SystemProfiles;
27+
import org.openhab.core.thing.profiles.TimeSeriesProfile;
2828
import org.openhab.core.types.Command;
2929
import org.openhab.core.types.State;
30+
import org.openhab.core.types.TimeSeries;
3031
import org.openhab.core.types.Type;
3132
import org.openhab.core.types.UnDefType;
3233
import org.slf4j.Logger;
@@ -38,7 +39,7 @@
3839
* @author Stefan Triller - Initial contribution
3940
*/
4041
@NonNullByDefault
41-
public class SystemOffsetProfile implements StateProfile {
42+
public class SystemOffsetProfile implements TimeSeriesProfile {
4243

4344
static final String OFFSET_PARAM = "offset";
4445

@@ -94,6 +95,14 @@ public void onStateUpdateFromHandler(State state) {
9495
callback.sendUpdate((State) applyOffset(state, true));
9596
}
9697

98+
@Override
99+
public void onTimeSeriesFromHandler(TimeSeries timeSeries) {
100+
TimeSeries transformedTimeSeries = new TimeSeries(timeSeries.getPolicy());
101+
timeSeries.getStates().forEach(
102+
entry -> transformedTimeSeries.add(entry.timestamp(), (State) applyOffset(entry.state(), true)));
103+
callback.sendTimeSeries(transformedTimeSeries);
104+
}
105+
97106
@SuppressWarnings({ "rawtypes", "unchecked" })
98107
private Type applyOffset(Type state, boolean towardsItem) {
99108
if (state instanceof UnDefType) {

bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/profiles/SystemOffsetProfileTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import static org.hamcrest.CoreMatchers.is;
1616
import static org.hamcrest.MatcherAssert.assertThat;
1717
import static org.hamcrest.number.IsCloseTo.closeTo;
18-
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
2020

21+
import java.time.Instant;
22+
2123
import javax.measure.Unit;
2224
import javax.measure.quantity.Temperature;
2325

@@ -35,6 +37,9 @@
3537
import org.openhab.core.thing.profiles.ProfileContext;
3638
import org.openhab.core.types.Command;
3739
import org.openhab.core.types.State;
40+
import org.openhab.core.types.TimeSeries;
41+
import org.openhab.core.types.TimeSeries.Entry;
42+
import org.openhab.core.types.TimeSeries.Policy;
3843

3944
/**
4045
* Tests for the system:offset profile
@@ -201,6 +206,29 @@ public void testQuantityTypeWithUnitOneOnStateUpdateFromHandlerDecimalOffset() {
201206
assertEquals(Units.ONE, decResult.getUnit());
202207
}
203208

209+
@Test
210+
public void testTimeSeriesFromHandler() {
211+
ProfileCallback callback = mock(ProfileCallback.class);
212+
SystemOffsetProfile offsetProfile = createProfile(callback, "3");
213+
214+
TimeSeries ts = new TimeSeries(Policy.ADD);
215+
Instant now = Instant.now();
216+
ts.add(now, new DecimalType(23));
217+
218+
offsetProfile.onTimeSeriesFromHandler(ts);
219+
220+
ArgumentCaptor<TimeSeries> capture = ArgumentCaptor.forClass(TimeSeries.class);
221+
verify(callback, times(1)).sendTimeSeries(capture.capture());
222+
223+
TimeSeries result = capture.getValue();
224+
assertEquals(ts.getStates().count(), result.getStates().count());
225+
Entry entry = result.getStates().findFirst().get();
226+
assertNotNull(entry);
227+
assertEquals(now, entry.timestamp());
228+
DecimalType decResult = (DecimalType) entry.state();
229+
assertEquals(26, decResult.intValue());
230+
}
231+
204232
private SystemOffsetProfile createProfile(ProfileCallback callback, String offset) {
205233
ProfileContext context = mock(ProfileContext.class);
206234
Configuration config = new Configuration();

0 commit comments

Comments
 (0)