Skip to content

Commit

Permalink
change motionSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKoepke committed Feb 22, 2025
1 parent f1c9df1 commit e9c74fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.akop.homesystem.models.devices.sensor.MotionSensor;
import ch.akop.homesystem.models.devices.sensor.Sensor;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import lombok.Data;

Expand All @@ -11,6 +12,7 @@ public class SensorDto {
private String id;
private String name;
private ZonedDateTime lastUpdate;
private LocalDateTime presenceChangedAt;
private boolean reachable;
private boolean presence;

Expand All @@ -25,6 +27,7 @@ public static SensorDto from(Sensor<?> sensor) {

public static SensorDto from(MotionSensor motionSensor) {
return from((Sensor<?>) motionSensor)
.setPresence(motionSensor.getIsMoving$().blockingFirst());
.setPresenceChangedAt(motionSensor.getMovingChangedAt())
.setPresence(motionSensor.isMoving());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.akop.homesystem.deconz.rest.State;
import io.reactivex.rxjava3.subjects.ReplaySubject;
import io.reactivex.rxjava3.subjects.Subject;
import java.time.LocalDateTime;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand All @@ -18,16 +19,40 @@ public class MotionSensor extends Sensor<MotionSensor> {
private final Subject<Integer> targetDistance$ = ReplaySubject.createWithSize(1);
private final boolean offersDistance;

private boolean moving;
private boolean dark;
private Integer targetDistance;
private LocalDateTime movingChangedAt;

@EqualsAndHashCode.Exclude
@ToString.Exclude
private LightLevel lightLevel;

@Override
protected void consumeInternalUpdate(State update) {
isMoving$.onNext(update.getPresence());
isDark$.onNext(update.getDark() != null && update.getDark());
if (update.getTargetdistance() != null) {
targetDistance$.onNext(update.getTargetdistance());
setMoving(update.getPresence());
setDark(update.getDark() != null && update.getDark());
setTargetDistance(targetDistance);
}

private void setMoving(boolean moving) {
if (moving == this.moving) {
return;
}
this.movingChangedAt = LocalDateTime.now();
this.moving = moving;
isMoving$.onNext(moving);
}

private void setDark(boolean dark) {
isDark$.onNext(dark);
this.dark = dark;
}

private void setTargetDistance(Integer targetDistance) {
this.targetDistance = targetDistance;
if (targetDistance != null) {
targetDistance$.onNext(targetDistance);
}
}
}

0 comments on commit e9c74fc

Please sign in to comment.