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 runtime: java11
entrypoint: java -jar target/tamagotchi-service-0.1.0.jar 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; package com.sap.tamagotchi.controller;
import com.sap.tamagotchi.model.CreateDevicePayload; import static org.springframework.http.ResponseEntity.ok;
import com.sap.tamagotchi.model.Device;
import com.sap.tamagotchi.service.TamagotchiService; import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList; import com.sap.tamagotchi.model.CreateDevicePayload;
import java.util.Collection; import com.sap.tamagotchi.model.Device;
import java.util.List; import com.sap.tamagotchi.service.TamagotchiService;
import static org.springframework.http.ResponseEntity.ok;
@RestController @RestController
public class DeviceController { public class DeviceController {
@ -44,4 +45,10 @@ public class DeviceController {
} }
return ok(devices); return ok(devices);
} }
@RequestMapping("/_ah/warmup")
public String warmup() {
return "warming up";
}
} }

View file

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

View file

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

View file

@ -14,8 +14,10 @@ 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.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
@ -82,7 +84,37 @@ public class TamagotchiService {
.values() .values()
.parallelStream() .parallelStream()
.filter(device -> !device.isAlive()) .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());
}
} }
} }