|
|
@@ -5,6 +5,7 @@ import com.emoon.ai.device.api.DeviceRegisterResult;
|
|
|
import com.emoon.ai.device.api.DeviceRegistryFacade;
|
|
|
import com.emoon.ai.device.api.SceneProfileResult;
|
|
|
import com.emoon.ai.device.application.DeviceCommandService;
|
|
|
+import com.emoon.ai.device.application.DeviceEventService;
|
|
|
import com.emoon.common.core.domain.R;
|
|
|
import com.emoon.openplatform.auth.AuthContextHolder;
|
|
|
import com.emoon.openplatform.auth.OpenPlatformAuthContext;
|
|
|
@@ -14,6 +15,9 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.format.DateTimeParseException;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
@@ -27,6 +31,7 @@ public class DeviceController {
|
|
|
|
|
|
private final DeviceRegistryFacade deviceRegistryFacade;
|
|
|
private final DeviceCommandService deviceCommandService;
|
|
|
+ private final DeviceEventService deviceEventService;
|
|
|
|
|
|
@PostMapping("/register")
|
|
|
public R<DeviceRegisterResult> register(
|
|
|
@@ -104,4 +109,36 @@ public class DeviceController {
|
|
|
result != null ? result : Map.of());
|
|
|
return R.ok(Map.of("acknowledged", "true"));
|
|
|
}
|
|
|
+
|
|
|
+ @PostMapping("/{deviceId}/events")
|
|
|
+ public R<Map<String, String>> ingestEvent(
|
|
|
+ HttpServletRequest httpRequest,
|
|
|
+ @PathVariable String deviceId,
|
|
|
+ @RequestBody Map<String, Object> body) {
|
|
|
+ OpenPlatformAuthContext auth = AuthContextHolder.require(httpRequest);
|
|
|
+ if (deviceRegistryFacade.scene(String.valueOf(auth.getProjectId()), deviceId) == null)
|
|
|
+ return R.fail("DEVICE_NOT_FOUND");
|
|
|
+
|
|
|
+ String eventId = (String) body.getOrDefault("eventId",
|
|
|
+ "evt_" + java.util.UUID.randomUUID().toString().substring(0, 12));
|
|
|
+ String eventType = (String) body.getOrDefault("eventType", "CUSTOM");
|
|
|
+ String traceId = (String) body.getOrDefault("traceId", null);
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> eventPayload = (Map<String, Object>) body.get("eventPayload");
|
|
|
+
|
|
|
+ LocalDateTime occurredAt;
|
|
|
+ try {
|
|
|
+ String occurredAtStr = (String) body.get("occurredAt");
|
|
|
+ occurredAt = occurredAtStr != null
|
|
|
+ ? LocalDateTime.parse(occurredAtStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
|
|
+ : LocalDateTime.now();
|
|
|
+ } catch (DateTimeParseException e) {
|
|
|
+ occurredAt = LocalDateTime.now();
|
|
|
+ }
|
|
|
+
|
|
|
+ deviceEventService.ingest(deviceId, String.valueOf(auth.getProjectId()),
|
|
|
+ eventId, eventType, eventPayload, occurredAt, traceId);
|
|
|
+ return R.ok(Map.of("accepted", "true", "eventId", eventId));
|
|
|
+ }
|
|
|
}
|