diff --git a/src/main/java/com/backend/glowhouse/controller/DeviceShadowController.java b/src/main/java/com/backend/glowhouse/controller/DeviceShadowController.java index 7c7e54cd15a3dab85c6f4afb71da0a3147814a41..d2c800bb2d6a46d24da41e7221c3dc4e857583eb 100644 --- a/src/main/java/com/backend/glowhouse/controller/DeviceShadowController.java +++ b/src/main/java/com/backend/glowhouse/controller/DeviceShadowController.java @@ -1,75 +1,32 @@ package com.backend.glowhouse.controller; import com.amazonaws.services.iot.client.AWSIotException; -import com.amazonaws.services.iot.client.AWSIotMqttClient; -import com.amazonaws.services.iot.client.AWSIotTopic; -import com.backend.glowhouse.config.AwsIotUtil; -import com.backend.glowhouse.model.DeviceShadow; +import com.backend.glowhouse.model.request.ShadowUpdateRequest; +import com.backend.glowhouse.service.DeviceShadowService; import com.fasterxml.jackson.core.JsonProcessingException; -import com.ixortalk.iot.client.core.IotClient; -import com.ixortalk.iot.client.core.config.IotListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; - -import javax.inject.Inject; - -import com.ixortalk.iot.client.core.IotClient; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; - -import javax.inject.Inject; - -import static org.springframework.http.ResponseEntity.ok; import static org.springframework.web.bind.annotation.RequestMethod.POST; @RestController @RequestMapping("/api/shadow") public class DeviceShadowController { - private static final Logger logger = LoggerFactory.getLogger(DeviceShadowController.class); - - @Inject - private IotClient iotClient; - - private String endpoint = "a2atwjvz13hwor-ats.iot.eu-west-1.amazonaws.com"; - - private String clientId = "ixortalk-iot-client-001"; - - private String cert = "/Users/Luke/Desktop/Repositories/glowhouse-workspace/glowhouse-backend/glowhouse/src/main/resources/aws/cert.crt"; - - private String key = "/Users/Luke/Desktop/Repositories/glowhouse-workspace/glowhouse-backend/glowhouse/src/main/resources/aws/private.key"; + @Autowired + private DeviceShadowService deviceShadowService; @PostMapping @RequestMapping(method = POST, path = "/update") - public ResponseEntity updateShadow(@RequestBody String payload) { - iotClient.publish("$aws/things/glowhouse/shadow/update", payload); - - return ok().build(); + public HttpStatus updateShadow(@RequestBody ShadowUpdateRequest updateRequest) throws JsonProcessingException, AWSIotException { + return deviceShadowService.handleShadowUpdateRequest(updateRequest); } @GetMapping("/get") public String getDeviceShadow(@RequestParam(value = "deviceId") String deviceId) throws AWSIotException { - AwsIotUtil.KeyStorePasswordPair pair = AwsIotUtil.getKeyStorePasswordPair(cert, key, null); - - assert pair != null; - AWSIotMqttClient awsIotClient = new AWSIotMqttClient(endpoint, clientId, pair.keyStore, pair.keyPassword); - DeviceShadow deviceShadow = new DeviceShadow(deviceId); - - awsIotClient.attach(deviceShadow); - awsIotClient.connect(); - - return deviceShadow.get(); - } - - @IotListener - public void listen(Object message) { - logger.info("Consumed message: " + message); + return deviceShadowService.handleShadowGetRequest(deviceId); } - } diff --git a/src/main/java/com/backend/glowhouse/model/DeviceShadow.java b/src/main/java/com/backend/glowhouse/model/DeviceShadow.java index b0762d16216a591a99e2aec24e264de0031a6dde..c70c4263af7ab9a771721f25ea2975e9e46c8720 100644 --- a/src/main/java/com/backend/glowhouse/model/DeviceShadow.java +++ b/src/main/java/com/backend/glowhouse/model/DeviceShadow.java @@ -10,6 +10,18 @@ public class DeviceShadow extends AWSIotDevice{ @AWSIotDeviceProperty private boolean pump; + @AWSIotDeviceProperty + private float humidity; + + @AWSIotDeviceProperty + private float temp; + + @AWSIotDeviceProperty + private float ph; + + @AWSIotDeviceProperty + private float waterLevel; + public DeviceShadow(String thingName) { super(thingName); } @@ -29,4 +41,36 @@ public class DeviceShadow extends AWSIotDevice{ public void setPump(boolean pump) { this.pump = pump; } + + public float getHumidity() { + return humidity; + } + + public void setHumidity(float humidity) { + this.humidity = humidity; + } + + public float getTemp() { + return temp; + } + + public void setTemp(float temp) { + this.temp = temp; + } + + public float getPh() { + return ph; + } + + public void setPh(float ph) { + this.ph = ph; + } + + public float getWaterLevel() { + return waterLevel; + } + + public void setWaterLevel(float waterLevel) { + this.waterLevel = waterLevel; + } } diff --git a/src/main/java/com/backend/glowhouse/model/request/ShadowUpdateRequest.java b/src/main/java/com/backend/glowhouse/model/request/ShadowUpdateRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..4cd4af1df65cfe55e653678bfbd6626aff4e9f39 --- /dev/null +++ b/src/main/java/com/backend/glowhouse/model/request/ShadowUpdateRequest.java @@ -0,0 +1,24 @@ +package com.backend.glowhouse.model.request; + +import com.backend.glowhouse.model.response.ShadowState; + +public class ShadowUpdateRequest { + private String deviceId; + private static ShadowState shadowState; + + public String getDeviceId() { + return this.deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public static ShadowState getShadowState() { + return shadowState; + } + + public void setShadowState(ShadowState shadowState) { + ShadowUpdateRequest.shadowState = shadowState; + } +} diff --git a/src/main/java/com/backend/glowhouse/model/response/ShadowParams.java b/src/main/java/com/backend/glowhouse/model/response/ShadowParams.java new file mode 100644 index 0000000000000000000000000000000000000000..e96d83211a756c63f2720f005ccfe29994fb629a --- /dev/null +++ b/src/main/java/com/backend/glowhouse/model/response/ShadowParams.java @@ -0,0 +1,22 @@ +package com.backend.glowhouse.model.response; + +public class ShadowParams { + private boolean light; + private boolean pump; + + public boolean isLight() { + return light; + } + + public void setLight(boolean light) { + this.light = light; + } + + public boolean isPump() { + return pump; + } + + public void setPump(boolean pump) { + this.pump = pump; + } +} diff --git a/src/main/java/com/backend/glowhouse/model/response/ShadowState.java b/src/main/java/com/backend/glowhouse/model/response/ShadowState.java new file mode 100644 index 0000000000000000000000000000000000000000..e34c91b6a13e77d30b7190db1f699be49ddce863 --- /dev/null +++ b/src/main/java/com/backend/glowhouse/model/response/ShadowState.java @@ -0,0 +1,22 @@ +package com.backend.glowhouse.model.response; + +public class ShadowState { + private ShadowParams desired; + private ShadowParams reported; + + public ShadowParams getDesired() { + return desired; + } + + public void setDesired(ShadowParams desired) { + this.desired = desired; + } + + public ShadowParams getReported() { + return reported; + } + + public void setReported(ShadowParams reported) { + this.reported = reported; + } +} diff --git a/src/main/java/com/backend/glowhouse/service/DeviceShadowService.java b/src/main/java/com/backend/glowhouse/service/DeviceShadowService.java new file mode 100644 index 0000000000000000000000000000000000000000..c1161c2547fc99431327d5335ded7bcd22a0a764 --- /dev/null +++ b/src/main/java/com/backend/glowhouse/service/DeviceShadowService.java @@ -0,0 +1,56 @@ +package com.backend.glowhouse.service; + +import com.amazonaws.services.iot.client.AWSIotException; +import com.amazonaws.services.iot.client.AWSIotMqttClient; +import com.backend.glowhouse.config.AwsIotUtil; +import com.backend.glowhouse.model.DeviceShadow; +import com.backend.glowhouse.model.request.ShadowUpdateRequest; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; + +@Service +public class DeviceShadowService { + private ObjectMapper mapper = new ObjectMapper(); + + private String endpoint = "a2atwjvz13hwor-ats.iot.eu-west-1.amazonaws.com"; + private String clientId = "glowhouse_backend"; + private String cert = "/var/certs/cert.crt"; + private String key = "/var/certs/private.key"; + + public String handleShadowGetRequest(String deviceId) throws AWSIotException { + AwsIotUtil.KeyStorePasswordPair pair = AwsIotUtil.getKeyStorePasswordPair(cert, key, null); + + assert pair != null; + AWSIotMqttClient awsIotClient = new AWSIotMqttClient(endpoint, clientId, pair.keyStore, pair.keyPassword); + DeviceShadow deviceShadow = new DeviceShadow(deviceId); + + awsIotClient.attach(deviceShadow); + awsIotClient.connect(); + + String shadowJson = deviceShadow.get(); + awsIotClient.disconnect(); + + return shadowJson; + } + + public HttpStatus handleShadowUpdateRequest(ShadowUpdateRequest shadowUpdateRequest) throws AWSIotException, JsonProcessingException { + String deviceShadowUpdate = "{\"state\": {\"desired\":" + mapper.writeValueAsString(shadowUpdateRequest.getShadowState().getDesired()) + "}}"; + + AwsIotUtil.KeyStorePasswordPair pair = AwsIotUtil.getKeyStorePasswordPair(cert, key, null); + + assert pair != null; + AWSIotMqttClient awsIotClient = new AWSIotMqttClient(endpoint, clientId, pair.keyStore, pair.keyPassword); + DeviceShadow deviceShadow = new DeviceShadow(shadowUpdateRequest.getDeviceId()); + + awsIotClient.attach(deviceShadow); + awsIotClient.connect(); + + deviceShadow.update(deviceShadowUpdate); + awsIotClient.disconnect(); + + return HttpStatus.OK; + } +}