diff --git a/src/main/java/org/ioteatime/meonghanyangserver/clients/iot/IotShadowMqttClient.java b/src/main/java/org/ioteatime/meonghanyangserver/clients/iot/IotShadowMqttClient.java index f463f4e9..fc0ec880 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/clients/iot/IotShadowMqttClient.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/clients/iot/IotShadowMqttClient.java @@ -1,6 +1,9 @@ package org.ioteatime.meonghanyangserver.clients.iot; +import java.util.HashMap; import lombok.RequiredArgsConstructor; +import org.ioteatime.meonghanyangserver.common.exception.InternalServerException; +import org.ioteatime.meonghanyangserver.common.type.IoTErrorType; import org.springframework.stereotype.Service; import software.amazon.awssdk.crt.mqtt.QualityOfService; import software.amazon.awssdk.iot.iotshadow.IotShadowClient; @@ -13,15 +16,21 @@ public class IotShadowMqttClient { private final IotShadowClient iotShadowClient; public void updateShadow(String thingName, String key, boolean value) { - ShadowState shadowState = new ShadowState(); - shadowState.desired.put(key, value); - shadowState.reportedIsNullable = true; + try { + ShadowState shadowState = new ShadowState(); + shadowState.desired = new HashMap<>(); + shadowState.desired.put(key, value); + shadowState.reportedIsNullable = true; - UpdateShadowRequest updateShadowRequest = new UpdateShadowRequest(); - updateShadowRequest.state = shadowState; + UpdateShadowRequest updateShadowRequest = new UpdateShadowRequest(); + updateShadowRequest.state = shadowState; - updateShadowRequest.thingName = thingName; + updateShadowRequest.thingName = thingName; - iotShadowClient.PublishUpdateShadow(updateShadowRequest, QualityOfService.AT_LEAST_ONCE); + iotShadowClient.PublishUpdateShadow( + updateShadowRequest, QualityOfService.AT_LEAST_ONCE); + } catch (Exception e) { + throw new InternalServerException(IoTErrorType.UPDATE_SHADOW); + } } } diff --git a/src/main/java/org/ioteatime/meonghanyangserver/common/type/IoTErrorType.java b/src/main/java/org/ioteatime/meonghanyangserver/common/type/IoTErrorType.java new file mode 100644 index 00000000..3771888a --- /dev/null +++ b/src/main/java/org/ioteatime/meonghanyangserver/common/type/IoTErrorType.java @@ -0,0 +1,23 @@ +package org.ioteatime.meonghanyangserver.common.type; + +public enum IoTErrorType implements ErrorTypeCode { + UPDATE_SHADOW("INTERNAL_SERVER", "Shadow 갱신에 실패하였습니다."); + + private final String message; + private final String description; + + IoTErrorType(String message, String description) { + this.message = message; + this.description = description; + } + + @Override + public String getMessage() { + return this.message; + } + + @Override + public String getDescription() { + return this.description; + } +}