diff --git a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DefunctNotification.java b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DefunctNotification.java index 17fc505..1666c9a 100644 --- a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DefunctNotification.java +++ b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DefunctNotification.java @@ -1,5 +1,6 @@ package com.sap.tamagotchi.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; public class DefunctNotification implements IoTMessage { @@ -11,6 +12,7 @@ public class DefunctNotification implements IoTMessage { this.message = message; } + @JsonIgnore @Override public String getTopic() { return "tamagotchi-defunct"; diff --git a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Device.java b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Device.java index 256f3a6..58226ca 100644 --- a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Device.java +++ b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Device.java @@ -61,14 +61,18 @@ public class Device { @JsonIgnore public void changeHealthScore(int delta) { + int oldScore = healthScore; if (healthScore >= 1) { healthScore += delta; if (healthScore > 150) healthScore /= 10; - } else { - healthScore = 0; } - messages.add(new DeviceEvent(id, owner, color, born, healthScore, Instant.now())); + + if (healthScore < 1) { + healthScore = 0; + messages.add(new DeviceEvent(id, owner, color, born, healthScore, oldScore, Instant.now())); + } else + messages.add(new DeviceEvent(id, owner, color, born, healthScore, null, Instant.now())); } @JsonIgnore diff --git a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DeviceEvent.java b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DeviceEvent.java index dccd389..d757795 100644 --- a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DeviceEvent.java +++ b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/DeviceEvent.java @@ -1,6 +1,7 @@ package com.sap.tamagotchi.model; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import java.time.Instant; @@ -18,15 +19,18 @@ public class DeviceEvent implements IoTMessage { private final Instant born; @JsonProperty("healthScore") private final Integer healthScore; + @JsonProperty("lastHealthScore") + private final Integer lastHealthScore; @JsonProperty("eventTime") private final Instant eventTime; - public DeviceEvent(String id, String owner, Color color, Instant born, Integer healthScore, Instant eventTime) { + public DeviceEvent(String id, String owner, Color color, Instant born, Integer healthScore, Integer lastHealthScore, Instant eventTime) { this.id = id; this.owner = owner; this.color = color; this.born = born; this.healthScore = healthScore; + this.lastHealthScore = lastHealthScore; this.eventTime = eventTime; } @@ -55,6 +59,12 @@ public class DeviceEvent implements IoTMessage { return healthScore; } + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonProperty("lastHealthScore") + public Integer getLastHealthScore() { + return lastHealthScore; + } + @JsonProperty("eventTime") public String getEventTime() { return eventTime.toString(); diff --git a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java index b01e7cd..4f97ae8 100644 --- a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java +++ b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java @@ -49,6 +49,7 @@ public class Owner { } } + @Scheduled(fixedDelay = 30000) public void killRendomDevice() { Collection devices = tamagotchiService.getDevices();