Group.lastValueChange, Property.lastValueChange

This commit is contained in:
Patrick Haßel 2024-11-21 13:33:13 +01:00
parent 201cfd9342
commit cd84f25577
9 changed files with 47 additions and 21 deletions

View File

@ -1,4 +1,4 @@
import {orNull, validateString} from '../common/validators';
import {orNull, validateDateOrNull, validateString} from '../common/validators';
import {State} from '../State/State';
export class Group {
@ -9,6 +9,7 @@ export class Group {
readonly description: string,
readonly dpt: string,
readonly state: State | null,
readonly lastValueChange: Date | null,
) {
// -
}
@ -20,6 +21,7 @@ export class Group {
validateString(json.description),
validateString(json.dpt),
orNull(json.state, State.fromJson),
validateDateOrNull(json.lastValueChange),
);
}

View File

@ -1,13 +1,13 @@
import {State} from "../State/State";
import {orNull, validateString} from "../common/validators";
import {orNull, validateDateOrNull, validateString} from "../common/validators";
export class Property {
constructor(
readonly id: string,
readonly type: string,
readonly lastState: State | null,
readonly state: State | null,
readonly lastValueChange: Date | null,
) {
// -
}
@ -16,8 +16,8 @@ export class Property {
return new Property(
validateString(json.id),
validateString(json.type),
orNull(json.lastState, State.fromJson),
orNull(json.state, State.fromJson),
validateDateOrNull(json.lastValueChange),
);
}

View File

@ -15,6 +15,7 @@
<div class="timestamp">
{{ device.stateProperty?.state?.timestamp | relative:now }}
{{ device.stateProperty?.lastValueChange | relative:now }}
</div>
</div>

View File

@ -23,7 +23,7 @@
</div>
<div class="stackRight timestamp">
{{ group.state?.timestamp | relative:now }}:
{{ group.lastValueChange | relative:now }}:
</div>
</div>

View File

@ -5,10 +5,10 @@ import de.ph87.home.property.State;
import jakarta.annotation.Nullable;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import tuwien.auto.calimero.GroupAddress;
import java.time.ZonedDateTime;
import java.util.Objects;
@Getter
@ -35,10 +35,12 @@ public class Group {
@ToString.Exclude
private long puid;
@Setter
@Nullable
private State<?> state;
@Nullable
private ZonedDateTime lastValueChange = null;
public Group(@NonNull final String id, @NonNull final GroupAddress address, @NonNull final String name, @NonNull final String description, @NonNull final DPT dpt, final long puid) {
this.id = id;
this.address = address;
@ -48,6 +50,13 @@ public class Group {
this.puid = puid;
}
public void setState(@NonNull final State<?> newState) {
if (newState.valueChanged(this.state)) {
lastValueChange = ZonedDateTime.now();
}
this.state = newState;
}
@Override
public boolean equals(final Object o) {
if (this == o) {

View File

@ -5,9 +5,9 @@ import de.ph87.home.web.IWebSocketMessage;
import jakarta.annotation.Nullable;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import java.time.ZonedDateTime;
import java.util.List;
@Getter
@ -36,9 +36,11 @@ public class GroupDto implements IWebSocketMessage {
@ToString.Exclude
private final long puid;
@Setter
@Nullable
private State<?> state;
private final State<?> state;
@Nullable
private final ZonedDateTime lastValueChange;
public GroupDto(@NonNull final Group group) {
this.id = group.getId();
@ -48,6 +50,7 @@ public class GroupDto implements IWebSocketMessage {
this.dpt = group.getDpt().toString();
this.puid = group.getPuid();
this.state = group.getState();
this.lastValueChange = group.getLastValueChange();
}
}

View File

@ -6,6 +6,7 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import java.time.ZonedDateTime;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@ -43,16 +44,17 @@ public class Property<T> implements IProperty<T> {
@ToString.Exclude
private final Consumer<Property<T>> onStateSet;
@Nullable
@ToString.Exclude
private State<T> lastState = null;
@Nullable
private State<T> state = null;
public void update(@Nullable final State<T> state) {
this.lastState = this.state;
this.state = state;
@Nullable
private ZonedDateTime lastValueChange = null;
public void update(@NonNull final State<T> newState) {
if (newState.valueChanged(this.state)) {
lastValueChange = ZonedDateTime.now();
}
this.state = newState;
this.onStateSet.accept(this);
}

View File

@ -5,6 +5,8 @@ import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import java.time.ZonedDateTime;
@Getter
@ToString
public class PropertyDto<T> implements IProperty<T> {
@ -22,17 +24,16 @@ public class PropertyDto<T> implements IProperty<T> {
}
@Nullable
@ToString.Exclude
private final State<T> lastState;
private final State<T> state;
@Nullable
private final State<T> state;
private final ZonedDateTime lastValueChange;
public PropertyDto(@NonNull final Property<T> property) {
this.id = property.getId();
this.type = property.getType();
this.state = property.getState();
this.lastState = property.getLastState();
this.lastValueChange = property.getLastValueChange();
}
}

View File

@ -7,6 +7,7 @@ import lombok.NonNull;
import lombok.ToString;
import java.time.ZonedDateTime;
import java.util.Objects;
@Getter
@ToString
@ -32,4 +33,11 @@ public class State<T> {
this.string = string;
}
public boolean valueChanged(@Nullable final State<?> oldState) {
if (oldState == null) {
return true;
}
return !Objects.equals(oldState.value, value);
}
}