use of software.amazon.awssdk.aws.greengrass.model.MQTTMessage in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploadRequestHandler method subscribeToMqttTopic.
/**
* Subscribes to Specified MQTT Topic for Video Upload Request Events.
*
* @param mqttTopic - Name of the MQTT topic where video upload request will be sent
* @param event - Callback event for onStart or onError
*/
public void subscribeToMqttTopic(String mqttTopic, VideoUploadRequestEvent event) {
StreamResponseHandler<IoTCoreMessage> streamResponseHandler;
streamResponseHandler = new StreamResponseHandler<IoTCoreMessage>() {
@Override
public void onStreamEvent(IoTCoreMessage ioTCoreMessage) {
log.info("onStreamEvent");
MQTTMessage mqttMessage = ioTCoreMessage.getMessage();
if (mqttMessage == null) {
log.error("Empty MQTT Message Received");
return;
}
String payload = new String(mqttMessage.getPayload(), StandardCharsets.UTF_8);
VideoUploadRequestMessage videoUploadRequestMessage = JSONUtils.jsonToVideoUplaodRequestMessage(payload);
List<AssetPropertyValue> propertyValueEntry = videoUploadRequestMessage.getPayload().getValues();
if (propertyValueEntry.size() == 1) {
AssetPropertyValue assetPropertyValue = propertyValueEntry.get(0);
long propertyUpdateTimestamp = assetPropertyValue.timestamp().timeInSeconds();
String value = assetPropertyValue.value().stringValue();
long startTimestamp = 0;
long endTimestamp = 0;
boolean isLive = false;
if (value.equalsIgnoreCase(MQTT_LIVE_VIDEO_UPLOAD_REQUEST_KEY)) {
// Live Uploading Request
isLive = true;
log.info("Live Streaming Request Received");
} else {
// On-Demand Video Uploading Request
String[] timestamps = value.split("-");
if (timestamps.length == 2) {
try {
startTimestamp = Long.parseLong(timestamps[0]);
endTimestamp = Long.parseLong(timestamps[1]);
log.info("On-Demand Streaming Request Received for Time Range " + timestamps[0] + "-" + timestamps[1]);
} catch (NumberFormatException ex) {
log.error("Invalid VideoUploadRequest Event Received. " + ex.getMessage());
}
} else {
log.error("Invalid VideoUploadRequest Event Received. Single hyphen required");
}
}
event.onStart(isLive, propertyUpdateTimestamp, startTimestamp, endTimestamp);
} else {
log.error("Invalid VideoUploadRequest Event Received. Single value required");
}
}
@Override
public boolean onStreamError(Throwable throwable) {
log.info("onStream Error: " + throwable.getMessage());
event.onError(throwable.getMessage());
// Handle error.
return false;
}
@Override
public void onStreamClosed() {
log.info("onStream Closed Called");
}
};
try {
SubscribeToIoTCoreRequest subscribeToIoTCoreRequest = new SubscribeToIoTCoreRequest();
subscribeToIoTCoreRequest.setTopicName(mqttTopic);
subscribeToIoTCoreRequest.setQos(QOS.AT_MOST_ONCE);
SubscribeToIoTCoreResponseHandler operationResponseHandler = greengrassCoreIPCClient.subscribeToIoTCore(subscribeToIoTCoreRequest, Optional.of(streamResponseHandler));
SubscribeToIoTCoreResponse resp = operationResponseHandler.getResponse().get();
log.info("Subscribe to MQTT Response: " + resp.toString());
} catch (ExecutionException ex) {
final String errorMessage = String.format("Could not Subscribe to MQTT topic %s. %s", mqttTopic, ex.getMessage());
log.error(errorMessage);
throw new EdgeConnectorForKVSException(errorMessage, ex);
} catch (InterruptedException ex) {
final String errorMessage = String.format("Could not Subscribe to MQTT topic %s. %s", mqttTopic, ex.getMessage());
log.error(errorMessage);
// restore interrupted state
Thread.currentThread().interrupt();
throw new EdgeConnectorForKVSException(errorMessage, ex);
}
}
use of software.amazon.awssdk.aws.greengrass.model.MQTTMessage in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesInvalidMessageWithoutHyphen_onStartNotCalled.
@Test
public void subscribeToMqttTopic_receivesInvalidMessageWithoutHyphen_onStartNotCalled() throws ExecutionException, InterruptedException {
MQTTMessage mqttMessage = new MQTTMessage();
mqttMessage.setTopicName(MQTT_TOPIC_NAME);
mqttMessage.setPayload(MQTT_PAYLOAD_INVALID_VALUE_WITHOUT_HYPHEN.getBytes(StandardCharsets.US_ASCII));
IoTCoreMessage invalidIoTCoreMessage = new IoTCoreMessage();
invalidIoTCoreMessage.setMessage(mqttMessage);
SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
when(responseCompletableFuture.get()).thenReturn(response);
when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
doAnswer(invocationOnMock -> {
Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
streamResponseHandler.get().onStreamEvent(invalidIoTCoreMessage);
return subscribeToIoTCoreResponseHandler;
}).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
verify(videoUploadRequestEvent, never()).onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, START_TIME, END_TIME);
}
use of software.amazon.awssdk.aws.greengrass.model.MQTTMessage in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesOnLiveEvent_onStart.
@Test
public void subscribeToMqttTopic_receivesOnLiveEvent_onStart() throws ExecutionException, InterruptedException {
MQTTMessage mqttMessage = new MQTTMessage();
mqttMessage.setTopicName(MQTT_TOPIC_NAME);
mqttMessage.setPayload(MQTT_PAYLOAD_LIVE.getBytes(StandardCharsets.US_ASCII));
IoTCoreMessage ioTCoreMessage = new IoTCoreMessage();
ioTCoreMessage.setMessage(mqttMessage);
SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
when(responseCompletableFuture.get()).thenReturn(response);
when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
doAnswer(invocationOnMock -> {
Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
streamResponseHandler.get().onStreamEvent(ioTCoreMessage);
return subscribeToIoTCoreResponseHandler;
}).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
verify(videoUploadRequestEvent).onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, 0, 0);
}
use of software.amazon.awssdk.aws.greengrass.model.MQTTMessage in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesMultipleValuesMessage_onStartNotCalled.
@Test
public void subscribeToMqttTopic_receivesMultipleValuesMessage_onStartNotCalled() throws ExecutionException, InterruptedException {
MQTTMessage mqttMessage = new MQTTMessage();
mqttMessage.setTopicName(MQTT_TOPIC_NAME);
mqttMessage.setPayload(MQTT_PAYLOAD_MULTIPLE_VALUES.getBytes(StandardCharsets.US_ASCII));
IoTCoreMessage multipleValuesIoTCoreMessage = new IoTCoreMessage();
multipleValuesIoTCoreMessage.setMessage(mqttMessage);
SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
when(responseCompletableFuture.get()).thenReturn(response);
when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
doAnswer(invocationOnMock -> {
Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
streamResponseHandler.get().onStreamEvent(multipleValuesIoTCoreMessage);
return subscribeToIoTCoreResponseHandler;
}).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
verify(videoUploadRequestEvent, never()).onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, START_TIME, END_TIME);
}
use of software.amazon.awssdk.aws.greengrass.model.MQTTMessage in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesInvalidMessageWithHyphen_onStartNotCalled.
@Test
public void subscribeToMqttTopic_receivesInvalidMessageWithHyphen_onStartNotCalled() throws ExecutionException, InterruptedException {
MQTTMessage mqttMessage = new MQTTMessage();
mqttMessage.setTopicName(MQTT_TOPIC_NAME);
mqttMessage.setPayload(MQTT_PAYLOAD_INVALID_VALUE_WITH_HYPHEN.getBytes(StandardCharsets.US_ASCII));
IoTCoreMessage invalidIoTCoreMessage = new IoTCoreMessage();
invalidIoTCoreMessage.setMessage(mqttMessage);
SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
when(responseCompletableFuture.get()).thenReturn(response);
when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
doAnswer(invocationOnMock -> {
Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
streamResponseHandler.get().onStreamEvent(invalidIoTCoreMessage);
return subscribeToIoTCoreResponseHandler;
}).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
verify(videoUploadRequestEvent, never()).onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, START_TIME, END_TIME);
}
Aggregations