66#include " command_stream_handler.h"
77
88#include < aws/crt/Api.h>
9- #include < aws/iotcommand/CommandExecutionsEvent .h>
9+ #include < aws/iotcommand/CommandExecutionEvent .h>
1010#include < aws/iotcommand/CommandExecutionsSubscriptionRequest.h>
1111#include < aws/iotcommand/RejectedErrorCode.h>
1212#include < aws/iotcommand/UpdateCommandExecutionRequest.h>
@@ -22,36 +22,46 @@ namespace Aws
2222 namespace IotcommandSample
2323 {
2424
25- CommandStreamHandler::CommandStreamHandler (std::shared_ptr<Aws::Iotcommand::IClientV2> commandClient)
25+ CommandStreamHandler::CommandStreamHandler (std::shared_ptr<Aws::Iotcommand::IClientV2> && commandClient)
2626 : m_commandClient(std::move(commandClient))
2727 {
28+ m_commandExecutor = std::make_shared<CommandExecutor>(m_commandClient);
2829 }
2930
3031 bool CommandStreamHandler::openJsonStream (
3132 Aws::Iotcommand::CommandDeviceType deviceType,
3233 const Aws::Crt::String &deviceId)
3334 {
35+ static uint64_t nextStreamId = 1 ;
36+
3437 Aws::Iotcommand::CommandExecutionsSubscriptionRequest request;
3538 request.DeviceType = deviceType;
3639 request.DeviceId = deviceId;
3740
38- Aws::Iot::RequestResponse::StreamingOperationOptions<Aws::Iotcommand::CommandExecutionsEvent > options;
41+ Aws::Iot::RequestResponse::StreamingOperationOptions<Aws::Iotcommand::CommandExecutionEvent > options;
3942 options.WithStreamHandler (
40- [](Aws::Iotcommand::CommandExecutionsEvent &&event)
43+ [this , deviceType, deviceId ](Aws::Iotcommand::CommandExecutionEvent &&event)
4144 {
4245 fprintf (stdout, " Received new command:\n execution ID: '%s'\n " , event.ExecutionId ->c_str ());
4346 if (event.ContentType )
4447 {
4548 fprintf (stdout, " payload format: '%s'\n " , event.ContentType ->c_str ());
4649 }
47- if (event.Timeout ) {
50+ if (event.Timeout )
51+ {
4852 fprintf (stdout, " execution timeout: %d\n " , *event.Timeout );
4953 }
50- if (event.Payload ) {
54+ if (event.Payload )
55+ {
5156 fprintf (stdout, " payload size: %lu\n " , event.Payload ->size ());
5257 }
58+
59+ CommandExecutionContext commandExecution{
60+ std::move (deviceType), std::move (deviceId), std::move (event)};
61+ m_commandExecutor->executeCommand (std::move (commandExecution));
5362 });
54- auto streamId = m_nextStreamId++;
63+
64+ auto streamId = nextStreamId++;
5565 options.WithSubscriptionStatusEventHandler (
5666 [streamId](Aws::Iot::RequestResponse::SubscriptionStatusEvent &&event)
5767 { s_onSubscriptionStatusEvent (streamId, std::move (event)); });
@@ -68,7 +78,7 @@ namespace Aws
6878 for (const auto &iter : m_streams)
6979 {
7080 uint64_t streamId = iter.first ;
71- const StreamingOperationWrapper &wrapper = iter.second ;
81+ const StreamingOperation &wrapper = iter.second ;
7282 fprintf (
7383 stdout,
7484 " %" PRIu64 " : device type '%s', device ID '%s', payload type '%s'\n " ,
@@ -86,81 +96,13 @@ namespace Aws
8696 return true ;
8797 }
8898
89- void CommandStreamHandler::commandExecutionHandler (const Aws::Crt::String &executionId)
90- {
91- std::promise<void > updatePromise;
92- Aws::Iotcommand::UpdateCommandExecutionRequest request;
93- request.DeviceType = Aws::Iotcommand::CommandDeviceType::THING;
94- request.DeviceId = " laptop_test_0001" ;
95- request.ExecutionId = executionId;
96- request.Status = Aws::Iotcommand::CommandStatus::SUCCEEDED;
97- Aws::Iotcommand::StatusReason statusReason;
98- statusReason.ReasonCode = " I-CAN-FAIL-TOO" ;
99- statusReason.ReasonDescription = " But I want to succeed..." ;
100- request.StatusReason = statusReason;
101- m_commandClient->UpdateCommandExecution (
102- request,
103- [&updatePromise](Aws::Iotcommand::UpdateCommandExecutionResult &&result)
104- {
105- if (result.IsSuccess ())
106- {
107- fprintf (
108- stdout,
109- " ========= Successfully updated execution for ID %s\n " ,
110- result.GetResponse ().ExecutionId ->c_str ());
111- }
112- else
113- {
114- // ==== LoadFromObject: '{"error":"ResourceNotFound","errorMessage":"The command execution
115- // kokoko was not found.","executionId":"kokoko"}'
116- // ==== LoadFromObject: '{"error":"InvalidStateTransition","errorMessage":"Command execution
117- // status cannot be updated to CREATED.","executionId":"12fa1636-f9a9-442f-a367-e311db5d4e73"}'
118- fprintf (stdout, " ========= Error: internal code: %d\n " , result.GetError ().GetErrorCode ());
119- if (result.GetError ().HasModeledError ())
120- {
121- if (result.GetError ().GetModeledError ().ErrorMessage )
122- {
123- fprintf (
124- stdout,
125- " ========= Error: message %s\n " ,
126- result.GetError ().GetModeledError ().ErrorMessage ->c_str ());
127- }
128- if (result.GetError ().GetModeledError ().Error )
129- {
130- fprintf (
131- stdout,
132- " ========= Error: code: %d\n " ,
133- static_cast <int >(*result.GetError ().GetModeledError ().Error ));
134- fprintf (
135- stdout,
136- " ========= Error: code str: %s\n " ,
137- Aws::Iotcommand::RejectedErrorCodeMarshaller::ToString (
138- *result.GetError ().GetModeledError ().Error ));
139- }
140- if (result.GetError ().GetModeledError ().ExecutionId )
141- {
142- fprintf (
143- stdout,
144- " ========= Error: execution ID: %s\n " ,
145- result.GetError ().GetModeledError ().ExecutionId ->c_str ());
146- }
147- }
148- }
149- updatePromise.set_value ();
150- });
151-
152- fprintf (stdout, " ==== waiting for update\n " );
153- updatePromise.get_future ().wait ();
154- fprintf (stdout, " ==== updated\n " );
155- }
156-
15799 void CommandStreamHandler::registerStream (
158100 uint64_t id,
159101 std::shared_ptr<Aws::Iot::RequestResponse::IStreamingOperation> &&operation,
160102 Aws::Iotcommand::CommandDeviceType deviceType,
161103 Aws::Crt::String deviceId)
162104 {
163- StreamingOperationWrapper wrapper;
105+ StreamingOperation wrapper;
164106 wrapper.stream = std::move (operation);
165107
166108 wrapper.deviceType = deviceType;
0 commit comments