|
5 | 5 | #include "yaml-cpp/yaml.h" // IWYU pragma: keep |
6 | 6 |
|
7 | 7 | bool HelloWorldModule::Initialize(aimrt::CoreRef core) { |
8 | | - core_ = core; |
| 8 | + ctx_ptr_ = std::make_shared<aimrt::context::Context>(core); |
| 9 | + ctx_ptr_->LetMe(); |
9 | 10 |
|
10 | 11 | try { |
| 12 | + // Write your other initialization logic here ... |
11 | 13 | // Read custom module configuration |
12 | | - auto file_path = core_.GetConfigurator().GetConfigFilePath(); |
| 14 | + auto file_path = ctx_ptr_->GetConfigFilePath(); |
13 | 15 | if (!file_path.empty()) { |
14 | 16 | YAML::Node cfg_node = YAML::LoadFile(file_path.data()); |
15 | | - for (const auto& itr : cfg_node) { |
16 | | - auto k = itr.first.as<std::string>(); |
17 | | - auto v = itr.second.as<std::string>(); |
18 | | - AIMRT_INFO("cfg [{} : {}]", k, v); |
19 | | - } |
| 17 | + param1_ = cfg_node["param1"].as<std::string>(); |
| 18 | + param2_ = cfg_node["param2"].as<int>(); |
20 | 19 | } |
21 | | - // Write your other initialization logic here ... |
| 20 | + |
| 21 | + // Get executor handle |
| 22 | + executor_ = ctx_ptr_->CreateExecutor("work_executor"); |
| 23 | + |
| 24 | + timer_ = aimrt::executor::CreateTimer( |
| 25 | + executor_, |
| 26 | + std::chrono::milliseconds(1000), |
| 27 | + [this]() { |
| 28 | + ctx_ptr_->LetMe(); |
| 29 | + Task(); }, |
| 30 | + false); |
22 | 31 |
|
23 | 32 | } catch (const std::exception& e) { |
24 | 33 | AIMRT_ERROR("Init failed, {}", e.what()); |
25 | 34 | return false; |
26 | 35 | } |
27 | | - |
28 | | - AIMRT_INFO("Init succeeded."); |
29 | | - |
30 | 36 | return true; |
31 | 37 | } |
32 | 38 |
|
33 | 39 | bool HelloWorldModule::Start() { |
34 | 40 | // Write your runtime logic here |
35 | | - AIMRT_INFO("Start succeeded."); |
| 41 | + // use timer |
| 42 | + timer_->Reset(); |
| 43 | + |
| 44 | + // use executor |
| 45 | + executor_.Execute([this]() { ctx_ptr_->LetMe(); LoopTask(); }); |
| 46 | + |
| 47 | + // use co_task |
| 48 | + scope_.spawn(aimrt::co::On(aimrt::co::InlineScheduler(), CoLoopTask())); |
36 | 49 | return true; |
37 | 50 | } |
38 | 51 |
|
39 | 52 | void HelloWorldModule::Shutdown() { |
40 | 53 | // Write your resource release logic here |
41 | | - AIMRT_INFO("Shutdown succeeded."); |
| 54 | + ctx_ptr_->StopRunning(); |
| 55 | + timer_->Cancel(); |
| 56 | + aimrt::co::SyncWait(scope_.complete()); |
| 57 | +} |
| 58 | + |
| 59 | +void HelloWorldModule::Task() { |
| 60 | + AIMRT_INFO("Single Task param1: {}, param2: {}", param1_, param2_); |
| 61 | +} |
| 62 | + |
| 63 | +void HelloWorldModule::LoopTask() { |
| 64 | + while (ctx_ptr_->Running()) { |
| 65 | + AIMRT_WARN("Loop Task param1: {}, param2: {}", param1_, param2_); |
| 66 | + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +aimrt::co::Task<void> HelloWorldModule::CoLoopTask() { |
| 71 | + auto scheduler = aimrt::co::AimRTScheduler(executor_); |
| 72 | + while (ctx_ptr_->Running()) { |
| 73 | + ctx_ptr_->LetMe(); |
| 74 | + AIMRT_ERROR("CoLoop Task param1: {}, param2: {}", param1_, param2_); |
| 75 | + co_await aimrt::co::ScheduleAfter(scheduler, std::chrono::milliseconds(1000)); |
| 76 | + } |
| 77 | + co_return; |
42 | 78 | } |
0 commit comments