| 1 | package org.sanmibuh.tedee.lock.infrastructure.primary; | |
| 2 | ||
| 3 | import static org.sanmibuh.tedee.lock.infrastructure.TedeeInfrastructureConfiguration.*; | |
| 4 | import static org.springframework.http.HttpStatus.*; | |
| 5 | ||
| 6 | import lombok.RequiredArgsConstructor; | |
| 7 | import lombok.extern.slf4j.Slf4j; | |
| 8 | import org.sanmibuh.ddd.domain.DomainException; | |
| 9 | import org.sanmibuh.ddd.port.CommandBus; | |
| 10 | import org.sanmibuh.tedee.lock.application.ReportLockStatusCommand; | |
| 11 | import org.springframework.context.annotation.ImportRuntimeHints; | |
| 12 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 13 | import org.springframework.web.bind.annotation.PostMapping; | |
| 14 | import org.springframework.web.bind.annotation.RequestBody; | |
| 15 | import org.springframework.web.bind.annotation.ResponseStatus; | |
| 16 | import org.springframework.web.bind.annotation.RestController; | |
| 17 | import tools.jackson.databind.JsonNode; | |
| 18 | import tools.jackson.databind.json.JsonMapper; | |
| 19 | ||
| 20 | @Slf4j | |
| 21 | @RestController | |
| 22 | @RequiredArgsConstructor | |
| 23 | @ImportRuntimeHints(TedeeEventRuntimeHints.class) | |
| 24 | final class TedeeEventController { | |
| 25 | ||
| 26 | private static final String LOCK_STATUS_CHANGED = "lock-status-changed"; | |
| 27 | ||
| 28 | private final CommandBus commandBus; | |
| 29 | private final JsonMapper jsonMapper; | |
| 30 | ||
| 31 | @PostMapping(EVENTS_PATH) | |
| 32 | @ResponseStatus(NO_CONTENT) | |
| 33 | void receive(@RequestBody final TedeeEvent event) { | |
| 34 |
1
1. receive : negated conditional → KILLED |
if (LOCK_STATUS_CHANGED.equals(event.event())) { |
| 35 | final var data = jsonMapper.treeToValue(event.data(), LockStatusChangedData.class); | |
| 36 |
1
1. receive : removed call to org/sanmibuh/ddd/port/CommandBus::dispatch → KILLED |
commandBus.dispatch( |
| 37 | new ReportLockStatusCommand( | |
| 38 | data.deviceId(), data.state(), data.jammed(), data.doorState())); | |
| 39 | } else { | |
| 40 | log.warn("Ignoring unknown Tedee Bridge event {}", event.event()); | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | @ExceptionHandler(DomainException.class) | |
| 45 | @ResponseStatus(NO_CONTENT) | |
| 46 | void handleDomainException(final DomainException exception) { | |
| 47 | log.warn("Ignoring rejected Tedee Bridge event: {}", exception.getMessage()); | |
| 48 | } | |
| 49 | ||
| 50 | record TedeeEvent(String event, JsonNode data) {} | |
| 51 | ||
| 52 | record LockStatusChangedData(int deviceId, int state, int jammed, int doorState) {} | |
| 53 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 36 |
1.1 |