Skip to content
Snippets Groups Projects
Commit 127b6229 authored by Luke O'mahony's avatar Luke O'mahony
Browse files

add: created new rest controller for aws iot shadow

parent 4cd7d64d
Branches
No related merge requests found
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);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment