Merge branch 'feature/device-service' of https://github.com/jazzm0/microservices-demo into feature/device-service

This commit is contained in:
Elisa Stelling 2019-12-11 11:47:52 +01:00
commit d3c49e83ca
4 changed files with 38 additions and 22 deletions

View file

@ -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;
}
}

View file

@ -65,6 +65,8 @@ public class Device {
healthScore += delta; healthScore += delta;
if (healthScore > 150) if (healthScore > 150)
healthScore /= 10; healthScore /= 10;
} else {
healthScore = 0;
} }
messages.add(new DeviceEvent(id, owner, color, born, healthScore, Instant.now())); messages.add(new DeviceEvent(id, owner, color, born, healthScore, Instant.now()));
} }

View file

@ -4,7 +4,9 @@
package com.sap.tamagotchi.model; package com.sap.tamagotchi.model;
import static com.sap.tamagotchi.service.TamagotchiService.DEVICE_EVENT_PROCESSOR_SCHEDULE; import static com.sap.tamagotchi.service.TamagotchiService.DEVICE_EVENT_PROCESSOR_SCHEDULE;
import java.util.Collection; import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
@ -52,7 +54,7 @@ public class Owner {
Collection<Device> devices = tamagotchiService.getDevices(); Collection<Device> devices = tamagotchiService.getDevices();
if (devices != null) { if (devices != null && devices.iterator().hasNext()) {
Device first = devices.iterator().next(); Device first = devices.iterator().next();
Care care = new Care(); Care care = new Care();
care.setFeed(-100000); care.setFeed(-100000);

View file

@ -14,10 +14,9 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sap.tamagotchi.model.Care; import com.sap.tamagotchi.model.Care;
import com.sap.tamagotchi.model.DefunctNotification;
import com.sap.tamagotchi.model.Device; import com.sap.tamagotchi.model.Device;
import com.sap.tamagotchi.model.IoTMessage;
import com.sap.tamagotchi.publisher.PublisherService; import com.sap.tamagotchi.publisher.PublisherService;
@Service @Service
@ -85,8 +84,8 @@ public class TamagotchiService {
.parallelStream() .parallelStream()
.filter(device -> !device.isAlive()) .filter(device -> !device.isAlive())
.forEach(device -> { .forEach(device -> {
deviceRegistry.remove(device.getId());
sendTamagotchiDefunctNotifiction(device.getId()); sendTamagotchiDefunctNotifiction(device.getId());
deviceRegistry.remove(device.getId());
LOGGER.info("{} has died", device.getId()); LOGGER.info("{} has died", device.getId());
}); });
} }
@ -94,25 +93,13 @@ public class TamagotchiService {
private void sendTamagotchiDefunctNotifiction(String id) { private void sendTamagotchiDefunctNotifiction(String id) {
Device device = deviceRegistry.get(id); Device device = deviceRegistry.get(id);
if (device == null) {
IoTMessage m = new IoTMessage() { return;
@JsonProperty("message")
private String message = String.format("Tamagotchi %s of %s passed away", device.getOwner(), device.getOwner());
@Override
public String getTopic() {
return "tamagotchi-defunct";
} }
String defunctMessage = String.format("Tamagotchi %s of %s passed away", device.getId(), device.getOwner());
public String getMessage() { DefunctNotification defunctNotification = new DefunctNotification(defunctMessage);
return message;
}
};
try { try {
publisherService.publish(m); publisherService.publish(defunctNotification);
LOGGER.info("defunct notification sent for {}", device.getId()); LOGGER.info("defunct notification sent for {}", device.getId());
} catch (Exception ex) { } catch (Exception ex) {
LOGGER.error("sendTamagotchiDefunctNotifiction failed: {}", ex.getMessage()); LOGGER.error("sendTamagotchiDefunctNotifiction failed: {}", ex.getMessage());