diff --git a/src/main/java/com/backend/glowhouse/controller/DeviceShadowController.java b/src/main/java/com/backend/glowhouse/controller/DeviceShadowController.java new file mode 100644 index 0000000000000000000000000000000000000000..7c7e54cd15a3dab85c6f4afb71da0a3147814a41 --- /dev/null +++ b/src/main/java/com/backend/glowhouse/controller/DeviceShadowController.java @@ -0,0 +1,75 @@ +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.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.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"; + + @PostMapping + @RequestMapping(method = POST, path = "/update") + public ResponseEntity updateShadow(@RequestBody String payload) { + iotClient.publish("$aws/things/glowhouse/shadow/update", payload); + + return ok().build(); + } + + @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); + } + +}