|
9 | 9 | #include <aws/iotcommand/CommandExecutionsEvent.h> |
10 | 10 | #include <aws/iotcommand/CommandExecutionsSubscriptionRequest.h> |
11 | 11 | #include <aws/iotcommand/IotCommandClientV2.h> |
| 12 | +#include <aws/iotcommand/UpdateCommandExecutionRequest.h> |
| 13 | +#include <aws/iotcommand/UpdateCommandExecutionResponse.h> |
| 14 | +#include <aws/iotcommand/V2ServiceError.h> |
12 | 15 |
|
13 | 16 | #include <algorithm> |
14 | 17 | #include <cinttypes> |
@@ -70,6 +73,8 @@ static void s_onSubscriptionStatusEvent(uint64_t id, Aws::Iot::RequestResponse:: |
70 | 73 | Aws::Crt::ErrorDebugString(event.GetErrorCode())); |
71 | 74 | } |
72 | 75 |
|
| 76 | +void updateExecution(const Aws::Crt::String &executionId) {} |
| 77 | + |
73 | 78 | int main(int argc, char *argv[]) |
74 | 79 | { |
75 | 80 | /************************ Setup ****************************/ |
@@ -109,21 +114,61 @@ int main(int argc, char *argv[]) |
109 | 114 |
|
110 | 115 | context.m_protocolClient->Start(); |
111 | 116 |
|
112 | | - Aws::Iotcommand::CommandExecutionsSubscriptionRequest request; |
113 | | - request.DeviceType = "things"; |
114 | | - request.DeviceId = "laptop_test_0001"; |
115 | | - |
116 | | - Aws::Iot::RequestResponse::StreamingOperationOptions<Aws::Iotcommand::CommandExecutionsEvent> options; |
117 | | - options.WithStreamHandler([](CommandExecutionsEvent &&event) |
118 | | - { fprintf(stdout, "Got new event %s\n", event.ExecutionId->c_str()); }); |
119 | | - uint32_t streamId = 8; |
120 | | - options.WithSubscriptionStatusEventHandler([streamId](Aws::Iot::RequestResponse::SubscriptionStatusEvent &&event) |
121 | | - { s_onSubscriptionStatusEvent(streamId, std::move(event)); }); |
| 117 | + Aws::Crt::String executionId; |
122 | 118 |
|
123 | | - auto operation = context.m_commandClient->CreateCommandExecutionsStream(request, options); |
124 | | - operation->Open(); |
| 119 | + { |
| 120 | + std::promise<void> executionIdPromise; |
| 121 | + Aws::Iotcommand::CommandExecutionsSubscriptionRequest request; |
| 122 | + request.DeviceType = "things"; |
| 123 | + request.DeviceId = "laptop_test_0001"; |
| 124 | + |
| 125 | + Aws::Iot::RequestResponse::StreamingOperationOptions<Aws::Iotcommand::CommandExecutionsEvent> options; |
| 126 | + options.WithStreamHandler( |
| 127 | + [&executionIdPromise, &executionId](CommandExecutionsEvent &&event) |
| 128 | + { |
| 129 | + fprintf(stdout, "==== Got new event %s\n", event.ExecutionId->c_str()); |
| 130 | + executionId = *event.ExecutionId; |
| 131 | + executionIdPromise.set_value(); |
| 132 | + }); |
| 133 | + uint32_t streamId = 8; |
| 134 | + options.WithSubscriptionStatusEventHandler( |
| 135 | + [streamId](Aws::Iot::RequestResponse::SubscriptionStatusEvent &&event) |
| 136 | + { s_onSubscriptionStatusEvent(streamId, std::move(event)); }); |
| 137 | + |
| 138 | + auto operation = context.m_commandClient->CreateCommandExecutionsStream(request, options); |
| 139 | + operation->Open(); |
| 140 | + |
| 141 | + fprintf(stdout, "==== waiting for a command\n"); |
| 142 | + executionIdPromise.get_future().wait(); |
| 143 | + fprintf(stdout, "==== received new command\n"); |
| 144 | + } |
125 | 145 |
|
126 | | - std::this_thread::sleep_for(std::chrono::seconds(20)); |
| 146 | + { |
| 147 | + std::promise<void> updatePromise; |
| 148 | + Aws::Iotcommand::UpdateCommandExecutionRequest request; |
| 149 | + request.DeviceType = "things"; |
| 150 | + request.DeviceId = "laptop_test_0001"; |
| 151 | + request.ExecutionId = executionId; |
| 152 | + request.Status = Aws::Iotcommand::CommandStatus::SUCCEEDED; |
| 153 | + context.m_commandClient->UpdateCommandExecution( |
| 154 | + request, |
| 155 | + [&updatePromise](Aws::Iotcommand::UpdateCommandExecutionResult &&result) |
| 156 | + { |
| 157 | + if (result.IsSuccess()) |
| 158 | + { |
| 159 | + fprintf(stdout, "========= Success\n"); |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + fprintf(stdout, "========= Error: code %d\n", result.GetError().GetErrorCode()); |
| 164 | + } |
| 165 | + updatePromise.set_value(); |
| 166 | + }); |
| 167 | + |
| 168 | + fprintf(stdout, "==== waiting for update\n"); |
| 169 | + updatePromise.get_future().wait(); |
| 170 | + fprintf(stdout, "==== updated\n"); |
| 171 | + } |
127 | 172 |
|
128 | 173 | return 0; |
129 | 174 | } |
0 commit comments