Merge branch 'feature/device-service' of https://github.com/jazzm0/microservices-demo into feature/device-service
This commit is contained in:
commit
d3c49e83ca
4 changed files with 38 additions and 22 deletions
|
@ -0,0 +1,25 @@
|
|||
package com.sap.tamagotchi.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class DefunctNotification implements IoTMessage {
|
||||
|
||||
@JsonProperty("message")
|
||||
private final String message;
|
||||
|
||||
public DefunctNotification(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
@Override
|
||||
public String getTopic() {
|
||||
return "tamagotchi-defunct";
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
|
@ -65,6 +65,8 @@ public class Device {
|
|||
healthScore += delta;
|
||||
if (healthScore > 150)
|
||||
healthScore /= 10;
|
||||
} else {
|
||||
healthScore = 0;
|
||||
}
|
||||
messages.add(new DeviceEvent(id, owner, color, born, healthScore, Instant.now()));
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
package com.sap.tamagotchi.model;
|
||||
|
||||
import static com.sap.tamagotchi.service.TamagotchiService.DEVICE_EVENT_PROCESSOR_SCHEDULE;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
@ -52,7 +54,7 @@ public class Owner {
|
|||
|
||||
Collection<Device> devices = tamagotchiService.getDevices();
|
||||
|
||||
if (devices != null) {
|
||||
if (devices != null && devices.iterator().hasNext()) {
|
||||
Device first = devices.iterator().next();
|
||||
Care care = new Care();
|
||||
care.setFeed(-100000);
|
||||
|
|
|
@ -14,10 +14,9 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
|||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.sap.tamagotchi.model.Care;
|
||||
import com.sap.tamagotchi.model.DefunctNotification;
|
||||
import com.sap.tamagotchi.model.Device;
|
||||
import com.sap.tamagotchi.model.IoTMessage;
|
||||
import com.sap.tamagotchi.publisher.PublisherService;
|
||||
|
||||
@Service
|
||||
|
@ -85,8 +84,8 @@ public class TamagotchiService {
|
|||
.parallelStream()
|
||||
.filter(device -> !device.isAlive())
|
||||
.forEach(device -> {
|
||||
deviceRegistry.remove(device.getId());
|
||||
sendTamagotchiDefunctNotifiction(device.getId());
|
||||
deviceRegistry.remove(device.getId());
|
||||
LOGGER.info("{} has died", device.getId());
|
||||
});
|
||||
}
|
||||
|
@ -94,25 +93,13 @@ public class TamagotchiService {
|
|||
private void sendTamagotchiDefunctNotifiction(String id) {
|
||||
|
||||
Device device = deviceRegistry.get(id);
|
||||
|
||||
IoTMessage m = new IoTMessage() {
|
||||
|
||||
@JsonProperty("message")
|
||||
private String message = String.format("Tamagotchi %s of %s passed away", device.getOwner(), device.getOwner());
|
||||
|
||||
@Override
|
||||
public String getTopic() {
|
||||
return "tamagotchi-defunct";
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
if (device == null) {
|
||||
return;
|
||||
}
|
||||
String defunctMessage = String.format("Tamagotchi %s of %s passed away", device.getId(), device.getOwner());
|
||||
DefunctNotification defunctNotification = new DefunctNotification(defunctMessage);
|
||||
try {
|
||||
publisherService.publish(m);
|
||||
publisherService.publish(defunctNotification);
|
||||
LOGGER.info("defunct notification sent for {}", device.getId());
|
||||
} catch (Exception ex) {
|
||||
LOGGER.error("sendTamagotchiDefunctNotifiction failed: {}", ex.getMessage());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue