This commit is contained in:
Elisa Stelling 2019-12-11 10:30:42 +01:00
commit 81e3545234
5 changed files with 61 additions and 15 deletions

View file

@ -1,2 +1,8 @@
runtime: java11
entrypoint: java -jar target/tamagotchi-service-0.1.0.jar
inbound_services:
- warmup
automatic_scaling:
max_instances: 1
min_instances: 1

View file

@ -1,8 +1,11 @@
package com.sap.tamagotchi.controller;
import com.sap.tamagotchi.model.CreateDevicePayload;
import com.sap.tamagotchi.model.Device;
import com.sap.tamagotchi.service.TamagotchiService;
import static org.springframework.http.ResponseEntity.ok;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
@ -10,11 +13,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.springframework.http.ResponseEntity.ok;
import com.sap.tamagotchi.model.CreateDevicePayload;
import com.sap.tamagotchi.model.Device;
import com.sap.tamagotchi.service.TamagotchiService;
@RestController
public class DeviceController {
@ -44,4 +45,10 @@ public class DeviceController {
}
return ok(devices);
}
@RequestMapping("/_ah/warmup")
public String warmup() {
return "warming up";
}
}

View file

@ -45,8 +45,8 @@ public class Device {
}
@JsonProperty("born")
public Instant getBorn() {
return born;
public String getBorn() {
return born.toString();
}
@JsonProperty("healthScore")

View file

@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Instant;
public class DeviceEvent implements IoTMessage {
@JsonProperty("id")
@ -45,8 +46,8 @@ public class DeviceEvent implements IoTMessage {
}
@JsonProperty("born")
public Instant getBorn() {
return born;
public String getBorn() {
return born.toString();
}
@JsonProperty("healthScore")
@ -55,8 +56,8 @@ public class DeviceEvent implements IoTMessage {
}
@JsonProperty("eventTime")
public Instant getEventTime() {
return eventTime;
public String getEventTime() {
return eventTime.toString();
}
@JsonProperty("isAlive")

View file

@ -14,8 +14,10 @@ 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.Device;
import com.sap.tamagotchi.model.IoTMessage;
import com.sap.tamagotchi.publisher.PublisherService;
@Service
@ -82,7 +84,37 @@ public class TamagotchiService {
.values()
.parallelStream()
.filter(device -> !device.isAlive())
.forEach(device -> deviceRegistry.remove(device.getId()));
.forEach(device -> {
deviceRegistry.remove(device.getId());
sendTamagotchiDefunctNotifiction(device.getId());
});
}
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;
}
};
try {
publisherService.publish(m);
} catch (Exception ex) {
LOGGER.error("sendTamagotchiDefunctNotifiction failed: {}", ex.getMessage());
}
}
}